@@ -458,3 +458,125 @@ func TestNodePublishVolumeWILabelCheck(t *testing.T) {
458458 }
459459 }
460460}
461+
462+ func TestAddFuseMountOptions (t * testing.T ) {
463+ t .Parallel ()
464+
465+ vc := map [string ]string {
466+ VolumeContextKeyPodNamespace : "sample-namespace" ,
467+ VolumeContextKeyServiceAccountName : "sample-service-account" ,
468+ }
469+ sampleIdentityPool := "sample-identity-pool"
470+ sampleIdentityProvider := "sample-identity-provider"
471+ cases := []struct {
472+ name string
473+ inputFuseMountOptions []string
474+ expectedFuseMountOptions []string
475+ identityProvider string
476+ volumeContextParams map [string ]string
477+ enableSidecarBucketAccessCheckForVersion bool
478+ enableCloudProfilerForVersion bool
479+ hostNetworkEnabled bool
480+ identityPool string
481+ expectErr bool
482+ }{
483+ {
484+ name : "validate fuse mount options for host network workloads" ,
485+ enableSidecarBucketAccessCheckForVersion : false ,
486+ hostNetworkEnabled : true ,
487+ identityProvider : sampleIdentityProvider ,
488+ identityPool : sampleIdentityPool ,
489+ inputFuseMountOptions : []string {},
490+ expectedFuseMountOptions : []string {
491+ util .OptInHnw + "=true" ,
492+ util .TokenServerIdentityProviderConst + "=" + sampleIdentityProvider ,
493+ },
494+ },
495+ {
496+ name : "validate sidecar bucket access check is disabled when host network is enabled" ,
497+ enableSidecarBucketAccessCheckForVersion : true ,
498+ hostNetworkEnabled : true ,
499+ identityProvider : sampleIdentityProvider ,
500+ identityPool : sampleIdentityPool ,
501+ inputFuseMountOptions : []string {},
502+ expectedFuseMountOptions : []string {
503+ util .OptInHnw + "=true" ,
504+ util .TokenServerIdentityProviderConst + "=" + sampleIdentityProvider ,
505+ },
506+ },
507+ {
508+ name : "enable sidecar bucket access check on non-host network and validate fuse mount options are correctly set" ,
509+ volumeContextParams : vc ,
510+ enableSidecarBucketAccessCheckForVersion : true ,
511+ identityProvider : sampleIdentityProvider ,
512+ identityPool : sampleIdentityPool ,
513+ inputFuseMountOptions : []string {},
514+ expectedFuseMountOptions : []string {
515+ util .PodNamespaceConst + "=" + vc [VolumeContextKeyPodNamespace ],
516+ util .ServiceAccountNameConst + "=" + vc [VolumeContextKeyServiceAccountName ],
517+ util .TokenServerIdentityPoolConst + "=" + sampleIdentityPool ,
518+ util .TokenServerIdentityProviderConst + "=" + sampleIdentityProvider ,
519+ util .EnableSidecarBucketAccessCheckConst + "=true" ,
520+ },
521+ },
522+ {
523+ name : "verify sidecar bucket access disabled does not set fuse mount options" ,
524+ volumeContextParams : vc ,
525+ enableSidecarBucketAccessCheckForVersion : false ,
526+ identityProvider : sampleIdentityProvider ,
527+ identityPool : sampleIdentityPool ,
528+ inputFuseMountOptions : []string {},
529+ expectedFuseMountOptions : []string {},
530+ },
531+ {
532+ name : "validate cloud profiler flag is correctly set" ,
533+ enableCloudProfilerForVersion : true ,
534+ inputFuseMountOptions : []string {},
535+ expectedFuseMountOptions : []string {
536+ util .EnableCloudProfilerForSidecarConst + "=true" ,
537+ },
538+ },
539+ {
540+ name : "validate sidecar bucket access check when identity provider is not set" ,
541+ volumeContextParams : vc ,
542+ enableSidecarBucketAccessCheckForVersion : true ,
543+ identityProvider : "" ,
544+ identityPool : sampleIdentityPool ,
545+ expectedFuseMountOptions : nil ,
546+ expectErr : true ,
547+ },
548+ {
549+ name : "validate sidecar bucket access check when identity pool is not set" ,
550+ volumeContextParams : vc ,
551+ enableSidecarBucketAccessCheckForVersion : true ,
552+ identityProvider : sampleIdentityProvider ,
553+ identityPool : "" ,
554+ expectedFuseMountOptions : nil ,
555+ expectErr : true ,
556+ },
557+ {
558+ name : "validate cloud profiler with host network" ,
559+ hostNetworkEnabled : true ,
560+ enableCloudProfilerForVersion : true ,
561+ identityProvider : sampleIdentityProvider ,
562+ expectedFuseMountOptions : []string {
563+ util .EnableCloudProfilerForSidecarConst + "=true" ,
564+ util .OptInHnw + "=true" ,
565+ util .TokenServerIdentityProviderConst + "=" + sampleIdentityProvider ,
566+ },
567+ },
568+ }
569+
570+ for _ , tc := range cases {
571+ t .Run (tc .name , func (t * testing.T ) {
572+ gotFuseMountOption , err := addFuseMountOptions (tc .identityProvider , tc .identityPool , tc .inputFuseMountOptions , tc .volumeContextParams , tc .hostNetworkEnabled , tc .enableSidecarBucketAccessCheckForVersion , tc .enableCloudProfilerForVersion )
573+ if (err != nil ) != tc .expectErr {
574+ t .Errorf ("for test case %q, got error: %v, but expectErr: %v" , tc .name , err , tc .expectErr )
575+ }
576+ less := func (a , b string ) bool { return a < b }
577+ if diff := cmp .Diff (tc .expectedFuseMountOptions , gotFuseMountOption , cmpopts .SortSlices (less )); diff != "" {
578+ t .Errorf ("got unexpected options args for testcase %s (-got, +want)\n %s" , tc .name , diff )
579+ }
580+ })
581+ }
582+ }
0 commit comments