sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/routetables/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 routetables
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
    24  	. "github.com/onsi/gomega"
    25  	"k8s.io/utils/ptr"
    26  )
    27  
    28  var (
    29  	fakeRouteTable = armnetwork.RouteTable{
    30  		ID:       ptr.To("fake-id"),
    31  		Location: ptr.To("fake-location"),
    32  		Name:     ptr.To("fake-name"),
    33  	}
    34  	fakeRouteTableSpec = RouteTableSpec{
    35  		Name:        "test-rt-1",
    36  		Location:    "fake-location",
    37  		ClusterName: "cluster",
    38  		AdditionalTags: map[string]string{
    39  			"foo": "bar",
    40  		},
    41  	}
    42  	fakeRouteTableTags = map[string]*string{
    43  		"sigs.k8s.io_cluster-api-provider-azure_cluster_cluster": ptr.To("owned"),
    44  		"foo":  ptr.To("bar"),
    45  		"Name": ptr.To("test-rt-1"),
    46  	}
    47  )
    48  
    49  func TestRouteTableSpec_Parameters(t *testing.T) {
    50  	testCases := []struct {
    51  		name          string
    52  		spec          *RouteTableSpec
    53  		existing      interface{}
    54  		expect        func(g *WithT, result interface{})
    55  		expectedError string
    56  	}{
    57  		{
    58  			name:     "error when existing is not of RouteTable type",
    59  			spec:     &RouteTableSpec{},
    60  			existing: struct{}{},
    61  			expect: func(g *WithT, result interface{}) {
    62  				g.Expect(result).To(BeNil())
    63  			},
    64  			expectedError: "struct {} is not an armnetwork.RouteTable",
    65  		},
    66  		{
    67  			name:     "get result as nil when existing RouteTable is present",
    68  			spec:     &fakeRouteTableSpec,
    69  			existing: fakeRouteTable,
    70  			expect: func(g *WithT, result interface{}) {
    71  				g.Expect(result).To(BeNil())
    72  			},
    73  			expectedError: "",
    74  		},
    75  		{
    76  			name:     "get result as nil when existing RouteTable is present with empty data",
    77  			spec:     &fakeRouteTableSpec,
    78  			existing: armnetwork.RouteTable{},
    79  			expect: func(g *WithT, result interface{}) {
    80  				g.Expect(result).To(BeNil())
    81  			},
    82  			expectedError: "",
    83  		},
    84  		{
    85  			name:     "get RouteTable when all values are present",
    86  			spec:     &fakeRouteTableSpec,
    87  			existing: nil,
    88  			expect: func(g *WithT, result interface{}) {
    89  				g.Expect(result).To(BeAssignableToTypeOf(armnetwork.RouteTable{}))
    90  				g.Expect(result.(armnetwork.RouteTable).Location).To(Equal(ptr.To[string](fakeRouteTableSpec.Location)))
    91  				g.Expect(result.(armnetwork.RouteTable).Tags).To(Equal(fakeRouteTableTags))
    92  			},
    93  			expectedError: "",
    94  		},
    95  	}
    96  	for _, tc := range testCases {
    97  		tc := tc
    98  		t.Run(tc.name, func(t *testing.T) {
    99  			g := NewWithT(t)
   100  			t.Parallel()
   101  
   102  			result, err := tc.spec.Parameters(context.TODO(), tc.existing)
   103  			if tc.expectedError != "" {
   104  				g.Expect(err).To(HaveOccurred())
   105  				g.Expect(err).To(MatchError(tc.expectedError))
   106  			} else {
   107  				g.Expect(err).NotTo(HaveOccurred())
   108  			}
   109  			tc.expect(g, result)
   110  		})
   111  	}
   112  }