conntrack_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright 2015 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 conntrack
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/utils/exec"
  20. fakeexec "k8s.io/utils/exec/testing"
  21. utilnet "k8s.io/utils/net"
  22. )
  23. func familyParamStr(isIPv6 bool) string {
  24. if isIPv6 {
  25. return " -f ipv6"
  26. }
  27. return ""
  28. }
  29. func TestExecConntrackTool(t *testing.T) {
  30. fcmd := fakeexec.FakeCmd{
  31. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  32. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  33. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  34. func() ([]byte, error) {
  35. return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted")
  36. },
  37. },
  38. }
  39. fexec := fakeexec.FakeExec{
  40. CommandScript: []fakeexec.FakeCommandAction{
  41. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  42. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  43. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  44. },
  45. LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
  46. }
  47. testCases := [][]string{
  48. {"-L", "-p", "udp"},
  49. {"-D", "-p", "udp", "-d", "10.0.240.1"},
  50. {"-D", "-p", "udp", "--orig-dst", "10.240.0.2", "--dst-nat", "10.0.10.2"},
  51. }
  52. expectErr := []bool{false, false, true}
  53. for i := range testCases {
  54. err := Exec(&fexec, testCases[i]...)
  55. if expectErr[i] {
  56. if err == nil {
  57. t.Errorf("expected err, got %v", err)
  58. }
  59. } else {
  60. if err != nil {
  61. t.Errorf("expected success, got %v", err)
  62. }
  63. }
  64. execCmd := strings.Join(fcmd.CombinedOutputLog[i], " ")
  65. expectCmd := fmt.Sprintf("%s %s", "conntrack", strings.Join(testCases[i], " "))
  66. if execCmd != expectCmd {
  67. t.Errorf("expect execute command: %s, but got: %s", expectCmd, execCmd)
  68. }
  69. }
  70. }
  71. func TestClearUDPConntrackForIP(t *testing.T) {
  72. fcmd := fakeexec.FakeCmd{
  73. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  74. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  75. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  76. func() ([]byte, error) {
  77. return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted")
  78. },
  79. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  80. },
  81. }
  82. fexec := fakeexec.FakeExec{
  83. CommandScript: []fakeexec.FakeCommandAction{
  84. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  85. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  86. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  87. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  88. },
  89. LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
  90. }
  91. testCases := []struct {
  92. name string
  93. ip string
  94. }{
  95. {"IPv4 success", "10.240.0.3"},
  96. {"IPv4 success", "10.240.0.5"},
  97. {"IPv4 simulated error", "10.240.0.4"},
  98. {"IPv6 success", "2001:db8::10"},
  99. }
  100. svcCount := 0
  101. for _, tc := range testCases {
  102. if err := ClearEntriesForIP(&fexec, tc.ip, v1.ProtocolUDP); err != nil {
  103. t.Errorf("%s test case:, Unexpected error: %v", tc.name, err)
  104. }
  105. expectCommand := fmt.Sprintf("conntrack -D --orig-dst %s -p udp", tc.ip) + familyParamStr(utilnet.IsIPv6String(tc.ip))
  106. execCommand := strings.Join(fcmd.CombinedOutputLog[svcCount], " ")
  107. if expectCommand != execCommand {
  108. t.Errorf("%s test case: Expect command: %s, but executed %s", tc.name, expectCommand, execCommand)
  109. }
  110. svcCount++
  111. }
  112. if svcCount != fexec.CommandCalls {
  113. t.Errorf("Expect command executed %d times, but got %d", svcCount, fexec.CommandCalls)
  114. }
  115. }
  116. func TestClearUDPConntrackForPort(t *testing.T) {
  117. fcmd := fakeexec.FakeCmd{
  118. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  119. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  120. func() ([]byte, error) {
  121. return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted")
  122. },
  123. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  124. },
  125. }
  126. fexec := fakeexec.FakeExec{
  127. CommandScript: []fakeexec.FakeCommandAction{
  128. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  129. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  130. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  131. },
  132. LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
  133. }
  134. testCases := []struct {
  135. name string
  136. port int
  137. isIPv6 bool
  138. }{
  139. {"IPv4, no error", 8080, false},
  140. {"IPv4, simulated error", 9090, false},
  141. {"IPv6, no error", 6666, true},
  142. }
  143. svcCount := 0
  144. for _, tc := range testCases {
  145. err := ClearEntriesForPort(&fexec, tc.port, tc.isIPv6, v1.ProtocolUDP)
  146. if err != nil {
  147. t.Errorf("%s test case: Unexpected error: %v", tc.name, err)
  148. }
  149. expectCommand := fmt.Sprintf("conntrack -D -p udp --dport %d", tc.port) + familyParamStr(tc.isIPv6)
  150. execCommand := strings.Join(fcmd.CombinedOutputLog[svcCount], " ")
  151. if expectCommand != execCommand {
  152. t.Errorf("%s test case: Expect command: %s, but executed %s", tc.name, expectCommand, execCommand)
  153. }
  154. svcCount++
  155. }
  156. if svcCount != fexec.CommandCalls {
  157. t.Errorf("Expect command executed %d times, but got %d", svcCount, fexec.CommandCalls)
  158. }
  159. }
  160. func TestDeleteUDPConnections(t *testing.T) {
  161. fcmd := fakeexec.FakeCmd{
  162. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  163. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  164. func() ([]byte, error) {
  165. return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted")
  166. },
  167. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  168. },
  169. }
  170. fexec := fakeexec.FakeExec{
  171. CommandScript: []fakeexec.FakeCommandAction{
  172. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  173. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  174. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  175. },
  176. LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
  177. }
  178. testCases := []struct {
  179. name string
  180. origin string
  181. dest string
  182. }{
  183. {
  184. name: "IPv4 success",
  185. origin: "1.2.3.4",
  186. dest: "10.20.30.40",
  187. },
  188. {
  189. name: "IPv4 simulated failure",
  190. origin: "2.3.4.5",
  191. dest: "20.30.40.50",
  192. },
  193. {
  194. name: "IPv6 success",
  195. origin: "fd00::600d:f00d",
  196. dest: "2001:db8::5",
  197. },
  198. }
  199. svcCount := 0
  200. for i, tc := range testCases {
  201. err := ClearEntriesForNAT(&fexec, tc.origin, tc.dest, v1.ProtocolUDP)
  202. if err != nil {
  203. t.Errorf("%s test case: unexpected error: %v", tc.name, err)
  204. }
  205. expectCommand := fmt.Sprintf("conntrack -D --orig-dst %s --dst-nat %s -p udp", tc.origin, tc.dest) + familyParamStr(utilnet.IsIPv6String(tc.origin))
  206. execCommand := strings.Join(fcmd.CombinedOutputLog[i], " ")
  207. if expectCommand != execCommand {
  208. t.Errorf("%s test case: Expect command: %s, but executed %s", tc.name, expectCommand, execCommand)
  209. }
  210. svcCount++
  211. }
  212. if svcCount != fexec.CommandCalls {
  213. t.Errorf("Expect command executed %d times, but got %d", svcCount, fexec.CommandCalls)
  214. }
  215. }
  216. func TestClearUDPConntrackForPortNAT(t *testing.T) {
  217. fcmd := fakeexec.FakeCmd{
  218. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  219. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  220. func() ([]byte, error) {
  221. return []byte(""), fmt.Errorf("conntrack v1.4.2 (conntrack-tools): 0 flow entries have been deleted")
  222. },
  223. func() ([]byte, error) { return []byte("1 flow entries have been deleted"), nil },
  224. },
  225. }
  226. fexec := fakeexec.FakeExec{
  227. CommandScript: []fakeexec.FakeCommandAction{
  228. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  229. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  230. func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  231. },
  232. LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
  233. }
  234. testCases := []struct {
  235. name string
  236. port int
  237. dest string
  238. }{
  239. {
  240. name: "IPv4 success",
  241. port: 30211,
  242. dest: "1.2.3.4",
  243. },
  244. }
  245. svcCount := 0
  246. for i, tc := range testCases {
  247. err := ClearEntriesForPortNAT(&fexec, tc.dest, tc.port, v1.ProtocolUDP)
  248. if err != nil {
  249. t.Errorf("%s test case: unexpected error: %v", tc.name, err)
  250. }
  251. expectCommand := fmt.Sprintf("conntrack -D -p udp --dport %d --dst-nat %s", tc.port, tc.dest) + familyParamStr(utilnet.IsIPv6String(tc.dest))
  252. execCommand := strings.Join(fcmd.CombinedOutputLog[i], " ")
  253. if expectCommand != execCommand {
  254. t.Errorf("%s test case: Expect command: %s, but executed %s", tc.name, expectCommand, execCommand)
  255. }
  256. svcCount++
  257. }
  258. if svcCount != fexec.CommandCalls {
  259. t.Errorf("Expect command executed %d times, but got %d", svcCount, fexec.CommandCalls)
  260. }
  261. }