@@ -28,6 +28,7 @@ import (
28
28
testingexec "k8s.io/utils/exec/testing"
29
29
30
30
csi "github.com/container-storage-interface/spec/lib/go/csi"
31
+ "github.com/google/go-cmp/cmp"
31
32
"google.golang.org/grpc/codes"
32
33
"google.golang.org/grpc/status"
33
34
"k8s.io/mount-utils"
@@ -109,21 +110,33 @@ func TestNodeGetVolumeStats(t *testing.T) {
109
110
Readonly : false ,
110
111
VolumeCapability : stdVolCap ,
111
112
}
113
+
112
114
_ , err = ns .NodePublishVolume (context .Background (), req )
113
115
if err != nil {
114
116
t .Fatalf ("Failed to set up test by publishing default vol: %v" , err )
115
117
}
116
118
117
119
testCases := []struct {
118
- name string
119
- volumeID string
120
- volumePath string
121
- expectErr bool
120
+ name string
121
+ volumeID string
122
+ volumePath string
123
+ expectedResp * csi.NodeGetVolumeStatsResponse
124
+ deviceCapacity int
125
+ expectErr bool
122
126
}{
123
127
{
124
- name : "normal" ,
125
- volumeID : defaultVolumeID ,
126
- volumePath : targetPath ,
128
+ name : "normal" ,
129
+ volumeID : defaultVolumeID ,
130
+ volumePath : targetPath ,
131
+ deviceCapacity : 300 * 1024 * 1024 * 1024 , // 300 GB
132
+ expectedResp : & csi.NodeGetVolumeStatsResponse {
133
+ Usage : []* csi.VolumeUsage {
134
+ {
135
+ Unit : csi .VolumeUsage_BYTES ,
136
+ Total : 300 * 1024 * 1024 * 1024 , // 300 GB,
137
+ },
138
+ },
139
+ },
127
140
},
128
141
{
129
142
name : "no vol id" ,
@@ -145,18 +158,38 @@ func TestNodeGetVolumeStats(t *testing.T) {
145
158
146
159
for _ , tc := range testCases {
147
160
t .Run (tc .name , func (t * testing.T ) {
161
+ actionList := []testingexec.FakeCommandAction {
162
+ makeFakeCmd (
163
+ & testingexec.FakeCmd {
164
+ CombinedOutputScript : []testingexec.FakeAction {
165
+ func () ([]byte , []byte , error ) {
166
+ return []byte (fmt .Sprintf ("%d" , tc .deviceCapacity )), nil , nil
167
+ },
168
+ },
169
+ },
170
+ "blockdev" ,
171
+ strings .Split ("--getsize64 /dev/disk/fake-path" , " " )... ,
172
+ ),
173
+ }
174
+
175
+ mounter := mountmanager .NewFakeSafeMounterWithCustomExec (& testingexec.FakeExec {CommandScript : actionList })
176
+ gceDriver := getTestGCEDriverWithCustomMounter (t , mounter )
177
+ ns := gceDriver .ns
148
178
149
179
req := & csi.NodeGetVolumeStatsRequest {
150
180
VolumeId : tc .volumeID ,
151
181
VolumePath : tc .volumePath ,
152
182
}
153
- _ , err := ns .NodeGetVolumeStats (context .Background (), req )
183
+ resp , err := ns .NodeGetVolumeStats (context .Background (), req )
154
184
if err != nil && ! tc .expectErr {
155
185
t .Fatalf ("Got unexpected err: %v" , err )
156
186
}
157
187
if err == nil && tc .expectErr {
158
188
t .Fatal ("Did not get error but expected one" )
159
189
}
190
+ if diff := cmp .Diff (tc .expectedResp , resp ); diff != "" {
191
+ t .Errorf ("NodeGetVolumeStats(%s): -want, +got \n %s" , req , diff )
192
+ }
160
193
})
161
194
}
162
195
}
@@ -394,13 +427,17 @@ func TestNodeStageVolume(t *testing.T) {
394
427
stagingPath := filepath .Join (tempDir , defaultStagingPath )
395
428
396
429
testCases := []struct {
397
- name string
398
- req * csi.NodeStageVolumeRequest
399
- deviceSize int
400
- blockExtSize int
401
- readonlyBit string
402
- expResize bool
403
- expErrCode codes.Code
430
+ name string
431
+ req * csi.NodeStageVolumeRequest
432
+ deviceSize int
433
+ blockExtSize int
434
+ readonlyBit string
435
+ expResize bool
436
+ expReadAheadUpdate bool
437
+ expReadAheadKB string
438
+ readAheadSectors string
439
+ sectorSizeInBytes int
440
+ expErrCode codes.Code
404
441
}{
405
442
{
406
443
name : "Valid request, no resize because block and filesystem sizes match" ,
@@ -450,6 +487,54 @@ func TestNodeStageVolume(t *testing.T) {
450
487
readonlyBit : "0" ,
451
488
expResize : false ,
452
489
},
490
+ {
491
+ name : "Valid request, update readahead" ,
492
+ req : & csi.NodeStageVolumeRequest {
493
+ VolumeId : volumeID ,
494
+ StagingTargetPath : stagingPath ,
495
+ VolumeCapability : & csi.VolumeCapability {
496
+ AccessType : & csi.VolumeCapability_Mount {
497
+ Mount : & csi.VolumeCapability_MountVolume {
498
+ MountFlags : []string {"read_ahead_kb=4096" },
499
+ },
500
+ },
501
+ AccessMode : & csi.VolumeCapability_AccessMode {
502
+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
503
+ },
504
+ },
505
+ },
506
+ deviceSize : 5 ,
507
+ blockExtSize : 1 ,
508
+ readonlyBit : "0" ,
509
+ expResize : true ,
510
+ expReadAheadUpdate : true ,
511
+ readAheadSectors : "8192" ,
512
+ sectorSizeInBytes : 512 ,
513
+ },
514
+ {
515
+ name : "Valid request, update readahead (different sectorsize)" ,
516
+ req : & csi.NodeStageVolumeRequest {
517
+ VolumeId : volumeID ,
518
+ StagingTargetPath : stagingPath ,
519
+ VolumeCapability : & csi.VolumeCapability {
520
+ AccessType : & csi.VolumeCapability_Mount {
521
+ Mount : & csi.VolumeCapability_MountVolume {
522
+ MountFlags : []string {"read_ahead_kb=4096" },
523
+ },
524
+ },
525
+ AccessMode : & csi.VolumeCapability_AccessMode {
526
+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
527
+ },
528
+ },
529
+ },
530
+ deviceSize : 5 ,
531
+ blockExtSize : 1 ,
532
+ readonlyBit : "0" ,
533
+ expResize : true ,
534
+ expReadAheadUpdate : true ,
535
+ readAheadSectors : "4194304" ,
536
+ sectorSizeInBytes : 1 ,
537
+ },
453
538
{
454
539
name : "Invalid request (Bad Access Mode)" ,
455
540
req : & csi.NodeStageVolumeRequest {
@@ -493,10 +578,47 @@ func TestNodeStageVolume(t *testing.T) {
493
578
},
494
579
expErrCode : codes .InvalidArgument ,
495
580
},
581
+ {
582
+ name : "Invalid request (Invalid read_ahead_kb)" ,
583
+ req : & csi.NodeStageVolumeRequest {
584
+ VolumeId : volumeID ,
585
+ StagingTargetPath : stagingPath ,
586
+ VolumeCapability : & csi.VolumeCapability {
587
+ AccessType : & csi.VolumeCapability_Mount {
588
+ Mount : & csi.VolumeCapability_MountVolume {
589
+ MountFlags : []string {"read_ahead_kb=not_a_number" },
590
+ },
591
+ },
592
+ AccessMode : & csi.VolumeCapability_AccessMode {
593
+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
594
+ },
595
+ },
596
+ },
597
+ expErrCode : codes .InvalidArgument ,
598
+ },
599
+ {
600
+ name : "Invalid request (negative read_ahead_kb)" ,
601
+ req : & csi.NodeStageVolumeRequest {
602
+ VolumeId : volumeID ,
603
+ StagingTargetPath : stagingPath ,
604
+ VolumeCapability : & csi.VolumeCapability {
605
+ AccessType : & csi.VolumeCapability_Mount {
606
+ Mount : & csi.VolumeCapability_MountVolume {
607
+ MountFlags : []string {"read_ahead_kb=-4096" },
608
+ },
609
+ },
610
+ AccessMode : & csi.VolumeCapability_AccessMode {
611
+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
612
+ },
613
+ },
614
+ },
615
+ expErrCode : codes .InvalidArgument ,
616
+ },
496
617
}
497
618
for _ , tc := range testCases {
498
619
t .Logf ("Test case: %s" , tc .name )
499
620
resizeCalled := false
621
+ readAheadUpdateCalled := false
500
622
actionList := []testingexec.FakeCommandAction {
501
623
makeFakeCmd (
502
624
& testingexec.FakeCmd {
@@ -593,6 +715,33 @@ func TestNodeStageVolume(t *testing.T) {
593
715
),
594
716
}... )
595
717
}
718
+ if tc .expReadAheadUpdate {
719
+ actionList = append (actionList , []testingexec.FakeCommandAction {
720
+ makeFakeCmd (
721
+ & testingexec.FakeCmd {
722
+ CombinedOutputScript : []testingexec.FakeAction {
723
+ func () ([]byte , []byte , error ) {
724
+ return []byte (fmt .Sprintf ("%d" , tc .sectorSizeInBytes )), nil , nil
725
+ },
726
+ },
727
+ },
728
+ "blockdev" ,
729
+ []string {"--getss" , "/dev/disk/fake-path" }... ,
730
+ ),
731
+ makeFakeCmd (
732
+ & testingexec.FakeCmd {
733
+ CombinedOutputScript : []testingexec.FakeAction {
734
+ func () (_ []byte , args []byte , _ error ) {
735
+ readAheadUpdateCalled = true
736
+ return []byte {}, nil , nil
737
+ },
738
+ },
739
+ },
740
+ "blockdev" ,
741
+ []string {"--setra" , tc .readAheadSectors , "/dev/disk/fake-path" }... ,
742
+ ),
743
+ }... )
744
+ }
596
745
mounter := mountmanager .NewFakeSafeMounterWithCustomExec (& testingexec.FakeExec {CommandScript : actionList })
597
746
gceDriver := getTestGCEDriverWithCustomMounter (t , mounter )
598
747
ns := gceDriver .ns
@@ -616,6 +765,12 @@ func TestNodeStageVolume(t *testing.T) {
616
765
if tc .expResize == false && resizeCalled == true {
617
766
t .Fatalf ("Test called resize, but it was not expected." )
618
767
}
768
+ if tc .expReadAheadUpdate == true && readAheadUpdateCalled == false {
769
+ t .Fatalf ("Test did not update read ahead, but it was expected." )
770
+ }
771
+ if tc .expReadAheadUpdate == false && readAheadUpdateCalled == true {
772
+ t .Fatalf ("Test updated read ahead, but it was not expected." )
773
+ }
619
774
}
620
775
}
621
776
0 commit comments