sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/privatedns/link_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  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    27  )
    28  
    29  var (
    30  	linkSpec = LinkSpec{
    31  		Name:              "my-link",
    32  		ZoneName:          "my-zone",
    33  		SubscriptionID:    "123",
    34  		VNetResourceGroup: "my-vnet-rg",
    35  		VNetName:          "my-vnet",
    36  		ResourceGroup:     "my-rg",
    37  		ClusterName:       "my-cluster",
    38  		AdditionalTags:    nil,
    39  	}
    40  )
    41  
    42  func TestLinkSpec_ResourceName(t *testing.T) {
    43  	g := NewWithT(t)
    44  	g.Expect(linkSpec.ResourceName()).Should(Equal("my-link"))
    45  }
    46  
    47  func TestLinkSpec_ResourceGroupName(t *testing.T) {
    48  	g := NewWithT(t)
    49  	g.Expect(linkSpec.ResourceGroupName()).Should(Equal("my-rg"))
    50  }
    51  
    52  func TestLinkSpec_OwnerResourceName(t *testing.T) {
    53  	g := NewWithT(t)
    54  	g.Expect(linkSpec.OwnerResourceName()).Should(Equal("my-zone"))
    55  }
    56  
    57  func TestLinkSpec_Parameters(t *testing.T) {
    58  	testcases := []struct {
    59  		name          string
    60  		spec          LinkSpec
    61  		existing      interface{}
    62  		expect        func(g *WithT, result interface{})
    63  		expectedError string
    64  	}{
    65  		{
    66  			name:          "new private dns virtual network link",
    67  			expectedError: "",
    68  			spec:          linkSpec,
    69  			expect: func(g *WithT, result interface{}) {
    70  				g.Expect(result).To(Equal(armprivatedns.VirtualNetworkLink{
    71  					Properties: &armprivatedns.VirtualNetworkLinkProperties{
    72  						VirtualNetwork: &armprivatedns.SubResource{
    73  							ID: ptr.To("/subscriptions/123/resourceGroups/my-vnet-rg/providers/Microsoft.Network/virtualNetworks/my-vnet"),
    74  						},
    75  						RegistrationEnabled: ptr.To(false),
    76  					},
    77  					Location: ptr.To(azure.Global),
    78  					Tags: map[string]*string{
    79  						"sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"),
    80  					},
    81  				}))
    82  			},
    83  		},
    84  		{
    85  			name:          "existing managed private virtual network link",
    86  			expectedError: "",
    87  			spec:          linkSpec,
    88  			existing: armprivatedns.VirtualNetworkLink{Tags: map[string]*string{
    89  				"sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"),
    90  			}},
    91  			expect: func(g *WithT, result interface{}) {
    92  				g.Expect(result).To(BeNil())
    93  			},
    94  		},
    95  		{
    96  			name:          "existing unmanaged private dns zone",
    97  			expectedError: "",
    98  			spec:          linkSpec,
    99  			existing:      armprivatedns.VirtualNetworkLink{},
   100  			expect: func(g *WithT, result interface{}) {
   101  				g.Expect(result).To(BeNil())
   102  			},
   103  		},
   104  		{
   105  			name:          "type cast error",
   106  			expectedError: "string is not an armprivatedns.VirtualNetworkLink",
   107  			spec:          linkSpec,
   108  			existing:      "I'm not armprivatedns.VirtualNetworkLink",
   109  		},
   110  	}
   111  
   112  	for _, tc := range testcases {
   113  		tc := tc
   114  		t.Run(tc.name, func(t *testing.T) {
   115  			g := NewWithT(t)
   116  			t.Parallel()
   117  
   118  			result, err := tc.spec.Parameters(context.TODO(), tc.existing)
   119  			if tc.expectedError != "" {
   120  				g.Expect(err).To(HaveOccurred())
   121  				g.Expect(err).To(MatchError(tc.expectedError))
   122  			} else {
   123  				g.Expect(err).NotTo(HaveOccurred())
   124  				tc.expect(g, result)
   125  			}
   126  		})
   127  	}
   128  }