@@ -325,7 +325,11 @@ var _ = Describe("GCE PD CSI Driver Multi-Zone", func() {
325325
326326 // Attach disk to instance in the first zone.
327327 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+ })
329333 detachers = append (detachers , detacher )
330334 Expect (err ).To (BeNil (), "failed attach in zone 0" )
331335 testFileName := filepath .Join (args .publishDir , "force-attach-test" )
@@ -341,7 +345,11 @@ var _ = Describe("GCE PD CSI Driver Multi-Zone", func() {
341345
342346 // Now force attach to the second instance without detaching.
343347 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+ })
345353 detachers = append (detachers , detacher )
346354 Expect (err ).To (BeNil (), "failed force attach in zone 1" )
347355 readContents , err = testutils .ReadFile (tc1 .Instance , testFileName )
@@ -389,9 +397,20 @@ func testAttachWriteReadDetach(volID string, volName string, instance *remote.In
389397 return testLifecycleWithVerify (volID , volName , instance , client , readOnly , false /* fs */ , writeFile , verifyReadFile )
390398}
391399
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 ) {
393407 // 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+ }
395414 if err != nil {
396415 return fmt .Errorf ("ControllerPublishVolume failed with error for disk %v on node %v: %v" , volID , instance .GetNodeID (), err .Error ()), nil , nil
397416 }
@@ -406,7 +425,7 @@ func testAttachAndMount(volID string, volName string, instance *remote.InstanceI
406425
407426 // Stage Disk
408427 stageDir := filepath .Join ("/tmp/" , volName , "stage" )
409- if useBlock {
428+ if args . useBlock {
410429 err = client .NodeStageBlockVolume (volID , stageDir )
411430 } else {
412431 err = client .NodeStageExt4Volume (volID , stageDir )
@@ -435,7 +454,7 @@ func testAttachAndMount(volID string, volName string, instance *remote.InstanceI
435454 // Mount Disk
436455 publishDir := filepath .Join ("/tmp/" , volName , "mount" )
437456
438- if useBlock {
457+ if args . useBlock {
439458 err = client .NodePublishBlockVolume (volID , stageDir , publishDir )
440459 } else {
441460 err = client .NodePublishVolume (volID , stageDir , publishDir )
@@ -445,23 +464,40 @@ func testAttachAndMount(volID string, volName string, instance *remote.InstanceI
445464 unstageAndDetach ()
446465 return fmt .Errorf ("NodePublishVolume failed with error: %v" , err .Error ()), nil , nil
447466 }
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 ()
450477 unstageAndDetach ()
478+ }
479+
480+ err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" , ! args .readOnly /* recursive */ )
481+ if err != nil {
482+ unpublishUnstageAndDetach ()
451483 return fmt .Errorf ("Chmod failed with error: %v" , err .Error ()), nil , nil
452484 }
453485
454- args := & verifyArgs {
486+ returnArgs := & verifyArgs {
455487 publishDir : publishDir ,
456488 stageDir : stageDir ,
457489 }
458490
459- return nil , unstageAndDetach , args
491+ return nil , unpublishUnstageAndDetach , returnArgs
460492}
461493
462494func testLifecycleWithVerify (volID string , volName string , instance * remote.InstanceInfo , client * remote.CsiClient , readOnly , useBlock bool , firstMountVerify , secondMountVerify verifyFunc ) error {
463495 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+ })
465501 if err != nil {
466502 return fmt .Errorf ("failed to attach and mount: %w" , err )
467503 }
@@ -489,7 +525,7 @@ func testLifecycleWithVerify(volID string, volName string, instance *remote.Inst
489525 if err != nil {
490526 return fmt .Errorf ("NodePublishVolume failed with error: %v" , err .Error ())
491527 }
492- err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" )
528+ err = testutils .ForceChmod (instance , filepath .Join ("/tmp/" , volName ), "777" , ! readOnly /* recursive */ )
493529 if err != nil {
494530 return fmt .Errorf ("Chmod failed with error: %v" , err )
495531 }
0 commit comments