sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/natgateways/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 natgateways 18 19 import ( 20 "context" 21 "testing" 22 23 asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701" 24 "github.com/Azure/azure-service-operator/v2/pkg/genruntime" 25 "github.com/google/go-cmp/cmp" 26 . "github.com/onsi/gomega" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/utils/ptr" 29 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 30 ) 31 32 var ( 33 fakeNatGatewaySpec = &NatGatewaySpec{ 34 Name: "my-natgateway", 35 ResourceGroup: "my-rg", 36 SubscriptionID: "123", 37 Location: "eastus", 38 NatGatewayIP: infrav1.PublicIPSpec{ 39 Name: "my-natgateway-ip", 40 DNSName: "Standard", 41 }, 42 ClusterName: "my-cluster", 43 IsVnetManaged: true, 44 AdditionalTags: infrav1.Tags{}, 45 } 46 locationPtr = ptr.To("eastus") 47 standardSKUPtr = ptr.To(asonetworkv1.NatGatewaySku_Name_Standard) 48 existingNatGateway = &asonetworkv1.NatGateway{ 49 TypeMeta: metav1.TypeMeta{ 50 APIVersion: "network.azure.com/v1api20220701", 51 Kind: "NatGateway", 52 }, 53 ObjectMeta: metav1.ObjectMeta{ 54 Name: "my-natgateway", 55 Namespace: "dummy-ns", 56 }, 57 Spec: asonetworkv1.NatGateway_Spec{ 58 AzureName: "my-natgateway", 59 IdleTimeoutInMinutes: ptr.To(6), 60 Location: locationPtr, 61 Owner: &genruntime.KnownResourceReference{ 62 Name: "my-rg", 63 }, 64 PublicIpAddresses: []asonetworkv1.ApplicationGatewaySubResource{ 65 { 66 Reference: &genruntime.ResourceReference{ 67 ARMID: "/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Network/publicIPAddresses/my-natgateway-ip", 68 }, 69 }, 70 }, 71 Sku: &asonetworkv1.NatGatewaySku{ 72 Name: ptr.To(asonetworkv1.NatGatewaySku_Name_Standard), 73 }, 74 Tags: map[string]string{ 75 "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": "owned", 76 "Name": "my-natgateway", 77 }, 78 }, 79 Status: asonetworkv1.NatGateway_STATUS{ 80 Id: ptr.To("/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Network/natGateways/test-1111-node-natgw-1"), 81 IdleTimeoutInMinutes: ptr.To(4), 82 Location: locationPtr, 83 Name: ptr.To("my-natgateway"), 84 ProvisioningState: ptr.To(asonetworkv1.ApplicationGatewayProvisioningState_STATUS_Succeeded), 85 PublicIpAddresses: []asonetworkv1.ApplicationGatewaySubResource_STATUS{ 86 { 87 Id: ptr.To("/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Network/publicIPAddresses/my-natgateway-ip"), 88 }, 89 }, 90 Sku: &asonetworkv1.NatGatewaySku_STATUS{ 91 Name: ptr.To(asonetworkv1.NatGatewaySku_Name_STATUS_Standard), 92 }, 93 }, 94 } 95 ) 96 97 func TestParameters(t *testing.T) { 98 testcases := []struct { 99 name string 100 spec *NatGatewaySpec 101 existingSpec *asonetworkv1.NatGateway 102 expect func(g *WithT, existing *asonetworkv1.NatGateway, parameters *asonetworkv1.NatGateway) 103 }{ 104 { 105 name: "create a new NAT Gateway spec when existing aso resource is nil", 106 spec: fakeNatGatewaySpec, 107 existingSpec: nil, 108 expect: func(g *WithT, existing *asonetworkv1.NatGateway, parameters *asonetworkv1.NatGateway) { 109 g.Expect(parameters).NotTo(BeNil()) 110 g.Expect(parameters.Spec.AzureName).NotTo(BeNil()) 111 g.Expect(parameters.Spec.AzureName).To(Equal("my-natgateway")) 112 g.Expect(parameters.Spec.Owner).NotTo(BeNil()) 113 g.Expect(parameters.Spec.Owner.Name).To(Equal("my-rg")) 114 g.Expect(parameters.Spec.Location).NotTo(BeNil()) 115 g.Expect(parameters.Spec.Location).To(Equal(locationPtr)) 116 g.Expect(parameters.Spec.Sku.Name).NotTo(BeNil()) 117 g.Expect(parameters.Spec.Sku.Name).To(Equal(standardSKUPtr)) 118 g.Expect(parameters.Spec.PublicIpAddresses).To(HaveLen(1)) 119 g.Expect(parameters.Spec.PublicIpAddresses[0].Reference.ARMID).To(Equal("/subscriptions/123/resourceGroups/my-rg/providers/Microsoft.Network/publicIPAddresses/my-natgateway-ip")) 120 g.Expect(parameters.Spec.Tags).NotTo(BeEmpty()) 121 g.Expect(parameters.Spec.Tags).To(HaveKeyWithValue("Name", "my-natgateway")) 122 g.Expect(parameters.Spec.Tags).To(HaveKeyWithValue("sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster", "owned")) 123 }, 124 }, 125 { 126 name: "reconcile a NAT Gateway spec when there is an existing aso resource. User added extra spec fields", 127 spec: fakeNatGatewaySpec, 128 existingSpec: existingNatGateway, 129 expect: func(g *WithT, existing *asonetworkv1.NatGateway, parameters *asonetworkv1.NatGateway) { 130 diff := cmp.Diff(existing, parameters) 131 g.Expect(diff).To(BeEmpty()) 132 }, 133 }, 134 } 135 for _, tc := range testcases { 136 tc := tc 137 t.Run(tc.name, func(t *testing.T) { 138 g := NewWithT(t) 139 t.Parallel() 140 141 result, _ := tc.spec.Parameters(context.TODO(), tc.existingSpec.DeepCopy()) 142 tc.expect(g, tc.existingSpec, result) 143 }) 144 } 145 }