@@ -15,12 +15,17 @@ limitations under the License.
15
15
package gceGCEDriver
16
16
17
17
import (
18
+ "errors"
19
+ "fmt"
18
20
"testing"
19
21
20
22
"context"
23
+
21
24
csi "github.com/container-storage-interface/spec/lib/go/csi"
22
25
"google.golang.org/grpc/codes"
23
26
"google.golang.org/grpc/status"
27
+ "k8s.io/kubernetes/pkg/util/mount"
28
+ utilexec "k8s.io/utils/exec"
24
29
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
25
30
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
26
31
)
@@ -30,8 +35,16 @@ const defaultTargetPath = "/mnt/test"
30
35
const defaultStagingPath = "/staging"
31
36
32
37
func getTestGCEDriver (t * testing.T ) * GCEDriver {
38
+ return getCustomTestGCEDriver (t , mountmanager .NewFakeSafeMounter (), mountmanager .NewFakeDeviceUtils (), metadataservice .NewFakeService ())
39
+ }
40
+
41
+ func getTestGCEDriverWithCustomMounter (t * testing.T , mounter * mount.SafeFormatAndMount ) * GCEDriver {
42
+ return getCustomTestGCEDriver (t , mounter , mountmanager .NewFakeDeviceUtils (), metadataservice .NewFakeService ())
43
+ }
44
+
45
+ func getCustomTestGCEDriver (t * testing.T , mounter * mount.SafeFormatAndMount , deviceUtils mountmanager.DeviceUtils , metaService metadataservice.MetadataService ) * GCEDriver {
33
46
gceDriver := GetGCEDriver ()
34
- err := gceDriver .SetupGCEDriver (nil , mountmanager . NewFakeSafeMounter (), mountmanager . NewFakeDeviceUtils (), metadataservice . NewFakeService () , driver , "test-vendor" )
47
+ err := gceDriver .SetupGCEDriver (nil , mounter , deviceUtils , metaService , driver , "test-vendor" )
35
48
if err != nil {
36
49
t .Fatalf ("Failed to setup GCE Driver: %v" , err )
37
50
}
@@ -328,6 +341,87 @@ func TestNodeStageVolume(t *testing.T) {
328
341
}
329
342
}
330
343
344
+ func TestNodeExpandVolume (t * testing.T ) {
345
+ // TODO: Add tests/functionality for non-existant volume
346
+ var resizedBytes int64 = 2000000000
347
+ volumeID := "project/test001/zones/c1/disks/testDisk"
348
+ testCases := []struct {
349
+ name string
350
+ req * csi.NodeExpandVolumeRequest
351
+ blockDevice bool
352
+ expRespBytes int64
353
+ expErrCode codes.Code
354
+ }{
355
+ {
356
+ name : "ext4 fs expand" ,
357
+ req : & csi.NodeExpandVolumeRequest {
358
+ VolumeId : volumeID ,
359
+ VolumePath : "some-path" ,
360
+ CapacityRange : & csi.CapacityRange {
361
+ RequiredBytes : resizedBytes ,
362
+ },
363
+ },
364
+ blockDevice : false ,
365
+ expRespBytes : resizedBytes ,
366
+ },
367
+ {
368
+ name : "block device expand" ,
369
+ req : & csi.NodeExpandVolumeRequest {
370
+ VolumeId : volumeID ,
371
+ VolumePath : "some-path" ,
372
+ CapacityRange : & csi.CapacityRange {
373
+ RequiredBytes : resizedBytes ,
374
+ },
375
+ },
376
+ blockDevice : true ,
377
+ },
378
+ }
379
+ for _ , tc := range testCases {
380
+ t .Logf ("Test case: %s" , tc .name )
381
+
382
+ execCallback := func (cmd string , args ... string ) ([]byte , error ) {
383
+ if cmd == "blkid" {
384
+ if tc .blockDevice {
385
+ // blkid returns exit code 2 when run on unformatted device
386
+ return nil , utilexec.CodeExitError {
387
+ Err : errors .New ("this is an exit error" ),
388
+ Code : 2 ,
389
+ }
390
+ } else {
391
+ return []byte ("DEVNAME=/dev/sdb\n TYPE=ext4" ), nil
392
+ }
393
+ } else if cmd == "resize2fs" {
394
+ if tc .blockDevice {
395
+ t .Fatalf ("resize fs called on block device" )
396
+ }
397
+ return nil , nil
398
+ }
399
+ return nil , fmt .Errorf ("fake exec got unknown call to %v %v" , cmd , args )
400
+ }
401
+ mounter := mountmanager .NewFakeSafeMounterWithCustomExec (mount .NewFakeExec (execCallback ))
402
+ gceDriver := getTestGCEDriverWithCustomMounter (t , mounter )
403
+
404
+ resp , err := gceDriver .ns .NodeExpandVolume (context .Background (), tc .req )
405
+ if err != nil {
406
+ serverError , ok := status .FromError (err )
407
+ if ! ok {
408
+ t .Fatalf ("Could not get error status code from err: %v" , err )
409
+ }
410
+ if serverError .Code () != tc .expErrCode {
411
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
412
+ }
413
+ continue
414
+ }
415
+ if tc .expErrCode != codes .OK {
416
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
417
+ }
418
+
419
+ if resp .CapacityBytes != tc .expRespBytes {
420
+ t .Fatalf ("Expected bytes: %v, got: %v" , tc .expRespBytes , resp .CapacityBytes )
421
+ }
422
+ }
423
+ }
424
+
331
425
func TestNodeUnstageVolume (t * testing.T ) {
332
426
gceDriver := getTestGCEDriver (t )
333
427
ns := gceDriver .ns
0 commit comments