sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/groups/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 groups 18 19 import ( 20 "context" 21 "testing" 22 23 asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601" 24 . "github.com/onsi/gomega" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/utils/ptr" 27 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 28 ) 29 30 func TestParameters(t *testing.T) { 31 tests := []struct { 32 name string 33 spec *GroupSpec 34 existing *asoresourcesv1.ResourceGroup 35 expected *asoresourcesv1.ResourceGroup 36 }{ 37 { 38 name: "no existing group", 39 spec: &GroupSpec{ 40 Name: "name", 41 AzureName: "azure-name", 42 Location: "location", 43 ClusterName: "cluster", 44 AdditionalTags: infrav1.Tags{"some": "tags"}, 45 }, 46 existing: nil, 47 expected: &asoresourcesv1.ResourceGroup{ 48 Spec: asoresourcesv1.ResourceGroup_Spec{ 49 AzureName: "azure-name", 50 Location: ptr.To("location"), 51 Tags: map[string]string{ 52 "some": "tags", 53 "sigs.k8s.io_cluster-api-provider-azure_cluster_cluster": "owned", 54 "sigs.k8s.io_cluster-api-provider-azure_role": "common", 55 "Name": "name", 56 }, 57 }, 58 }, 59 }, 60 { 61 name: "existing group", 62 existing: &asoresourcesv1.ResourceGroup{ 63 ObjectMeta: metav1.ObjectMeta{Name: "a unique name"}, 64 }, 65 expected: &asoresourcesv1.ResourceGroup{ 66 ObjectMeta: metav1.ObjectMeta{Name: "a unique name"}, 67 }, 68 }, 69 } 70 for _, test := range tests { 71 t.Run(test.name, func(t *testing.T) { 72 g := NewWithT(t) 73 74 actual, err := test.spec.Parameters(context.Background(), test.existing) 75 g.Expect(err).NotTo(HaveOccurred()) 76 if test.expected == nil { 77 g.Expect(actual).To(BeNil()) 78 } else { 79 g.Expect(actual).To(Equal(test.expected)) 80 } 81 }) 82 } 83 } 84 85 func TestWasManaged(t *testing.T) { 86 clusterName := "cluster" 87 88 tests := []struct { 89 name string 90 object *asoresourcesv1.ResourceGroup 91 expected bool 92 }{ 93 { 94 name: "no owned label", 95 object: &asoresourcesv1.ResourceGroup{}, 96 expected: false, 97 }, 98 { 99 name: "wrong owned label value", 100 object: &asoresourcesv1.ResourceGroup{ 101 Status: asoresourcesv1.ResourceGroup_STATUS{ 102 Tags: infrav1.Build(infrav1.BuildParams{ 103 ClusterName: clusterName, 104 Lifecycle: infrav1.ResourceLifecycle("not owned"), 105 }), 106 }, 107 }, 108 expected: false, 109 }, 110 { 111 name: "with owned label", 112 object: &asoresourcesv1.ResourceGroup{ 113 Status: asoresourcesv1.ResourceGroup_STATUS{ 114 Tags: infrav1.Build(infrav1.BuildParams{ 115 ClusterName: clusterName, 116 Lifecycle: infrav1.ResourceLifecycleOwned, 117 }), 118 }, 119 }, 120 expected: true, 121 }, 122 } 123 124 for _, test := range tests { 125 t.Run(test.name, func(t *testing.T) { 126 g := NewWithT(t) 127 128 s := &GroupSpec{ 129 ClusterName: clusterName, 130 } 131 132 g.Expect(s.WasManaged(test.object)).To(Equal(test.expected)) 133 }) 134 } 135 }