@@ -18,8 +18,18 @@ package blobfuse
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
22
+ "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
23
+ "github.com/golang/mock/gomock"
24
+ "k8s.io/legacy-cloud-providers/azure"
25
+ "k8s.io/legacy-cloud-providers/azure/clients/storageaccountclient/mockstorageaccountclient"
26
+ "k8s.io/legacy-cloud-providers/azure/retry"
27
+ "reflect"
21
28
"testing"
22
29
30
+ "google.golang.org/grpc/codes"
31
+ "google.golang.org/grpc/status"
32
+
23
33
"github.com/container-storage-interface/spec/lib/go/csi"
24
34
"github.com/stretchr/testify/assert"
25
35
)
@@ -38,3 +48,365 @@ func TestControllerGetCapabilities(t *testing.T) {
38
48
assert .NotNil (t , resp )
39
49
assert .Equal (t , resp .Capabilities , controlCap )
40
50
}
51
+
52
+ func TestCreateVolume (t * testing.T ) {
53
+ stdVolumeCapability := & csi.VolumeCapability {
54
+ AccessType : & csi.VolumeCapability_Mount {
55
+ Mount : & csi.VolumeCapability_MountVolume {},
56
+ },
57
+ }
58
+ stdVolumeCapabilities := []* csi.VolumeCapability {
59
+ stdVolumeCapability ,
60
+ }
61
+ controllerservicecapabilityRPC := & csi.ControllerServiceCapability_RPC {
62
+ Type : csi .ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME ,
63
+ }
64
+ controllerServiceCapability := & csi.ControllerServiceCapability {
65
+ Type : & csi.ControllerServiceCapability_Rpc {
66
+ Rpc : controllerservicecapabilityRPC ,
67
+ },
68
+ }
69
+ testCases := []struct {
70
+ name string
71
+ testFunc func (t * testing.T )
72
+ }{
73
+ {
74
+ name : "invalid create volume req" ,
75
+ testFunc : func (t * testing.T ) {
76
+ d := NewFakeDriver ()
77
+ req := & csi.CreateVolumeRequest {}
78
+ _ , err := d .CreateVolume (context .Background (), req )
79
+ expectedErr := status .Error (codes .InvalidArgument , "CREATE_DELETE_VOLUME" )
80
+ if ! reflect .DeepEqual (err , expectedErr ) {
81
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
82
+ }
83
+ },
84
+ },
85
+ {
86
+ name : "volume Name missing" ,
87
+ testFunc : func (t * testing.T ) {
88
+ d := NewFakeDriver ()
89
+ d .Cap = []* csi.ControllerServiceCapability {
90
+ controllerServiceCapability ,
91
+ }
92
+ req := & csi.CreateVolumeRequest {}
93
+ _ , err := d .CreateVolume (context .Background (), req )
94
+ expectedErr := status .Error (codes .InvalidArgument , "CreateVolume Name must be provided" )
95
+ if ! reflect .DeepEqual (err , expectedErr ) {
96
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
97
+ }
98
+ },
99
+ },
100
+ {
101
+ name : "volume capacity missing" ,
102
+ testFunc : func (t * testing.T ) {
103
+ d := NewFakeDriver ()
104
+ req := & csi.CreateVolumeRequest {
105
+ Name : "unit-test" ,
106
+ }
107
+ d .Cap = []* csi.ControllerServiceCapability {
108
+ controllerServiceCapability ,
109
+ }
110
+ _ , err := d .CreateVolume (context .Background (), req )
111
+ expectedErr := status .Error (codes .InvalidArgument , "CreateVolume Volume capabilities must be provided" )
112
+ if ! reflect .DeepEqual (err , expectedErr ) {
113
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
114
+ }
115
+ },
116
+ },
117
+ {
118
+ name : "getStorageAccounts error" ,
119
+ testFunc : func (t * testing.T ) {
120
+ d := NewFakeDriver ()
121
+ req := & csi.CreateVolumeRequest {
122
+ Name : "unit-test" ,
123
+ VolumeCapabilities : stdVolumeCapabilities ,
124
+ }
125
+ d .Cap = []* csi.ControllerServiceCapability {
126
+ controllerServiceCapability ,
127
+ }
128
+ d .cloud = & azure.Cloud {}
129
+ ctrl := gomock .NewController (t )
130
+ defer ctrl .Finish ()
131
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
132
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
133
+ rerr := & retry.Error {
134
+ RawError : fmt .Errorf ("test" ),
135
+ }
136
+ mockStorageAccountsClient .EXPECT ().ListByResourceGroup (gomock .Any (), gomock .Any ()).Return (nil , rerr ).AnyTimes ()
137
+ _ , err := d .CreateVolume (context .Background (), req )
138
+ expectedErr := fmt .Errorf ("could not get storage key for storage account : could not list storage accounts for account type : Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: test" )
139
+ if ! reflect .DeepEqual (err , expectedErr ) {
140
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
141
+ }
142
+ },
143
+ },
144
+ }
145
+ for _ , tc := range testCases {
146
+ t .Run (tc .name , tc .testFunc )
147
+ }
148
+ }
149
+
150
+ func TestDeleteVolume (t * testing.T ) {
151
+ controllerservicecapabilityRPC := & csi.ControllerServiceCapability_RPC {
152
+ Type : csi .ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME ,
153
+ }
154
+ controllerServiceCapability := & csi.ControllerServiceCapability {
155
+ Type : & csi.ControllerServiceCapability_Rpc {
156
+ Rpc : controllerservicecapabilityRPC ,
157
+ },
158
+ }
159
+ testCases := []struct {
160
+ name string
161
+ testFunc func (t * testing.T )
162
+ }{
163
+ {
164
+ name : "volume Name missing" ,
165
+ testFunc : func (t * testing.T ) {
166
+ d := NewFakeDriver ()
167
+ req := & csi.DeleteVolumeRequest {}
168
+ _ , err := d .DeleteVolume (context .Background (), req )
169
+ expectedErr := status .Error (codes .InvalidArgument , "Volume ID missing in request" )
170
+ if ! reflect .DeepEqual (err , expectedErr ) {
171
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
172
+ }
173
+ },
174
+ },
175
+ {
176
+ name : "invalid create volume req" ,
177
+ testFunc : func (t * testing.T ) {
178
+ d := NewFakeDriver ()
179
+ req := & csi.DeleteVolumeRequest {
180
+ VolumeId : "unit-test" ,
181
+ }
182
+ _ , err := d .DeleteVolume (context .Background (), req )
183
+ expectedErr := fmt .Errorf ("invalid delete volume req: volume_id:\" unit-test\" " )
184
+ if ! reflect .DeepEqual (err , expectedErr ) {
185
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
186
+ }
187
+ },
188
+ },
189
+ {
190
+ name : "invalid volume Id" ,
191
+ testFunc : func (t * testing.T ) {
192
+ d := NewFakeDriver ()
193
+ d .Cap = []* csi.ControllerServiceCapability {
194
+ controllerServiceCapability ,
195
+ }
196
+ req := & csi.DeleteVolumeRequest {
197
+ VolumeId : "unit-test" ,
198
+ }
199
+ _ , err := d .DeleteVolume (context .Background (), req )
200
+ expectedErr := error (nil )
201
+ if ! reflect .DeepEqual (err , expectedErr ) {
202
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
203
+ }
204
+ },
205
+ },
206
+ {
207
+ name : "ListKeys error" ,
208
+ testFunc : func (t * testing.T ) {
209
+ d := NewFakeDriver ()
210
+ d .Cap = []* csi.ControllerServiceCapability {
211
+ controllerServiceCapability ,
212
+ }
213
+ req := & csi.DeleteVolumeRequest {
214
+ VolumeId : "unit#test#test" ,
215
+ }
216
+ d .cloud = & azure.Cloud {}
217
+ ctrl := gomock .NewController (t )
218
+ defer ctrl .Finish ()
219
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
220
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
221
+ rerr := & retry.Error {
222
+ RawError : fmt .Errorf ("test" ),
223
+ }
224
+ accountListKeysResult := storage.AccountListKeysResult {}
225
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (accountListKeysResult , rerr ).AnyTimes ()
226
+ expectedErr := fmt .Errorf ("no key for storage account(test) under resource group(unit), err Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: test" )
227
+ _ , err := d .DeleteVolume (context .Background (), req )
228
+ if ! reflect .DeepEqual (err , expectedErr ) {
229
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
230
+ }
231
+ },
232
+ },
233
+ }
234
+ for _ , tc := range testCases {
235
+ t .Run (tc .name , tc .testFunc )
236
+ }
237
+ }
238
+
239
+ func TestValidateVolumeCapabilities (t * testing.T ) {
240
+ stdVolumeCapability := & csi.VolumeCapability {
241
+ AccessType : & csi.VolumeCapability_Mount {
242
+ Mount : & csi.VolumeCapability_MountVolume {},
243
+ },
244
+ }
245
+ stdVolumeCapabilities := []* csi.VolumeCapability {
246
+ stdVolumeCapability ,
247
+ }
248
+ controllerservicecapabilityRPC := & csi.ControllerServiceCapability_RPC {
249
+ Type : csi .ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME ,
250
+ }
251
+ controllerServiceCapability := & csi.ControllerServiceCapability {
252
+ Type : & csi.ControllerServiceCapability_Rpc {
253
+ Rpc : controllerservicecapabilityRPC ,
254
+ },
255
+ }
256
+ testCases := []struct {
257
+ name string
258
+ testFunc func (t * testing.T )
259
+ }{
260
+ {
261
+ name : "volume ID missing" ,
262
+ testFunc : func (t * testing.T ) {
263
+ d := NewFakeDriver ()
264
+ req := & csi.ValidateVolumeCapabilitiesRequest {}
265
+ _ , err := d .ValidateVolumeCapabilities (context .Background (), req )
266
+ expectedErr := status .Error (codes .InvalidArgument , "Volume ID missing in request" )
267
+ if ! reflect .DeepEqual (err , expectedErr ) {
268
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
269
+ }
270
+ },
271
+ },
272
+ {
273
+ name : "volume capability missing" ,
274
+ testFunc : func (t * testing.T ) {
275
+ d := NewFakeDriver ()
276
+ req := & csi.ValidateVolumeCapabilitiesRequest {
277
+ VolumeId : "unit-test" ,
278
+ }
279
+ _ , err := d .ValidateVolumeCapabilities (context .Background (), req )
280
+ expectedErr := status .Error (codes .InvalidArgument , "Volume capabilities missing in request" )
281
+ if ! reflect .DeepEqual (err , expectedErr ) {
282
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
283
+ }
284
+ },
285
+ },
286
+ {
287
+ name : "invalid volume Id" ,
288
+ testFunc : func (t * testing.T ) {
289
+ d := NewFakeDriver ()
290
+ req := & csi.ValidateVolumeCapabilitiesRequest {
291
+ VolumeId : "unit-test" ,
292
+ VolumeCapabilities : stdVolumeCapabilities ,
293
+ }
294
+ _ , err := d .ValidateVolumeCapabilities (context .Background (), req )
295
+ expectedErr := status .Error (codes .NotFound , "error parsing volume id: \" unit-test\" , should at least contain two #" )
296
+ if ! reflect .DeepEqual (err , expectedErr ) {
297
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
298
+ }
299
+ },
300
+ },
301
+ {
302
+ name : "ListKeys error" ,
303
+ testFunc : func (t * testing.T ) {
304
+ d := NewFakeDriver ()
305
+ d .Cap = []* csi.ControllerServiceCapability {
306
+ controllerServiceCapability ,
307
+ }
308
+ req := & csi.ValidateVolumeCapabilitiesRequest {
309
+ VolumeId : "unit#test#test" ,
310
+ VolumeCapabilities : stdVolumeCapabilities ,
311
+ }
312
+ d .cloud = & azure.Cloud {}
313
+ ctrl := gomock .NewController (t )
314
+ defer ctrl .Finish ()
315
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
316
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
317
+ rerr := & retry.Error {
318
+ RawError : fmt .Errorf ("test" ),
319
+ }
320
+ accountListKeysResult := storage.AccountListKeysResult {}
321
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (accountListKeysResult , rerr ).AnyTimes ()
322
+ expectedErr := fmt .Errorf ("no key for storage account(test) under resource group(unit), err Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: test" )
323
+ _ , err := d .ValidateVolumeCapabilities (context .Background (), req )
324
+ if ! reflect .DeepEqual (err , expectedErr ) {
325
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
326
+ }
327
+ },
328
+ },
329
+ }
330
+ for _ , tc := range testCases {
331
+ t .Run (tc .name , tc .testFunc )
332
+ }
333
+ }
334
+
335
+ func TestGetCapacity (t * testing.T ) {
336
+ d := NewFakeDriver ()
337
+ req := csi.GetCapacityRequest {}
338
+ resp , err := d .GetCapacity (context .Background (), & req )
339
+ assert .Nil (t , resp )
340
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
341
+ t .Errorf ("Unexpected error: %v" , err )
342
+ }
343
+ }
344
+
345
+ func TestListVolumes (t * testing.T ) {
346
+ d := NewFakeDriver ()
347
+ req := csi.ListVolumesRequest {}
348
+ resp , err := d .ListVolumes (context .Background (), & req )
349
+ assert .Nil (t , resp )
350
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
351
+ t .Errorf ("Unexpected error: %v" , err )
352
+ }
353
+ }
354
+
355
+ func TestControllerPublishVolume (t * testing.T ) {
356
+ d := NewFakeDriver ()
357
+ req := csi.ControllerPublishVolumeRequest {}
358
+ resp , err := d .ControllerPublishVolume (context .Background (), & req )
359
+ assert .Nil (t , resp )
360
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
361
+ t .Errorf ("Unexpected error: %v" , err )
362
+ }
363
+ }
364
+
365
+ func TestControllerUnpublishVolume (t * testing.T ) {
366
+ d := NewFakeDriver ()
367
+ req := csi.ControllerUnpublishVolumeRequest {}
368
+ resp , err := d .ControllerUnpublishVolume (context .Background (), & req )
369
+ assert .Nil (t , resp )
370
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
371
+ t .Errorf ("Unexpected error: %v" , err )
372
+ }
373
+ }
374
+
375
+ func TestCreateSnapshots (t * testing.T ) {
376
+ d := NewFakeDriver ()
377
+ req := csi.CreateSnapshotRequest {}
378
+ resp , err := d .CreateSnapshot (context .Background (), & req )
379
+ assert .Nil (t , resp )
380
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
381
+ t .Errorf ("Unexpected error: %v" , err )
382
+ }
383
+ }
384
+ func TestDeleteSnapshots (t * testing.T ) {
385
+ d := NewFakeDriver ()
386
+ req := csi.DeleteSnapshotRequest {}
387
+ resp , err := d .DeleteSnapshot (context .Background (), & req )
388
+ assert .Nil (t , resp )
389
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
390
+ t .Errorf ("Unexpected error: %v" , err )
391
+ }
392
+ }
393
+
394
+ func TestListSnapshots (t * testing.T ) {
395
+ d := NewFakeDriver ()
396
+ req := csi.ListSnapshotsRequest {}
397
+ resp , err := d .ListSnapshots (context .Background (), & req )
398
+ assert .Nil (t , resp )
399
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "" )) {
400
+ t .Errorf ("Unexpected error: %v" , err )
401
+ }
402
+ }
403
+
404
+ func TestControllerExpandVolume (t * testing.T ) {
405
+ d := NewFakeDriver ()
406
+ req := csi.ControllerExpandVolumeRequest {}
407
+ resp , err := d .ControllerExpandVolume (context .Background (), & req )
408
+ assert .Nil (t , resp )
409
+ if ! reflect .DeepEqual (err , status .Error (codes .Unimplemented , "ControllerExpandVolume is not yet implemented" )) {
410
+ t .Errorf ("Unexpected error: %v" , err )
411
+ }
412
+ }
0 commit comments