container_bridge_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright 2014 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 kubelet
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/util/dbus"
  17. "k8s.io/kubernetes/pkg/util/exec"
  18. "k8s.io/kubernetes/pkg/util/iptables"
  19. "k8s.io/kubernetes/pkg/util/sets"
  20. )
  21. func TestEnsureIPTablesMasqRuleNew(t *testing.T) {
  22. fcmd := exec.FakeCmd{
  23. CombinedOutputScript: []exec.FakeCombinedOutputAction{
  24. // iptables version check
  25. func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
  26. // Status 1 on the first call.
  27. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
  28. // Success on the second call.
  29. func() ([]byte, error) { return []byte{}, nil },
  30. },
  31. }
  32. fexec := exec.FakeExec{
  33. CommandScript: []exec.FakeCommandAction{
  34. // iptables version check
  35. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  36. // The second Command() call is checking the rule. Failure of that means create it.
  37. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  38. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  39. },
  40. }
  41. runner := iptables.New(&fexec, dbus.NewFake(nil, nil), iptables.ProtocolIpv4)
  42. defer runner.Destroy()
  43. err := ensureIPTablesMasqRule(runner, "127.0.0.0/8")
  44. if err != nil {
  45. t.Errorf("expected success, got %v", err)
  46. }
  47. if fcmd.CombinedOutputCalls != 3 {
  48. t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
  49. }
  50. if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables", "-t", "nat", "-A", "POSTROUTING",
  51. "-m", "comment", "--comment", "kubelet: SNAT outbound cluster traffic",
  52. "!", "-d", "127.0.0.0/8", "-j", "MASQUERADE") {
  53. t.Errorf("wrong CombinedOutput() log, got %#v", fcmd.CombinedOutputLog[2])
  54. }
  55. }
  56. func TestEnsureIPTablesMasqRuleAlreadyExists(t *testing.T) {
  57. fcmd := exec.FakeCmd{
  58. CombinedOutputScript: []exec.FakeCombinedOutputAction{
  59. // iptables version check
  60. func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
  61. // Success.
  62. func() ([]byte, error) { return []byte{}, nil },
  63. },
  64. }
  65. fexec := exec.FakeExec{
  66. CommandScript: []exec.FakeCommandAction{
  67. // iptables version check
  68. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  69. // The second Command() call is checking the rule. Success of that exec means "done".
  70. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  71. },
  72. }
  73. runner := iptables.New(&fexec, dbus.NewFake(nil, nil), iptables.ProtocolIpv4)
  74. defer runner.Destroy()
  75. err := ensureIPTablesMasqRule(runner, "127.0.0.0/8")
  76. if err != nil {
  77. t.Errorf("expected success, got %v", err)
  78. }
  79. if fcmd.CombinedOutputCalls != 2 {
  80. t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
  81. }
  82. if !sets.NewString(fcmd.CombinedOutputLog[1]...).HasAll("iptables", "-t", "nat", "-C", "POSTROUTING",
  83. "-m", "comment", "--comment", "kubelet: SNAT outbound cluster traffic",
  84. "!", "-d", "127.0.0.0/8", "-j", "MASQUERADE") {
  85. t.Errorf("wrong CombinedOutput() log, got %#v", fcmd.CombinedOutputLog[1])
  86. }
  87. }
  88. func TestEnsureIPTablesMasqRuleErrorChecking(t *testing.T) {
  89. fcmd := exec.FakeCmd{
  90. CombinedOutputScript: []exec.FakeCombinedOutputAction{
  91. // iptables version check
  92. func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
  93. // Status 2 on the first call.
  94. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 2} },
  95. },
  96. }
  97. fexec := exec.FakeExec{
  98. CommandScript: []exec.FakeCommandAction{
  99. // iptables version check
  100. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  101. // The second Command() call is checking the rule. Failure of that means create it.
  102. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  103. },
  104. }
  105. runner := iptables.New(&fexec, dbus.NewFake(nil, nil), iptables.ProtocolIpv4)
  106. defer runner.Destroy()
  107. err := ensureIPTablesMasqRule(runner, "127.0.0.0/8")
  108. if err == nil {
  109. t.Errorf("expected failure")
  110. }
  111. if fcmd.CombinedOutputCalls != 2 {
  112. t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
  113. }
  114. }
  115. func TestEnsureIPTablesMasqRuleErrorCreating(t *testing.T) {
  116. fcmd := exec.FakeCmd{
  117. CombinedOutputScript: []exec.FakeCombinedOutputAction{
  118. // iptables version check
  119. func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
  120. // Status 1 on the first call.
  121. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
  122. // Status 1 on the second call.
  123. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
  124. },
  125. }
  126. fexec := exec.FakeExec{
  127. CommandScript: []exec.FakeCommandAction{
  128. // iptables version check
  129. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  130. // The second Command() call is checking the rule. Failure of that means create it.
  131. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  132. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  133. },
  134. }
  135. runner := iptables.New(&fexec, dbus.NewFake(nil, nil), iptables.ProtocolIpv4)
  136. defer runner.Destroy()
  137. err := ensureIPTablesMasqRule(runner, "127.0.0.0/8")
  138. if err == nil {
  139. t.Errorf("expected failure")
  140. }
  141. if fcmd.CombinedOutputCalls != 3 {
  142. t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
  143. }
  144. }