@@ -537,18 +537,34 @@ func TestNodeStageVolume(t *testing.T) {
537537 defer os .RemoveAll (tempDir )
538538 stagingPath := filepath .Join (tempDir , defaultStagingPath )
539539
540+ btrfsUUID := "00000000-0000-0000-0000-000000000001"
541+ btrfsPrefix := fmt .Sprintf ("%s/sys/fs/btrfs/%s/allocation" , tempDir , btrfsUUID )
542+
543+ for _ , suffix := range []string {"data" , "metadata" } {
544+ dir := btrfsPrefix + "/" + suffix
545+ if err := os .MkdirAll (dir , 0755 ); err != nil {
546+ t .Fatalf ("Failed to set up fake sysfs dir %q: %v" , dir , err )
547+ }
548+ fname := dir + "/bg_reclaim_threshold"
549+ if err := os .WriteFile (fname , []byte ("0\n " ), 0644 ); err != nil {
550+ t .Fatalf ("write %q: %v" , fname , err )
551+ }
552+ }
553+
540554 testCases := []struct {
541- name string
542- req * csi.NodeStageVolumeRequest
543- deviceSize int
544- blockExtSize int
545- readonlyBit string
546- expResize bool
547- expReadAheadUpdate bool
548- expReadAheadKB string
549- readAheadSectors string
550- sectorSizeInBytes int
551- expErrCode codes.Code
555+ name string
556+ req * csi.NodeStageVolumeRequest
557+ deviceSize int
558+ blockExtSize int
559+ readonlyBit string
560+ expResize bool
561+ expReadAheadUpdate bool
562+ expReadAheadKB string
563+ readAheadSectors string
564+ btrfsReclaimData string
565+ btrfsReclaimMetadata string
566+ sectorSizeInBytes int
567+ expErrCode codes.Code
552568 }{
553569 {
554570 name : "Valid request, no resize because block and filesystem sizes match" ,
@@ -598,6 +614,76 @@ func TestNodeStageVolume(t *testing.T) {
598614 readonlyBit : "0" ,
599615 expResize : false ,
600616 },
617+ {
618+ name : "btrfs-allocation-data-bg_reclaim_threshold is ignored on non-btrfs" ,
619+ req : & csi.NodeStageVolumeRequest {
620+ VolumeId : volumeID ,
621+ StagingTargetPath : stagingPath ,
622+ VolumeCapability : & csi.VolumeCapability {
623+ AccessType : & csi.VolumeCapability_Mount {
624+ Mount : & csi.VolumeCapability_MountVolume {
625+ FsType : "ext4" ,
626+ MountFlags : []string {"btrfs-allocation-data-bg_reclaim_threshold=90" },
627+ },
628+ },
629+ AccessMode : & csi.VolumeCapability_AccessMode {
630+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
631+ },
632+ },
633+ },
634+ deviceSize : 1 ,
635+ blockExtSize : 1 ,
636+ readonlyBit : "0" ,
637+ btrfsReclaimData : "0" ,
638+ },
639+ {
640+ name : "Valid request, set btrfs-allocation-data-bg_reclaim_threshold=90" ,
641+ req : & csi.NodeStageVolumeRequest {
642+ VolumeId : volumeID ,
643+ StagingTargetPath : stagingPath ,
644+ VolumeCapability : & csi.VolumeCapability {
645+ AccessType : & csi.VolumeCapability_Mount {
646+ Mount : & csi.VolumeCapability_MountVolume {
647+ FsType : "btrfs" ,
648+ MountFlags : []string {"btrfs-allocation-data-bg_reclaim_threshold=90" },
649+ },
650+ },
651+ AccessMode : & csi.VolumeCapability_AccessMode {
652+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
653+ },
654+ },
655+ },
656+ deviceSize : 1 ,
657+ blockExtSize : 1 ,
658+ readonlyBit : "0" ,
659+ btrfsReclaimData : "90" ,
660+ },
661+ {
662+ name : "Valid request, set btrfs-allocation-{,meta}data-bg_reclaim_threshold" ,
663+ req : & csi.NodeStageVolumeRequest {
664+ VolumeId : volumeID ,
665+ StagingTargetPath : stagingPath ,
666+ VolumeCapability : & csi.VolumeCapability {
667+ AccessType : & csi.VolumeCapability_Mount {
668+ Mount : & csi.VolumeCapability_MountVolume {
669+ FsType : "btrfs" ,
670+ MountFlags : []string {
671+ "btrfs-allocation-data-bg_reclaim_threshold=90" ,
672+ "btrfs-allocation-metadata-bg_reclaim_threshold=91" ,
673+ },
674+ },
675+ },
676+ AccessMode : & csi.VolumeCapability_AccessMode {
677+ Mode : csi .VolumeCapability_AccessMode_SINGLE_NODE_WRITER ,
678+ },
679+ },
680+ },
681+ deviceSize : 1 ,
682+ blockExtSize : 1 ,
683+ readonlyBit : "0" ,
684+ btrfsReclaimData : "90" ,
685+ btrfsReclaimMetadata : "91" ,
686+ },
601687 {
602688 name : "Valid request, update readahead" ,
603689 req : & csi.NodeStageVolumeRequest {
@@ -730,6 +816,7 @@ func TestNodeStageVolume(t *testing.T) {
730816 t .Logf ("Test case: %s" , tc .name )
731817 resizeCalled := false
732818 readAheadUpdateCalled := false
819+ blkidCalled := false
733820 actionList := []testingexec.FakeCommandAction {
734821 makeFakeCmd (
735822 & testingexec.FakeCmd {
@@ -853,9 +940,26 @@ func TestNodeStageVolume(t *testing.T) {
853940 ),
854941 }... )
855942 }
943+ if tc .btrfsReclaimData != "" || tc .btrfsReclaimMetadata != "" {
944+ actionList = append (actionList , []testingexec.FakeCommandAction {
945+ makeFakeCmd (
946+ & testingexec.FakeCmd {
947+ OutputScript : []testingexec.FakeAction {
948+ func () ([]byte , []byte , error ) {
949+ blkidCalled = true
950+ return []byte (btrfsUUID + "\n " ), nil , nil
951+ },
952+ },
953+ },
954+ "blkid" ,
955+ []string {"--match-tag" , "UUID" , "--output" , "value" , stagingPath }... ,
956+ ),
957+ }... )
958+ }
856959 mounter := mountmanager .NewFakeSafeMounterWithCustomExec (& testingexec.FakeExec {CommandScript : actionList })
857960 gceDriver := getTestGCEDriverWithCustomMounter (t , mounter )
858961 ns := gceDriver .ns
962+ ns .SysfsPath = tempDir + "/sys"
859963 _ , err := ns .NodeStageVolume (context .Background (), tc .req )
860964 if err != nil {
861965 serverError , ok := status .FromError (err )
@@ -882,6 +986,30 @@ func TestNodeStageVolume(t *testing.T) {
882986 if tc .expReadAheadUpdate == false && readAheadUpdateCalled == true {
883987 t .Fatalf ("Test updated read ahead, but it was not expected." )
884988 }
989+ if tc .btrfsReclaimData == "" && tc .btrfsReclaimMetadata == "" && blkidCalled {
990+ t .Fatalf ("blkid was called, but was not expected." )
991+ }
992+
993+ if tc .btrfsReclaimData != "" {
994+ fname := btrfsPrefix + "/data/bg_reclaim_threshold"
995+ got , err := os .ReadFile (fname )
996+ if err != nil {
997+ t .Fatalf ("read %q: %v" , fname , err )
998+ }
999+ if s := strings .TrimSpace (string (got )); s != tc .btrfsReclaimData {
1000+ t .Fatalf ("%q: expected %q, got %q" , fname , tc .btrfsReclaimData , s )
1001+ }
1002+ }
1003+ if tc .btrfsReclaimMetadata != "" {
1004+ fname := btrfsPrefix + "/metadata/bg_reclaim_threshold"
1005+ got , err := os .ReadFile (fname )
1006+ if err != nil {
1007+ t .Fatalf ("read %q: %v" , fname , err )
1008+ }
1009+ if s := strings .TrimSpace (string (got )); s != tc .btrfsReclaimMetadata {
1010+ t .Fatalf ("%q: expected %q, got %q" , fname , tc .btrfsReclaimMetadata , s )
1011+ }
1012+ }
8851013 }
8861014}
8871015
0 commit comments