ebtables_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2016 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 ebtables
  14. import (
  15. "k8s.io/kubernetes/pkg/util/exec"
  16. "strings"
  17. "testing"
  18. )
  19. func testEnsureChain(t *testing.T) {
  20. fcmd := exec.FakeCmd{
  21. CombinedOutputScript: []exec.FakeCombinedOutputAction{
  22. // Does not Exists
  23. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
  24. // Success
  25. func() ([]byte, error) { return []byte{}, nil },
  26. // Exists
  27. func() ([]byte, error) { return nil, nil },
  28. // Does not Exists
  29. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
  30. // Fail to create chain
  31. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 2} },
  32. },
  33. }
  34. fexec := exec.FakeExec{
  35. CommandScript: []exec.FakeCommandAction{
  36. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  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. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  40. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  41. },
  42. }
  43. runner := New(&fexec)
  44. exists, err := runner.EnsureChain(TableFilter, "TEST-CHAIN")
  45. if exists {
  46. t.Errorf("expected exists = false")
  47. }
  48. if err != nil {
  49. t.Errorf("expected err = nil")
  50. }
  51. exists, err = runner.EnsureChain(TableFilter, "TEST-CHAIN")
  52. if !exists {
  53. t.Errorf("expected exists = true")
  54. }
  55. if err != nil {
  56. t.Errorf("expected err = nil")
  57. }
  58. exists, err = runner.EnsureChain(TableFilter, "TEST-CHAIN")
  59. if exists {
  60. t.Errorf("expected exists = false")
  61. }
  62. errStr := "Failed to ensure TEST-CHAIN chain: exit 2, output:"
  63. if err == nil || !strings.Contains(err.Error(), errStr) {
  64. t.Errorf("expected error: %q", errStr)
  65. }
  66. }
  67. func testEnsureRule(t *testing.T) {
  68. fcmd := exec.FakeCmd{
  69. CombinedOutputScript: []exec.FakeCombinedOutputAction{
  70. // Exists
  71. func() ([]byte, error) {
  72. return []byte(`Bridge table: filter
  73. Bridge chain: OUTPUT, entries: 4, policy: ACCEPT
  74. -j TEST
  75. `), nil
  76. },
  77. // Does not Exists.
  78. func() ([]byte, error) {
  79. return []byte(`Bridge table: filter
  80. Bridge chain: TEST, entries: 0, policy: ACCEPT`), nil
  81. },
  82. // Fail to create
  83. func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 2} },
  84. },
  85. }
  86. fexec := exec.FakeExec{
  87. CommandScript: []exec.FakeCommandAction{
  88. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  89. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  90. func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
  91. },
  92. }
  93. runner := New(&fexec)
  94. exists, err := runner.EnsureRule(Append, TableFilter, ChainOutput, "-j", "TEST")
  95. if !exists {
  96. t.Errorf("expected exists = true")
  97. }
  98. if err != nil {
  99. t.Errorf("expected err = nil")
  100. }
  101. exists, err = runner.EnsureRule(Append, TableFilter, ChainOutput, "-j", "NEXT-TEST")
  102. if exists {
  103. t.Errorf("expected exists = false")
  104. }
  105. errStr := "Failed to ensure rule: exist 2, output: "
  106. if err == nil || err.Error() != errStr {
  107. t.Errorf("expected error: %q", errStr)
  108. }
  109. }