@@ -21,6 +21,7 @@ import (
21
21
"io/ioutil"
22
22
"os"
23
23
"path/filepath"
24
+ "strings"
24
25
"testing"
25
26
26
27
"k8s.io/utils/exec"
@@ -40,11 +41,11 @@ const defaultTargetPath = "/mnt/test"
40
41
const defaultStagingPath = "/staging"
41
42
42
43
func getTestGCEDriver (t * testing.T ) * GCEDriver {
43
- return getCustomTestGCEDriver (t , mountmanager .NewFakeSafeMounter (), deviceutils .NewFakeDeviceUtils (), metadataservice .NewFakeService ())
44
+ return getCustomTestGCEDriver (t , mountmanager .NewFakeSafeMounter (), deviceutils .NewFakeDeviceUtils (false ), metadataservice .NewFakeService ())
44
45
}
45
46
46
47
func getTestGCEDriverWithCustomMounter (t * testing.T , mounter * mount.SafeFormatAndMount ) * GCEDriver {
47
- return getCustomTestGCEDriver (t , mounter , deviceutils .NewFakeDeviceUtils (), metadataservice .NewFakeService ())
48
+ return getCustomTestGCEDriver (t , mounter , deviceutils .NewFakeDeviceUtils (false ), metadataservice .NewFakeService ())
48
49
}
49
50
50
51
func getCustomTestGCEDriver (t * testing.T , mounter * mount.SafeFormatAndMount , deviceUtils deviceutils.DeviceUtils , metaService metadataservice.MetadataService ) * GCEDriver {
@@ -60,7 +61,7 @@ func getCustomTestGCEDriver(t *testing.T, mounter *mount.SafeFormatAndMount, dev
60
61
func getTestBlockingGCEDriver (t * testing.T , readyToExecute chan chan struct {}) * GCEDriver {
61
62
gceDriver := GetGCEDriver ()
62
63
mounter := mountmanager .NewFakeSafeBlockingMounter (readyToExecute )
63
- nodeServer := NewNodeServer (gceDriver , mounter , deviceutils .NewFakeDeviceUtils (), metadataservice .NewFakeService (), mountmanager .NewFakeStatter (mounter ))
64
+ nodeServer := NewNodeServer (gceDriver , mounter , deviceutils .NewFakeDeviceUtils (false ), metadataservice .NewFakeService (), mountmanager .NewFakeStatter (mounter ))
64
65
err := gceDriver .SetupGCEDriver (driver , "test-vendor" , nil , nil , nil , nodeServer )
65
66
if err != nil {
66
67
t .Fatalf ("Failed to setup GCE Driver: %v" , err )
@@ -381,26 +382,61 @@ func TestNodeStageVolume(t *testing.T) {
381
382
stagingPath := filepath .Join (tempDir , defaultStagingPath )
382
383
383
384
testCases := []struct {
384
- name string
385
- req * csi.NodeStageVolumeRequest
386
- expErrCode codes.Code
385
+ name string
386
+ req * csi.NodeStageVolumeRequest
387
+ deviceSize int
388
+ blockExtSize int
389
+ readonlyBit string
390
+ expResize bool
391
+ expErrCode codes.Code
387
392
}{
388
393
{
389
- name : "Valid request" ,
394
+ name : "Valid request, no resize because block and filesystem sizes match " ,
390
395
req : & csi.NodeStageVolumeRequest {
391
396
VolumeId : volumeID ,
392
397
StagingTargetPath : stagingPath ,
393
398
VolumeCapability : stdVolCap ,
394
399
},
400
+ deviceSize : 1 ,
401
+ blockExtSize : 1 ,
402
+ readonlyBit : "0" ,
403
+ expResize : false ,
395
404
},
396
405
{
397
- name : "Invalid request (Bad Access Mode) " ,
406
+ name : "Valid request, no resize bc readonly " ,
398
407
req : & csi.NodeStageVolumeRequest {
399
408
VolumeId : volumeID ,
400
409
StagingTargetPath : stagingPath ,
401
- VolumeCapability : createVolumeCapability ( csi . VolumeCapability_AccessMode_UNKNOWN ) ,
410
+ VolumeCapability : stdVolCap ,
402
411
},
403
- expErrCode : codes .InvalidArgument ,
412
+ deviceSize : 1 ,
413
+ blockExtSize : 1 ,
414
+ readonlyBit : "1" ,
415
+ expResize : false ,
416
+ },
417
+ {
418
+ name : "Valid request, resize bc size" ,
419
+ req : & csi.NodeStageVolumeRequest {
420
+ VolumeId : volumeID ,
421
+ StagingTargetPath : stagingPath ,
422
+ VolumeCapability : stdVolCap ,
423
+ },
424
+ deviceSize : 5 ,
425
+ blockExtSize : 1 ,
426
+ readonlyBit : "0" ,
427
+ expResize : true ,
428
+ },
429
+ {
430
+ name : "Valid request, no resize bc readonly capability" ,
431
+ req : & csi.NodeStageVolumeRequest {
432
+ VolumeId : volumeID ,
433
+ StagingTargetPath : stagingPath ,
434
+ VolumeCapability : createVolumeCapability (csi .VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY ),
435
+ },
436
+ deviceSize : 5 ,
437
+ blockExtSize : 1 ,
438
+ readonlyBit : "0" ,
439
+ expResize : false ,
404
440
},
405
441
{
406
442
name : "Invalid request (Bad Access Mode)" ,
@@ -448,6 +484,7 @@ func TestNodeStageVolume(t *testing.T) {
448
484
}
449
485
for _ , tc := range testCases {
450
486
t .Logf ("Test case: %s" , tc .name )
487
+ resizeCalled := false
451
488
actionList := []testingexec.FakeCommandAction {
452
489
makeFakeCmd (
453
490
& testingexec.FakeCmd {
@@ -458,26 +495,40 @@ func TestNodeStageVolume(t *testing.T) {
458
495
},
459
496
},
460
497
"blkid" ,
498
+ strings .Split ("-p -s TYPE -s PTTYPE -o export /dev/disk/fake-path" , " " )... ,
461
499
),
462
500
makeFakeCmd (
463
501
& testingexec.FakeCmd {
464
502
CombinedOutputScript : []testingexec.FakeAction {
465
503
func () ([]byte , []byte , error ) {
466
- return []byte ("1" ), nil , nil
504
+ return []byte ("" ), nil , nil
505
+ },
506
+ },
507
+ },
508
+ "fsck" ,
509
+ strings .Split ("-a /dev/disk/fake-path" , " " )... ,
510
+ ),
511
+ makeFakeCmd (
512
+ & testingexec.FakeCmd {
513
+ CombinedOutputScript : []testingexec.FakeAction {
514
+ func () ([]byte , []byte , error ) {
515
+ return []byte (tc .readonlyBit ), nil , nil
467
516
},
468
517
},
469
518
},
470
519
"blockdev" ,
520
+ strings .Split ("--getro /dev/disk/fake-path" , " " )... ,
471
521
),
472
522
makeFakeCmd (
473
523
& testingexec.FakeCmd {
474
524
CombinedOutputScript : []testingexec.FakeAction {
475
525
func () ([]byte , []byte , error ) {
476
- return []byte ("1" ), nil , nil
526
+ return []byte (fmt . Sprintf ( "%d" , tc . deviceSize ) ), nil , nil
477
527
},
478
528
},
479
529
},
480
530
"blockdev" ,
531
+ strings .Split ("--getsize64 /dev/disk/fake-path" , " " )... ,
481
532
),
482
533
makeFakeCmd (
483
534
& testingexec.FakeCmd {
@@ -488,18 +539,48 @@ func TestNodeStageVolume(t *testing.T) {
488
539
},
489
540
},
490
541
"blkid" ,
542
+ strings .Split ("-p -s TYPE -s PTTYPE -o export /dev/disk/fake-path" , " " )... ,
491
543
),
492
544
makeFakeCmd (
493
545
& testingexec.FakeCmd {
494
546
CombinedOutputScript : []testingexec.FakeAction {
495
547
func () ([]byte , []byte , error ) {
496
- return []byte (fmt .Sprintf ("block size: 1 \n block count: 1" )), nil , nil
548
+ return []byte (fmt .Sprintf ("block size: %d \n block count: 1" , tc . blockExtSize )), nil , nil
497
549
},
498
550
},
499
551
},
500
552
"dumpe2fs" ,
553
+ strings .Split ("-h /dev/disk/fake-path" , " " )... ,
501
554
),
502
555
}
556
+
557
+ if tc .expResize {
558
+ actionList = append (actionList , []testingexec.FakeCommandAction {
559
+ makeFakeCmd (
560
+ & testingexec.FakeCmd {
561
+ CombinedOutputScript : []testingexec.FakeAction {
562
+ func () ([]byte , []byte , error ) {
563
+ return []byte (fmt .Sprintf ("DEVNAME=/dev/sdb\n TYPE=ext4" )), nil , nil
564
+ },
565
+ },
566
+ },
567
+ "blkid" ,
568
+ strings .Split ("-p -s TYPE -s PTTYPE -o export /dev/disk/fake-path" , " " )... ,
569
+ ),
570
+ makeFakeCmd (
571
+ & testingexec.FakeCmd {
572
+ CombinedOutputScript : []testingexec.FakeAction {
573
+ func () ([]byte , []byte , error ) {
574
+ resizeCalled = true
575
+ return []byte (fmt .Sprintf ("DEVNAME=/dev/sdb\n TYPE=ext4" )), nil , nil
576
+ },
577
+ },
578
+ },
579
+ "resize2fs" ,
580
+ strings .Split ("/dev/disk/fake-path" , " " )... ,
581
+ ),
582
+ }... )
583
+ }
503
584
mounter := mountmanager .NewFakeSafeMounterWithCustomExec (& testingexec.FakeExec {CommandScript : actionList })
504
585
gceDriver := getTestGCEDriverWithCustomMounter (t , mounter )
505
586
ns := gceDriver .ns
@@ -517,6 +598,12 @@ func TestNodeStageVolume(t *testing.T) {
517
598
if tc .expErrCode != codes .OK {
518
599
t .Fatalf ("Expected error: %v, got no error" , tc .expErrCode )
519
600
}
601
+ if tc .expResize == true && resizeCalled == false {
602
+ t .Fatalf ("Test did not call resize, but it was expected." )
603
+ }
604
+ if tc .expResize == false && resizeCalled == true {
605
+ t .Fatalf ("Test called resize, but it was not expected." )
606
+ }
520
607
}
521
608
}
522
609
0 commit comments