sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/availabilitysets/spec_test.go (about) 1 /* 2 Copyright 2021 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 availabilitysets 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/services/resourceskus" 27 ) 28 29 var ( 30 fakeSetSpecMissingCap = AvailabilitySetSpec{ 31 Name: "test-as", 32 ResourceGroup: "test-rg", 33 ClusterName: "test-cluster", 34 Location: "test-location", 35 SKU: &resourceskus.SKU{}, 36 AdditionalTags: map[string]string{}, 37 } 38 ) 39 40 func TestParameters(t *testing.T) { 41 testcases := []struct { 42 name string 43 spec *AvailabilitySetSpec 44 existing interface{} 45 expect func(g *WithT, result interface{}) 46 expectedError string 47 }{ 48 { 49 name: "error when no SKU is present", 50 spec: &fakeSetSpecMissing, 51 existing: nil, 52 expect: func(g *WithT, result interface{}) { 53 g.Expect(result).To(BeNil()) 54 }, 55 expectedError: "unable to get required availability set SKU from machine cache", 56 }, 57 { 58 name: "error when SKU capability is missing", 59 spec: &fakeSetSpecMissingCap, 60 existing: nil, 61 expect: func(g *WithT, result interface{}) { 62 g.Expect(result).To(BeNil()) 63 }, 64 expectedError: "unable to get required availability set SKU capability MaximumPlatformFaultDomainCount", 65 }, 66 { 67 name: "get parameters when all values are present", 68 spec: &fakeSetSpec, 69 existing: nil, 70 expect: func(g *WithT, result interface{}) { 71 g.Expect(result).To(BeAssignableToTypeOf(armcompute.AvailabilitySet{})) 72 g.Expect(result.(armcompute.AvailabilitySet).Properties.PlatformFaultDomainCount).To(Equal(ptr.To[int32](int32(fakeFaultDomainCount)))) 73 }, 74 expectedError: "", 75 }, 76 } 77 for _, tc := range testcases { 78 tc := tc 79 t.Run(tc.name, func(t *testing.T) { 80 g := NewWithT(t) 81 t.Parallel() 82 83 result, err := tc.spec.Parameters(context.TODO(), tc.existing) 84 if tc.expectedError != "" { 85 g.Expect(err).To(HaveOccurred()) 86 g.Expect(err).To(MatchError(tc.expectedError)) 87 } else { 88 g.Expect(err).NotTo(HaveOccurred()) 89 } 90 tc.expect(g, result) 91 }) 92 } 93 }