sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/publicips/spec_test.go (about) 1 /* 2 Copyright 2022 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 publicips 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 24 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4" 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 var ( 32 fakePublicIPSpecWithDNS = PublicIPSpec{ 33 Name: "my-publicip", 34 DNSName: "fakedns.mydomain.io", 35 Location: "centralIndia", 36 ClusterName: "my-cluster", 37 AdditionalTags: infrav1.Tags{ 38 "foo": "bar", 39 }, 40 FailureDomains: []*string{ptr.To("failure-domain-id-1"), ptr.To("failure-domain-id-2"), ptr.To("failure-domain-id-3")}, 41 } 42 43 fakePublicIPSpecWithoutDNS = PublicIPSpec{ 44 Name: "my-publicip-2", 45 Location: "centralIndia", 46 ClusterName: "my-cluster", 47 AdditionalTags: infrav1.Tags{ 48 "foo": "bar", 49 }, 50 FailureDomains: []*string{ptr.To("failure-domain-id-1"), ptr.To("failure-domain-id-2"), ptr.To("failure-domain-id-3")}, 51 } 52 53 fakePublicIPWithDNS = armnetwork.PublicIPAddress{ 54 Name: ptr.To("my-publicip"), 55 SKU: &armnetwork.PublicIPAddressSKU{Name: ptr.To(armnetwork.PublicIPAddressSKUNameStandard)}, 56 Location: ptr.To("centralIndia"), 57 Tags: map[string]*string{ 58 "Name": ptr.To("my-publicip"), 59 "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"), 60 "foo": ptr.To("bar"), 61 }, 62 Properties: &armnetwork.PublicIPAddressPropertiesFormat{ 63 PublicIPAddressVersion: ptr.To(armnetwork.IPVersionIPv4), 64 PublicIPAllocationMethod: ptr.To(armnetwork.IPAllocationMethodStatic), 65 DNSSettings: &armnetwork.PublicIPAddressDNSSettings{ 66 DomainNameLabel: ptr.To("fakedns"), 67 Fqdn: ptr.To("fakedns.mydomain.io"), 68 }, 69 }, 70 Zones: []*string{ptr.To("failure-domain-id-1"), ptr.To("failure-domain-id-2"), ptr.To("failure-domain-id-3")}, 71 } 72 73 fakePublicIPWithoutDNS = armnetwork.PublicIPAddress{ 74 Name: ptr.To("my-publicip-2"), 75 SKU: &armnetwork.PublicIPAddressSKU{Name: ptr.To(armnetwork.PublicIPAddressSKUNameStandard)}, 76 Location: ptr.To("centralIndia"), 77 Tags: map[string]*string{ 78 "Name": ptr.To("my-publicip-2"), 79 "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"), 80 "foo": ptr.To("bar"), 81 }, 82 Properties: &armnetwork.PublicIPAddressPropertiesFormat{ 83 PublicIPAddressVersion: ptr.To(armnetwork.IPVersionIPv4), 84 PublicIPAllocationMethod: ptr.To(armnetwork.IPAllocationMethodStatic), 85 }, 86 Zones: []*string{ptr.To("failure-domain-id-1"), ptr.To("failure-domain-id-2"), ptr.To("failure-domain-id-3")}, 87 } 88 89 fakePublicIPIpv6 = armnetwork.PublicIPAddress{ 90 Name: ptr.To("my-publicip-ipv6"), 91 SKU: &armnetwork.PublicIPAddressSKU{Name: ptr.To(armnetwork.PublicIPAddressSKUNameStandard)}, 92 Location: ptr.To("centralIndia"), 93 Tags: map[string]*string{ 94 "Name": ptr.To("my-publicip-ipv6"), 95 "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"), 96 "foo": ptr.To("bar"), 97 }, 98 Properties: &armnetwork.PublicIPAddressPropertiesFormat{ 99 PublicIPAddressVersion: ptr.To(armnetwork.IPVersionIPv6), 100 PublicIPAllocationMethod: ptr.To(armnetwork.IPAllocationMethodStatic), 101 DNSSettings: &armnetwork.PublicIPAddressDNSSettings{ 102 DomainNameLabel: ptr.To("fakename"), 103 Fqdn: ptr.To("fakename.mydomain.io"), 104 }, 105 }, 106 Zones: []*string{ptr.To("failure-domain-id-1"), ptr.To("failure-domain-id-2"), ptr.To("failure-domain-id-3")}, 107 } 108 ) 109 110 func TestParameters(t *testing.T) { 111 testCases := []struct { 112 name string 113 existing interface{} 114 spec PublicIPSpec 115 expected interface{} 116 expectedError string 117 }{ 118 { 119 name: "noop if public IP exists", 120 existing: fakePublicIPWithDNS, 121 spec: fakePublicIPSpecWithDNS, 122 expected: nil, 123 expectedError: "", 124 }, 125 { 126 name: "public ipv4 address with dns", 127 existing: nil, 128 spec: fakePublicIPSpecWithDNS, 129 expected: fakePublicIPWithDNS, 130 expectedError: "", 131 }, 132 { 133 name: "public ipv4 address without dns", 134 existing: nil, 135 spec: fakePublicIPSpecWithoutDNS, 136 expected: fakePublicIPWithoutDNS, 137 expectedError: "", 138 }, 139 { 140 name: "public ipv6 address with dns", 141 existing: nil, 142 spec: fakePublicIPSpecIpv6, // In publicips_test.go 143 expected: fakePublicIPIpv6, 144 expectedError: "", 145 }, 146 } 147 148 for _, tc := range testCases { 149 tc := tc 150 t.Run(tc.name, func(t *testing.T) { 151 g := NewWithT(t) 152 t.Parallel() 153 154 result, err := tc.spec.Parameters(context.TODO(), tc.existing) 155 if tc.expectedError != "" { 156 g.Expect(err).To(HaveOccurred()) 157 g.Expect(err).To(MatchError(tc.expectedError)) 158 } else { 159 g.Expect(err).NotTo(HaveOccurred()) 160 } 161 162 if !reflect.DeepEqual(result, tc.expected) { 163 t.Errorf("Diff between expected result and actual result:\n%s", cmp.Diff(tc.expected, result)) 164 } 165 }) 166 } 167 }