@@ -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
}
@@ -319,6 +332,87 @@ func TestNodeStageVolume(t *testing.T) {
319
332
}
320
333
}
321
334
335
+ func TestNodeExpandVolume (t * testing.T ) {
336
+ // TODO: Add tests/functionality for non-existant volume
337
+ var resizedBytes int64 = 2000000000
338
+ volumeID := "project/test001/zones/c1/disks/testDisk"
339
+ testCases := []struct {
340
+ name string
341
+ req * csi.NodeExpandVolumeRequest
342
+ blockDevice bool
343
+ expRespBytes int64
344
+ expErrCode codes.Code
345
+ }{
346
+ {
347
+ name : "ext4 fs expand" ,
348
+ req : & csi.NodeExpandVolumeRequest {
349
+ VolumeId : volumeID ,
350
+ VolumePath : "some-path" ,
351
+ CapacityRange : & csi.CapacityRange {
352
+ RequiredBytes : resizedBytes ,
353
+ },
354
+ },
355
+ blockDevice : false ,
356
+ expRespBytes : resizedBytes ,
357
+ },
358
+ {
359
+ name : "block device expand" ,
360
+ req : & csi.NodeExpandVolumeRequest {
361
+ VolumeId : volumeID ,
362
+ VolumePath : "some-path" ,
363
+ CapacityRange : & csi.CapacityRange {
364
+ RequiredBytes : resizedBytes ,
365
+ },
366
+ },
367
+ blockDevice : true ,
368
+ },
369
+ }
370
+ for _ , tc := range testCases {
371
+ t .Logf ("Test case: %s" , tc .name )
372
+
373
+ execCallback := func (cmd string , args ... string ) ([]byte , error ) {
374
+ if cmd == "blkid" {
375
+ if tc .blockDevice {
376
+ // blkid returns exit code 2 when run on unformatted device
377
+ return nil , utilexec.CodeExitError {
378
+ Err : errors .New ("this is an exit error" ),
379
+ Code : 2 ,
380
+ }
381
+ } else {
382
+ return []byte ("DEVNAME=/dev/sdb\n TYPE=ext4" ), nil
383
+ }
384
+ } else if cmd == "resize2fs" {
385
+ if tc .blockDevice {
386
+ t .Fatalf ("resize fs called on block device" )
387
+ }
388
+ return nil , nil
389
+ }
390
+ return nil , fmt .Errorf ("fake exec got unknown call to %v %v" , cmd , args )
391
+ }
392
+ mounter := mountmanager .NewFakeSafeMounterWithCustomExec (mount .NewFakeExec (execCallback ))
393
+ gceDriver := getTestGCEDriverWithCustomMounter (t , mounter )
394
+
395
+ resp , err := gceDriver .ns .NodeExpandVolume (context .Background (), tc .req )
396
+ if err != nil {
397
+ serverError , ok := status .FromError (err )
398
+ if ! ok {
399
+ t .Fatalf ("Could not get error status code from err: %v" , err )
400
+ }
401
+ if serverError .Code () != tc .expErrCode {
402
+ t .Fatalf ("Expected error code: %v, got: %v. err : %v" , tc .expErrCode , serverError .Code (), err )
403
+ }
404
+ continue
405
+ }
406
+ if tc .expErrCode != codes .OK {
407
+ t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
408
+ }
409
+
410
+ if resp .CapacityBytes != tc .expRespBytes {
411
+ t .Fatalf ("Expected bytes: %v, got: %v" , tc .expRespBytes , resp .CapacityBytes )
412
+ }
413
+ }
414
+ }
415
+
322
416
func TestNodeUnstageVolume (t * testing.T ) {
323
417
gceDriver := getTestGCEDriver (t )
324
418
ns := gceDriver .ns
0 commit comments