From e2c733a702ebe5f4275c71f1ea91772447f58c04 Mon Sep 17 00:00:00 2001 From: ZeroMagic Date: Wed, 19 Feb 2020 11:24:29 +0000 Subject: [PATCH] test: export a fake Azure cloud Via the exported GetTestCloud(), we can reuse the code for the unit tests in Azure related CSI drivers. Signed-off-by: ZeroMagic --- .../k8s.io/legacy-cloud-providers/azure/BUILD | 1 - .../azure/azure_controller_common_test.go | 10 +- .../azure/azure_controller_standard_test.go | 6 +- .../azure/azure_fakes.go | 59 ++++++++ .../azure/azure_instances_test.go | 6 +- .../azure/azure_loadbalancer_test.go | 28 ++-- .../azure/azure_standard_test.go | 8 +- .../azure/azure_test.go | 138 ++++++------------ .../azure/azure_vmss_test.go | 2 +- .../azure/azure_wrap_test.go | 4 +- 10 files changed, 135 insertions(+), 127 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD b/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD index a44e91ee135..c8fae57ba9a 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD @@ -120,7 +120,6 @@ go_test( "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", "//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library", - "//staging/src/k8s.io/client-go/tools/record:go_default_library", "//staging/src/k8s.io/cloud-provider:go_default_library", "//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library", "//staging/src/k8s.io/legacy-cloud-providers/azure/auth:go_default_library", diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common_test.go index 0815d83a2c8..3b0162c7a0a 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common_test.go @@ -63,7 +63,7 @@ func TestCommonAttachDisk(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() common := &controllerCommon{ location: testCloud.Location, storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix, @@ -112,7 +112,7 @@ func TestCommonDetachDisk(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() common := &controllerCommon{ location: testCloud.Location, storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix, @@ -153,7 +153,7 @@ func TestGetDiskLun(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() common := &controllerCommon{ location: testCloud.Location, storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix, @@ -192,7 +192,7 @@ func TestGetNextDiskLun(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() common := &controllerCommon{ location: testCloud.Location, storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix, @@ -234,7 +234,7 @@ func TestDisksAreAttached(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() common := &controllerCommon{ location: testCloud.Location, storageEndpointSuffix: testCloud.Environment.StorageEndpointSuffix, diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_standard_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_standard_test.go index eef3d5673a8..bafeca2ea5a 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_standard_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_standard_test.go @@ -53,7 +53,7 @@ func TestStandardAttachDisk(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() vmSet := testCloud.vmSet setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false) @@ -90,7 +90,7 @@ func TestStandardDetachDisk(t *testing.T) { } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() vmSet := testCloud.vmSet setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false) @@ -140,7 +140,7 @@ func TestGetDataDisks(t *testing.T) { }, } for i, test := range testCases { - testCloud := getTestCloud() + testCloud := GetTestCloud() vmSet := testCloud.vmSet setTestVirtualMachines(testCloud, map[string]string{"vm1": "PowerState/Running"}, false) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go index ee44f38a9d0..fc9dda47404 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go @@ -34,7 +34,10 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/client-go/tools/record" cloudprovider "k8s.io/cloud-provider" + "k8s.io/legacy-cloud-providers/azure/auth" azcache "k8s.io/legacy-cloud-providers/azure/cache" "k8s.io/legacy-cloud-providers/azure/retry" ) @@ -875,6 +878,10 @@ func (fDC *fakeDisksClient) CreateOrUpdate(ctx context.Context, resourceGroupNam fDC.mutex.Lock() defer fDC.mutex.Unlock() + provisioningStateSucceeded := string(compute.ProvisioningStateSucceeded) + diskParameter.DiskProperties = &compute.DiskProperties{ProvisioningState: &provisioningStateSucceeded} + diskParameter.ID = &diskName + if _, ok := fDC.FakeStore[resourceGroupName]; !ok { fDC.FakeStore[resourceGroupName] = make(map[string]compute.Disk) } @@ -991,3 +998,55 @@ func (f *fakeVMSet) GetDataDisks(nodeName types.NodeName, crt azcache.AzureCache func (f *fakeVMSet) GetPowerStatusByNodeName(name string) (string, error) { return "", fmt.Errorf("unimplemented") } + +// GetTestCloud returns a fake azure cloud for unit tests in Azure related CSI drivers +func GetTestCloud() (az *Cloud) { + az = &Cloud{ + Config: Config{ + AzureAuthConfig: auth.AzureAuthConfig{ + TenantID: "tenant", + SubscriptionID: "subscription", + }, + ResourceGroup: "rg", + VnetResourceGroup: "rg", + RouteTableResourceGroup: "rg", + SecurityGroupResourceGroup: "rg", + Location: "westus", + VnetName: "vnet", + SubnetName: "subnet", + SecurityGroupName: "nsg", + RouteTableName: "rt", + PrimaryAvailabilitySetName: "as", + MaximumLoadBalancerRuleCount: 250, + VMType: vmTypeStandard, + }, + nodeZones: map[string]sets.String{}, + nodeInformerSynced: func() bool { return true }, + nodeResourceGroups: map[string]string{}, + unmanagedNodes: sets.NewString(), + routeCIDRs: map[string]string{}, + eventRecorder: &record.FakeRecorder{}, + } + az.DisksClient = newFakeDisksClient() + az.InterfacesClient = newFakeAzureInterfacesClient() + az.LoadBalancerClient = newFakeAzureLBClient() + az.PublicIPAddressesClient = newFakeAzurePIPClient(az.Config.SubscriptionID) + az.RoutesClient = newFakeRoutesClient() + az.RouteTablesClient = newFakeRouteTablesClient() + az.SecurityGroupsClient = newFakeAzureNSGClient() + az.SubnetsClient = newFakeAzureSubnetsClient() + az.VirtualMachineScaleSetsClient = newFakeVirtualMachineScaleSetsClient() + az.VirtualMachineScaleSetVMsClient = newFakeVirtualMachineScaleSetVMsClient() + az.VirtualMachinesClient = newFakeAzureVirtualMachinesClient() + az.vmSet = newAvailabilitySet(az) + az.vmCache, _ = az.newVMCache() + az.lbCache, _ = az.newLBCache() + az.nsgCache, _ = az.newNSGCache() + az.rtCache, _ = az.newRouteTableCache() + + common := &controllerCommon{cloud: az} + az.controllerCommon = common + az.ManagedDiskController = &ManagedDiskController{common: common} + + return az +} diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances_test.go index bd3d5eae27b..483dce0e054 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances_test.go @@ -82,7 +82,7 @@ func setTestVirtualMachines(c *Cloud, vmList map[string]string, isDataDisksFull } func TestInstanceID(t *testing.T) { - cloud := getTestCloud() + cloud := GetTestCloud() cloud.Config.UseInstanceMetadata = true testcases := []struct { @@ -215,7 +215,7 @@ func TestInstanceShutdownByProviderID(t *testing.T) { } for _, test := range testcases { - cloud := getTestCloud() + cloud := GetTestCloud() setTestVirtualMachines(cloud, test.vmList, false) providerID := "azure://" + cloud.getStandardMachineID("subscription", "rg", test.nodeName) hasShutdown, err := cloud.InstanceShutdownByProviderID(context.Background(), providerID) @@ -236,7 +236,7 @@ func TestInstanceShutdownByProviderID(t *testing.T) { } func TestNodeAddresses(t *testing.T) { - cloud := getTestCloud() + cloud := GetTestCloud() cloud.Config.UseInstanceMetadata = true metadataTemplate := `{"compute":{"name":"%s"},"network":{"interface":[{"ipv4":{"ipAddress":[{"privateIpAddress":"%s","publicIpAddress":"%s"}]},"ipv6":{"ipAddress":[{"privateIpAddress":"%s","publicIpAddress":"%s"}]}}]}}` diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go index 8b971d2d17e..6589a8f8ff0 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go @@ -378,7 +378,7 @@ func TestEnsureLoadBalancerDeleted(t *testing.T) { }, } - az := getTestCloud() + az := GetTestCloud() for i, c := range tests { clusterResources := getClusterResources(az, vmCount, availabilitySetCount) getTestSecurityGroup(az) @@ -488,7 +488,7 @@ func TestServiceOwnsPublicIP(t *testing.T) { } func TestGetPublicIPAddressResourceGroup(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() for i, c := range []struct { desc string @@ -693,7 +693,7 @@ func TestGetServiceLoadBalancer(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 3, 3) for _, existingLB := range test.existingLBs { @@ -903,7 +903,7 @@ func TestIsFrontendIPChanged(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() err := az.SubnetsClient.CreateOrUpdate(context.TODO(), "rg", "vnet", "testSubnet", test.exsistingSubnet) if err != nil { t.Fatalf("TestCase[%d] meets unexpected error: %v", i, err) @@ -960,7 +960,7 @@ func TestDeterminePublicIPName(t *testing.T) { }, } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() service := getTestService("test1", v1.ProtocolTCP, nil, 80) service.Spec.LoadBalancerIP = test.loadBalancerIP for _, existingPIP := range test.exsistingPIPs { @@ -1117,7 +1117,7 @@ func TestReconcileLoadBalancerRule(t *testing.T) { }, } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() az.Config.LoadBalancerSku = test.loadBalancerSku probe, lbrule, err := az.reconcileLoadBalancerRule(&test.service, test.wantLb, "frontendIPConfigID", "backendPoolID", "lbname", to.Int32Ptr(0)) @@ -1547,7 +1547,7 @@ func TestReconcileLoadBalancer(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() az.Config.LoadBalancerSku = test.loadBalancerSku az.DisableOutboundSNAT = test.disableOutboundSnat if test.preConfigLBType != "" { @@ -1583,7 +1583,7 @@ func TestReconcileLoadBalancer(t *testing.T) { } func TestGetServiceLoadBalancerStatus(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() service := getTestService("test1", v1.ProtocolTCP, nil, 80) internalService := getInternalTestService("test1", 80) @@ -1804,7 +1804,7 @@ func TestReconcileSecurityGroup(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() for name, sg := range test.existingSgs { err := az.SecurityGroupsClient.CreateOrUpdate(context.TODO(), "rg", name, sg, "") if err != nil { @@ -1852,7 +1852,7 @@ func TestSafeDeletePublicIP(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() err := az.PublicIPAddressesClient.CreateOrUpdate(context.TODO(), "rg", "pip1", network.PublicIPAddress{ Name: to.StringPtr("pip1"), PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{ @@ -1971,7 +1971,7 @@ func TestReconcilePublicIP(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() service := getTestService("test1", v1.ProtocolTCP, nil, 80) service.Annotations = test.annotations for _, pip := range test.existingPIPs { @@ -2082,7 +2082,7 @@ func TestEnsurePublicIPExists(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() service := getTestService("test1", v1.ProtocolTCP, nil, 80) for _, pip := range test.existingPIPs { err := az.PublicIPAddressesClient.CreateOrUpdate(context.TODO(), "rg", to.String(pip.Name), pip) @@ -2134,7 +2134,7 @@ func TestShouldUpdateLoadBalancer(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() service := getTestService("test1", v1.ProtocolTCP, nil, 80) if test.lbHasDeletionTimestamp { service.ObjectMeta.DeletionTimestamp = &metav1.Time{Time: time.Now()} @@ -2221,7 +2221,7 @@ func TestIsBackendPoolPreConfigured(t *testing.T) { } for i, test := range testCases { - az := getTestCloud() + az := GetTestCloud() az.Config.PreConfiguredBackendPoolLoadBalancerTypes = test.preConfiguredBackendPoolLoadBalancerTypes var service v1.Service if test.isInternalService { diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go index e6bc513f1f4..98969b828db 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard_test.go @@ -127,7 +127,7 @@ func TestGenerateStorageAccountName(t *testing.T) { } func TestMapLoadBalancerNameToVMSet(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() az.PrimaryAvailabilitySetName = "primary" cases := []struct { @@ -175,7 +175,7 @@ func TestMapLoadBalancerNameToVMSet(t *testing.T) { } func TestGetAzureLoadBalancerName(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() az.PrimaryAvailabilitySetName = "primary" cases := []struct { @@ -256,7 +256,7 @@ func TestGetAzureLoadBalancerName(t *testing.T) { } func TestGetLoadBalancingRuleName(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() az.PrimaryAvailabilitySetName = "primary" svc := &v1.Service{ @@ -337,7 +337,7 @@ func TestGetLoadBalancingRuleName(t *testing.T) { } func TestGetFrontendIPConfigName(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() az.PrimaryAvailabilitySetName = "primary" svc := &v1.Service{ diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go index 58e597107a7..dcd1921cc16 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go @@ -35,9 +35,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/client-go/tools/record" servicehelpers "k8s.io/cloud-provider/service/helpers" - "k8s.io/legacy-cloud-providers/azure/auth" "k8s.io/legacy-cloud-providers/azure/retry" ) @@ -69,7 +67,7 @@ func TestFlipServiceInternalAnnotation(t *testing.T) { // Test additional of a new service/port. func TestAddPort(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicea", v1.ProtocolTCP, nil, 80) clusterResources := getClusterResources(az, 1, 1) @@ -110,7 +108,7 @@ func TestLoadBalancerExternalServiceModeSelection(t *testing.T) { } func testLoadBalancerServiceDefaultModeSelection(t *testing.T, isInternal bool) { - az := getTestCloud() + az := GetTestCloud() const vmCount = 8 const availabilitySetCount = 4 const serviceCount = 9 @@ -165,7 +163,7 @@ func testLoadBalancerServiceDefaultModeSelection(t *testing.T, isInternal bool) // Validate even distribution of external services across load balancers // based on number of availability sets func testLoadBalancerServiceAutoModeSelection(t *testing.T, isInternal bool) { - az := getTestCloud() + az := GetTestCloud() const vmCount = 8 const availabilitySetCount = 4 const serviceCount = 9 @@ -226,7 +224,7 @@ func testLoadBalancerServiceAutoModeSelection(t *testing.T, isInternal bool) { // services will be assigned load balancers that are part of the provided availability sets // specified in service annotation func testLoadBalancerServicesSpecifiedSelection(t *testing.T, isInternal bool) { - az := getTestCloud() + az := GetTestCloud() const vmCount = 8 const availabilitySetCount = 4 const serviceCount = 9 @@ -269,7 +267,7 @@ func testLoadBalancerServicesSpecifiedSelection(t *testing.T, isInternal bool) { } func testLoadBalancerMaxRulesServices(t *testing.T, isInternal bool) { - az := getTestCloud() + az := GetTestCloud() const vmCount = 1 const availabilitySetCount = 1 @@ -329,7 +327,7 @@ func testLoadBalancerMaxRulesServices(t *testing.T, isInternal bool) { // Validate service deletion in lb auto selection mode func testLoadBalancerServiceAutoModeDeleteSelection(t *testing.T, isInternal bool) { - az := getTestCloud() + az := GetTestCloud() const vmCount = 8 const availabilitySetCount = 4 const serviceCount = 9 @@ -387,7 +385,7 @@ func testLoadBalancerServiceAutoModeDeleteSelection(t *testing.T, isInternal boo // Test addition of a new service on an internal LB with a subnet. func TestReconcileLoadBalancerAddServiceOnInternalSubnet(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc := getInternalTestService("servicea", 80) addTestSubnet(t, az, &svc) @@ -406,7 +404,7 @@ func TestReconcileLoadBalancerAddServiceOnInternalSubnet(t *testing.T) { } func TestReconcileSecurityGroupFromAnyDestinationAddressPrefixToLoadBalancerIP(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("serviceea", v1.ProtocolTCP, nil, 80) svc1.Spec.LoadBalancerIP = "192.168.0.0" getTestSecurityGroup(az) @@ -423,7 +421,7 @@ func TestReconcileSecurityGroupFromAnyDestinationAddressPrefixToLoadBalancerIP(t } func TestReconcileSecurityGroupDynamicLoadBalancerIP(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicea", v1.ProtocolTCP, nil, 80) svc1.Spec.LoadBalancerIP = "" getTestSecurityGroup(az) @@ -437,7 +435,7 @@ func TestReconcileSecurityGroupDynamicLoadBalancerIP(t *testing.T) { // Test addition of services on an internal LB using both default and explicit subnets. func TestReconcileLoadBalancerAddServicesOnMultipleSubnets(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc1 := getTestService("service1", v1.ProtocolTCP, nil, 8081) svc2 := getInternalTestService("service2", 8081) @@ -474,7 +472,7 @@ func TestReconcileLoadBalancerAddServicesOnMultipleSubnets(t *testing.T) { // Test moving a service exposure from one subnet to another. func TestReconcileLoadBalancerEditServiceSubnet(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc := getInternalTestService("service1", 8081) addTestSubnet(t, az, &svc) @@ -503,7 +501,7 @@ func TestReconcileLoadBalancerEditServiceSubnet(t *testing.T) { } func TestReconcileLoadBalancerNodeHealth(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc := getTestService("servicea", v1.ProtocolTCP, nil, 80) svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal @@ -524,7 +522,7 @@ func TestReconcileLoadBalancerNodeHealth(t *testing.T) { // Test removing all services results in removing the frontend ip configuration func TestReconcileLoadBalancerRemoveService(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) @@ -548,7 +546,7 @@ func TestReconcileLoadBalancerRemoveService(t *testing.T) { // Test removing all service ports results in removing the frontend ip configuration func TestReconcileLoadBalancerRemoveAllPortsRemovesFrontendConfig(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc := getTestService("servicea", v1.ProtocolTCP, nil, 80) @@ -574,7 +572,7 @@ func TestReconcileLoadBalancerRemoveAllPortsRemovesFrontendConfig(t *testing.T) // Test removal of a port from an existing service. func TestReconcileLoadBalancerRemovesPort(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) @@ -594,7 +592,7 @@ func TestReconcileLoadBalancerRemovesPort(t *testing.T) { // Test reconciliation of multiple services on same port func TestReconcileLoadBalancerMultipleServices(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() clusterResources := getClusterResources(az, 1, 1) svc1 := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) svc2 := getTestService("serviceb", v1.ProtocolTCP, nil, 80) @@ -622,7 +620,7 @@ func findLBRuleForPort(lbRules []network.LoadBalancingRule, port int32) (network } func TestServiceDefaultsToNoSessionPersistence(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("service-sa-omitted", v1.ProtocolTCP, nil, 7170) clusterResources := getClusterResources(az, 1, 1) @@ -642,7 +640,7 @@ func TestServiceDefaultsToNoSessionPersistence(t *testing.T) { } func TestServiceRespectsNoSessionAffinity(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("service-sa-none", v1.ProtocolTCP, nil, 7170) svc.Spec.SessionAffinity = v1.ServiceAffinityNone clusterResources := getClusterResources(az, 1, 1) @@ -665,7 +663,7 @@ func TestServiceRespectsNoSessionAffinity(t *testing.T) { } func TestServiceRespectsClientIPSessionAffinity(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("service-sa-clientip", v1.ProtocolTCP, nil, 7170) svc.Spec.SessionAffinity = v1.ServiceAffinityClientIP clusterResources := getClusterResources(az, 1, 1) @@ -688,7 +686,7 @@ func TestServiceRespectsClientIPSessionAffinity(t *testing.T) { } func TestReconcileSecurityGroupNewServiceAddsPort(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() getTestSecurityGroup(az) svc1 := getTestService("servicea", v1.ProtocolTCP, nil, 80) clusterResources := getClusterResources(az, 1, 1) @@ -704,7 +702,7 @@ func TestReconcileSecurityGroupNewServiceAddsPort(t *testing.T) { } func TestReconcileSecurityGroupNewInternalServiceAddsPort(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() getTestSecurityGroup(az) svc1 := getInternalTestService("serviceea", 80) addTestSubnet(t, az, &svc1) @@ -721,7 +719,7 @@ func TestReconcileSecurityGroupNewInternalServiceAddsPort(t *testing.T) { } func TestReconcileSecurityGroupRemoveService(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() service1 := getTestService("servicea", v1.ProtocolTCP, nil, 81) service2 := getTestService("serviceb", v1.ProtocolTCP, nil, 82) clusterResources := getClusterResources(az, 1, 1) @@ -743,7 +741,7 @@ func TestReconcileSecurityGroupRemoveService(t *testing.T) { } func TestReconcileSecurityGroupRemoveServiceRemovesPort(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) clusterResources := getClusterResources(az, 1, 1) @@ -761,7 +759,7 @@ func TestReconcileSecurityGroupRemoveServiceRemovesPort(t *testing.T) { } func TestReconcileSecurityWithSourceRanges(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) svc.Spec.LoadBalancerSourceRanges = []string{ "192.168.0.0/24", @@ -782,7 +780,7 @@ func TestReconcileSecurityWithSourceRanges(t *testing.T) { } func TestReconcileSecurityGroupEtagMismatch(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() sg := getTestSecurityGroup(az) cachedSG := *sg @@ -805,7 +803,7 @@ func TestReconcileSecurityGroupEtagMismatch(t *testing.T) { } func TestReconcilePublicIPWithNewService(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) pip, err := az.reconcilePublicIP(testClusterName, &svc, "", true /* wantLb*/) @@ -826,7 +824,7 @@ func TestReconcilePublicIPWithNewService(t *testing.T) { } func TestReconcilePublicIPRemoveService(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicea", v1.ProtocolTCP, nil, 80, 443) pip, err := az.reconcilePublicIP(testClusterName, &svc, "", true /* wantLb*/) @@ -846,7 +844,7 @@ func TestReconcilePublicIPRemoveService(t *testing.T) { } func TestReconcilePublicIPWithInternalService(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getInternalTestService("servicea", 80, 443) pip, err := az.reconcilePublicIP(testClusterName, &svc, "", true /* wantLb*/) @@ -858,7 +856,7 @@ func TestReconcilePublicIPWithInternalService(t *testing.T) { } func TestReconcilePublicIPWithExternalAndInternalSwitch(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getInternalTestService("servicea", 80, 443) pip, err := az.reconcilePublicIP(testClusterName, &svc, "", true /* wantLb*/) @@ -883,54 +881,6 @@ func TestReconcilePublicIPWithExternalAndInternalSwitch(t *testing.T) { validatePublicIP(t, pip, &svc, true) } -func getTestCloud() (az *Cloud) { - az = &Cloud{ - Config: Config{ - AzureAuthConfig: auth.AzureAuthConfig{ - TenantID: "tenant", - SubscriptionID: "subscription", - }, - ResourceGroup: "rg", - VnetResourceGroup: "rg", - RouteTableResourceGroup: "rg", - SecurityGroupResourceGroup: "rg", - Location: "westus", - VnetName: "vnet", - SubnetName: "subnet", - SecurityGroupName: "nsg", - RouteTableName: "rt", - PrimaryAvailabilitySetName: "as", - MaximumLoadBalancerRuleCount: 250, - VMType: vmTypeStandard, - }, - nodeZones: map[string]sets.String{}, - nodeInformerSynced: func() bool { return true }, - nodeResourceGroups: map[string]string{}, - unmanagedNodes: sets.NewString(), - routeCIDRs: map[string]string{}, - eventRecorder: &record.FakeRecorder{}, - } - az.DisksClient = newFakeDisksClient() - az.InterfacesClient = newFakeAzureInterfacesClient() - az.LoadBalancerClient = newFakeAzureLBClient() - az.PublicIPAddressesClient = newFakeAzurePIPClient(az.Config.SubscriptionID) - az.RoutesClient = newFakeRoutesClient() - az.RouteTablesClient = newFakeRouteTablesClient() - az.SecurityGroupsClient = newFakeAzureNSGClient() - az.SubnetsClient = newFakeAzureSubnetsClient() - az.VirtualMachineScaleSetsClient = newFakeVirtualMachineScaleSetsClient() - az.VirtualMachineScaleSetVMsClient = newFakeVirtualMachineScaleSetVMsClient() - az.VirtualMachinesClient = newFakeAzureVirtualMachinesClient() - az.vmSet = newAvailabilitySet(az) - az.vmCache, _ = az.newVMCache() - az.lbCache, _ = az.newLBCache() - az.nsgCache, _ = az.newNSGCache() - az.rtCache, _ = az.newRouteTableCache() - az.controllerCommon = &controllerCommon{cloud: az} - - return az -} - const networkInterfacesIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkInterfaces/%s" const primaryIPConfigIDTemplate = "%s/ipConfigurations/ipconfig" @@ -1159,7 +1109,7 @@ func getTestSecurityGroup(az *Cloud, services ...v1.Service) *network.SecurityGr } func validateLoadBalancer(t *testing.T, loadBalancer *network.LoadBalancer, services ...v1.Service) { - az := getTestCloud() + az := GetTestCloud() expectedRuleCount := 0 expectedFrontendIPCount := 0 expectedProbeCount := 0 @@ -1375,7 +1325,7 @@ func securityRuleMatches(serviceSourceRange string, servicePort v1.ServicePort, } func validateSecurityGroup(t *testing.T, securityGroup *network.SecurityGroup, services ...v1.Service) { - az := getTestCloud() + az := GetTestCloud() seenRules := make(map[string]string) for _, svc := range services { for _, wantedRule := range svc.Spec.Ports { @@ -1702,7 +1652,7 @@ func validateEmptyConfig(t *testing.T, config string) { } func TestGetNodeNameByProviderID(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() providers := []struct { providerID string name types.NodeName @@ -1783,7 +1733,7 @@ func addTestSubnet(t *testing.T, az *Cloud, svc *v1.Service) { } func TestIfServiceSpecifiesSharedRuleAndRuleDoesNotExistItIsCreated(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicesr", v1.ProtocolTCP, nil, 80) svc.Spec.LoadBalancerIP = "192.168.77.88" svc.Annotations[ServiceAnnotationSharedSecurityRule] = "true" @@ -1822,7 +1772,7 @@ func TestIfServiceSpecifiesSharedRuleAndRuleDoesNotExistItIsCreated(t *testing.T } func TestIfServiceSpecifiesSharedRuleAndRuleExistsThenTheServicesPortAndAddressAreAdded(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc := getTestService("servicesr", v1.ProtocolTCP, nil, 80) svc.Spec.LoadBalancerIP = "192.168.77.88" svc.Annotations[ServiceAnnotationSharedSecurityRule] = "true" @@ -1874,7 +1824,7 @@ func TestIfServiceSpecifiesSharedRuleAndRuleExistsThenTheServicesPortAndAddressA } func TestIfServicesSpecifySharedRuleButDifferentPortsThenSeparateRulesAreCreated(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 4444) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -1943,7 +1893,7 @@ func TestIfServicesSpecifySharedRuleButDifferentPortsThenSeparateRulesAreCreated } func TestIfServicesSpecifySharedRuleButDifferentProtocolsThenSeparateRulesAreCreated(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 4444) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2010,7 +1960,7 @@ func TestIfServicesSpecifySharedRuleButDifferentProtocolsThenSeparateRulesAreCre } func TestIfServicesSpecifySharedRuleButDifferentSourceAddressesThenSeparateRulesAreCreated(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 80) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2081,7 +2031,7 @@ func TestIfServicesSpecifySharedRuleButDifferentSourceAddressesThenSeparateRules } func TestIfServicesSpecifySharedRuleButSomeAreOnDifferentPortsThenRulesAreSeparatedOrConsoliatedByPort(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 4444) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2181,7 +2131,7 @@ func TestIfServicesSpecifySharedRuleButSomeAreOnDifferentPortsThenRulesAreSepara } func TestIfServiceSpecifiesSharedRuleAndServiceIsDeletedThenTheServicesPortAndAddressAreRemoved(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 80) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2236,7 +2186,7 @@ func TestIfServiceSpecifiesSharedRuleAndServiceIsDeletedThenTheServicesPortAndAd } func TestIfSomeServicesShareARuleAndOneIsDeletedItIsRemovedFromTheRightRule(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 4444) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2343,7 +2293,7 @@ func TestIfSomeServicesShareARuleAndOneIsDeletedItIsRemovedFromTheRightRule(t *t } func TestIfServiceSpecifiesSharedRuleAndLastServiceIsDeletedThenRuleIsDeleted(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 4444) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2423,7 +2373,7 @@ func TestIfServiceSpecifiesSharedRuleAndLastServiceIsDeletedThenRuleIsDeleted(t } func TestCanCombineSharedAndPrivateRulesInSameGroup(t *testing.T) { - az := getTestCloud() + az := GetTestCloud() svc1 := getTestService("servicesr1", v1.ProtocolTCP, nil, 4444) svc1.Spec.LoadBalancerIP = "192.168.77.88" @@ -2680,7 +2630,7 @@ func TestGetResourceGroups(t *testing.T) { }, } - az := getTestCloud() + az := GetTestCloud() for _, test := range tests { az.nodeResourceGroups = test.nodeResourceGroups if test.informerSynced { @@ -2730,7 +2680,7 @@ func TestGetNodeResourceGroup(t *testing.T) { }, } - az := getTestCloud() + az := GetTestCloud() for _, test := range tests { az.nodeResourceGroups = test.nodeResourceGroups if test.informerSynced { diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go index f77e966f693..65885698307 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go @@ -38,7 +38,7 @@ func newTestScaleSet(scaleSetName, zone string, faultDomain int32, vmList []stri } func newTestScaleSetWithState(scaleSetName, zone string, faultDomain int32, vmList []string, state string) (*scaleSet, error) { - cloud := getTestCloud() + cloud := GetTestCloud() setTestVirtualMachineCloud(cloud, scaleSetName, zone, faultDomain, vmList, state) ss, err := newScaleSet(cloud) if err != nil { diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap_test.go index 73d9b69fd7f..c227a0f913c 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap_test.go @@ -89,7 +89,7 @@ func TestIsNodeUnmanaged(t *testing.T) { }, } - az := getTestCloud() + az := GetTestCloud() for _, test := range tests { az.unmanagedNodes = test.unmanagedNodes if test.expectErr { @@ -137,7 +137,7 @@ func TestIsNodeUnmanagedByProviderID(t *testing.T) { }, } - az := getTestCloud() + az := GetTestCloud() for _, test := range tests { isUnmanagedNode := az.IsNodeUnmanagedByProviderID(test.providerID) assert.Equal(t, test.expected, isUnmanagedNode, test.providerID)