store_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2014 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 cache
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/util/sets"
  17. )
  18. // Test public interface
  19. func doTestStore(t *testing.T, store Store) {
  20. mkObj := func(id string, val string) testStoreObject {
  21. return testStoreObject{id: id, val: val}
  22. }
  23. store.Add(mkObj("foo", "bar"))
  24. if item, ok, _ := store.Get(mkObj("foo", "")); !ok {
  25. t.Errorf("didn't find inserted item")
  26. } else {
  27. if e, a := "bar", item.(testStoreObject).val; e != a {
  28. t.Errorf("expected %v, got %v", e, a)
  29. }
  30. }
  31. store.Update(mkObj("foo", "baz"))
  32. if item, ok, _ := store.Get(mkObj("foo", "")); !ok {
  33. t.Errorf("didn't find inserted item")
  34. } else {
  35. if e, a := "baz", item.(testStoreObject).val; e != a {
  36. t.Errorf("expected %v, got %v", e, a)
  37. }
  38. }
  39. store.Delete(mkObj("foo", ""))
  40. if _, ok, _ := store.Get(mkObj("foo", "")); ok {
  41. t.Errorf("found deleted item??")
  42. }
  43. // Test List.
  44. store.Add(mkObj("a", "b"))
  45. store.Add(mkObj("c", "d"))
  46. store.Add(mkObj("e", "e"))
  47. {
  48. found := sets.String{}
  49. for _, item := range store.List() {
  50. found.Insert(item.(testStoreObject).val)
  51. }
  52. if !found.HasAll("b", "d", "e") {
  53. t.Errorf("missing items, found: %v", found)
  54. }
  55. if len(found) != 3 {
  56. t.Errorf("extra items")
  57. }
  58. }
  59. // Test Replace.
  60. store.Replace([]interface{}{
  61. mkObj("foo", "foo"),
  62. mkObj("bar", "bar"),
  63. }, "0")
  64. {
  65. found := sets.String{}
  66. for _, item := range store.List() {
  67. found.Insert(item.(testStoreObject).val)
  68. }
  69. if !found.HasAll("foo", "bar") {
  70. t.Errorf("missing items")
  71. }
  72. if len(found) != 2 {
  73. t.Errorf("extra items")
  74. }
  75. }
  76. }
  77. // Test public interface
  78. func doTestIndex(t *testing.T, indexer Indexer) {
  79. mkObj := func(id string, val string) testStoreObject {
  80. return testStoreObject{id: id, val: val}
  81. }
  82. // Test Index
  83. expected := map[string]sets.String{}
  84. expected["b"] = sets.NewString("a", "c")
  85. expected["f"] = sets.NewString("e")
  86. expected["h"] = sets.NewString("g")
  87. indexer.Add(mkObj("a", "b"))
  88. indexer.Add(mkObj("c", "b"))
  89. indexer.Add(mkObj("e", "f"))
  90. indexer.Add(mkObj("g", "h"))
  91. {
  92. for k, v := range expected {
  93. found := sets.String{}
  94. indexResults, err := indexer.Index("by_val", mkObj("", k))
  95. if err != nil {
  96. t.Errorf("Unexpected error %v", err)
  97. }
  98. for _, item := range indexResults {
  99. found.Insert(item.(testStoreObject).id)
  100. }
  101. items := v.List()
  102. if !found.HasAll(items...) {
  103. t.Errorf("missing items, index %s, expected %v but found %v", k, items, found.List())
  104. }
  105. }
  106. }
  107. }
  108. func testStoreKeyFunc(obj interface{}) (string, error) {
  109. return obj.(testStoreObject).id, nil
  110. }
  111. func testStoreIndexFunc(obj interface{}) ([]string, error) {
  112. return []string{obj.(testStoreObject).val}, nil
  113. }
  114. func testStoreIndexers() Indexers {
  115. indexers := Indexers{}
  116. indexers["by_val"] = testStoreIndexFunc
  117. return indexers
  118. }
  119. type testStoreObject struct {
  120. id string
  121. val string
  122. }
  123. func TestCache(t *testing.T) {
  124. doTestStore(t, NewStore(testStoreKeyFunc))
  125. }
  126. func TestFIFOCache(t *testing.T) {
  127. doTestStore(t, NewFIFO(testStoreKeyFunc))
  128. }
  129. func TestUndeltaStore(t *testing.T) {
  130. nop := func([]interface{}) {}
  131. doTestStore(t, NewUndeltaStore(nop, testStoreKeyFunc))
  132. }
  133. func TestIndex(t *testing.T) {
  134. doTestIndex(t, NewIndexer(testStoreKeyFunc, testStoreIndexers()))
  135. }