2
0

fake.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 the Kilo authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package iptables
  15. import (
  16. "fmt"
  17. "github.com/coreos/go-iptables/iptables"
  18. )
  19. type statusExiter interface {
  20. ExitStatus() int
  21. }
  22. var _ statusExiter = (*iptables.Error)(nil)
  23. var _ statusExiter = statusError(0)
  24. type statusError int
  25. func (s statusError) Error() string {
  26. return fmt.Sprintf("%d", s)
  27. }
  28. func (s statusError) ExitStatus() int {
  29. return int(s)
  30. }
  31. type fakeClient struct {
  32. storage []Rule
  33. }
  34. var _ Client = &fakeClient{}
  35. func (f *fakeClient) AppendUnique(table, chain string, spec ...string) error {
  36. exists, err := f.Exists(table, chain, spec...)
  37. if err != nil {
  38. return err
  39. }
  40. if exists {
  41. return nil
  42. }
  43. f.storage = append(f.storage, &rule{table: table, chain: chain, spec: spec})
  44. return nil
  45. }
  46. func (f *fakeClient) Delete(table, chain string, spec ...string) error {
  47. r := &rule{table: table, chain: chain, spec: spec}
  48. for i := range f.storage {
  49. if f.storage[i].String() == r.String() {
  50. copy(f.storage[i:], f.storage[i+1:])
  51. f.storage[len(f.storage)-1] = nil
  52. f.storage = f.storage[:len(f.storage)-1]
  53. break
  54. }
  55. }
  56. return nil
  57. }
  58. func (f *fakeClient) Exists(table, chain string, spec ...string) (bool, error) {
  59. r := &rule{table: table, chain: chain, spec: spec}
  60. for i := range f.storage {
  61. if f.storage[i].String() == r.String() {
  62. return true, nil
  63. }
  64. }
  65. return false, nil
  66. }
  67. func (f *fakeClient) ClearChain(table, name string) error {
  68. for i := range f.storage {
  69. r, ok := f.storage[i].(*rule)
  70. if !ok {
  71. continue
  72. }
  73. if table == r.table && name == r.chain {
  74. if err := f.Delete(table, name, r.spec...); err != nil {
  75. return nil
  76. }
  77. }
  78. }
  79. return f.DeleteChain(table, name)
  80. }
  81. func (f *fakeClient) DeleteChain(table, name string) error {
  82. for i := range f.storage {
  83. r, ok := f.storage[i].(*rule)
  84. if !ok {
  85. continue
  86. }
  87. if table == r.table && name == r.chain {
  88. return fmt.Errorf("cannot delete chain %s; rules exist", name)
  89. }
  90. }
  91. c := &chain{table: table, chain: name}
  92. for i := range f.storage {
  93. if f.storage[i].String() == c.String() {
  94. copy(f.storage[i:], f.storage[i+1:])
  95. f.storage[len(f.storage)-1] = nil
  96. f.storage = f.storage[:len(f.storage)-1]
  97. break
  98. }
  99. }
  100. return nil
  101. }
  102. func (f *fakeClient) NewChain(table, name string) error {
  103. c := &chain{table: table, chain: name}
  104. for i := range f.storage {
  105. if f.storage[i].String() == c.String() {
  106. return statusError(1)
  107. }
  108. }
  109. f.storage = append(f.storage, c)
  110. return nil
  111. }