sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/aksextensions/spec_test.go (about) 1 /* 2 Copyright 2023 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 aksextensions 18 19 import ( 20 "context" 21 "testing" 22 23 asokubernetesconfigurationv1 "github.com/Azure/azure-service-operator/v2/api/kubernetesconfiguration/v1api20230501" 24 "github.com/Azure/azure-service-operator/v2/pkg/genruntime" 25 . "github.com/onsi/gomega" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/utils/ptr" 28 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 29 ) 30 31 var ( 32 fakeAKSExtension = asokubernetesconfigurationv1.Extension{ 33 Spec: asokubernetesconfigurationv1.Extension_Spec{ 34 AksAssignedIdentity: &asokubernetesconfigurationv1.Extension_Properties_AksAssignedIdentity_Spec{ 35 Type: (*asokubernetesconfigurationv1.Extension_Properties_AksAssignedIdentity_Type_Spec)(&fakeAKSExtensionSpec1.AKSAssignedIdentityType), 36 }, 37 AzureName: fakeAKSExtensionSpec1.Name, 38 AutoUpgradeMinorVersion: ptr.To(true), 39 ConfigurationSettings: map[string]string{ 40 "fake-key": "fake-value", 41 }, 42 ExtensionType: fakeAKSExtensionSpec1.ExtensionType, 43 Owner: &genruntime.ArbitraryOwnerReference{ 44 ARMID: fakeAKSExtensionSpec1.Owner, 45 }, 46 Plan: &asokubernetesconfigurationv1.Plan{ 47 Name: ptr.To(fakeAKSExtensionSpec1.Plan.Name), 48 Product: ptr.To(fakeAKSExtensionSpec1.Plan.Product), 49 Publisher: ptr.To(fakeAKSExtensionSpec1.Plan.Publisher), 50 Version: ptr.To(fakeAKSExtensionSpec1.Plan.Version), 51 }, 52 ReleaseTrain: fakeAKSExtensionSpec1.ReleaseTrain, 53 Version: fakeAKSExtensionSpec1.Version, 54 Identity: &asokubernetesconfigurationv1.Identity{ 55 Type: (*asokubernetesconfigurationv1.Identity_Type)(&fakeAKSExtensionSpec1.ExtensionIdentity), 56 }, 57 }, 58 } 59 fakeAKSExtensionSpec1 = AKSExtensionSpec{ 60 Name: "fake-aks-extension", 61 Namespace: "fake-namespace", 62 AKSAssignedIdentityType: "SystemAssigned", 63 AutoUpgradeMinorVersion: ptr.To(true), 64 ConfigurationSettings: map[string]string{ 65 "fake-key": "fake-value", 66 }, 67 ExtensionType: ptr.To("fake-extension-type"), 68 ReleaseTrain: ptr.To("fake-release-train"), 69 Version: ptr.To("fake-version"), 70 Owner: "fake-owner", 71 Plan: &infrav1.ExtensionPlan{ 72 Name: "fake-plan-name", 73 }, 74 ExtensionIdentity: "SystemAssigned", 75 } 76 fakeAKSExtensionStatus = asokubernetesconfigurationv1.Extension_STATUS{ 77 Name: ptr.To(fakeAKSExtensionSpec1.Name), 78 ProvisioningState: ptr.To(asokubernetesconfigurationv1.ProvisioningStateDefinition_STATUS_Succeeded), 79 } 80 ) 81 82 func getASOAKSExtension(changes ...func(*asokubernetesconfigurationv1.Extension)) *asokubernetesconfigurationv1.Extension { 83 aksExtension := fakeAKSExtension.DeepCopy() 84 for _, change := range changes { 85 change(aksExtension) 86 } 87 return aksExtension 88 } 89 90 func TestAzureAKSExtensionSpec_Parameters(t *testing.T) { 91 testcases := []struct { 92 name string 93 spec *AKSExtensionSpec 94 existing *asokubernetesconfigurationv1.Extension 95 expect func(g *WithT, result asokubernetesconfigurationv1.Extension) 96 expectedError string 97 }{ 98 { 99 name: "Creating a new AKS Extension", 100 spec: &fakeAKSExtensionSpec1, 101 existing: nil, 102 expect: func(g *WithT, result asokubernetesconfigurationv1.Extension) { 103 g.Expect(result).To(Not(BeNil())) 104 105 // ObjectMeta is populated later in the codeflow 106 g.Expect(result.ObjectMeta).To(Equal(metav1.ObjectMeta{})) 107 108 // Spec is populated from the spec passed in 109 g.Expect(result.Spec).To(Equal(getASOAKSExtension().Spec)) 110 }, 111 }, 112 { 113 name: "user updates to AKS Extension resource and capz should overwrite it", 114 spec: &fakeAKSExtensionSpec1, 115 existing: getASOAKSExtension( 116 // user added AutoUpgradeMinorVersion which should be overwritten by capz 117 func(aksExtension *asokubernetesconfigurationv1.Extension) { 118 aksExtension.Spec.AutoUpgradeMinorVersion = ptr.To(false) 119 }, 120 // user added Status 121 func(aksExtension *asokubernetesconfigurationv1.Extension) { 122 aksExtension.Status = fakeAKSExtensionStatus 123 }, 124 ), 125 expect: func(g *WithT, result asokubernetesconfigurationv1.Extension) { 126 g.Expect(result).To(Not(BeNil())) 127 128 // ObjectMeta is populated later in the codeflow 129 g.Expect(result.ObjectMeta).To(Equal(metav1.ObjectMeta{})) 130 131 // Spec is populated from the spec passed in 132 g.Expect(result.Spec).To(Equal(getASOAKSExtension().Spec)) 133 134 // Status should be carried over 135 g.Expect(result.Status).To(Equal(fakeAKSExtensionStatus)) 136 }, 137 }, 138 } 139 for _, tc := range testcases { 140 tc := tc 141 t.Run(tc.name, func(t *testing.T) { 142 g := NewWithT(t) 143 t.Parallel() 144 145 result, err := tc.spec.Parameters(context.TODO(), tc.existing) 146 if tc.expectedError != "" { 147 g.Expect(err).To(HaveOccurred()) 148 g.Expect(err).To(MatchError(tc.expectedError)) 149 } else { 150 g.Expect(err).NotTo(HaveOccurred()) 151 } 152 tc.expect(g, *result) 153 }) 154 } 155 }