fake.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 testing
  14. import (
  15. "bytes"
  16. "fmt"
  17. "strings"
  18. "k8s.io/kubernetes/pkg/util/iptables"
  19. )
  20. const (
  21. Destination = "-d "
  22. Source = "-s "
  23. DPort = "--dport "
  24. Protocol = "-p "
  25. Jump = "-j "
  26. Reject = "REJECT"
  27. ToDest = "--to-destination "
  28. Recent = "recent "
  29. MatchSet = "--match-set "
  30. SrcType = "--src-type "
  31. )
  32. type Rule map[string]string
  33. // no-op implementation of iptables Interface
  34. type FakeIPTables struct {
  35. Lines []byte
  36. }
  37. func NewFake() *FakeIPTables {
  38. return &FakeIPTables{}
  39. }
  40. func (*FakeIPTables) GetVersion() (string, error) {
  41. return "0.0.0", nil
  42. }
  43. func (*FakeIPTables) EnsureChain(table iptables.Table, chain iptables.Chain) (bool, error) {
  44. return true, nil
  45. }
  46. func (*FakeIPTables) FlushChain(table iptables.Table, chain iptables.Chain) error {
  47. return nil
  48. }
  49. func (*FakeIPTables) DeleteChain(table iptables.Table, chain iptables.Chain) error {
  50. return nil
  51. }
  52. func (*FakeIPTables) EnsureRule(position iptables.RulePosition, table iptables.Table, chain iptables.Chain, args ...string) (bool, error) {
  53. return true, nil
  54. }
  55. func (*FakeIPTables) DeleteRule(table iptables.Table, chain iptables.Chain, args ...string) error {
  56. return nil
  57. }
  58. func (*FakeIPTables) IsIpv6() bool {
  59. return false
  60. }
  61. func (f *FakeIPTables) Save(table iptables.Table) ([]byte, error) {
  62. lines := make([]byte, len(f.Lines))
  63. copy(lines, f.Lines)
  64. return lines, nil
  65. }
  66. func (f *FakeIPTables) SaveInto(table iptables.Table, buffer *bytes.Buffer) error {
  67. buffer.Write(f.Lines)
  68. return nil
  69. }
  70. func (*FakeIPTables) Restore(table iptables.Table, data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
  71. return nil
  72. }
  73. func (f *FakeIPTables) RestoreAll(data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
  74. f.Lines = data
  75. return nil
  76. }
  77. func (*FakeIPTables) AddReloadFunc(reloadFunc func()) {}
  78. func (*FakeIPTables) Destroy() {}
  79. func getToken(line, separator string) string {
  80. tokens := strings.Split(line, separator)
  81. if len(tokens) == 2 {
  82. return strings.Split(tokens[1], " ")[0]
  83. }
  84. return ""
  85. }
  86. // GetChain returns a list of rules for the given chain.
  87. // The chain name must match exactly.
  88. // The matching is pretty dumb, don't rely on it for anything but testing.
  89. func (f *FakeIPTables) GetRules(chainName string) (rules []Rule) {
  90. for _, l := range strings.Split(string(f.Lines), "\n") {
  91. if strings.Contains(l, fmt.Sprintf("-A %v", chainName)) {
  92. newRule := Rule(map[string]string{})
  93. for _, arg := range []string{Destination, Source, DPort, Protocol, Jump, ToDest, Recent, MatchSet, SrcType} {
  94. tok := getToken(l, arg)
  95. if tok != "" {
  96. newRule[arg] = tok
  97. }
  98. }
  99. rules = append(rules, newRule)
  100. }
  101. }
  102. return
  103. }
  104. var _ = iptables.Interface(&FakeIPTables{})