fake.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2017 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. "fmt"
  16. "k8s.io/apimachinery/pkg/util/sets"
  17. "k8s.io/kubernetes/pkg/util/ipset"
  18. )
  19. // FakeIPSet is a no-op implementation of ipset Interface
  20. type FakeIPSet struct {
  21. // version of ipset util
  22. Version string
  23. // The key of Sets map is the ip set name
  24. Sets map[string]*ipset.IPSet
  25. // The key of Entries map is the ip set name where the entries exists
  26. Entries map[string]sets.String
  27. }
  28. // NewFake create a new fake ipset interface - it initialize the FakeIPSet.
  29. func NewFake(version string) *FakeIPSet {
  30. return &FakeIPSet{
  31. Version: version,
  32. Sets: make(map[string]*ipset.IPSet),
  33. Entries: make(map[string]sets.String),
  34. }
  35. }
  36. // GetVersion is part of interface.
  37. func (f *FakeIPSet) GetVersion() (string, error) {
  38. return f.Version, nil
  39. }
  40. // FlushSet is part of interface. It deletes all entries from a named set but keeps the set itself.
  41. func (f *FakeIPSet) FlushSet(set string) error {
  42. if f.Entries == nil {
  43. return fmt.Errorf("entries map can't be nil")
  44. }
  45. // delete all entry elements
  46. for true {
  47. if _, has := f.Entries[set].PopAny(); has {
  48. continue
  49. }
  50. break
  51. }
  52. return nil
  53. }
  54. // DestroySet is part of interface. It deletes both the entries and the set itself.
  55. func (f *FakeIPSet) DestroySet(set string) error {
  56. delete(f.Sets, set)
  57. delete(f.Entries, set)
  58. return nil
  59. }
  60. // DestroyAllSets is part of interface.
  61. func (f *FakeIPSet) DestroyAllSets() error {
  62. f.Sets = nil
  63. f.Entries = nil
  64. return nil
  65. }
  66. // CreateSet is part of interface.
  67. func (f *FakeIPSet) CreateSet(set *ipset.IPSet, ignoreExistErr bool) error {
  68. if f.Sets[set.Name] != nil {
  69. if !ignoreExistErr {
  70. // already exists
  71. return fmt.Errorf("Set cannot be created: set with the same name already exists")
  72. }
  73. return nil
  74. }
  75. f.Sets[set.Name] = set
  76. // initialize entry map
  77. f.Entries[set.Name] = sets.NewString()
  78. return nil
  79. }
  80. // AddEntry is part of interface.
  81. func (f *FakeIPSet) AddEntry(entry string, set *ipset.IPSet, ignoreExistErr bool) error {
  82. if f.Entries[set.Name].Has(entry) {
  83. if !ignoreExistErr {
  84. // already exists
  85. return fmt.Errorf("Element cannot be added to the set: it's already added")
  86. }
  87. return nil
  88. }
  89. f.Entries[set.Name].Insert(entry)
  90. return nil
  91. }
  92. // DelEntry is part of interface.
  93. func (f *FakeIPSet) DelEntry(entry string, set string) error {
  94. if f.Entries == nil {
  95. return fmt.Errorf("entries map can't be nil")
  96. }
  97. f.Entries[set].Delete(entry)
  98. return nil
  99. }
  100. // TestEntry is part of interface.
  101. func (f *FakeIPSet) TestEntry(entry string, set string) (bool, error) {
  102. if f.Entries == nil {
  103. return false, fmt.Errorf("entries map can't be nil")
  104. }
  105. found := f.Entries[set].Has(entry)
  106. return found, nil
  107. }
  108. // ListEntries is part of interface.
  109. func (f *FakeIPSet) ListEntries(set string) ([]string, error) {
  110. if f.Entries == nil {
  111. return nil, fmt.Errorf("entries map can't be nil")
  112. }
  113. return f.Entries[set].UnsortedList(), nil
  114. }
  115. // ListSets is part of interface.
  116. func (f *FakeIPSet) ListSets() ([]string, error) {
  117. res := []string{}
  118. for set := range f.Sets {
  119. res = append(res, set)
  120. }
  121. return res, nil
  122. }
  123. var _ = ipset.Interface(&FakeIPSet{})