|
| 1 | +package ingress |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + networking "k8s.io/api/networking/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "k8s.io/apimachinery/pkg/types" |
| 13 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_patchDryRunPlanAnnotation(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + ingAnnotations map[string]string |
| 21 | + planJSON string |
| 22 | + wantAnnotation string |
| 23 | + wantOtherAnnotations map[string]string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "writes annotation when not present", |
| 27 | + ingAnnotations: nil, |
| 28 | + planJSON: `{"id":"test-stack"}`, |
| 29 | + wantAnnotation: `{"id":"test-stack"}`, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "skips patch when value unchanged", |
| 33 | + ingAnnotations: map[string]string{ |
| 34 | + dryRunPlanAnnotation: `{"id":"test-stack"}`, |
| 35 | + }, |
| 36 | + planJSON: `{"id":"test-stack"}`, |
| 37 | + wantAnnotation: `{"id":"test-stack"}`, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "updates annotation when value changed", |
| 41 | + ingAnnotations: map[string]string{ |
| 42 | + dryRunPlanAnnotation: `{"id":"old-stack"}`, |
| 43 | + }, |
| 44 | + planJSON: `{"id":"new-stack"}`, |
| 45 | + wantAnnotation: `{"id":"new-stack"}`, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "preserves existing annotations", |
| 49 | + ingAnnotations: map[string]string{ |
| 50 | + "alb.ingress.kubernetes.io/scheme": "internet-facing", |
| 51 | + }, |
| 52 | + planJSON: `{"id":"test-stack"}`, |
| 53 | + wantAnnotation: `{"id":"test-stack"}`, |
| 54 | + wantOtherAnnotations: map[string]string{ |
| 55 | + "alb.ingress.kubernetes.io/scheme": "internet-facing", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + ing := &networking.Ingress{ |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + Name: "test-ingress", |
| 65 | + Namespace: "default", |
| 66 | + Annotations: tt.ingAnnotations, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + scheme := runtime.NewScheme() |
| 71 | + require.NoError(t, clientgoscheme.AddToScheme(scheme)) |
| 72 | + |
| 73 | + k8sClient := fake.NewClientBuilder(). |
| 74 | + WithScheme(scheme). |
| 75 | + WithObjects(ing). |
| 76 | + Build() |
| 77 | + |
| 78 | + err := patchDryRunPlanAnnotation(context.Background(), k8sClient, ing, tt.planJSON) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + // Verify the annotation was persisted |
| 82 | + updatedIng := &networking.Ingress{} |
| 83 | + err = k8sClient.Get(context.Background(), types.NamespacedName{ |
| 84 | + Name: "test-ingress", |
| 85 | + Namespace: "default", |
| 86 | + }, updatedIng) |
| 87 | + require.NoError(t, err) |
| 88 | + assert.Equal(t, tt.wantAnnotation, updatedIng.Annotations[dryRunPlanAnnotation]) |
| 89 | + |
| 90 | + // Verify other annotations were not clobbered |
| 91 | + for k, v := range tt.wantOtherAnnotations { |
| 92 | + assert.Equal(t, v, updatedIng.Annotations[k], "annotation %s should be preserved", k) |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments