|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cache |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 25 | + "k8s.io/apimachinery/pkg/labels" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | + schema "k8s.io/apimachinery/pkg/runtime/schema" |
| 28 | +) |
| 29 | + |
| 30 | +func TestListersListAll(t *testing.T) { |
| 31 | + mkObj := func(id string, val string) testStoreObject { |
| 32 | + return testStoreObject{id: id, val: val} |
| 33 | + } |
| 34 | + |
| 35 | + store := NewStore(testStoreKeyFunc) |
| 36 | + |
| 37 | + err := store.Add(mkObj("foo", "bar")) |
| 38 | + if err != nil { |
| 39 | + t.Errorf("store obj add failed") |
| 40 | + } |
| 41 | + |
| 42 | + err = store.Add(mkObj("foo-1", "bar-1")) |
| 43 | + if err != nil { |
| 44 | + t.Errorf("store obj add failed") |
| 45 | + } |
| 46 | + |
| 47 | + expectedOutput := []testStoreObject{ |
| 48 | + mkObj("foo", "bar"), |
| 49 | + mkObj("foo-1", "bar-1"), |
| 50 | + } |
| 51 | + actualOutput := []testStoreObject{} |
| 52 | + |
| 53 | + err = ListAll(store, labels.Everything(), func(obj interface{}) { |
| 54 | + actualOutput = append(actualOutput, obj.(testStoreObject)) |
| 55 | + }) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + t.Errorf("unexpected error: %v", err) |
| 59 | + } |
| 60 | + if !reflect.DeepEqual(expectedOutput, actualOutput) { |
| 61 | + t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput)) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestListersListAllByNamespace(t *testing.T) { |
| 66 | + objs := []*unstructured.Unstructured{ |
| 67 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 68 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"), |
| 69 | + newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"), |
| 70 | + } |
| 71 | + indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{}) |
| 72 | + for _, obj := range objs { |
| 73 | + err := indexer.Add(obj) |
| 74 | + if err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + expectedOutput := []*unstructured.Unstructured{ |
| 80 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 81 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"), |
| 82 | + } |
| 83 | + namespaceToList := "ns-foo" |
| 84 | + |
| 85 | + actualOutput := []*unstructured.Unstructured{} |
| 86 | + appendFn := func(obj interface{}) { |
| 87 | + actualOutput = append(actualOutput, obj.(*unstructured.Unstructured)) |
| 88 | + } |
| 89 | + |
| 90 | + err := ListAllByNamespace(indexer, namespaceToList, labels.Everything(), appendFn) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if !reflect.DeepEqual(expectedOutput, actualOutput) { |
| 96 | + t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput)) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestGenericListerListMethod(t *testing.T) { |
| 101 | + objs := []*unstructured.Unstructured{ |
| 102 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 103 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"), |
| 104 | + newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"), |
| 105 | + } |
| 106 | + indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{}) |
| 107 | + for _, obj := range objs { |
| 108 | + err := indexer.Add(obj) |
| 109 | + if err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + } |
| 113 | + target := NewGenericLister(indexer, schema.GroupResource{Group: "group", Resource: "resource"}) |
| 114 | + |
| 115 | + expectedOutput := []runtime.Object{ |
| 116 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 117 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo1"), |
| 118 | + newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"), |
| 119 | + } |
| 120 | + actualOutput, err := target.List(labels.Everything()) |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + if !reflect.DeepEqual(expectedOutput, actualOutput) { |
| 126 | + t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput)) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestGenericListerByNamespaceMethod(t *testing.T) { |
| 131 | + objs := []*unstructured.Unstructured{ |
| 132 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 133 | + newUnstructured("group/version", "TheKind", "ns-faa", "name-faa1"), |
| 134 | + newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"), |
| 135 | + } |
| 136 | + indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{}) |
| 137 | + for _, obj := range objs { |
| 138 | + err := indexer.Add(obj) |
| 139 | + if err != nil { |
| 140 | + t.Fatal(err) |
| 141 | + } |
| 142 | + } |
| 143 | + target := NewGenericLister(indexer, schema.GroupResource{Group: "group", Resource: "resource"}) |
| 144 | + |
| 145 | + expectedOutput := []runtime.Object{ |
| 146 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 147 | + } |
| 148 | + namespaceToList := "ns-foo" |
| 149 | + actualOutput, err := target.ByNamespace(namespaceToList).List(labels.Everything()) |
| 150 | + |
| 151 | + if err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + if !reflect.DeepEqual(expectedOutput, actualOutput) { |
| 155 | + t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput)) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestGenericListerGetMethod(t *testing.T) { |
| 160 | + objs := []*unstructured.Unstructured{ |
| 161 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 162 | + newUnstructured("group/version", "TheKind", "ns-faa", "name-faa1"), |
| 163 | + newUnstructured("group/version", "TheKind", "ns-bar", "name-bar"), |
| 164 | + } |
| 165 | + indexer := NewIndexer(MetaNamespaceKeyFunc, Indexers{}) |
| 166 | + for _, obj := range objs { |
| 167 | + err := indexer.Add(obj) |
| 168 | + if err != nil { |
| 169 | + t.Fatal(err) |
| 170 | + } |
| 171 | + } |
| 172 | + target := NewGenericLister(indexer, schema.GroupResource{Group: "group", Resource: "resource"}) |
| 173 | + |
| 174 | + expectedOutput := []runtime.Object{ |
| 175 | + newUnstructured("group/version", "TheKind", "ns-foo", "name-foo"), |
| 176 | + }[0] |
| 177 | + key := "ns-foo/name-foo" |
| 178 | + actualOutput, err := target.Get(key) |
| 179 | + |
| 180 | + if err != nil { |
| 181 | + t.Fatal(err) |
| 182 | + } |
| 183 | + if !reflect.DeepEqual(expectedOutput, actualOutput) { |
| 184 | + t.Fatalf("unexpected object has been returned expected = %v actual = %v, diff = %v", expectedOutput, actualOutput, cmp.Diff(expectedOutput, actualOutput)) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func newUnstructured(apiVersion, kind, namespace, name string) *unstructured.Unstructured { |
| 189 | + return &unstructured.Unstructured{ |
| 190 | + Object: map[string]interface{}{ |
| 191 | + "apiVersion": apiVersion, |
| 192 | + "kind": kind, |
| 193 | + "metadata": map[string]interface{}{ |
| 194 | + "namespace": namespace, |
| 195 | + "name": name, |
| 196 | + }, |
| 197 | + }, |
| 198 | + } |
| 199 | +} |
0 commit comments