@@ -44,19 +44,36 @@ func TestStandardAttachDisk(t *testing.T) {
44
44
defer ctrl .Finish ()
45
45
46
46
testCases := []struct {
47
- desc string
48
- nodeName types.NodeName
49
- expectedErr bool
47
+ desc string
48
+ nodeName types.NodeName
49
+ isManagedDisk bool
50
+ isAttachFail bool
51
+ expectedErr bool
50
52
}{
51
53
{
52
- desc : "an error shall be returned if there's no corresponding vms" ,
53
- nodeName : "vm2" ,
54
- expectedErr : true ,
54
+ desc : "an error shall be returned if there's no corresponding vms" ,
55
+ nodeName : "vm2" ,
56
+ isManagedDisk : true ,
57
+ expectedErr : true ,
55
58
},
56
59
{
57
- desc : "no error shall be returned if everything's good" ,
58
- nodeName : "vm1" ,
59
- expectedErr : false ,
60
+ desc : "no error shall be returned if everything's good" ,
61
+ nodeName : "vm1" ,
62
+ isManagedDisk : true ,
63
+ expectedErr : false ,
64
+ },
65
+ {
66
+ desc : "no error shall be returned if everything's good with non managed disk" ,
67
+ nodeName : "vm1" ,
68
+ isManagedDisk : false ,
69
+ expectedErr : false ,
70
+ },
71
+ {
72
+ desc : "an error shall be returned if update attach disk failed" ,
73
+ nodeName : "vm1" ,
74
+ isManagedDisk : true ,
75
+ isAttachFail : true ,
76
+ expectedErr : true ,
60
77
},
61
78
}
62
79
@@ -66,12 +83,28 @@ func TestStandardAttachDisk(t *testing.T) {
66
83
expectedVMs := setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, false )
67
84
mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
68
85
for _ , vm := range expectedVMs {
86
+ vm .StorageProfile = & compute.StorageProfile {
87
+ OsDisk : & compute.OSDisk {
88
+ Name : to .StringPtr ("osdisk1" ),
89
+ ManagedDisk : & compute.ManagedDiskParameters {
90
+ ID : to .StringPtr ("ManagedID" ),
91
+ DiskEncryptionSet : & compute.DiskEncryptionSetParameters {
92
+ ID : to .StringPtr ("DiskEncryptionSetID" ),
93
+ },
94
+ },
95
+ },
96
+ DataDisks : & []compute.DataDisk {},
97
+ }
69
98
mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
70
99
}
71
100
mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , "vm2" , gomock .Any ()).Return (compute.VirtualMachine {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
72
- mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
101
+ if test .isAttachFail {
102
+ mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (& retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
103
+ } else {
104
+ mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
105
+ }
73
106
74
- err := vmSet .AttachDisk (true , "" ,
107
+ err := vmSet .AttachDisk (test . isManagedDisk , "" ,
75
108
"uri" , test .nodeName , 0 , compute .CachingTypesReadOnly , "" , false )
76
109
assert .Equal (t , test .expectedErr , err != nil , "TestCase[%d]: %s, err: %v" , i , test .desc , err )
77
110
}
@@ -85,6 +118,7 @@ func TestStandardDetachDisk(t *testing.T) {
85
118
desc string
86
119
nodeName types.NodeName
87
120
diskName string
121
+ isDetachFail bool
88
122
expectedError bool
89
123
}{
90
124
{
@@ -104,6 +138,12 @@ func TestStandardDetachDisk(t *testing.T) {
104
138
diskName : "disk1" ,
105
139
expectedError : false ,
106
140
},
141
+ {
142
+ desc : "an error shall be returned if detach disk failed" ,
143
+ nodeName : "vm1" ,
144
+ isDetachFail : true ,
145
+ expectedError : true ,
146
+ },
107
147
}
108
148
109
149
for i , test := range testCases {
@@ -115,7 +155,11 @@ func TestStandardDetachDisk(t *testing.T) {
115
155
mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
116
156
}
117
157
mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , "vm2" , gomock .Any ()).Return (compute.VirtualMachine {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
118
- mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
158
+ if test .isDetachFail {
159
+ mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (& retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
160
+ } else {
161
+ mockVMsClient .EXPECT ().Update (gomock .Any (), testCloud .ResourceGroup , gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
162
+ }
119
163
120
164
err := vmSet .DetachDisk (test .diskName , "" , test .nodeName )
121
165
assert .Equal (t , test .expectedError , err != nil , "TestCase[%d]: %s" , i , test .desc )
@@ -129,6 +173,7 @@ func TestGetDataDisks(t *testing.T) {
129
173
var testCases = []struct {
130
174
desc string
131
175
nodeName types.NodeName
176
+ isDataDiskNull bool
132
177
expectedDataDisks []compute.DataDisk
133
178
expectedError bool
134
179
crt azcache.AzureCacheReadType
@@ -164,13 +209,24 @@ func TestGetDataDisks(t *testing.T) {
164
209
expectedError : false ,
165
210
crt : azcache .CacheReadTypeUnsafe ,
166
211
},
212
+ {
213
+ desc : "nil shall be returned if DataDisk is null" ,
214
+ nodeName : "vm1" ,
215
+ isDataDiskNull : true ,
216
+ expectedDataDisks : nil ,
217
+ expectedError : false ,
218
+ crt : azcache .CacheReadTypeDefault ,
219
+ },
167
220
}
168
221
for i , test := range testCases {
169
222
testCloud := GetTestCloud (ctrl )
170
223
vmSet := testCloud .vmSet
171
224
expectedVMs := setTestVirtualMachines (testCloud , map [string ]string {"vm1" : "PowerState/Running" }, false )
172
225
mockVMsClient := testCloud .VirtualMachinesClient .(* mockvmclient.MockInterface )
173
226
for _ , vm := range expectedVMs {
227
+ if test .isDataDiskNull {
228
+ vm .StorageProfile = & compute.StorageProfile {}
229
+ }
174
230
mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , * vm .Name , gomock .Any ()).Return (vm , nil ).AnyTimes ()
175
231
}
176
232
mockVMsClient .EXPECT ().Get (gomock .Any (), testCloud .ResourceGroup , gomock .Not ("vm1" ), gomock .Any ()).Return (compute.VirtualMachine {}, & retry.Error {HTTPStatusCode : http .StatusNotFound , RawError : cloudprovider .InstanceNotFound }).AnyTimes ()
0 commit comments