sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/roleassignments/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 roleassignments 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2" 24 . "github.com/onsi/gomega" 25 "k8s.io/utils/ptr" 26 ) 27 28 var ( 29 fakeRoleAssignment = armauthorization.RoleAssignment{ 30 ID: ptr.To("fake-id"), 31 Name: ptr.To("fake-name"), 32 Type: ptr.To("fake-type"), 33 } 34 fakeRoleAssignmentSpec = RoleAssignmentSpec{ 35 PrincipalID: ptr.To("fake-principal-id"), 36 RoleDefinitionID: "fake-role-definition-id", 37 } 38 ) 39 40 func TestRoleAssignmentSpec_Parameters(t *testing.T) { 41 testCases := []struct { 42 name string 43 spec *RoleAssignmentSpec 44 existing interface{} 45 expect func(g *WithT, result interface{}) 46 expectedError string 47 }{ 48 { 49 name: "error when existing is not of RoleAssignment type", 50 spec: &RoleAssignmentSpec{}, 51 existing: struct{}{}, 52 expect: func(g *WithT, result interface{}) { 53 g.Expect(result).To(BeNil()) 54 }, 55 expectedError: "struct {} is not an armauthorization.RoleAssignment", 56 }, 57 { 58 name: "get result as nil when existing NatGateway is present", 59 spec: &fakeRoleAssignmentSpec, 60 existing: fakeRoleAssignment, 61 expect: func(g *WithT, result interface{}) { 62 g.Expect(result).To(BeNil()) 63 }, 64 expectedError: "", 65 }, 66 { 67 name: "get result as nil when existing NatGateway is present with empty data", 68 spec: &fakeRoleAssignmentSpec, 69 existing: armauthorization.RoleAssignment{}, 70 expect: func(g *WithT, result interface{}) { 71 g.Expect(result).To(BeNil()) 72 }, 73 expectedError: "", 74 }, 75 { 76 name: "get RoleAssignment when all values are present", 77 spec: &fakeRoleAssignmentSpec, 78 existing: nil, 79 expect: func(g *WithT, result interface{}) { 80 g.Expect(result).To(BeAssignableToTypeOf(armauthorization.RoleAssignmentCreateParameters{})) 81 g.Expect(result.(armauthorization.RoleAssignmentCreateParameters).Properties.RoleDefinitionID).To(Equal(ptr.To[string](fakeRoleAssignmentSpec.RoleDefinitionID))) 82 g.Expect(result.(armauthorization.RoleAssignmentCreateParameters).Properties.PrincipalID).To(Equal(fakeRoleAssignmentSpec.PrincipalID)) 83 }, 84 expectedError: "", 85 }, 86 } 87 for _, tc := range testCases { 88 tc := tc 89 t.Run(tc.name, func(t *testing.T) { 90 g := NewWithT(t) 91 t.Parallel() 92 93 result, err := tc.spec.Parameters(context.TODO(), tc.existing) 94 if tc.expectedError != "" { 95 g.Expect(err).To(HaveOccurred()) 96 g.Expect(err).To(MatchError(tc.expectedError)) 97 } else { 98 g.Expect(err).NotTo(HaveOccurred()) 99 } 100 tc.expect(g, result) 101 }) 102 } 103 }