ebtables_test.go 3.9 KB

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