sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/vmextensions/spec_test.go (about) 1 /* 2 Copyright 2022 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 vmextensions 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" 24 . "github.com/onsi/gomega" 25 "k8s.io/utils/ptr" 26 "sigs.k8s.io/cluster-api-provider-azure/azure" 27 ) 28 29 var ( 30 fakeVMExtensionSpec = VMExtensionSpec{ 31 azure.ExtensionSpec{ 32 Name: "my-vm-extension", 33 VMName: "my-vm", 34 Publisher: "my-publisher", 35 Version: "1.0", 36 Settings: map[string]string{"my-setting": "my-value"}, 37 ProtectedSettings: map[string]string{"my-protected-setting": "my-protected-value"}, 38 }, 39 "my-rg", 40 "my-location", 41 } 42 43 fakeVMExtensionParams = armcompute.VirtualMachineExtension{ 44 Properties: &armcompute.VirtualMachineExtensionProperties{ 45 Publisher: ptr.To("my-publisher"), 46 Type: ptr.To("my-vm-extension"), 47 TypeHandlerVersion: ptr.To("1.0"), 48 Settings: map[string]string{"my-setting": "my-value"}, 49 ProtectedSettings: map[string]string{"my-protected-setting": "my-protected-value"}, 50 }, 51 Location: ptr.To("my-location"), 52 } 53 ) 54 55 func TestParameters(t *testing.T) { 56 testcases := []struct { 57 name string 58 spec *VMExtensionSpec 59 existing interface{} 60 expect func(g *WithT, result interface{}) 61 expectedError string 62 }{ 63 { 64 name: "get parameters for vmextension", 65 spec: &fakeVMExtensionSpec, 66 existing: nil, 67 expect: func(g *WithT, result interface{}) { 68 g.Expect(result).To(Equal(fakeVMExtensionParams)) 69 }, 70 expectedError: "", 71 }, 72 { 73 name: "vmextension that already exists", 74 spec: &fakeVMExtensionSpec, 75 existing: fakeVMExtensionParams, 76 expect: func(g *WithT, result interface{}) { 77 g.Expect(result).To(BeNil()) 78 }, 79 expectedError: "", 80 }, 81 } 82 for _, tc := range testcases { 83 tc := tc 84 t.Run(tc.name, func(t *testing.T) { 85 g := NewWithT(t) 86 t.Parallel() 87 88 result, err := tc.spec.Parameters(context.TODO(), tc.existing) 89 if tc.expectedError != "" { 90 g.Expect(err).To(HaveOccurred()) 91 g.Expect(err).To(MatchError(tc.expectedError)) 92 } else { 93 g.Expect(err).NotTo(HaveOccurred()) 94 } 95 tc.expect(g, result) 96 }) 97 } 98 }