Skip to content

Commit 7b494d7

Browse files
committed
Replace fake storages with mock clients in unit tests.
1 parent e057aa2 commit 7b494d7

File tree

6 files changed

+261
-414
lines changed

6 files changed

+261
-414
lines changed

cluster-autoscaler/cloudprovider/azure/azure_agent_pool_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ import (
2121
"testing"
2222
"time"
2323

24+
"k8s.io/legacy-cloud-providers/azure/clients/vmclient/mockvmclient"
25+
26+
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2427
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
2528
"github.com/Azure/go-autorest/autorest/date"
2629
"github.com/Azure/go-autorest/autorest/to"
30+
"github.com/golang/mock/gomock"
2731
"github.com/stretchr/testify/assert"
2832
)
2933

@@ -105,3 +109,23 @@ func TestDeleteOutdatedDeployments(t *testing.T) {
105109
assert.Equal(t, test.expectedDeploymentsNames, existedDeploymentsNames, test.desc)
106110
}
107111
}
112+
113+
func TestGetVirtualMachinesFromCache(t *testing.T) {
114+
ctrl := gomock.NewController(t)
115+
defer ctrl.Finish()
116+
117+
testAS := newTestAgentPool(newTestAzureManager(t), "testAS")
118+
expectedVMs := []compute.VirtualMachine{
119+
{
120+
Tags: map[string]*string{"poolName": to.StringPtr("testAS")},
121+
},
122+
}
123+
124+
mockVMClient := mockvmclient.NewMockInterface(ctrl)
125+
mockVMClient.EXPECT().List(gomock.Any(), testAS.manager.config.ResourceGroup).Return(expectedVMs, nil)
126+
testAS.manager.azClient.virtualMachinesClient = mockVMClient
127+
128+
vms, err := testAS.getVirtualMachinesFromCache()
129+
assert.Equal(t, 1, len(vms))
130+
assert.NoError(t, err)
131+
}

cluster-autoscaler/cloudprovider/azure/azure_cloud_provider_test.go

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,18 @@ package azure
1919
import (
2020
"testing"
2121

22-
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
22+
apiv1 "k8s.io/api/core/v1"
23+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
24+
"k8s.io/legacy-cloud-providers/azure/clients/vmssclient/mockvmssclient"
25+
"k8s.io/legacy-cloud-providers/azure/clients/vmssvmclient/mockvmssvmclient"
26+
2327
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
2428
"github.com/Azure/go-autorest/autorest/azure"
25-
"github.com/Azure/go-autorest/autorest/to"
29+
"github.com/golang/mock/gomock"
2630
"github.com/stretchr/testify/assert"
27-
28-
apiv1 "k8s.io/api/core/v1"
29-
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
3031
)
3132

3233
func newTestAzureManager(t *testing.T) *AzureManager {
33-
vmssName := "test-asg"
34-
skuName := "Standard_D4_v2"
35-
location := "eastus"
36-
var vmssCapacity int64 = 3
3734
manager := &AzureManager{
3835
env: azure.PublicCloud,
3936
explicitlyConfigured: make(map[string]bool),
@@ -44,43 +41,9 @@ func newTestAzureManager(t *testing.T) *AzureManager {
4441
},
4542

4643
azClient: &azClient{
47-
disksClient: &DisksClientMock{},
48-
interfacesClient: &InterfacesClientMock{},
49-
storageAccountsClient: &AccountsClientMock{},
5044
deploymentsClient: &DeploymentsClientMock{
5145
FakeStore: make(map[string]resources.DeploymentExtended),
5246
},
53-
virtualMachinesClient: &VirtualMachinesClientMock{
54-
FakeStore: make(map[string]map[string]compute.VirtualMachine),
55-
},
56-
virtualMachineScaleSetsClient: &VirtualMachineScaleSetsClientMock{
57-
FakeStore: map[string]map[string]compute.VirtualMachineScaleSet{
58-
"test": {
59-
"test-asg": {
60-
Name: &vmssName,
61-
Sku: &compute.Sku{
62-
Capacity: &vmssCapacity,
63-
Name: &skuName,
64-
},
65-
VirtualMachineScaleSetProperties: &compute.VirtualMachineScaleSetProperties{},
66-
Location: &location,
67-
},
68-
},
69-
},
70-
},
71-
virtualMachineScaleSetVMsClient: &VirtualMachineScaleSetVMsClientMock{
72-
FakeStore: map[string]map[string]compute.VirtualMachineScaleSetVM{
73-
"test": {
74-
"0": {
75-
ID: to.StringPtr(fakeVirtualMachineScaleSetVMID),
76-
InstanceID: to.StringPtr("0"),
77-
VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{
78-
VMID: to.StringPtr("123E4567-E89B-12D3-A456-426655440000"),
79-
},
80-
},
81-
},
82-
},
83-
},
8447
},
8548
}
8649

@@ -128,7 +91,20 @@ func TestNodeGroups(t *testing.T) {
12891
}
12992

13093
func TestNodeGroupForNode(t *testing.T) {
94+
ctrl := gomock.NewController(t)
95+
defer ctrl.Finish()
96+
97+
expectedVMSSVMs := newTestVMSSVMList()
98+
expectedScaleSets := newTestVMSSList(3, "test-asg", "eastus")
99+
131100
provider := newTestProvider(t)
101+
mockVMSSClient := mockvmssclient.NewMockInterface(ctrl)
102+
mockVMSSClient.EXPECT().List(gomock.Any(), provider.azureManager.config.ResourceGroup).Return(expectedScaleSets, nil)
103+
provider.azureManager.azClient.virtualMachineScaleSetsClient = mockVMSSClient
104+
mockVMSSVMClient := mockvmssvmclient.NewMockInterface(ctrl)
105+
mockVMSSVMClient.EXPECT().List(gomock.Any(), provider.azureManager.config.ResourceGroup, "test-asg", gomock.Any()).Return(expectedVMSSVMs, nil).AnyTimes()
106+
provider.azureManager.azClient.virtualMachineScaleSetVMsClient = mockVMSSVMClient
107+
132108
registered := provider.azureManager.RegisterAsg(
133109
newTestScaleSet(provider.azureManager, "test-asg"))
134110
assert.True(t, registered)

0 commit comments

Comments
 (0)