sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/privatedns/zone_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  	zoneSpec = ZoneSpec{
    31  		Name:           "my-zone",
    32  		ResourceGroup:  "my-rg",
    33  		ClusterName:    "my-cluster",
    34  		AdditionalTags: nil,
    35  	}
    36  )
    37  
    38  func TestZoneSpec_ResourceName(t *testing.T) {
    39  	g := NewWithT(t)
    40  	g.Expect(zoneSpec.ResourceName()).Should(Equal("my-zone"))
    41  }
    42  
    43  func TestZoneSpec_ResourceGroupName(t *testing.T) {
    44  	g := NewWithT(t)
    45  	g.Expect(zoneSpec.ResourceGroupName()).Should(Equal("my-rg"))
    46  }
    47  
    48  func TestZoneSpec_OwnerResourceName(t *testing.T) {
    49  	g := NewWithT(t)
    50  	g.Expect(zoneSpec.OwnerResourceName()).Should(Equal(""))
    51  }
    52  
    53  func TestZoneSpec_Parameters(t *testing.T) {
    54  	testcases := []struct {
    55  		name          string
    56  		spec          ZoneSpec
    57  		existing      interface{}
    58  		expect        func(g *WithT, result interface{})
    59  		expectedError string
    60  	}{
    61  		{
    62  			name:          "new private dns zone",
    63  			expectedError: "",
    64  			spec:          zoneSpec,
    65  			expect: func(g *WithT, result interface{}) {
    66  				g.Expect(result).To(Equal(armprivatedns.PrivateZone{
    67  					Location: ptr.To(azure.Global),
    68  					Tags: map[string]*string{
    69  						"sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"),
    70  					},
    71  				}))
    72  			},
    73  		},
    74  		{
    75  			name:          "existing managed private dns zone",
    76  			expectedError: "",
    77  			spec:          zoneSpec,
    78  			existing: armprivatedns.PrivateZone{Tags: map[string]*string{
    79  				"sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": ptr.To("owned"),
    80  			}},
    81  			expect: func(g *WithT, result interface{}) {
    82  				g.Expect(result).To(BeNil())
    83  			},
    84  		},
    85  		{
    86  			name:          "existing unmanaged private dns zone",
    87  			expectedError: "",
    88  			spec:          zoneSpec,
    89  			existing:      armprivatedns.PrivateZone{},
    90  			expect: func(g *WithT, result interface{}) {
    91  				g.Expect(result).To(BeNil())
    92  			},
    93  		},
    94  		{
    95  			name:          "type cast error",
    96  			expectedError: "string is not an armprivatedns.PrivateZone",
    97  			spec:          zoneSpec,
    98  			existing:      "I'm not armprivatedns.PrivateZone",
    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  }