|
| 1 | +/* |
| 2 | +Copyright 2026 The Kube Bind 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 claimedresources |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 24 | + |
| 25 | + konnectortypes "github.com/kube-bind/kube-bind/pkg/konnector/types" |
| 26 | +) |
| 27 | + |
| 28 | +func TestSetSourceMetadataAnnotations(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + obj *unstructured.Unstructured |
| 32 | + sourceNS string |
| 33 | + sourceUID string |
| 34 | + nsKey string |
| 35 | + uidKey string |
| 36 | + expectedAnnotations map[string]string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "provider annotations on empty object", |
| 40 | + obj: &unstructured.Unstructured{}, |
| 41 | + sourceNS: "provider-ns", |
| 42 | + sourceUID: "provider-uid-123", |
| 43 | + nsKey: konnectortypes.ProviderNamespaceAnnotationKey, |
| 44 | + uidKey: konnectortypes.ProviderUIDAnnotationKey, |
| 45 | + expectedAnnotations: map[string]string{ |
| 46 | + konnectortypes.ProviderNamespaceAnnotationKey: "provider-ns", |
| 47 | + konnectortypes.ProviderUIDAnnotationKey: "provider-uid-123", |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "consumer annotations on empty object", |
| 52 | + obj: &unstructured.Unstructured{}, |
| 53 | + sourceNS: "consumer-ns", |
| 54 | + sourceUID: "consumer-uid-456", |
| 55 | + nsKey: konnectortypes.ConsumerNamespaceAnnotationKey, |
| 56 | + uidKey: konnectortypes.ConsumerUIDAnnotationKey, |
| 57 | + expectedAnnotations: map[string]string{ |
| 58 | + konnectortypes.ConsumerNamespaceAnnotationKey: "consumer-ns", |
| 59 | + konnectortypes.ConsumerUIDAnnotationKey: "consumer-uid-456", |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "preserves existing annotations", |
| 64 | + obj: func() *unstructured.Unstructured { |
| 65 | + obj := &unstructured.Unstructured{} |
| 66 | + obj.SetAnnotations(map[string]string{ |
| 67 | + "existing-key": "existing-value", |
| 68 | + }) |
| 69 | + return obj |
| 70 | + }(), |
| 71 | + sourceNS: "my-ns", |
| 72 | + sourceUID: "my-uid", |
| 73 | + nsKey: konnectortypes.ProviderNamespaceAnnotationKey, |
| 74 | + uidKey: konnectortypes.ProviderUIDAnnotationKey, |
| 75 | + expectedAnnotations: map[string]string{ |
| 76 | + konnectortypes.ProviderNamespaceAnnotationKey: "my-ns", |
| 77 | + konnectortypes.ProviderUIDAnnotationKey: "my-uid", |
| 78 | + "existing-key": "existing-value", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + |
| 87 | + konnectortypes.SetSourceMetadataAnnotations(tt.obj, tt.sourceNS, tt.sourceUID, tt.nsKey, tt.uidKey) |
| 88 | + |
| 89 | + annotations := tt.obj.GetAnnotations() |
| 90 | + for key, expected := range tt.expectedAnnotations { |
| 91 | + require.Equal(t, expected, annotations[key], "annotation %s mismatch", key) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestCandidateFromOwnerObjPreservesKubeBindAnnotations(t *testing.T) { |
| 98 | + obj := &unstructured.Unstructured{} |
| 99 | + obj.SetAnnotations(map[string]string{ |
| 100 | + konnectortypes.ProviderNamespaceAnnotationKey: "provider-ns", |
| 101 | + konnectortypes.ProviderUIDAnnotationKey: "provider-uid", |
| 102 | + konnectortypes.ConsumerNamespaceAnnotationKey: "consumer-ns", |
| 103 | + konnectortypes.ConsumerUIDAnnotationKey: "consumer-uid", |
| 104 | + "kcp.io/cluster": "should-be-stripped", |
| 105 | + "user-annotation": "should-be-kept", |
| 106 | + }) |
| 107 | + obj.SetNamespace("original-ns") |
| 108 | + obj.SetName("test-obj") |
| 109 | + |
| 110 | + candidate := candidateFromOwnerObj("target-ns", obj) |
| 111 | + |
| 112 | + annotations := candidate.GetAnnotations() |
| 113 | + require.Equal(t, "provider-ns", annotations[konnectortypes.ProviderNamespaceAnnotationKey]) |
| 114 | + require.Equal(t, "provider-uid", annotations[konnectortypes.ProviderUIDAnnotationKey]) |
| 115 | + require.Equal(t, "consumer-ns", annotations[konnectortypes.ConsumerNamespaceAnnotationKey]) |
| 116 | + require.Equal(t, "consumer-uid", annotations[konnectortypes.ConsumerUIDAnnotationKey]) |
| 117 | + require.Equal(t, "should-be-kept", annotations["user-annotation"]) |
| 118 | + _, hasKcp := annotations["kcp.io/cluster"] |
| 119 | + require.False(t, hasKcp, "kcp.io/cluster annotation should be stripped") |
| 120 | +} |
0 commit comments