|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + |
| 14 | +package informers |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 22 | + "k8s.io/apimachinery/pkg/labels" |
| 23 | + "k8s.io/client-go/tools/cache" |
| 24 | + "sigs.k8s.io/external-dns/source/annotations" |
| 25 | +) |
| 26 | + |
| 27 | +func TestIndexerWithOptions_FilterByAnnotation(t *testing.T) { |
| 28 | + indexers := IndexerWithOptions[*unstructured.Unstructured]( |
| 29 | + IndexSelectorWithAnnotationFilter("example-annotation"), |
| 30 | + ) |
| 31 | + |
| 32 | + obj := &unstructured.Unstructured{} |
| 33 | + obj.SetAnnotations(map[string]string{"example-annotation": "value"}) |
| 34 | + obj.SetNamespace("default") |
| 35 | + obj.SetName("test-object") |
| 36 | + |
| 37 | + keys, err := indexers[IndexWithSelectors](obj) |
| 38 | + assert.NoError(t, err) |
| 39 | + assert.Equal(t, []string{"default/test-object"}, keys) |
| 40 | +} |
| 41 | + |
| 42 | +func TestIndexerWithOptions_FilterByLabel(t *testing.T) { |
| 43 | + labelSelector := labels.SelectorFromSet(labels.Set{"app": "nginx"}) |
| 44 | + indexers := IndexerWithOptions[*corev1.Pod]( |
| 45 | + IndexSelectorWithLabelSelector(labelSelector), |
| 46 | + ) |
| 47 | + |
| 48 | + obj := &corev1.Pod{} |
| 49 | + obj.SetLabels(map[string]string{"app": "nginx"}) |
| 50 | + obj.SetNamespace("default") |
| 51 | + obj.SetName("test-object") |
| 52 | + |
| 53 | + keys, err := indexers[IndexWithSelectors](obj) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, []string{"default/test-object"}, keys) |
| 56 | +} |
| 57 | + |
| 58 | +func TestIndexerWithOptions_NoMatch(t *testing.T) { |
| 59 | + labelSelector := labels.SelectorFromSet(labels.Set{"app": "nginx"}) |
| 60 | + indexers := IndexerWithOptions[*unstructured.Unstructured]( |
| 61 | + IndexSelectorWithLabelSelector(labelSelector), |
| 62 | + ) |
| 63 | + |
| 64 | + obj := &unstructured.Unstructured{} |
| 65 | + obj.SetLabels(map[string]string{"app": "apache"}) |
| 66 | + obj.SetNamespace("default") |
| 67 | + obj.SetName("test-object") |
| 68 | + |
| 69 | + keys, err := indexers[IndexWithSelectors](obj) |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.Nil(t, keys) |
| 72 | +} |
| 73 | + |
| 74 | +func TestIndexerWithOptions_InvalidType(t *testing.T) { |
| 75 | + indexers := IndexerWithOptions[*unstructured.Unstructured]() |
| 76 | + |
| 77 | + obj := "invalid-object" |
| 78 | + |
| 79 | + keys, err := indexers[IndexWithSelectors](obj) |
| 80 | + assert.Error(t, err) |
| 81 | + assert.Nil(t, keys) |
| 82 | + assert.Contains(t, err.Error(), "object is not of type") |
| 83 | +} |
| 84 | + |
| 85 | +func TestIndexerWithOptions_EmptyOptions(t *testing.T) { |
| 86 | + indexers := IndexerWithOptions[*unstructured.Unstructured]() |
| 87 | + |
| 88 | + obj := &unstructured.Unstructured{} |
| 89 | + obj.SetNamespace("default") |
| 90 | + obj.SetName("test-object") |
| 91 | + |
| 92 | + keys, err := indexers["withSelectors"](obj) |
| 93 | + assert.NoError(t, err) |
| 94 | + assert.Equal(t, []string{"default/test-object"}, keys) |
| 95 | +} |
| 96 | + |
| 97 | +func TestIndexerWithOptions_AnnotationFilterNoMatch(t *testing.T) { |
| 98 | + indexers := IndexerWithOptions[*unstructured.Unstructured]( |
| 99 | + IndexSelectorWithAnnotationFilter("example-annotation=value"), |
| 100 | + ) |
| 101 | + |
| 102 | + obj := &unstructured.Unstructured{} |
| 103 | + obj.SetAnnotations(map[string]string{"other-annotation": "value"}) |
| 104 | + obj.SetNamespace("default") |
| 105 | + obj.SetName("test-object") |
| 106 | + |
| 107 | + keys, err := indexers[IndexWithSelectors](obj) |
| 108 | + assert.NoError(t, err) |
| 109 | + assert.Nil(t, keys) |
| 110 | +} |
| 111 | + |
| 112 | +func TestIndexSelectorWithAnnotationFilter(t *testing.T) { |
| 113 | + tests := []struct { |
| 114 | + name string |
| 115 | + input string |
| 116 | + expectedFilter labels.Selector |
| 117 | + }{ |
| 118 | + { |
| 119 | + name: "valid input", |
| 120 | + input: "key=value", |
| 121 | + expectedFilter: func() labels.Selector { s, _ := annotations.ParseFilter("key=value"); return s }(), |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "empty input", |
| 125 | + input: "", |
| 126 | + expectedFilter: nil, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "key only filter", |
| 130 | + input: "app", |
| 131 | + expectedFilter: func() labels.Selector { s, _ := annotations.ParseFilter("app"); return s }(), |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "poisoned intput", |
| 135 | + input: "=app", |
| 136 | + expectedFilter: nil, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + for _, tt := range tests { |
| 141 | + t.Run(tt.name, func(t *testing.T) { |
| 142 | + options := &IndexSelectorOptions{} |
| 143 | + IndexSelectorWithAnnotationFilter(tt.input)(options) |
| 144 | + assert.Equal(t, tt.expectedFilter, options.annotationFilter) |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestGetByKey_ObjectExists(t *testing.T) { |
| 150 | + indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 151 | + pod := &corev1.Pod{} |
| 152 | + pod.SetNamespace("default") |
| 153 | + pod.SetName("test-pod") |
| 154 | + |
| 155 | + err := indexer.Add(pod) |
| 156 | + assert.NoError(t, err) |
| 157 | + |
| 158 | + result, err := GetByKey[*corev1.Pod](indexer, "default/test-pod") |
| 159 | + assert.NoError(t, err) |
| 160 | + assert.NotNil(t, result) |
| 161 | + assert.Equal(t, "test-pod", result.GetName()) |
| 162 | +} |
| 163 | + |
| 164 | +func TestGetByKey_ObjectDoesNotExist(t *testing.T) { |
| 165 | + indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 166 | + |
| 167 | + result, err := GetByKey[*corev1.Pod](indexer, "default/non-existent-pod") |
| 168 | + assert.NoError(t, err) |
| 169 | + assert.Nil(t, result) |
| 170 | +} |
| 171 | + |
| 172 | +func TestGetByKey_TypeAssertionFailure(t *testing.T) { |
| 173 | + indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}) |
| 174 | + service := &corev1.Service{} |
| 175 | + service.SetNamespace("default") |
| 176 | + service.SetName("test-service") |
| 177 | + |
| 178 | + err := indexer.Add(service) |
| 179 | + assert.NoError(t, err) |
| 180 | + |
| 181 | + result, err := GetByKey[*corev1.Pod](indexer, "default/test-service") |
| 182 | + assert.Error(t, err) |
| 183 | + assert.Contains(t, err.Error(), "object is not of type") |
| 184 | + assert.Nil(t, result) |
| 185 | +} |
0 commit comments