Skip to content

Commit fbe928c

Browse files
committed
add unit test for in progress deletion cases
1 parent d9aaf4d commit fbe928c

File tree

3 files changed

+89
-12
lines changed

3 files changed

+89
-12
lines changed

cluster-autoscaler/cloudprovider/azure/azure_cloud_provider_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
2323
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
2424
"github.com/Azure/go-autorest/autorest/azure"
25+
"github.com/Azure/go-autorest/autorest/to"
2526
"github.com/stretchr/testify/assert"
2627

2728
apiv1 "k8s.io/api/core/v1"
@@ -67,7 +68,19 @@ func newTestAzureManager(t *testing.T) *AzureManager {
6768
},
6869
},
6970
},
70-
virtualMachineScaleSetVMsClient: &VirtualMachineScaleSetVMsClientMock{},
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+
},
7184
},
7285
}
7386

cluster-autoscaler/cloudprovider/azure/azure_fakes.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func (client *VirtualMachineScaleSetsClientMock) List(ctx context.Context, resou
110110
// VirtualMachineScaleSetVMsClientMock mocks for VirtualMachineScaleSetVMsClient.
111111
type VirtualMachineScaleSetVMsClientMock struct {
112112
mock.Mock
113+
mutex sync.Mutex
114+
FakeStore map[string]map[string]compute.VirtualMachineScaleSetVM
113115
}
114116

115117
// Get gets a VirtualMachineScaleSetVM by VMScaleSetName and instanceID.
@@ -128,18 +130,14 @@ func (m *VirtualMachineScaleSetVMsClientMock) Get(ctx context.Context, resourceG
128130

129131
// List gets a list of VirtualMachineScaleSetVMs.
130132
func (m *VirtualMachineScaleSetVMsClientMock) List(ctx context.Context, resourceGroupName string, virtualMachineScaleSetName string, expand string) (result []compute.VirtualMachineScaleSetVM, rerr *retry.Error) {
131-
ID := fakeVirtualMachineScaleSetVMID
132-
instanceID := "0"
133-
vmID := "123E4567-E89B-12D3-A456-426655440000"
134-
properties := compute.VirtualMachineScaleSetVMProperties{
135-
VMID: &vmID,
136-
}
137-
result = append(result, compute.VirtualMachineScaleSetVM{
138-
ID: &ID,
139-
InstanceID: &instanceID,
140-
VirtualMachineScaleSetVMProperties: &properties,
141-
})
133+
m.mutex.Lock()
134+
defer m.mutex.Unlock()
142135

136+
if _, ok := m.FakeStore[resourceGroupName]; ok {
137+
for _, v := range m.FakeStore[resourceGroupName] {
138+
result = append(result, v)
139+
}
140+
}
143141
return result, nil
144142
}
145143

cluster-autoscaler/cloudprovider/azure/azure_scale_set_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,72 @@ func TestDeleteNodes(t *testing.T) {
226226
scaleSetClient.AssertNumberOfCalls(t, "DeleteInstances", 1)
227227
}
228228

229+
func TestDeleteNoConflictRequest(t *testing.T) {
230+
vmssName := "test-asg"
231+
var vmssCapacity int64 = 3
232+
233+
manager := newTestAzureManager(t)
234+
vmsClient := &VirtualMachineScaleSetVMsClientMock{
235+
FakeStore: map[string]map[string]compute.VirtualMachineScaleSetVM{
236+
"test": {
237+
"0": {
238+
ID: to.StringPtr(fakeVirtualMachineScaleSetVMID),
239+
InstanceID: to.StringPtr("0"),
240+
VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{
241+
VMID: to.StringPtr("123E4567-E89B-12D3-A456-426655440000"),
242+
ProvisioningState: to.StringPtr("Deleting"),
243+
},
244+
},
245+
},
246+
},
247+
}
248+
249+
scaleSetClient := &VirtualMachineScaleSetsClientMock{
250+
FakeStore: map[string]map[string]compute.VirtualMachineScaleSet{
251+
"test": {
252+
"test-asg": {
253+
Name: &vmssName,
254+
Sku: &compute.Sku{
255+
Capacity: &vmssCapacity,
256+
},
257+
},
258+
},
259+
},
260+
}
261+
262+
response := autorest.Response{
263+
Response: &http.Response{
264+
Status: "OK",
265+
},
266+
}
267+
268+
scaleSetClient.On("DeleteInstances", mock.Anything, "test-asg", mock.Anything, mock.Anything).Return(response, nil)
269+
manager.azClient.virtualMachineScaleSetsClient = scaleSetClient
270+
manager.azClient.virtualMachineScaleSetVMsClient = vmsClient
271+
272+
resourceLimiter := cloudprovider.NewResourceLimiter(
273+
map[string]int64{cloudprovider.ResourceNameCores: 1, cloudprovider.ResourceNameMemory: 10000000},
274+
map[string]int64{cloudprovider.ResourceNameCores: 10, cloudprovider.ResourceNameMemory: 100000000})
275+
provider, err := BuildAzureCloudProvider(manager, resourceLimiter)
276+
assert.NoError(t, err)
277+
278+
registered := manager.RegisterAsg(newTestScaleSet(manager, "test-asg"))
279+
assert.True(t, registered)
280+
281+
node := &apiv1.Node{
282+
Spec: apiv1.NodeSpec{
283+
ProviderID: "azure://" + fakeVirtualMachineScaleSetVMID,
284+
},
285+
}
286+
287+
scaleSet, ok := provider.NodeGroups()[0].(*ScaleSet)
288+
assert.True(t, ok)
289+
290+
err = scaleSet.DeleteNodes([]*apiv1.Node{node})
291+
// ensure that DeleteInstances isn't called
292+
scaleSetClient.AssertNumberOfCalls(t, "DeleteInstances", 0)
293+
}
294+
229295
func TestId(t *testing.T) {
230296
provider := newTestProvider(t)
231297
registered := provider.azureManager.RegisterAsg(

0 commit comments

Comments
 (0)