sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/privatedns/record_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 privatedns 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns" 24 . "github.com/onsi/gomega" 25 "k8s.io/utils/ptr" 26 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 27 ) 28 29 var ( 30 recordSpec = RecordSpec{ 31 Record: infrav1.AddressRecord{Hostname: "privatednsHostname", IP: "10.0.0.8"}, 32 ZoneName: "my-zone", 33 ResourceGroup: "my-rg", 34 } 35 36 recordSpecIpv6 = RecordSpec{ 37 Record: infrav1.AddressRecord{Hostname: "privatednsHostname", IP: "2603:1030:805:2::b"}, 38 ZoneName: "my-zone", 39 ResourceGroup: "my-rg", 40 } 41 ) 42 43 func TestRecordSpec_ResourceName(t *testing.T) { 44 g := NewWithT(t) 45 g.Expect(recordSpec.ResourceName()).Should(Equal("privatednsHostname")) 46 } 47 48 func TestRecordSpec_ResourceGroupName(t *testing.T) { 49 g := NewWithT(t) 50 g.Expect(recordSpec.ResourceGroupName()).Should(Equal("my-rg")) 51 } 52 53 func TestRecordSpec_OwnerResourceName(t *testing.T) { 54 g := NewWithT(t) 55 g.Expect(recordSpec.OwnerResourceName()).Should(Equal("my-zone")) 56 } 57 58 func TestRecordSpec_Parameters(t *testing.T) { 59 testcases := []struct { 60 name string 61 spec RecordSpec 62 existing interface{} 63 expect func(g *WithT, result interface{}) 64 expectedError string 65 }{ 66 { 67 name: "new private dns record for ipv4", 68 expectedError: "", 69 spec: recordSpec, 70 expect: func(g *WithT, result interface{}) { 71 g.Expect(result).To(Equal(armprivatedns.RecordSet{ 72 Properties: &armprivatedns.RecordSetProperties{ 73 TTL: ptr.To[int64](300), 74 ARecords: []*armprivatedns.ARecord{ 75 { 76 IPv4Address: ptr.To("10.0.0.8"), 77 }, 78 }, 79 }, 80 })) 81 }, 82 }, 83 { 84 name: "new private dns record for ipv6", 85 expectedError: "", 86 spec: recordSpecIpv6, 87 expect: func(g *WithT, result interface{}) { 88 g.Expect(result).To(Equal(armprivatedns.RecordSet{ 89 Properties: &armprivatedns.RecordSetProperties{ 90 TTL: ptr.To[int64](300), 91 AaaaRecords: []*armprivatedns.AaaaRecord{ 92 { 93 IPv6Address: ptr.To("2603:1030:805:2::b"), 94 }, 95 }, 96 }, 97 })) 98 }, 99 }, 100 } 101 102 for _, tc := range testcases { 103 tc := tc 104 t.Run(tc.name, func(t *testing.T) { 105 g := NewWithT(t) 106 t.Parallel() 107 108 result, err := tc.spec.Parameters(context.TODO(), tc.existing) 109 if tc.expectedError != "" { 110 g.Expect(err).To(HaveOccurred()) 111 g.Expect(err).To(MatchError(tc.expectedError)) 112 } else { 113 g.Expect(err).NotTo(HaveOccurred()) 114 tc.expect(g, result) 115 } 116 }) 117 } 118 }