From d2525441c2645928c37070d6494b47fa083dce68 Mon Sep 17 00:00:00 2001 From: Tyler Lloyd Date: Fri, 31 May 2024 08:27:53 -0400 Subject: [PATCH] test: refactor unit tests to use common OS VMSS builder --- pkg/provider/azure_vmss_test.go | 87 ++++++++++++++++----------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/pkg/provider/azure_vmss_test.go b/pkg/provider/azure_vmss_test.go index 0c71f4807b..658ef6fa70 100644 --- a/pkg/provider/azure_vmss_test.go +++ b/pkg/provider/azure_vmss_test.go @@ -58,6 +58,42 @@ const ( testLBBackendpoolID2 = "/subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Network/loadBalancers/lb/backendAddressPools/backendpool-2" ) +// helper enum for setting the OS variant +// of the VMSS image ref. +type osVersion int + +const ( + unspecified osVersion = iota + windows2019 + windows2022 + ubuntu +) + +func buildTestOSSpecificVMSSWithLB(name, namePrefix string, lbBackendpoolIDs []string, os osVersion, ipv6 bool) compute.VirtualMachineScaleSet { + vmss := buildTestVMSSWithLB(name, namePrefix, lbBackendpoolIDs, ipv6) + switch os { + case windows2019: + vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile = &compute.VirtualMachineScaleSetStorageProfile{ + OsDisk: &compute.VirtualMachineScaleSetOSDisk{ + OsType: compute.OperatingSystemTypesWindows, + }, + ImageReference: &compute.ImageReference{ + ID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/AKS-Windows/providers/Microsoft.Compute/galleries/AKSWindows/images/windows-2019-containerd/versions/17763.5820.240516"), + }, + } + case windows2022: + vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile = &compute.VirtualMachineScaleSetStorageProfile{ + OsDisk: &compute.VirtualMachineScaleSetOSDisk{ + OsType: compute.OperatingSystemTypesWindows, + }, + ImageReference: &compute.ImageReference{ + ID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/AKS-Windows/providers/Microsoft.Compute/galleries/AKSWindows/images/windows-2022-containerd/versions/20348.5820.240516"), + }, + } + } + return vmss +} + func buildTestVMSSWithLB(name, namePrefix string, lbBackendpoolIDs []string, ipv6 bool) compute.VirtualMachineScaleSet { lbBackendpoolsV4, lbBackendpoolsV6 := make([]compute.SubResource, 0), make([]compute.SubResource, 0) for _, id := range lbBackendpoolIDs { @@ -2347,7 +2383,7 @@ func TestEnsureVMSSInPool(t *testing.T) { vmSetName string clusterIP string nodes []*v1.Node - mutate func(compute.VirtualMachineScaleSet) + os osVersion isBasicLB bool isVMSSDeallocating bool isVMSSNilNICConfig bool @@ -2465,16 +2501,7 @@ func TestEnsureVMSSInPool(t *testing.T) { expectedPutVMSS: false, setIPv6Config: false, expectedErr: nil, - mutate: func(vmss compute.VirtualMachineScaleSet) { - vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile = &compute.VirtualMachineScaleSetStorageProfile{ - OsDisk: &compute.VirtualMachineScaleSetOSDisk{ - OsType: compute.OperatingSystemTypesWindows, - }, - ImageReference: &compute.ImageReference{ - ID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/AKS-Windows/providers/Microsoft.Compute/galleries/AKSWindows/images/windows-2019-containerd/versions/17763.5820.240516"), - }, - } - }, + os: windows2019, }, { description: "ensureVMSSInPool should add Windows2019 VM to IPv4 backend pool even if service is IPv6", @@ -2491,16 +2518,7 @@ func TestEnsureVMSSInPool(t *testing.T) { expectedPutVMSS: true, setIPv6Config: false, expectedErr: nil, - mutate: func(vmss compute.VirtualMachineScaleSet) { - vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile = &compute.VirtualMachineScaleSetStorageProfile{ - OsDisk: &compute.VirtualMachineScaleSetOSDisk{ - OsType: compute.OperatingSystemTypesWindows, - }, - ImageReference: &compute.ImageReference{ - ID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/AKS-Windows/providers/Microsoft.Compute/galleries/AKSWindows/images/windows-2019-containerd/versions/17763.5820.240516"), - }, - } - }, + os: windows2019, }, { description: "ensureVMSSInPool should add Windows 2022 VM to IPv6 backend pool", @@ -2517,16 +2535,7 @@ func TestEnsureVMSSInPool(t *testing.T) { expectedPutVMSS: true, setIPv6Config: true, expectedErr: nil, - mutate: func(vmss compute.VirtualMachineScaleSet) { - vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile = &compute.VirtualMachineScaleSetStorageProfile{ - OsDisk: &compute.VirtualMachineScaleSetOSDisk{ - OsType: compute.OperatingSystemTypesWindows, - }, - ImageReference: &compute.ImageReference{ - ID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/AKS-Windows/providers/Microsoft.Compute/galleries/AKSWindows/images/windows-2022-containerd/versions/20348.5820.240516"), - }, - } - }, + os: windows2022, }, { description: "ensureVMSSInPool should fail if no IPv6 network config - Windows 2022", @@ -2543,16 +2552,7 @@ func TestEnsureVMSSInPool(t *testing.T) { expectedPutVMSS: false, setIPv6Config: false, expectedErr: fmt.Errorf("failed to find a primary IP configuration (IPv6=true) for the VMSS VM or VMSS \"vmss\""), - mutate: func(vmss compute.VirtualMachineScaleSet) { - vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile = &compute.VirtualMachineScaleSetStorageProfile{ - OsDisk: &compute.VirtualMachineScaleSetOSDisk{ - OsType: compute.OperatingSystemTypesWindows, - }, - ImageReference: &compute.ImageReference{ - ID: ptr.To("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/AKS-Windows/providers/Microsoft.Compute/galleries/AKSWindows/images/windows-2022-containerd/versions/20348.5820.240516"), - }, - } - }, + os: windows2022, }, { description: "ensureVMSSInPool should update the VMSS correctly for IPv6", @@ -2638,10 +2638,7 @@ func TestEnsureVMSSInPool(t *testing.T) { ss.LoadBalancerSku = consts.LoadBalancerSkuStandard } - expectedVMSS := buildTestVMSSWithLB(testVMSSName, "vmss-vm-", []string{testLBBackendpoolID0}, test.setIPv6Config) - if test.mutate != nil { - test.mutate(expectedVMSS) - } + expectedVMSS := buildTestOSSpecificVMSSWithLB(testVMSSName, "vmss-vm-", []string{testLBBackendpoolID0}, test.os, test.setIPv6Config) if test.isVMSSDeallocating { expectedVMSS.ProvisioningState = pointer.String(consts.ProvisionStateDeleting) }