traffic_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package iptables
  14. import (
  15. "reflect"
  16. "testing"
  17. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  18. iptablestest "k8s.io/kubernetes/pkg/util/iptables/testing"
  19. )
  20. func TestNoOpLocalDetector(t *testing.T) {
  21. cases := []struct {
  22. chain string
  23. args []string
  24. expectedJumpIfOutput []string
  25. expectedJumpIfNotOutput []string
  26. }{
  27. {
  28. chain: "TEST",
  29. args: []string{"arg1", "arg2"},
  30. expectedJumpIfOutput: []string{"arg1", "arg2"},
  31. expectedJumpIfNotOutput: []string{"arg1", "arg2"},
  32. },
  33. }
  34. for _, c := range cases {
  35. localDetector := NewNoOpLocalDetector()
  36. if localDetector.IsImplemented() {
  37. t.Error("DetectLocalByCIDR returns true for IsImplemented")
  38. }
  39. jumpIf := localDetector.JumpIfLocal(c.args, c.chain)
  40. jumpIfNot := localDetector.JumpIfNotLocal(c.args, c.chain)
  41. if !reflect.DeepEqual(jumpIf, c.expectedJumpIfOutput) {
  42. t.Errorf("JumpIf, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, jumpIf)
  43. }
  44. if !reflect.DeepEqual(jumpIfNot, c.expectedJumpIfNotOutput) {
  45. t.Errorf("JumpIfNot, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, jumpIfNot)
  46. }
  47. }
  48. }
  49. func TestNewDetectLocalByCIDR(t *testing.T) {
  50. cases := []struct {
  51. cidr string
  52. ipt utiliptables.Interface
  53. errExpected bool
  54. }{
  55. {
  56. cidr: "10.0.0.0/14",
  57. ipt: iptablestest.NewFake(),
  58. errExpected: false,
  59. },
  60. {
  61. cidr: "2002::1234:abcd:ffff:c0a8:101/64",
  62. ipt: iptablestest.NewIpv6Fake(),
  63. errExpected: false,
  64. },
  65. {
  66. cidr: "10.0.0.0/14",
  67. ipt: iptablestest.NewIpv6Fake(),
  68. errExpected: true,
  69. },
  70. {
  71. cidr: "2002::1234:abcd:ffff:c0a8:101/64",
  72. ipt: iptablestest.NewFake(),
  73. errExpected: true,
  74. },
  75. {
  76. cidr: "10.0.0.0",
  77. ipt: iptablestest.NewFake(),
  78. errExpected: true,
  79. },
  80. {
  81. cidr: "2002::1234:abcd:ffff:c0a8:101",
  82. ipt: iptablestest.NewIpv6Fake(),
  83. errExpected: true,
  84. },
  85. {
  86. cidr: "",
  87. ipt: iptablestest.NewFake(),
  88. errExpected: true,
  89. },
  90. {
  91. cidr: "",
  92. ipt: iptablestest.NewIpv6Fake(),
  93. errExpected: true,
  94. },
  95. }
  96. for i, c := range cases {
  97. r, err := NewDetectLocalByCIDR(c.cidr, c.ipt)
  98. if c.errExpected {
  99. if err == nil {
  100. t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
  101. }
  102. continue
  103. }
  104. if err != nil {
  105. t.Errorf("Case[%d] failed with error: %v", i, err)
  106. }
  107. }
  108. }
  109. func TestDetectLocalByCIDR(t *testing.T) {
  110. cases := []struct {
  111. cidr string
  112. ipt utiliptables.Interface
  113. chain string
  114. args []string
  115. expectedJumpIfOutput []string
  116. expectedJumpIfNotOutput []string
  117. }{
  118. {
  119. cidr: "10.0.0.0/14",
  120. ipt: iptablestest.NewFake(),
  121. chain: "TEST",
  122. args: []string{"arg1", "arg2"},
  123. expectedJumpIfOutput: []string{"arg1", "arg2", "-s", "10.0.0.0/14", "-j", "TEST"},
  124. expectedJumpIfNotOutput: []string{"arg1", "arg2", "!", "-s", "10.0.0.0/14", "-j", "TEST"},
  125. },
  126. {
  127. cidr: "2002::1234:abcd:ffff:c0a8:101/64",
  128. ipt: iptablestest.NewIpv6Fake(),
  129. chain: "TEST",
  130. args: []string{"arg1", "arg2"},
  131. expectedJumpIfOutput: []string{"arg1", "arg2", "-s", "2002::1234:abcd:ffff:c0a8:101/64", "-j", "TEST"},
  132. expectedJumpIfNotOutput: []string{"arg1", "arg2", "!", "-s", "2002::1234:abcd:ffff:c0a8:101/64", "-j", "TEST"},
  133. },
  134. }
  135. for _, c := range cases {
  136. localDetector, err := NewDetectLocalByCIDR(c.cidr, c.ipt)
  137. if err != nil {
  138. t.Errorf("Error initializing localDetector: %v", err)
  139. continue
  140. }
  141. if !localDetector.IsImplemented() {
  142. t.Error("DetectLocalByCIDR returns false for IsImplemented")
  143. }
  144. jumpIf := localDetector.JumpIfLocal(c.args, c.chain)
  145. jumpIfNot := localDetector.JumpIfNotLocal(c.args, c.chain)
  146. if !reflect.DeepEqual(jumpIf, c.expectedJumpIfOutput) {
  147. t.Errorf("JumpIf, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, jumpIf)
  148. }
  149. if !reflect.DeepEqual(jumpIfNot, c.expectedJumpIfNotOutput) {
  150. t.Errorf("JumpIfNot, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, jumpIfNot)
  151. }
  152. }
  153. }