@@ -325,7 +325,11 @@ var _ = Describe("GCE PD CSI Driver Multi-Zone", func() {
325
325
326
326
// Attach disk to instance in the first zone.
327
327
tc0 := zoneToContext [zones [0 ]]
328
- err , detacher , args := testAttachAndMount (volume .VolumeId , volName , tc0 .Instance , tc0 .Client , false /* useBlock */ , false /* forceAttach */ )
328
+ err , detacher , args := testAttachAndMount (volume .VolumeId , volName , tc0 .Instance , tc0 .Client , attachAndMountArgs {
329
+ readOnly : false ,
330
+ useBlock : false ,
331
+ forceAttach : false ,
332
+ })
329
333
detachers = append (detachers , detacher )
330
334
Expect (err ).To (BeNil (), "failed attach in zone 0" )
331
335
testFileName := filepath .Join (args .publishDir , "force-attach-test" )
@@ -341,7 +345,11 @@ var _ = Describe("GCE PD CSI Driver Multi-Zone", func() {
341
345
342
346
// Now force attach to the second instance without detaching.
343
347
tc1 := zoneToContext [zones [1 ]]
344
- err , detacher , args = testAttachAndMount (volume .VolumeId , volName , tc1 .Instance , tc1 .Client , false /* useBlock */ , true /* forceAttach */ )
348
+ err , detacher , _ = testAttachAndMount (volume .VolumeId , volName , tc1 .Instance , tc1 .Client , attachAndMountArgs {
349
+ readOnly : false ,
350
+ useBlock : false ,
351
+ forceAttach : true ,
352
+ })
345
353
detachers = append (detachers , detacher )
346
354
Expect (err ).To (BeNil (), "failed force attach in zone 1" )
347
355
readContents , err = testutils .ReadFile (tc1 .Instance , testFileName )
@@ -389,9 +397,20 @@ func testAttachWriteReadDetach(volID string, volName string, instance *remote.In
389
397
return testLifecycleWithVerify (volID , volName , instance , client , readOnly , false /* fs */ , writeFile , verifyReadFile )
390
398
}
391
399
392
- func testAttachAndMount (volID string , volName string , instance * remote.InstanceInfo , client * remote.CsiClient , useBlock , forceAttach bool ) (error , func (), * verifyArgs ) {
400
+ type attachAndMountArgs struct {
401
+ readOnly bool
402
+ useBlock bool
403
+ forceAttach bool
404
+ }
405
+
406
+ func testAttachAndMount (volID string , volName string , instance * remote.InstanceInfo , client * remote.CsiClient , args attachAndMountArgs ) (error , func (), * verifyArgs ) {
393
407
// Attach Disk
394
- err := client .ControllerPublishVolumeReadWrite (volID , instance .GetNodeID (), forceAttach )
408
+ var err error
409
+ if args .readOnly {
410
+ err = client .ControllerPublishVolumeReadOnly (volID , instance .GetNodeID ())
411
+ } else {
412
+ err = client .ControllerPublishVolumeReadWrite (volID , instance .GetNodeID (), args .forceAttach )
413
+ }
395
414
if err != nil {
396
415
return fmt .Errorf ("ControllerPublishVolume failed with error for disk %v on node %v: %v" , volID , instance .GetNodeID (), err .Error ()), nil , nil
397
416
}
@@ -406,7 +425,7 @@ func testAttachAndMount(volID string, volName string, instance *remote.InstanceI
406
425
407
426
// Stage Disk
408
427
stageDir := filepath .Join ("/tmp/" , volName , "stage" )
409
- if useBlock {
428
+ if args . useBlock {
410
429
err = client .NodeStageBlockVolume (volID , stageDir )
411
430
} else {
412
431
err = client .NodeStageExt4Volume (volID , stageDir )
@@ -435,7 +454,7 @@ func testAttachAndMount(volID string, volName string, instance *remote.InstanceI
435
454
// Mount Disk
436
455
publishDir := filepath .Join ("/tmp/" , volName , "mount" )
437
456
438
- if useBlock {
457
+ if args . useBlock {
439
458
err = client .NodePublishBlockVolume (volID , stageDir , publishDir )
440
459
} else {
441
460
err = client .NodePublishVolume (volID , stageDir , publishDir )
@@ -445,23 +464,40 @@ func testAttachAndMount(volID string, volName string, instance *remote.InstanceI
445
464
unstageAndDetach ()
446
465
return fmt .Errorf ("NodePublishVolume failed with error: %v" , err .Error ()), nil , nil
447
466
}
448
- err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" )
449
- if err != nil {
467
+
468
+ unpublish := func () {
469
+ // Unpublish Disk
470
+ err = client .NodeUnpublishVolume (volID , publishDir )
471
+ if err != nil {
472
+ klog .Errorf ("Failed to unpublish volume: %v" , err )
473
+ }
474
+ }
475
+ unpublishUnstageAndDetach := func () {
476
+ unpublish ()
450
477
unstageAndDetach ()
478
+ }
479
+
480
+ err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" , ! args .readOnly /* recursive */ )
481
+ if err != nil {
482
+ unpublishUnstageAndDetach ()
451
483
return fmt .Errorf ("Chmod failed with error: %v" , err .Error ()), nil , nil
452
484
}
453
485
454
- args := & verifyArgs {
486
+ returnArgs := & verifyArgs {
455
487
publishDir : publishDir ,
456
488
stageDir : stageDir ,
457
489
}
458
490
459
- return nil , unstageAndDetach , args
491
+ return nil , unpublishUnstageAndDetach , returnArgs
460
492
}
461
493
462
494
func testLifecycleWithVerify (volID string , volName string , instance * remote.InstanceInfo , client * remote.CsiClient , readOnly , useBlock bool , firstMountVerify , secondMountVerify verifyFunc ) error {
463
495
klog .Infof ("Starting testAttachWriteReadDetach with volume %v node %v with readonly %v\n " , volID , instance .GetNodeID (), readOnly )
464
- err , detacher , args := testAttachAndMount (volID , volName , instance , client , useBlock , false /* forceAttach */ )
496
+ err , detacher , args := testAttachAndMount (volID , volName , instance , client , attachAndMountArgs {
497
+ readOnly : readOnly ,
498
+ useBlock : useBlock ,
499
+ forceAttach : false ,
500
+ })
465
501
if err != nil {
466
502
return fmt .Errorf ("failed to attach and mount: %w" , err )
467
503
}
@@ -489,7 +525,7 @@ func testLifecycleWithVerify(volID string, volName string, instance *remote.Inst
489
525
if err != nil {
490
526
return fmt .Errorf ("NodePublishVolume failed with error: %v" , err .Error ())
491
527
}
492
- err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" )
528
+ err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" , ! readOnly /* recursive */ )
493
529
if err != nil {
494
530
return fmt .Errorf ("Chmod failed with error: %v" , err )
495
531
}
0 commit comments