@@ -38,6 +38,7 @@ import (
38
38
utilfeature "k8s.io/apiserver/pkg/util/feature"
39
39
fakeclient "k8s.io/client-go/kubernetes/fake"
40
40
clitesting "k8s.io/client-go/testing"
41
+ "k8s.io/client-go/tools/record"
41
42
featuregatetesting "k8s.io/component-base/featuregate/testing"
42
43
pkgauthenticationv1 "k8s.io/kubernetes/pkg/apis/authentication/v1"
43
44
pkgcorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
@@ -360,6 +361,132 @@ func TestMounterSetUp(t *testing.T) {
360
361
}
361
362
}
362
363
364
+ type mockVolumeOwnershipChanger struct {
365
+ triggerError bool
366
+ }
367
+
368
+ func (m * mockVolumeOwnershipChanger ) ChangePermissions () error {
369
+ if m .triggerError {
370
+ return fmt .Errorf ("mock error" )
371
+ }
372
+ return nil
373
+ }
374
+
375
+ func (m * mockVolumeOwnershipChanger ) AddProgressNotifier (pod * corev1.Pod , recorder record.EventRecorder ) volume.VolumeOwnershipChanger {
376
+ return m
377
+ }
378
+
379
+ func TestMounterSetupJsonFileHandling (t * testing.T ) {
380
+ testCases := []struct {
381
+ name string
382
+ volumeID string
383
+ setupShouldFail bool
384
+ errorType error
385
+ failOwnershipChange bool
386
+ shouldRemoveFile bool
387
+ }{
388
+ {
389
+ name : "transient error should not remove json file" ,
390
+ volumeID : fakecsi .NodePublishTimeOut_VolumeID ,
391
+ setupShouldFail : true ,
392
+ shouldRemoveFile : false ,
393
+ },
394
+ {
395
+ name : "final error should remove json file" ,
396
+ volumeID : fakecsi .NodePublishFinalError_VolumeID ,
397
+ setupShouldFail : true ,
398
+ shouldRemoveFile : true ,
399
+ },
400
+ {
401
+ name : "error in fsgroup permission change, should not remove json file" ,
402
+ volumeID : testVol ,
403
+ failOwnershipChange : true ,
404
+ setupShouldFail : true ,
405
+ shouldRemoveFile : false ,
406
+ },
407
+ }
408
+
409
+ for _ , tc := range testCases {
410
+ t .Run (tc .name , func (t * testing.T ) {
411
+ modes := []storage.VolumeLifecycleMode {
412
+ storage .VolumeLifecyclePersistent ,
413
+ }
414
+ csiDriver := getTestCSIDriver ("file-driver" , nil , nil , modes )
415
+ fileFsPolicy := storage .FileFSGroupPolicy
416
+ csiDriver .Spec .FSGroupPolicy = & fileFsPolicy
417
+
418
+ fakeClient := fakeclient .NewSimpleClientset (csiDriver )
419
+ plug , tmpDir := newTestPlugin (t , fakeClient )
420
+ defer os .RemoveAll (tmpDir )
421
+
422
+ registerFakePlugin ("test-driver" , "endpoint" , []string {"1.0.0" }, t )
423
+ pv := makeTestPV ("test-vol" , 10 , "file-driver" , tc .volumeID )
424
+ pv .Spec .MountOptions = []string {"foo=bar" , "baz=qux" }
425
+
426
+ mounter , err := plug .NewMounter (
427
+ volume .NewSpecFromPersistentVolume (pv , pv .Spec .PersistentVolumeSource .CSI .ReadOnly ),
428
+ & corev1.Pod {
429
+ ObjectMeta : meta.ObjectMeta {UID : testPodUID , Namespace : testns , Name : testPod },
430
+ Spec : corev1.PodSpec {
431
+ ServiceAccountName : testAccount ,
432
+ },
433
+ },
434
+ )
435
+ if err != nil {
436
+ t .Fatalf ("failed to make a new Mounter: %v" , err )
437
+ }
438
+
439
+ if mounter == nil {
440
+ t .Fatal ("failed to create CSI mounter" )
441
+ }
442
+
443
+ csiMounter := mounter .(* csiMountMgr )
444
+ csiMounter .csiClient = setupClient (t , true )
445
+
446
+ attachID := getAttachmentName (csiMounter .volumeID , string (csiMounter .driverName ), string (plug .host .GetNodeName ()))
447
+ attachment := makeTestAttachment (attachID , "test-node" , csiMounter .spec .Name ())
448
+ _ , err = csiMounter .k8s .StorageV1 ().VolumeAttachments ().Create (context .TODO (), attachment , meta.CreateOptions {})
449
+ if err != nil {
450
+ t .Fatalf ("failed to setup VolumeAttachment: %v" , err )
451
+ }
452
+
453
+ var mounterArgs volume.MounterArgs
454
+ fsGroup := int64 (2000 )
455
+ mounterArgs .FsGroup = & fsGroup
456
+ if tc .failOwnershipChange {
457
+ mounterArgs .VolumeOwnershipApplicator = & mockVolumeOwnershipChanger {triggerError : true }
458
+ }
459
+
460
+ dataDir := filepath .Dir (mounter .GetPath ())
461
+
462
+ // Mounter.SetUp()
463
+ err = csiMounter .SetUp (mounterArgs )
464
+ if tc .setupShouldFail {
465
+ if err == nil {
466
+ t .Error ("test should fail, but no error occurred" )
467
+ }
468
+ } else if err != nil {
469
+ t .Fatal ("unexpected error:" , err )
470
+ }
471
+
472
+ // Check if the json file exists or not
473
+ dataFile := filepath .Join (dataDir , volDataFileName )
474
+ _ , err = os .Stat (dataFile )
475
+ if tc .shouldRemoveFile {
476
+ if ! os .IsNotExist (err ) {
477
+ t .Errorf ("Expected json file to be removed, but it still exists: %v" , err )
478
+ }
479
+ } else {
480
+ if os .IsNotExist (err ) {
481
+ t .Errorf ("Expected json file to exist, but it was removed" )
482
+ } else if err != nil {
483
+ t .Errorf ("Unexpected error while checking json file: %v" , err )
484
+ }
485
+ }
486
+ })
487
+ }
488
+ }
489
+
363
490
func TestMounterSetUpSimple (t * testing.T ) {
364
491
fakeClient := fakeclient .NewSimpleClientset ()
365
492
plug , tmpDir := newTestPlugin (t , fakeClient )
0 commit comments