From 2e646b0e1264c35c35230a61b69f997239a0555a Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Mon, 15 Jan 2018 15:44:02 +0800 Subject: [PATCH] Add more unit tests --- pkg/cloudprovider/providers/azure/BUILD | 10 +- .../providers/azure/azure_standard_test.go | 53 ------- .../providers/azure/azure_vmss.go | 2 + .../providers/azure/azure_vmss_test.go | 149 ++++++++++++++++++ 4 files changed, 156 insertions(+), 58 deletions(-) delete mode 100644 pkg/cloudprovider/providers/azure/azure_standard_test.go create mode 100644 pkg/cloudprovider/providers/azure/azure_vmss_test.go diff --git a/pkg/cloudprovider/providers/azure/BUILD b/pkg/cloudprovider/providers/azure/BUILD index 8272b20219d..bd65300c2ed 100644 --- a/pkg/cloudprovider/providers/azure/BUILD +++ b/pkg/cloudprovider/providers/azure/BUILD @@ -12,6 +12,7 @@ go_library( "azure.go", "azure_backoff.go", "azure_blobDiskController.go", + "azure_cache.go", "azure_client.go", "azure_controllerCommon.go", "azure_fakes.go", @@ -21,12 +22,11 @@ go_library( "azure_managedDiskController.go", "azure_metrics.go", "azure_routes.go", + "azure_standard.go", "azure_storage.go", "azure_storageaccount.go", - "azure_util.go", - "azure_util_cache.go", - "azure_util_vmss.go", "azure_vmsets.go", + "azure_vmss.go", "azure_wrap.go", "azure_zones.go", ], @@ -64,11 +64,11 @@ go_library( go_test( name = "go_default_test", srcs = [ + "azure_cache_test.go", "azure_loadbalancer_test.go", "azure_metrics_test.go", "azure_test.go", - "azure_util_cache_test.go", - "azure_util_test.go", + "azure_vmss_test.go", "azure_wrap_test.go", ], embed = [":go_default_library"], diff --git a/pkg/cloudprovider/providers/azure/azure_standard_test.go b/pkg/cloudprovider/providers/azure/azure_standard_test.go deleted file mode 100644 index cac803c2eb0..00000000000 --- a/pkg/cloudprovider/providers/azure/azure_standard_test.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package azure - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGetScaleSetVMInstanceID(t *testing.T) { - tests := []struct { - msg string - machineName string - expectError bool - expectedInstanceID string - }{{ - msg: "invalid vmss instance name", - machineName: "vmvm", - expectError: true, - }, - { - msg: "valid vmss instance name", - machineName: "vm00000Z", - expectError: false, - expectedInstanceID: "35", - }, - } - - for i, test := range tests { - instanceID, err := getScaleSetVMInstanceID(test.machineName) - if test.expectError { - assert.Error(t, err, fmt.Sprintf("TestCase[%d]: %s", i, test.msg)) - } else { - assert.Equal(t, test.expectedInstanceID, instanceID, fmt.Sprintf("TestCase[%d]: %s", i, test.msg)) - } - } -} diff --git a/pkg/cloudprovider/providers/azure/azure_vmss.go b/pkg/cloudprovider/providers/azure/azure_vmss.go index 34585208977..a5ad369dc52 100644 --- a/pkg/cloudprovider/providers/azure/azure_vmss.go +++ b/pkg/cloudprovider/providers/azure/azure_vmss.go @@ -190,10 +190,12 @@ func (ss *scaleSet) getCachedVirtualMachine(nodeName string) (scaleSetVMInfo, er } // Update cache and try again. + glog.V(10).Infof("vmss cache before updateCache: %v", ss.cache) if err := ss.updateCache(); err != nil { glog.Errorf("updateCache failed with error: %v", err) return scaleSetVMInfo{}, err } + glog.V(10).Infof("vmss cache after updateCache: %v", ss.cache) vm, found = getVMFromCache(nodeName) if found { return vm, nil diff --git a/pkg/cloudprovider/providers/azure/azure_vmss_test.go b/pkg/cloudprovider/providers/azure/azure_vmss_test.go new file mode 100644 index 00000000000..7830eab783d --- /dev/null +++ b/pkg/cloudprovider/providers/azure/azure_vmss_test.go @@ -0,0 +1,149 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package azure + +import ( + "fmt" + "testing" + + "github.com/Azure/azure-sdk-for-go/arm/compute" + "github.com/stretchr/testify/assert" +) + +func newTestScaleSet() *scaleSet { + ss := newScaleSet(getTestCloud()) + return ss.(*scaleSet) +} + +func setTestVirtualMachineScaleSets(ss *scaleSet, scaleSetName string, vmList []string) { + virtualMachineScaleSetsClient := newFakeVirtualMachineScaleSetsClient() + scaleSets := make(map[string]map[string]compute.VirtualMachineScaleSet) + scaleSets["rg"] = map[string]compute.VirtualMachineScaleSet{ + scaleSetName: { + Name: &scaleSetName, + }, + } + virtualMachineScaleSetsClient.setFakeStore(scaleSets) + + virtualMachineScaleSetVMsClient := newFakeVirtualMachineScaleSetVMsClient() + ssVMs := make(map[string]map[string]compute.VirtualMachineScaleSetVM) + ssVMs["rg"] = make(map[string]compute.VirtualMachineScaleSetVM) + for i := range vmList { + ID := fmt.Sprintf("azure:///subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%d", scaleSetName, i) + nodeName := vmList[i] + instanceID := fmt.Sprintf("%d", i) + vmKey := fmt.Sprintf("%s-%s", scaleSetName, nodeName) + networkInterfaces := []compute.NetworkInterfaceReference{ + { + ID: &nodeName, + }, + } + ssVMs["rg"][vmKey] = compute.VirtualMachineScaleSetVM{ + VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ + OsProfile: &compute.OSProfile{ + ComputerName: &nodeName, + }, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &networkInterfaces, + }, + }, + ID: &ID, + InstanceID: &instanceID, + Location: &ss.Cloud.Location, + } + } + virtualMachineScaleSetVMsClient.setFakeStore(ssVMs) + + ss.Cloud.VirtualMachineScaleSetsClient = virtualMachineScaleSetsClient + ss.Cloud.VirtualMachineScaleSetVMsClient = virtualMachineScaleSetVMsClient +} + +func TestGetScaleSetVMInstanceID(t *testing.T) { + tests := []struct { + msg string + machineName string + expectError bool + expectedInstanceID string + }{{ + msg: "invalid vmss instance name", + machineName: "vmvm", + expectError: true, + }, + { + msg: "valid vmss instance name", + machineName: "vm00000Z", + expectError: false, + expectedInstanceID: "35", + }, + } + + for i, test := range tests { + instanceID, err := getScaleSetVMInstanceID(test.machineName) + if test.expectError { + assert.Error(t, err, fmt.Sprintf("TestCase[%d]: %s", i, test.msg)) + } else { + assert.Equal(t, test.expectedInstanceID, instanceID, fmt.Sprintf("TestCase[%d]: %s", i, test.msg)) + } + } +} + +func TestGetInstanceIDByNodeName(t *testing.T) { + ss := newTestScaleSet() + + testCases := []struct { + description string + scaleSet string + vmList []string + nodeName string + expected string + expectError bool + }{ + { + description: "scaleSet should get instance by node name", + scaleSet: "ss", + vmList: []string{"vm1", "vm2"}, + nodeName: "vm1", + expected: "azure:///subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/ss/virtualMachines/0", + }, + { + description: "scaleSet should get instance by node name with upper cases hostname", + scaleSet: "ss", + vmList: []string{"VM1", "vm2"}, + nodeName: "vm1", + expected: "azure:///subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/ss/virtualMachines/0", + }, + { + description: "scaleSet should not get instance for non-exist nodes", + scaleSet: "ss", + vmList: []string{"VM1", "vm2"}, + nodeName: "vm3", + expectError: true, + }, + } + + for _, test := range testCases { + setTestVirtualMachineScaleSets(ss, test.scaleSet, test.vmList) + real, err := ss.GetInstanceIDByNodeName(test.nodeName) + if test.expectError { + assert.Error(t, err, test.description) + continue + } + + assert.NoError(t, err, test.description) + assert.Equal(t, test.expected, real, test.description) + } +}