@@ -20,14 +20,20 @@ package azure
20
20
21
21
import (
22
22
"fmt"
23
+ "net/http"
23
24
"reflect"
24
25
"testing"
25
26
26
27
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-12-01/compute"
28
+ "github.com/Azure/go-autorest/autorest/to"
27
29
"github.com/golang/mock/gomock"
28
30
"github.com/stretchr/testify/assert"
29
31
30
32
"k8s.io/apimachinery/pkg/types"
33
+ cloudprovider "k8s.io/cloud-provider"
34
+ "k8s.io/legacy-cloud-providers/azure/clients/diskclient/mockdiskclient"
35
+ "k8s.io/legacy-cloud-providers/azure/clients/vmclient/mockvmclient"
36
+ "k8s.io/legacy-cloud-providers/azure/retry"
31
37
"k8s.io/utils/pointer"
32
38
)
33
39
@@ -40,12 +46,14 @@ func TestCommonAttachDisk(t *testing.T) {
40
46
vmList map [string ]string
41
47
nodeName types.NodeName
42
48
isDataDisksFull bool
49
+ existedDisk compute.Disk
43
50
expectedLun int32
44
51
expectedErr bool
45
52
}{
46
53
{
47
54
desc : "LUN -1 and error shall be returned if there's no such instance corresponding to given nodeName" ,
48
55
nodeName : "vm1" ,
56
+ existedDisk : compute.Disk {Name : to .StringPtr ("disk-name" )},
49
57
expectedLun : - 1 ,
50
58
expectedErr : true ,
51
59
},
@@ -54,13 +62,23 @@ func TestCommonAttachDisk(t *testing.T) {
54
62
vmList : map [string ]string {"vm1" : "PowerState/Running" },
55
63
nodeName : "vm1" ,
56
64
isDataDisksFull : true ,
65
+ existedDisk : compute.Disk {Name : to .StringPtr ("disk-name" )},
57
66
expectedLun : - 1 ,
58
67
expectedErr : true ,
59
68
},
60
69
{
61
70
desc : "correct LUN and no error shall be returned if everything is good" ,
62
71
vmList : map [string ]string {"vm1" : "PowerState/Running" },
63
72
nodeName : "vm1" ,
73
+ existedDisk : compute.Disk {Name : to .StringPtr ("disk-name" )},
74
+ expectedLun : 1 ,
75
+ expectedErr : false ,
76
+ },
77
+ {
78
+ desc : "an error shall be returned if there's no matching disk" ,
79
+ vmList : map [string ]string {"vm1" : "PowerState/Running" },
80
+ nodeName : "vm1" ,
81
+ existedDisk : compute.Disk {Name : to .StringPtr ("disk-name-1" )},
64
82
expectedLun : - 1 ,
65
83
expectedErr : true ,
66
84
},
@@ -76,9 +94,21 @@ func TestCommonAttachDisk(t *testing.T) {
76
94
cloud : testCloud ,
77
95
vmLockMap : newLockMap (),
78
96
}
79
- diskURI := fmt .Sprintf ("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/disk-name" ,
80
- testCloud .SubscriptionID , testCloud .ResourceGroup )
81
- setTestVirtualMachines (testCloud , test .vmList , test .isDataDisksFull )
97
+ diskURI := fmt .Sprintf ("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s" ,
98
+ testCloud .SubscriptionID , testCloud .ResourceGroup , * test .existedDisk .Name )
99
+ expectedVMs := setTestVirtualMachines (testCloud , test .vmList , test .isDataDisksFull )
100
+ mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
101
+ for _ , vm := range expectedVMs {
102
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
103
+ }
104
+ if len (expectedVMs ) == 0 {
105
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any ()).Return (compute.VirtualMachine {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
106
+ }
107
+ mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
108
+
109
+ mockDisksClient := testCloud .DisksClient .(* mockdiskclient.MockInterface )
110
+ mockDisksClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , "disk-name" ).Return (test .existedDisk , nil ).AnyTimes ()
111
+ mockDisksClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , gomock .Not ("disk-name" )).Return (compute.Disk {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
82
112
83
113
lun , err := common .AttachDisk (true , "" , diskURI , test .nodeName , compute .CachingTypesReadOnly )
84
114
assert .Equal (t , test .expectedLun , lun , "TestCase[%d]: %s" , i , test .desc )
@@ -130,7 +160,15 @@ func TestCommonDetachDisk(t *testing.T) {
130
160
}
131
161
diskURI := fmt .Sprintf ("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/disk-name" ,
132
162
testCloud .SubscriptionID , testCloud .ResourceGroup )
133
- setTestVirtualMachines (testCloud , test .vmList , false )
163
+ expectedVMs := setTestVirtualMachines (testCloud , test .vmList , false )
164
+ mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
165
+ for _ , vm := range expectedVMs {
166
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
167
+ }
168
+ if len (expectedVMs ) == 0 {
169
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any ()).Return (compute.VirtualMachine {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
170
+ }
171
+ mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
134
172
135
173
err := common .DetachDisk (test .diskName , diskURI , test .nodeName )
136
174
assert .Equal (t , test .expectedErr , err != nil , "TestCase[%d]: %s, err: %v" , i , test .desc , err )
@@ -172,7 +210,11 @@ func TestGetDiskLun(t *testing.T) {
172
210
cloud : testCloud ,
173
211
vmLockMap : newLockMap (),
174
212
}
175
- setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, false )
213
+ expectedVMs := setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, false )
214
+ mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
215
+ for _ , vm := range expectedVMs {
216
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
217
+ }
176
218
177
219
lun , err := common .GetDiskLun (test .diskName , test .diskURI , "vm1" )
178
220
assert .Equal (t , test .expectedLun , lun , "TestCase[%d]: %s" , i , test .desc )
@@ -214,7 +256,11 @@ func TestGetNextDiskLun(t *testing.T) {
214
256
cloud : testCloud ,
215
257
vmLockMap : newLockMap (),
216
258
}
217
- setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, test .isDataDisksFull )
259
+ expectedVMs := setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, test .isDataDisksFull )
260
+ mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
261
+ for _ , vm := range expectedVMs {
262
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
263
+ }
218
264
219
265
lun , err := common .GetNextDiskLun ("vm1" )
220
266
assert .Equal (t , test .expectedLun , lun , "TestCase[%d]: %s" , i , test .desc )
@@ -259,15 +305,20 @@ func TestDisksAreAttached(t *testing.T) {
259
305
cloud : testCloud ,
260
306
vmLockMap : newLockMap (),
261
307
}
262
- setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, false )
308
+ expectedVMs := setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, false )
309
+ mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
310
+ for _ , vm := range expectedVMs {
311
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
312
+ }
313
+ mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , "vm2" , gomock .Any ()).Return (compute.VirtualMachine {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
263
314
264
315
attached , err := common .DisksAreAttached (test .diskNames , test .nodeName )
265
316
assert .Equal (t , test .expectedAttached , attached , "TestCase[%d]: %s" , i , test .desc )
266
317
assert .Equal (t , test .expectedErr , err != nil , "TestCase[%d]: %s" , i , test .desc )
267
318
}
268
319
}
269
320
270
- func TestFilteredDetatchingDisks (t * testing.T ) {
321
+ func TestFilteredDetachingDisks (t * testing.T ) {
271
322
272
323
disks := []compute.DataDisk {
273
324
{
@@ -448,10 +499,10 @@ func TestCheckDiskExists(t *testing.T) {
448
499
newDiskName := "newdisk"
449
500
newDiskURI := fmt .Sprintf ("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s" ,
450
501
testCloud .SubscriptionID , testCloud .ResourceGroup , newDiskName )
451
- fDC := newFakeDisksClient ()
452
- rerr := fDC . CreateOrUpdate ( ctx , testCloud .ResourceGroup , newDiskName , compute. Disk {} )
453
- assert . Equal ( t , rerr == nil , true , "return error: %v" , rerr )
454
- testCloud .DisksClient = fDC
502
+
503
+ mockDisksClient := testCloud .DisksClient .( * mockdiskclient. MockInterface )
504
+ mockDisksClient . EXPECT (). Get ( gomock . Any (), testCloud . ResourceGroup , newDiskName ). Return (compute. Disk {}, nil ). AnyTimes ( )
505
+ mockDisksClient . EXPECT (). Get ( gomock . Any (), gomock . Not ( testCloud .ResourceGroup ), gomock . Any ()). Return (compute. Disk {}, & retry. Error { HTTPStatusCode : http . StatusNotFound , RawError : cloudprovider . InstanceNotFound }). AnyTimes ()
455
506
456
507
testCases := []struct {
457
508
diskURI string
@@ -503,10 +554,10 @@ func TestFilterNonExistingDisks(t *testing.T) {
503
554
testCloud .SubscriptionID , testCloud .ResourceGroup )
504
555
newDiskName := "newdisk"
505
556
newDiskURI := diskURIPrefix + newDiskName
506
- fDC := newFakeDisksClient ()
507
- rerr := fDC . CreateOrUpdate ( ctx , testCloud .ResourceGroup , newDiskName , compute. Disk {} )
508
- assert . Equal ( t , rerr == nil , true , "return error: %v" , rerr )
509
- testCloud .DisksClient = fDC
557
+
558
+ mockDisksClient := testCloud .DisksClient .( * mockdiskclient. MockInterface )
559
+ mockDisksClient . EXPECT (). Get ( gomock . Any (), testCloud . ResourceGroup , newDiskName ). Return (compute. Disk {}, nil ). AnyTimes ( )
560
+ mockDisksClient . EXPECT (). Get ( gomock . Any (), testCloud .ResourceGroup , gomock . Not ( newDiskName )). Return (compute. Disk {}, & retry. Error { HTTPStatusCode : http . StatusNotFound , RawError : cloudprovider . InstanceNotFound }). AnyTimes ()
510
561
511
562
disks := []compute.DataDisk {
512
563
{
0 commit comments