From 7950cdf449afffc7dec9b3de64ab320a5c3d1927 Mon Sep 17 00:00:00 2001 From: Smriti Dahal <93288516+smritidahal653@users.noreply.github.com> Date: Thu, 13 Apr 2023 17:40:57 -0700 Subject: [PATCH] test: added unit test for shouldCreateSubnet (#535) --- pkg/network/aci_network_test.go | 119 ++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/pkg/network/aci_network_test.go b/pkg/network/aci_network_test.go index d238f08e..c5626245 100644 --- a/pkg/network/aci_network_test.go +++ b/pkg/network/aci_network_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + aznetworkv2 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2" "github.com/google/uuid" "github.com/stretchr/testify/assert" testsutil "github.com/virtual-kubelet/azure-aci/pkg/tests" @@ -135,3 +136,121 @@ func TestFormDNSNameserversFitsLimits(t *testing.T) { assert.EqualValues(t, tc.expectedNameserver, appliedNameservers, tc.desc) } } + +func TestShouldCreateSubnet(t *testing.T) { + subnetName := "fakeSubnet" + fakeAddPrefix := "10.00.0/16" + providerSubnetCIDR := "10.00.0/17" + subnetDelegationService := "Microsoft.ContainerInstance/containerGroups" + fakeResourceType := "fakeResourceType" + + fakeServiceAssotiationLinks := []*aznetworkv2.ServiceAssociationLink{ + { + Properties: &aznetworkv2.ServiceAssociationLinkPropertiesFormat{ + LinkedResourceType: &fakeResourceType, + }, + }} + + currentSubnet := aznetworkv2.Subnet{ + Name: &subnetName, + } + + pn := ProviderNetwork{ + SubnetName: subnetName, + } + + cases := []struct { + description string + providerSubnetCIDR string + subnetProperties aznetworkv2.SubnetPropertiesFormat + expectedError error + expectedAssertions func(result bool) bool + }{ + { + description: "can create a subnet because all the checks pass", + providerSubnetCIDR: "", + subnetProperties: aznetworkv2.SubnetPropertiesFormat{ + AddressPrefix: &fakeAddPrefix, + }, + expectedAssertions: func(result bool) bool { + return assert.Equal(t, result, true, "subnet should be created") + }, + }, + { + description: "doesn't create a subnet because subnet is already linked to Microsoft.ContainerInstance/containerGroups", + providerSubnetCIDR: "", + subnetProperties: aznetworkv2.SubnetPropertiesFormat{ + AddressPrefix: &fakeAddPrefix, + ServiceAssociationLinks: []*aznetworkv2.ServiceAssociationLink{ + { + Properties: &aznetworkv2.ServiceAssociationLinkPropertiesFormat{ + LinkedResourceType: &subnetDelegationService, + }, + }}, + }, + expectedAssertions: func(result bool) bool { + return assert.Equal(t, result, false, "subnet should not be created because subnet already linked to Microsoft.ContainerInstance/containerGroups") + }, + }, + { + description: "doesn't create a subnet because subnet is being delegated to Microsoft.ContainerInstance/containerGroups", + providerSubnetCIDR: "", + subnetProperties: aznetworkv2.SubnetPropertiesFormat{ + AddressPrefix: &fakeAddPrefix, + Delegations: []*aznetworkv2.Delegation{ + { + Properties: &aznetworkv2.ServiceDelegationPropertiesFormat{ + ServiceName: &subnetDelegationService, + }, + }}, + }, + expectedAssertions: func(result bool) bool { + return assert.Equal(t, result, false, "subnet should not be created because subnet is being delegated to Microsoft.ContainerInstance/containerGroups") + }, + }, + { + description: "cannot create a subnet because Microsoft.ContainerInstance/containerGroups can't be delegated to the subnet", + providerSubnetCIDR: "", + subnetProperties: aznetworkv2.SubnetPropertiesFormat{ + AddressPrefix: &fakeAddPrefix, + ServiceAssociationLinks: fakeServiceAssotiationLinks, + }, + expectedError: fmt.Errorf("unable to delegate subnet '%s' to Azure Container Instance as it is used by other Azure resource: '%v'", pn.SubnetName, fakeServiceAssotiationLinks[0]), + }, + { + description: "cannot create subnet because current subnet references a route table", + providerSubnetCIDR: "", + subnetProperties: aznetworkv2.SubnetPropertiesFormat{ + AddressPrefix: &fakeAddPrefix, + RouteTable: &aznetworkv2.RouteTable{ + ID: &subnetName, + }, + }, + expectedError: fmt.Errorf("unable to delegate subnet '%s' to Azure Container Instance since it references the route table '%s'", pn.SubnetName, subnetName), + }, { + description: "cannot create subnet because provider subnet CIDR does not match with desired subnet", + providerSubnetCIDR: providerSubnetCIDR, + subnetProperties: aznetworkv2.SubnetPropertiesFormat{ + AddressPrefix: &fakeAddPrefix, + }, + expectedError: fmt.Errorf("found subnet '%s' using different CIDR: '%s'. desired: '%s'", pn.SubnetName, fakeAddPrefix, providerSubnetCIDR), + }, + } + for _, tc := range cases { + t.Run(tc.description, func(t *testing.T) { + pn.SubnetCIDR = tc.providerSubnetCIDR + currentSubnet.Properties = &tc.subnetProperties + + result, err := pn.shouldCreateSubnet(currentSubnet, true) + + if tc.expectedError != nil { + assert.Equal(t, err.Error(), tc.expectedError.Error(), "Error messages should match") + assert.Equal(t, result, false, "subnet should not be created") + } else { + assert.Equal(t, err, nil, "no error should be returned") + assert.Equal(t, tc.expectedAssertions(result), true, "Expected assertions should pass") + } + }) + } + +}