mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
Add more unit tests
This commit is contained in:
parent
66b023110f
commit
2e646b0e12
@ -12,6 +12,7 @@ go_library(
|
|||||||
"azure.go",
|
"azure.go",
|
||||||
"azure_backoff.go",
|
"azure_backoff.go",
|
||||||
"azure_blobDiskController.go",
|
"azure_blobDiskController.go",
|
||||||
|
"azure_cache.go",
|
||||||
"azure_client.go",
|
"azure_client.go",
|
||||||
"azure_controllerCommon.go",
|
"azure_controllerCommon.go",
|
||||||
"azure_fakes.go",
|
"azure_fakes.go",
|
||||||
@ -21,12 +22,11 @@ go_library(
|
|||||||
"azure_managedDiskController.go",
|
"azure_managedDiskController.go",
|
||||||
"azure_metrics.go",
|
"azure_metrics.go",
|
||||||
"azure_routes.go",
|
"azure_routes.go",
|
||||||
|
"azure_standard.go",
|
||||||
"azure_storage.go",
|
"azure_storage.go",
|
||||||
"azure_storageaccount.go",
|
"azure_storageaccount.go",
|
||||||
"azure_util.go",
|
|
||||||
"azure_util_cache.go",
|
|
||||||
"azure_util_vmss.go",
|
|
||||||
"azure_vmsets.go",
|
"azure_vmsets.go",
|
||||||
|
"azure_vmss.go",
|
||||||
"azure_wrap.go",
|
"azure_wrap.go",
|
||||||
"azure_zones.go",
|
"azure_zones.go",
|
||||||
],
|
],
|
||||||
@ -64,11 +64,11 @@ go_library(
|
|||||||
go_test(
|
go_test(
|
||||||
name = "go_default_test",
|
name = "go_default_test",
|
||||||
srcs = [
|
srcs = [
|
||||||
|
"azure_cache_test.go",
|
||||||
"azure_loadbalancer_test.go",
|
"azure_loadbalancer_test.go",
|
||||||
"azure_metrics_test.go",
|
"azure_metrics_test.go",
|
||||||
"azure_test.go",
|
"azure_test.go",
|
||||||
"azure_util_cache_test.go",
|
"azure_vmss_test.go",
|
||||||
"azure_util_test.go",
|
|
||||||
"azure_wrap_test.go",
|
"azure_wrap_test.go",
|
||||||
],
|
],
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
|
@ -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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -190,10 +190,12 @@ func (ss *scaleSet) getCachedVirtualMachine(nodeName string) (scaleSetVMInfo, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update cache and try again.
|
// Update cache and try again.
|
||||||
|
glog.V(10).Infof("vmss cache before updateCache: %v", ss.cache)
|
||||||
if err := ss.updateCache(); err != nil {
|
if err := ss.updateCache(); err != nil {
|
||||||
glog.Errorf("updateCache failed with error: %v", err)
|
glog.Errorf("updateCache failed with error: %v", err)
|
||||||
return scaleSetVMInfo{}, err
|
return scaleSetVMInfo{}, err
|
||||||
}
|
}
|
||||||
|
glog.V(10).Infof("vmss cache after updateCache: %v", ss.cache)
|
||||||
vm, found = getVMFromCache(nodeName)
|
vm, found = getVMFromCache(nodeName)
|
||||||
if found {
|
if found {
|
||||||
return vm, nil
|
return vm, nil
|
||||||
|
149
pkg/cloudprovider/providers/azure/azure_vmss_test.go
Normal file
149
pkg/cloudprovider/providers/azure/azure_vmss_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user