sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/virtualnetworks/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 virtualnetworks 18 19 import ( 20 "context" 21 "testing" 22 23 asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101" 24 "github.com/Azure/azure-service-operator/v2/pkg/genruntime" 25 "github.com/google/go-cmp/cmp" 26 . "github.com/onsi/gomega" 27 "k8s.io/utils/ptr" 28 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 29 ) 30 31 func TestParameters(t *testing.T) { 32 tests := []struct { 33 name string 34 spec VNetSpec 35 existing *asonetworkv1.VirtualNetwork 36 expected *asonetworkv1.VirtualNetwork 37 }{ 38 { 39 name: "new vnet", 40 spec: VNetSpec{ 41 ResourceGroup: "rg", 42 Name: "name", 43 CIDRs: []string{"cidr"}, 44 Location: "location", 45 ExtendedLocation: &infrav1.ExtendedLocationSpec{ 46 Name: "loc-name", 47 Type: "loc-type", 48 }, 49 ClusterName: "cluster", 50 AdditionalTags: map[string]string{"my": "tag"}, 51 }, 52 expected: &asonetworkv1.VirtualNetwork{ 53 Spec: asonetworkv1.VirtualNetwork_Spec{ 54 Tags: map[string]string{ 55 "my": "tag", 56 "sigs.k8s.io_cluster-api-provider-azure_cluster_cluster": "owned", 57 "sigs.k8s.io_cluster-api-provider-azure_role": "common", 58 "Name": "name", 59 }, 60 AzureName: "name", 61 Owner: &genruntime.KnownResourceReference{ 62 Name: "rg", 63 }, 64 Location: ptr.To("location"), 65 ExtendedLocation: &asonetworkv1.ExtendedLocation{ 66 Name: ptr.To("loc-name"), 67 Type: ptr.To(asonetworkv1.ExtendedLocationType("loc-type")), 68 }, 69 AddressSpace: &asonetworkv1.AddressSpace{ 70 AddressPrefixes: []string{"cidr"}, 71 }, 72 }, 73 }, 74 }, 75 { 76 name: "from existing vnet", 77 spec: VNetSpec{ 78 ResourceGroup: "rg", 79 Name: "name", 80 CIDRs: []string{"cidr"}, 81 Location: "location", 82 ExtendedLocation: &infrav1.ExtendedLocationSpec{ 83 Name: "loc-name", 84 Type: "loc-type", 85 }, 86 ClusterName: "cluster", 87 AdditionalTags: map[string]string{"my": "tag"}, 88 }, 89 existing: &asonetworkv1.VirtualNetwork{ 90 Spec: asonetworkv1.VirtualNetwork_Spec{ 91 Tags: map[string]string{ 92 "tags": "set", 93 "by": "user", 94 }, 95 }, 96 }, 97 expected: &asonetworkv1.VirtualNetwork{ 98 Spec: asonetworkv1.VirtualNetwork_Spec{ 99 Tags: map[string]string{ 100 "tags": "set", 101 "by": "user", 102 }, 103 AzureName: "name", 104 Owner: &genruntime.KnownResourceReference{ 105 Name: "rg", 106 }, 107 Location: ptr.To("location"), 108 ExtendedLocation: &asonetworkv1.ExtendedLocation{ 109 Name: ptr.To("loc-name"), 110 Type: ptr.To(asonetworkv1.ExtendedLocationType("loc-type")), 111 }, 112 AddressSpace: &asonetworkv1.AddressSpace{ 113 AddressPrefixes: []string{"cidr"}, 114 }, 115 }, 116 }, 117 }, 118 } 119 120 for _, test := range tests { 121 t.Run(test.name, func(t *testing.T) { 122 g := NewGomegaWithT(t) 123 actual, err := test.spec.Parameters(context.Background(), test.existing) 124 g.Expect(err).NotTo(HaveOccurred()) 125 g.Expect(cmp.Diff(test.expected, actual)).To(BeEmpty()) 126 }) 127 } 128 }