@@ -19,10 +19,16 @@ import (
19
19
20
20
csi "github.com/container-storage-interface/spec/lib/go/csi"
21
21
"golang.org/x/net/context"
22
+ "google.golang.org/grpc/codes"
23
+ "google.golang.org/grpc/status"
22
24
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
23
25
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
24
26
)
25
27
28
+ const defaultVolumeID = "project/test001/zones/c1/disks/testDisk"
29
+ const defaultTargetPath = "/mnt/test"
30
+ const defaultStagingPath = "/staging"
31
+
26
32
func getTestGCEDriver (t * testing.T ) * GCEDriver {
27
33
gceDriver := GetGCEDriver ()
28
34
err := gceDriver .SetupGCEDriver (nil , mountmanager .NewFakeSafeMounter (), mountmanager .NewFakeDeviceUtils (), metadataservice .NewFakeService (), driver , "test-vendor" )
@@ -86,3 +92,270 @@ func TestNodeGetVolumeLimits(t *testing.T) {
86
92
}
87
93
}
88
94
}
95
+
96
+ func TestNodePublishVolume (t * testing.T ) {
97
+ gceDriver := getTestGCEDriver (t )
98
+ ns := gceDriver .ns
99
+ testCases := []struct {
100
+ name string
101
+ req * csi.NodePublishVolumeRequest
102
+ expErrCode codes.Code
103
+ }{
104
+ {
105
+ name : "Valid request" ,
106
+ req : & csi.NodePublishVolumeRequest {
107
+ VolumeId : defaultVolumeID ,
108
+ TargetPath : defaultTargetPath ,
109
+ StagingTargetPath : defaultStagingPath ,
110
+ Readonly : false ,
111
+ VolumeCapability : & csi.VolumeCapability {},
112
+ },
113
+ },
114
+ {
115
+ name : "Invalid request (No VolumeId)" ,
116
+ req : & csi.NodePublishVolumeRequest {
117
+ TargetPath : defaultTargetPath ,
118
+ StagingTargetPath : defaultStagingPath ,
119
+ Readonly : false ,
120
+ VolumeCapability : & csi.VolumeCapability {},
121
+ },
122
+ expErrCode : codes .InvalidArgument ,
123
+ },
124
+ {
125
+ name : "Invalid request (No TargetPath)" ,
126
+ req : & csi.NodePublishVolumeRequest {
127
+ VolumeId : defaultVolumeID ,
128
+ StagingTargetPath : defaultStagingPath ,
129
+ Readonly : false ,
130
+ VolumeCapability : & csi.VolumeCapability {},
131
+ },
132
+ expErrCode : codes .InvalidArgument ,
133
+ },
134
+ {
135
+ name : "Invalid request (No StagingTargetPath)" ,
136
+ req : & csi.NodePublishVolumeRequest {
137
+ VolumeId : defaultVolumeID ,
138
+ TargetPath : defaultTargetPath ,
139
+ Readonly : false ,
140
+ VolumeCapability : & csi.VolumeCapability {},
141
+ },
142
+ expErrCode : codes .InvalidArgument ,
143
+ },
144
+ {
145
+ name : "Invalid request (Nil VolumeCapability)" ,
146
+ req : & csi.NodePublishVolumeRequest {
147
+ VolumeId : defaultVolumeID ,
148
+ TargetPath : defaultTargetPath ,
149
+ StagingTargetPath : defaultStagingPath ,
150
+ Readonly : false ,
151
+ VolumeCapability : nil ,
152
+ },
153
+ expErrCode : codes .InvalidArgument ,
154
+ },
155
+ }
156
+ for _ , tc := range testCases {
157
+ t .Logf ("Test case: %s" , tc .name )
158
+ _ , err := ns .NodePublishVolume (context .Background (), tc .req )
159
+ if err != nil {
160
+ serverError , ok := status .FromError (err )
161
+ if ! ok {
162
+ t .Fatalf ("Could not get error status code from err: %v" , err )
163
+ }
164
+ if serverError .Code () != tc .expErrCode {
165
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
166
+ }
167
+ continue
168
+ }
169
+ if tc .expErrCode != codes .OK {
170
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
171
+ }
172
+ }
173
+ }
174
+
175
+ func TestNodeUnpublishVolume (t * testing.T ) {
176
+ gceDriver := getTestGCEDriver (t )
177
+ ns := gceDriver .ns
178
+ testCases := []struct {
179
+ name string
180
+ req * csi.NodeUnpublishVolumeRequest
181
+ expErrCode codes.Code
182
+ }{
183
+ {
184
+ name : "Valid request" ,
185
+ req : & csi.NodeUnpublishVolumeRequest {
186
+ VolumeId : defaultVolumeID ,
187
+ TargetPath : defaultTargetPath ,
188
+ },
189
+ },
190
+ {
191
+ name : "Invalid request (No VolumeId)" ,
192
+ req : & csi.NodeUnpublishVolumeRequest {
193
+ TargetPath : defaultTargetPath ,
194
+ },
195
+ expErrCode : codes .InvalidArgument ,
196
+ },
197
+ {
198
+ name : "Invalid request (No TargetPath)" ,
199
+ req : & csi.NodeUnpublishVolumeRequest {
200
+ VolumeId : defaultVolumeID ,
201
+ },
202
+ expErrCode : codes .InvalidArgument ,
203
+ },
204
+ }
205
+ for _ , tc := range testCases {
206
+ t .Logf ("Test case: %s" , tc .name )
207
+ _ , err := ns .NodeUnpublishVolume (context .Background (), tc .req )
208
+ if err != nil {
209
+ serverError , ok := status .FromError (err )
210
+ if ! ok {
211
+ t .Fatalf ("Could not get error status code from err: %v" , err )
212
+ }
213
+ if serverError .Code () != tc .expErrCode {
214
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
215
+ }
216
+ continue
217
+ }
218
+ if tc .expErrCode != codes .OK {
219
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
220
+ }
221
+ }
222
+ }
223
+
224
+ func TestNodeStageVolume (t * testing.T ) {
225
+ gceDriver := getTestGCEDriver (t )
226
+ ns := gceDriver .ns
227
+ volumeID := "project/test001/zones/c1/disks/testDisk"
228
+ blockCap := & csi.VolumeCapability_Block {
229
+ Block : & csi.VolumeCapability_BlockVolume {},
230
+ }
231
+ cap := & csi.VolumeCapability {
232
+ AccessType : blockCap ,
233
+ }
234
+
235
+ testCases := []struct {
236
+ name string
237
+ req * csi.NodeStageVolumeRequest
238
+ expErrCode codes.Code
239
+ }{
240
+ {
241
+ name : "Valid request" ,
242
+ req : & csi.NodeStageVolumeRequest {
243
+ VolumeId : volumeID ,
244
+ StagingTargetPath : defaultStagingPath ,
245
+ VolumeCapability : & csi.VolumeCapability {},
246
+ },
247
+ },
248
+ {
249
+ name : "Invalid request (No VolumeId)" ,
250
+ req : & csi.NodeStageVolumeRequest {
251
+ StagingTargetPath : defaultStagingPath ,
252
+ VolumeCapability : & csi.VolumeCapability {},
253
+ },
254
+ expErrCode : codes .InvalidArgument ,
255
+ },
256
+ {
257
+ name : "Invalid request (No StagingTargetPath)" ,
258
+ req : & csi.NodeStageVolumeRequest {
259
+ VolumeId : volumeID ,
260
+ VolumeCapability : & csi.VolumeCapability {},
261
+ },
262
+ expErrCode : codes .InvalidArgument ,
263
+ },
264
+ {
265
+ name : "Invalid request (Nil VolumeCapability)" ,
266
+ req : & csi.NodeStageVolumeRequest {
267
+ VolumeId : volumeID ,
268
+ StagingTargetPath : defaultStagingPath ,
269
+ VolumeCapability : nil ,
270
+ },
271
+ expErrCode : codes .InvalidArgument ,
272
+ },
273
+ {
274
+ name : "Invalid request (No Mount in capability)" ,
275
+ req : & csi.NodeStageVolumeRequest {
276
+ VolumeId : volumeID ,
277
+ StagingTargetPath : defaultStagingPath ,
278
+ VolumeCapability : cap ,
279
+ },
280
+ expErrCode : codes .Unimplemented ,
281
+ },
282
+ // Capability Mount. codes.Unimplemented
283
+ }
284
+ for _ , tc := range testCases {
285
+ t .Logf ("Test case: %s" , tc .name )
286
+ _ , err := ns .NodeStageVolume (context .Background (), tc .req )
287
+ if err != nil {
288
+ serverError , ok := status .FromError (err )
289
+ if ! ok {
290
+ t .Fatalf ("Could not get error status code from err: %v" , err )
291
+ }
292
+ if serverError .Code () != tc .expErrCode {
293
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
294
+ }
295
+ continue
296
+ }
297
+ if tc .expErrCode != codes .OK {
298
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
299
+ }
300
+ }
301
+ }
302
+
303
+ func TestNodeUnstageVolume (t * testing.T ) {
304
+ gceDriver := getTestGCEDriver (t )
305
+ ns := gceDriver .ns
306
+ testCases := []struct {
307
+ name string
308
+ req * csi.NodeUnstageVolumeRequest
309
+ expErrCode codes.Code
310
+ }{
311
+ {
312
+ name : "Valid request" ,
313
+ req : & csi.NodeUnstageVolumeRequest {
314
+ VolumeId : defaultVolumeID ,
315
+ StagingTargetPath : defaultStagingPath ,
316
+ },
317
+ },
318
+ {
319
+ name : "Invalid request (No VolumeId)" ,
320
+ req : & csi.NodeUnstageVolumeRequest {
321
+ StagingTargetPath : defaultStagingPath ,
322
+ },
323
+ expErrCode : codes .InvalidArgument ,
324
+ },
325
+ {
326
+ name : "Invalid request (No StagingTargetPath)" ,
327
+ req : & csi.NodeUnstageVolumeRequest {
328
+ VolumeId : defaultVolumeID ,
329
+ },
330
+ expErrCode : codes .InvalidArgument ,
331
+ },
332
+ }
333
+ for _ , tc := range testCases {
334
+ t .Logf ("Test case: %s" , tc .name )
335
+ _ , err := ns .NodeUnstageVolume (context .Background (), tc .req )
336
+ if err != nil {
337
+ serverError , ok := status .FromError (err )
338
+ if ! ok {
339
+ t .Fatalf ("Could not get error status code from err: %v" , err )
340
+ }
341
+ if serverError .Code () != tc .expErrCode {
342
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
343
+ }
344
+ continue
345
+ }
346
+ if tc .expErrCode != codes .OK {
347
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
348
+ }
349
+ }
350
+ }
351
+
352
+ func TestNodeGetCapabilities (t * testing.T ) {
353
+ gceDriver := getTestGCEDriver (t )
354
+ ns := gceDriver .ns
355
+ req := & csi.NodeGetCapabilitiesRequest {}
356
+
357
+ _ , err := ns .NodeGetCapabilities (context .Background (), req )
358
+ if err != nil {
359
+ t .Fatalf ("Unexpedted error: %v" , err )
360
+ }
361
+ }
0 commit comments