@@ -25,6 +25,7 @@ import (
25
25
26
26
csipbv1 "github.com/container-storage-interface/spec/lib/go/csi"
27
27
api "k8s.io/api/core/v1"
28
+ "k8s.io/apimachinery/pkg/api/resource"
28
29
"k8s.io/kubernetes/pkg/volume/csi/fake"
29
30
)
30
31
@@ -40,6 +41,13 @@ func newFakeCsiDriverClient(t *testing.T, stagingCapable bool) *fakeCsiDriverCli
40
41
}
41
42
}
42
43
44
+ func newFakeCsiDriverClientWithExpansion (t * testing.T , stagingCapable bool , expansionSet bool ) * fakeCsiDriverClient {
45
+ return & fakeCsiDriverClient {
46
+ t : t ,
47
+ nodeClient : fake .NewNodeClientWithExpansion (stagingCapable , expansionSet ),
48
+ }
49
+ }
50
+
43
51
func (c * fakeCsiDriverClient ) NodeGetInfo (ctx context.Context ) (
44
52
nodeID string ,
45
53
maxVolumePerNode int64 ,
@@ -144,6 +152,28 @@ func (c *fakeCsiDriverClient) NodeUnstageVolume(ctx context.Context, volID, stag
144
152
return err
145
153
}
146
154
155
+ func (c * fakeCsiDriverClient ) NodeSupportsNodeExpand (ctx context.Context ) (bool , error ) {
156
+ c .t .Log ("calling fake.NodeSupportsNodeExpand..." )
157
+ req := & csipbv1.NodeGetCapabilitiesRequest {}
158
+
159
+ resp , err := c .nodeClient .NodeGetCapabilities (ctx , req )
160
+ if err != nil {
161
+ return false , err
162
+ }
163
+
164
+ capabilities := resp .GetCapabilities ()
165
+
166
+ if capabilities == nil {
167
+ return false , nil
168
+ }
169
+ for _ , capability := range capabilities {
170
+ if capability .GetRpc ().GetType () == csipbv1 .NodeServiceCapability_RPC_EXPAND_VOLUME {
171
+ return true , nil
172
+ }
173
+ }
174
+ return false , nil
175
+ }
176
+
147
177
func (c * fakeCsiDriverClient ) NodeSupportsStageUnstage (ctx context.Context ) (bool , error ) {
148
178
c .t .Log ("calling fake.NodeGetCapabilities for NodeSupportsStageUnstage..." )
149
179
req := & csipbv1.NodeGetCapabilitiesRequest {}
@@ -166,10 +196,29 @@ func (c *fakeCsiDriverClient) NodeSupportsStageUnstage(ctx context.Context) (boo
166
196
return stageUnstageSet , nil
167
197
}
168
198
199
+ func (c * fakeCsiDriverClient ) NodeExpandVolume (ctx context.Context , volumeid , volumePath string , newSize resource.Quantity ) (resource.Quantity , error ) {
200
+ c .t .Log ("calling fake.NodeExpandVolume" )
201
+ req := & csipbv1.NodeExpandVolumeRequest {
202
+ VolumeId : volumeid ,
203
+ VolumePath : volumePath ,
204
+ CapacityRange : & csipbv1.CapacityRange {RequiredBytes : newSize .Value ()},
205
+ }
206
+ resp , err := c .nodeClient .NodeExpandVolume (ctx , req )
207
+ if err != nil {
208
+ return newSize , err
209
+ }
210
+ updatedQuantity := resource .NewQuantity (resp .CapacityBytes , resource .BinarySI )
211
+ return * updatedQuantity , nil
212
+ }
213
+
169
214
func setupClient (t * testing.T , stageUnstageSet bool ) csiClient {
170
215
return newFakeCsiDriverClient (t , stageUnstageSet )
171
216
}
172
217
218
+ func setupClientWithExpansion (t * testing.T , stageUnstageSet bool , expansionSet bool ) csiClient {
219
+ return newFakeCsiDriverClientWithExpansion (t , stageUnstageSet , expansionSet )
220
+ }
221
+
173
222
func checkErr (t * testing.T , expectedAnError bool , actualError error ) {
174
223
t .Helper ()
175
224
@@ -415,3 +464,59 @@ func TestClientNodeUnstageVolume(t *testing.T) {
415
464
}
416
465
}
417
466
}
467
+
468
+ func TestNodeExpandVolume (t * testing.T ) {
469
+ testCases := []struct {
470
+ name string
471
+ volID string
472
+ volumePath string
473
+ newSize resource.Quantity
474
+ mustFail bool
475
+ err error
476
+ }{
477
+ {
478
+ name : "with all correct values" ,
479
+ volID : "vol-abcde" ,
480
+ volumePath : "/foo/bar" ,
481
+ newSize : resource .MustParse ("10Gi" ),
482
+ mustFail : false ,
483
+ },
484
+ {
485
+ name : "with missing volume-id" ,
486
+ volumePath : "/foo/bar" ,
487
+ newSize : resource .MustParse ("10Gi" ),
488
+ mustFail : true ,
489
+ },
490
+ {
491
+ name : "with missing volume path" ,
492
+ volID : "vol-1234" ,
493
+ newSize : resource .MustParse ("10Gi" ),
494
+ mustFail : true ,
495
+ },
496
+ {
497
+ name : "with invalid quantity" ,
498
+ volID : "vol-1234" ,
499
+ volumePath : "/foo/bar" ,
500
+ newSize : * resource .NewQuantity (- 10 , resource .DecimalSI ),
501
+ mustFail : true ,
502
+ },
503
+ }
504
+
505
+ for _ , tc := range testCases {
506
+ t .Logf ("Running test cases : %s" , tc .name )
507
+ fakeCloser := fake .NewCloser (t )
508
+ client := & csiDriverClient {
509
+ driverName : "Fake Driver Name" ,
510
+ nodeV1ClientCreator : func (addr csiAddr ) (csipbv1.NodeClient , io.Closer , error ) {
511
+ nodeClient := fake .NewNodeClient (false /* stagingCapable */ )
512
+ nodeClient .SetNextError (tc .err )
513
+ return nodeClient , fakeCloser , nil
514
+ },
515
+ }
516
+ _ , err := client .NodeExpandVolume (context .Background (), tc .volID , tc .volumePath , tc .newSize )
517
+ checkErr (t , tc .mustFail , err )
518
+ if ! tc .mustFail {
519
+ fakeCloser .Check ()
520
+ }
521
+ }
522
+ }
0 commit comments