sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/subnets/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 subnets 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 *SubnetSpec 35 existing *asonetworkv1.VirtualNetworksSubnet 36 expected *asonetworkv1.VirtualNetworksSubnet 37 }{ 38 { 39 name: "no existing subnet", 40 spec: &SubnetSpec{ 41 IsVNetManaged: true, 42 Name: "subnet", 43 SubscriptionID: "sub", 44 ResourceGroup: "rg", 45 VNetName: "vnet", 46 VNetResourceGroup: "vnet-rg", 47 CIDRs: []string{"cidr"}, 48 RouteTableName: "routetable", 49 NatGatewayName: "natgateway", 50 SecurityGroupName: "securitygroup", 51 ServiceEndpoints: infrav1.ServiceEndpoints{ 52 { 53 Service: "service", 54 Locations: []string{"location"}, 55 }, 56 }, 57 }, 58 existing: nil, 59 expected: &asonetworkv1.VirtualNetworksSubnet{ 60 Spec: asonetworkv1.VirtualNetworks_Subnet_Spec{ 61 AzureName: "subnet", 62 Owner: &genruntime.KnownResourceReference{ 63 Name: "vnet", 64 }, 65 AddressPrefixes: []string{"cidr"}, 66 AddressPrefix: ptr.To("cidr"), 67 RouteTable: &asonetworkv1.RouteTableSpec_VirtualNetworks_Subnet_SubResourceEmbedded{ 68 Reference: &genruntime.ResourceReference{ 69 ARMID: "/subscriptions/sub/resourceGroups/vnet-rg/providers/Microsoft.Network/routeTables/routetable", 70 }, 71 }, 72 NatGateway: &asonetworkv1.SubResource{ 73 Reference: &genruntime.ResourceReference{ 74 ARMID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/natGateways/natgateway", 75 }, 76 }, 77 NetworkSecurityGroup: &asonetworkv1.NetworkSecurityGroupSpec_VirtualNetworks_Subnet_SubResourceEmbedded{ 78 Reference: &genruntime.ResourceReference{ 79 ARMID: "/subscriptions/sub/resourceGroups/vnet-rg/providers/Microsoft.Network/networkSecurityGroups/securitygroup", 80 }, 81 }, 82 ServiceEndpoints: []asonetworkv1.ServiceEndpointPropertiesFormat{ 83 { 84 Service: ptr.To("service"), 85 Locations: []string{"location"}, 86 }, 87 }, 88 }, 89 }, 90 }, 91 { 92 name: "with existing subnet", 93 spec: &SubnetSpec{ 94 IsVNetManaged: true, 95 Name: "subnet", 96 SubscriptionID: "sub", 97 ResourceGroup: "rg", 98 VNetName: "vnet", 99 VNetResourceGroup: "vnet-rg", 100 CIDRs: []string{"cidr"}, 101 RouteTableName: "routetable", 102 NatGatewayName: "natgateway", 103 SecurityGroupName: "securitygroup", 104 ServiceEndpoints: infrav1.ServiceEndpoints{ 105 { 106 Service: "service", 107 Locations: []string{"location"}, 108 }, 109 }, 110 }, 111 existing: &asonetworkv1.VirtualNetworksSubnet{ 112 Status: asonetworkv1.VirtualNetworks_Subnet_STATUS{ 113 Id: ptr.To("status is preserved"), 114 }, 115 }, 116 expected: &asonetworkv1.VirtualNetworksSubnet{ 117 Spec: asonetworkv1.VirtualNetworks_Subnet_Spec{ 118 AzureName: "subnet", 119 Owner: &genruntime.KnownResourceReference{ 120 Name: "vnet", 121 }, 122 AddressPrefixes: []string{"cidr"}, 123 AddressPrefix: ptr.To("cidr"), 124 RouteTable: &asonetworkv1.RouteTableSpec_VirtualNetworks_Subnet_SubResourceEmbedded{ 125 Reference: &genruntime.ResourceReference{ 126 ARMID: "/subscriptions/sub/resourceGroups/vnet-rg/providers/Microsoft.Network/routeTables/routetable", 127 }, 128 }, 129 NatGateway: &asonetworkv1.SubResource{ 130 Reference: &genruntime.ResourceReference{ 131 ARMID: "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Network/natGateways/natgateway", 132 }, 133 }, 134 NetworkSecurityGroup: &asonetworkv1.NetworkSecurityGroupSpec_VirtualNetworks_Subnet_SubResourceEmbedded{ 135 Reference: &genruntime.ResourceReference{ 136 ARMID: "/subscriptions/sub/resourceGroups/vnet-rg/providers/Microsoft.Network/networkSecurityGroups/securitygroup", 137 }, 138 }, 139 ServiceEndpoints: []asonetworkv1.ServiceEndpointPropertiesFormat{ 140 { 141 Service: ptr.To("service"), 142 Locations: []string{"location"}, 143 }, 144 }, 145 }, 146 Status: asonetworkv1.VirtualNetworks_Subnet_STATUS{ 147 Id: ptr.To("status is preserved"), 148 }, 149 }, 150 }, 151 } 152 153 for _, test := range tests { 154 t.Run(test.name, func(t *testing.T) { 155 g := NewGomegaWithT(t) 156 157 result, err := test.spec.Parameters(context.Background(), test.existing) 158 g.Expect(err).NotTo(HaveOccurred()) 159 g.Expect(cmp.Diff(test.expected, result)).To(BeEmpty()) 160 }) 161 } 162 }