sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/identity_test.go (about) 1 /* 2 Copyright 2020 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 converters 18 19 import ( 20 "testing" 21 22 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5" 23 . "github.com/onsi/gomega" 24 "k8s.io/utils/ptr" 25 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 26 ) 27 28 var sampleSubjectFactory = []infrav1.UserAssignedIdentity{ 29 { 30 ProviderID: "azure:///foo", 31 }, 32 { 33 ProviderID: "azure:///bar", 34 }, 35 { 36 ProviderID: "/without/prefix", 37 }, 38 } 39 40 var expectedVMSDKObject = map[string]*armcompute.UserAssignedIdentitiesValue{ 41 "/foo": {}, 42 "/bar": {}, 43 "/without/prefix": {}, 44 } 45 46 var expectedVMSSSDKObject = map[string]*armcompute.UserAssignedIdentitiesValue{ 47 "/foo": {}, 48 "/bar": {}, 49 "/without/prefix": {}, 50 } 51 52 func Test_VMIdentityToVMSDK(t *testing.T) { 53 cases := []struct { 54 Name string 55 identityType infrav1.VMIdentity 56 uami []infrav1.UserAssignedIdentity 57 Expect func(*GomegaWithT, *armcompute.VirtualMachineIdentity, error) 58 }{ 59 { 60 Name: "Should return a system assigned identity when identity is system assigned", 61 identityType: infrav1.VMIdentitySystemAssigned, 62 Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) { 63 g.Expect(err).NotTo(HaveOccurred()) 64 g.Expect(m).Should(Equal(&armcompute.VirtualMachineIdentity{ 65 Type: ptr.To(armcompute.ResourceIdentityTypeSystemAssigned), 66 })) 67 }, 68 }, 69 { 70 Name: "Should return user assigned identities when identity is user assigned", 71 identityType: infrav1.VMIdentityUserAssigned, 72 uami: []infrav1.UserAssignedIdentity{{ProviderID: "my-uami-1"}, {ProviderID: "my-uami-2"}}, 73 Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) { 74 g.Expect(err).NotTo(HaveOccurred()) 75 g.Expect(m).Should(Equal(&armcompute.VirtualMachineIdentity{ 76 Type: ptr.To(armcompute.ResourceIdentityTypeUserAssigned), 77 UserAssignedIdentities: map[string]*armcompute.UserAssignedIdentitiesValue{ 78 "my-uami-1": {}, 79 "my-uami-2": {}, 80 }, 81 })) 82 }, 83 }, 84 { 85 Name: "Should fail when no user assigned identities are specified and identity is user assigned", 86 identityType: infrav1.VMIdentityUserAssigned, 87 uami: []infrav1.UserAssignedIdentity{}, 88 Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) { 89 g.Expect(err.Error()).Should(ContainSubstring(ErrUserAssignedIdentitiesNotFound.Error())) 90 }, 91 }, 92 { 93 Name: "Should return nil if no known identity is specified", 94 identityType: "", 95 Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) { 96 g.Expect(err).NotTo(HaveOccurred()) 97 g.Expect(m).Should(BeNil()) 98 }, 99 }, 100 } 101 102 for _, c := range cases { 103 c := c 104 t.Run(c.Name, func(t *testing.T) { 105 t.Parallel() 106 g := NewGomegaWithT(t) 107 subject, err := VMIdentityToVMSDK(c.identityType, c.uami) 108 c.Expect(g, subject, err) 109 }) 110 } 111 } 112 113 func Test_UserAssignedIdentitiesToVMSDK(t *testing.T) { 114 cases := []struct { 115 Name string 116 SubjectFactory []infrav1.UserAssignedIdentity 117 Expect func(*GomegaWithT, map[string]*armcompute.UserAssignedIdentitiesValue, error) 118 }{ 119 { 120 Name: "ShouldPopulateWithData", 121 SubjectFactory: sampleSubjectFactory, 122 Expect: func(g *GomegaWithT, m map[string]*armcompute.UserAssignedIdentitiesValue, err error) { 123 g.Expect(err).NotTo(HaveOccurred()) 124 g.Expect(m).Should(Equal(expectedVMSDKObject)) 125 }, 126 }, 127 128 { 129 Name: "ShouldFailWithError", 130 SubjectFactory: []infrav1.UserAssignedIdentity{}, 131 Expect: func(g *GomegaWithT, m map[string]*armcompute.UserAssignedIdentitiesValue, err error) { 132 g.Expect(err).Should(Equal(ErrUserAssignedIdentitiesNotFound)) 133 }, 134 }, 135 } 136 137 for _, c := range cases { 138 c := c 139 t.Run(c.Name, func(t *testing.T) { 140 t.Parallel() 141 g := NewGomegaWithT(t) 142 subject, err := UserAssignedIdentitiesToVMSDK(c.SubjectFactory) 143 c.Expect(g, subject, err) 144 }) 145 } 146 } 147 148 func Test_UserAssignedIdentitiesToVMSSSDK(t *testing.T) { 149 cases := []struct { 150 Name string 151 SubjectFactory []infrav1.UserAssignedIdentity 152 Expect func(*GomegaWithT, map[string]*armcompute.UserAssignedIdentitiesValue, error) 153 }{ 154 { 155 Name: "ShouldPopulateWithData", 156 SubjectFactory: sampleSubjectFactory, 157 Expect: func(g *GomegaWithT, m map[string]*armcompute.UserAssignedIdentitiesValue, err error) { 158 g.Expect(err).NotTo(HaveOccurred()) 159 g.Expect(m).Should(Equal(expectedVMSSSDKObject)) 160 }, 161 }, 162 163 { 164 Name: "ShouldFailWithError", 165 SubjectFactory: []infrav1.UserAssignedIdentity{}, 166 Expect: func(g *GomegaWithT, m map[string]*armcompute.UserAssignedIdentitiesValue, err error) { 167 g.Expect(err).Should(Equal(ErrUserAssignedIdentitiesNotFound)) 168 }, 169 }, 170 } 171 172 for _, c := range cases { 173 c := c 174 t.Run(c.Name, func(t *testing.T) { 175 t.Parallel() 176 g := NewGomegaWithT(t) 177 subject, err := UserAssignedIdentitiesToVMSSSDK(c.SubjectFactory) 178 c.Expect(g, subject, err) 179 }) 180 } 181 }