|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package tags |
| 5 | + |
| 6 | +import ( |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + "github.com/onsi/gomega" |
| 9 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 10 | + |
| 11 | + capav1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 12 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 13 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" |
| 14 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest" |
| 15 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/eks/mutation/testutils" |
| 16 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("Generate EKS Tags patches for managed control plane", func() { |
| 20 | + patchGenerator := func() mutation.GeneratePatches { |
| 21 | + return mutation.NewMetaGeneratePatchesHandler("", helpers.TestEnv.Client, NewClusterPatch()).(mutation.GeneratePatches) |
| 22 | + } |
| 23 | + |
| 24 | + testDefs := []capitest.PatchTestDef{ |
| 25 | + { |
| 26 | + Name: "unset variable", |
| 27 | + }, |
| 28 | + { |
| 29 | + Name: "additionalTags set for managed control plane", |
| 30 | + Vars: []runtimehooksv1.Variable{ |
| 31 | + capitest.VariableWithValue( |
| 32 | + v1alpha1.ClusterConfigVariableName, |
| 33 | + capav1.Tags{ |
| 34 | + "Environment": "production", |
| 35 | + "Team": "platform", |
| 36 | + }, |
| 37 | + v1alpha1.EKSVariableName, |
| 38 | + VariableName, |
| 39 | + ), |
| 40 | + }, |
| 41 | + RequestItem: testutils.NewEKSControlPlaneRequestItem("1234"), |
| 42 | + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{ |
| 43 | + Operation: "add", |
| 44 | + Path: "/spec/template/spec/additionalTags", |
| 45 | + ValueMatcher: gomega.And( |
| 46 | + gomega.HaveKeyWithValue("Environment", "production"), |
| 47 | + gomega.HaveKeyWithValue("Team", "platform"), |
| 48 | + ), |
| 49 | + }}, |
| 50 | + }, |
| 51 | + { |
| 52 | + Name: "empty additionalTags for managed control plane", |
| 53 | + Vars: []runtimehooksv1.Variable{ |
| 54 | + capitest.VariableWithValue( |
| 55 | + v1alpha1.ClusterConfigVariableName, |
| 56 | + capav1.Tags{}, |
| 57 | + v1alpha1.EKSVariableName, |
| 58 | + VariableName, |
| 59 | + ), |
| 60 | + }, |
| 61 | + RequestItem: testutils.NewEKSControlPlaneRequestItem("1234"), |
| 62 | + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{}, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "additionalTags with special characters for managed control plane", |
| 66 | + Vars: []runtimehooksv1.Variable{ |
| 67 | + capitest.VariableWithValue( |
| 68 | + v1alpha1.ClusterConfigVariableName, |
| 69 | + capav1.Tags{ |
| 70 | + "kubernetes.io/cluster/test-cluster": "owned", |
| 71 | + "Cost-Center": "12345", |
| 72 | + "Environment": "dev-test", |
| 73 | + }, |
| 74 | + v1alpha1.EKSVariableName, |
| 75 | + VariableName, |
| 76 | + ), |
| 77 | + }, |
| 78 | + RequestItem: testutils.NewEKSControlPlaneRequestItem("1234"), |
| 79 | + ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{ |
| 80 | + Operation: "add", |
| 81 | + Path: "/spec/template/spec/additionalTags", |
| 82 | + ValueMatcher: gomega.And( |
| 83 | + gomega.HaveKeyWithValue("kubernetes.io/cluster/test-cluster", "owned"), |
| 84 | + gomega.HaveKeyWithValue("Cost-Center", "12345"), |
| 85 | + gomega.HaveKeyWithValue("Environment", "dev-test"), |
| 86 | + ), |
| 87 | + }}, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + // create test node for each case |
| 92 | + for _, tt := range testDefs { |
| 93 | + It(tt.Name, func() { |
| 94 | + capitest.AssertGeneratePatches( |
| 95 | + GinkgoT(), |
| 96 | + patchGenerator, |
| 97 | + &tt, |
| 98 | + ) |
| 99 | + }) |
| 100 | + } |
| 101 | +}) |
0 commit comments