1
16
17 package cache
18
19 import (
20 "errors"
21 "testing"
22
23 "k8s.io/apimachinery/pkg/util/sets"
24 )
25
26
27 func doTestStore(t *testing.T, store Store) {
28 mkObj := func(id string, val string) testStoreObject {
29 return testStoreObject{id: id, val: val}
30 }
31
32 store.Add(mkObj("foo", "bar"))
33 if item, ok, _ := store.Get(mkObj("foo", "")); !ok {
34 t.Errorf("didn't find inserted item")
35 } else {
36 if e, a := "bar", item.(testStoreObject).val; e != a {
37 t.Errorf("expected %v, got %v", e, a)
38 }
39 }
40 store.Update(mkObj("foo", "baz"))
41 if item, ok, _ := store.Get(mkObj("foo", "")); !ok {
42 t.Errorf("didn't find inserted item")
43 } else {
44 if e, a := "baz", item.(testStoreObject).val; e != a {
45 t.Errorf("expected %v, got %v", e, a)
46 }
47 }
48 store.Delete(mkObj("foo", ""))
49 if _, ok, _ := store.Get(mkObj("foo", "")); ok {
50 t.Errorf("found deleted item??")
51 }
52
53
54 store.Add(mkObj("a", "b"))
55 store.Add(mkObj("c", "d"))
56 store.Add(mkObj("e", "e"))
57 {
58 found := sets.String{}
59 for _, item := range store.List() {
60 found.Insert(item.(testStoreObject).val)
61 }
62 if !found.HasAll("b", "d", "e") {
63 t.Errorf("missing items, found: %v", found)
64 }
65 if len(found) != 3 {
66 t.Errorf("extra items")
67 }
68 }
69
70
71 store.Replace([]interface{}{
72 mkObj("foo", "foo"),
73 mkObj("bar", "bar"),
74 }, "0")
75
76 {
77 found := sets.String{}
78 for _, item := range store.List() {
79 found.Insert(item.(testStoreObject).val)
80 }
81 if !found.HasAll("foo", "bar") {
82 t.Errorf("missing items")
83 }
84 if len(found) != 2 {
85 t.Errorf("extra items")
86 }
87 }
88 }
89
90
91 func doTestIndex(t *testing.T, indexer Indexer) {
92 mkObj := func(id string, val string) testStoreObject {
93 return testStoreObject{id: id, val: val}
94 }
95
96
97 expected := map[string]sets.String{}
98 expected["b"] = sets.NewString("a", "c")
99 expected["f"] = sets.NewString("e")
100 expected["h"] = sets.NewString("g")
101 indexer.Add(mkObj("a", "b"))
102 indexer.Add(mkObj("c", "b"))
103 indexer.Add(mkObj("e", "f"))
104 indexer.Add(mkObj("g", "h"))
105 {
106 for k, v := range expected {
107 found := sets.String{}
108 indexResults, err := indexer.Index("by_val", mkObj("", k))
109 if err != nil {
110 t.Errorf("Unexpected error %v", err)
111 }
112 for _, item := range indexResults {
113 found.Insert(item.(testStoreObject).id)
114 }
115 items := v.List()
116 if !found.HasAll(items...) {
117 t.Errorf("missing items, index %s, expected %v but found %v", k, items, found.List())
118 }
119 }
120 }
121 }
122
123 func testStoreKeyFunc(obj interface{}) (string, error) {
124 return obj.(testStoreObject).id, nil
125 }
126
127 func testStoreIndexFunc(obj interface{}) ([]string, error) {
128 return []string{obj.(testStoreObject).val}, nil
129 }
130
131 func testStoreIndexers() Indexers {
132 indexers := Indexers{}
133 indexers["by_val"] = testStoreIndexFunc
134 return indexers
135 }
136
137 type testStoreObject struct {
138 id string
139 val string
140 }
141
142 func TestCache(t *testing.T) {
143 doTestStore(t, NewStore(testStoreKeyFunc))
144 }
145
146 func TestFIFOCache(t *testing.T) {
147 doTestStore(t, NewFIFO(testStoreKeyFunc))
148 }
149
150 func TestUndeltaStore(t *testing.T) {
151 nop := func([]interface{}) {}
152 doTestStore(t, NewUndeltaStore(nop, testStoreKeyFunc))
153 }
154
155 func TestIndex(t *testing.T) {
156 doTestIndex(t, NewIndexer(testStoreKeyFunc, testStoreIndexers()))
157 }
158
159 func TestKeyError(t *testing.T) {
160 obj := 100
161 err := errors.New("error")
162 keyErr := KeyError{obj, err}
163
164 if errors.Unwrap(keyErr) != err {
165 t.Errorf("expected unwrap error: %v", err)
166 }
167
168 nestedKeyErr := KeyError{obj, keyErr}
169 if !errors.Is(keyErr, err) || !errors.Is(nestedKeyErr, err) {
170 t.Errorf("not match target error: %v", err)
171 }
172 }
173
View as plain text