sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/vnetpeerings/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 vnetpeerings
    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  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    27  )
    28  
    29  var (
    30  	fakeVnetPeering = armnetwork.VirtualNetworkPeering{
    31  		ID:   ptr.To("fake-id"),
    32  		Name: ptr.To("fake-name"),
    33  		Type: ptr.To("fake-type"),
    34  	}
    35  	fakeVnetPeeringSpec = VnetPeeringSpec{
    36  		PeeringName:               "hub-to-spoke",
    37  		RemoteVnetName:            "spoke-vnet",
    38  		RemoteResourceGroup:       "spoke-group",
    39  		SubscriptionID:            "sub1",
    40  		AllowForwardedTraffic:     ptr.To(true),
    41  		AllowGatewayTransit:       ptr.To(true),
    42  		AllowVirtualNetworkAccess: ptr.To(true),
    43  		UseRemoteGateways:         ptr.To(false),
    44  	}
    45  )
    46  
    47  func TestVnetPeeringSpec_Parameters(t *testing.T) {
    48  	testCases := []struct {
    49  		name          string
    50  		spec          *VnetPeeringSpec
    51  		existing      interface{}
    52  		expect        func(g *WithT, result interface{})
    53  		expectedError string
    54  	}{
    55  		{
    56  			name:     "error when existing is not of VnetPeering type",
    57  			spec:     &VnetPeeringSpec{},
    58  			existing: struct{}{},
    59  			expect: func(g *WithT, result interface{}) {
    60  				g.Expect(result).To(BeNil())
    61  			},
    62  			expectedError: "struct {} is not an armnetwork.VnetPeering",
    63  		},
    64  		{
    65  			name:     "get result as nil when existing VnetPeering is present",
    66  			spec:     &fakeVnetPeeringSpec,
    67  			existing: fakeVnetPeering,
    68  			expect: func(g *WithT, result interface{}) {
    69  				g.Expect(result).To(BeNil())
    70  			},
    71  			expectedError: "",
    72  		},
    73  		{
    74  			name:     "get result as nil when existing VnetPeering is present with empty data",
    75  			spec:     &fakeVnetPeeringSpec,
    76  			existing: armnetwork.VirtualNetworkPeering{},
    77  			expect: func(g *WithT, result interface{}) {
    78  				g.Expect(result).To(BeNil())
    79  			},
    80  			expectedError: "",
    81  		},
    82  		{
    83  			name:     "get VirtualNetworkPeering when all values are present",
    84  			spec:     &fakeVnetPeeringSpec,
    85  			existing: nil,
    86  			expect: func(g *WithT, result interface{}) {
    87  				g.Expect(result).To(BeAssignableToTypeOf(armnetwork.VirtualNetworkPeering{}))
    88  				g.Expect(result.(armnetwork.VirtualNetworkPeering).Name).To(Equal(ptr.To[string](fakeVnetPeeringSpec.ResourceName())))
    89  				g.Expect(result.(armnetwork.VirtualNetworkPeering).Properties.RemoteVirtualNetwork.ID).To(Equal(ptr.To[string](azure.VNetID(fakeVnetPeeringSpec.SubscriptionID,
    90  					fakeVnetPeeringSpec.RemoteResourceGroup, fakeVnetPeeringSpec.RemoteVnetName))))
    91  				g.Expect(result.(armnetwork.VirtualNetworkPeering).Properties.AllowForwardedTraffic).To(Equal(fakeVnetPeeringSpec.AllowForwardedTraffic))
    92  				g.Expect(result.(armnetwork.VirtualNetworkPeering).Properties.AllowGatewayTransit).To(Equal(fakeVnetPeeringSpec.AllowGatewayTransit))
    93  				g.Expect(result.(armnetwork.VirtualNetworkPeering).Properties.AllowVirtualNetworkAccess).To(Equal(fakeVnetPeeringSpec.AllowVirtualNetworkAccess))
    94  				g.Expect(result.(armnetwork.VirtualNetworkPeering).Properties.UseRemoteGateways).To(Equal(fakeVnetPeeringSpec.UseRemoteGateways))
    95  			},
    96  			expectedError: "",
    97  		},
    98  	}
    99  	for _, tc := range testCases {
   100  		tc := tc
   101  		t.Run(tc.name, func(t *testing.T) {
   102  			g := NewWithT(t)
   103  			t.Parallel()
   104  
   105  			result, err := tc.spec.Parameters(context.TODO(), tc.existing)
   106  			if tc.expectedError != "" {
   107  				g.Expect(err).To(HaveOccurred())
   108  				g.Expect(err).To(MatchError(tc.expectedError))
   109  			} else {
   110  				g.Expect(err).NotTo(HaveOccurred())
   111  			}
   112  			tc.expect(g, result)
   113  		})
   114  	}
   115  }