@@ -409,6 +409,7 @@ func TestCleanSubPaths(t *testing.T) {
409
409
// Function that validates directory structure after the test
410
410
validate func (base string ) error
411
411
expectError bool
412
+ unmount func (path string ) error
412
413
}{
413
414
{
414
415
name : "not-exists" ,
@@ -539,6 +540,64 @@ func TestCleanSubPaths(t *testing.T) {
539
540
return validateDirExists (baseSubdir )
540
541
},
541
542
},
543
+ {
544
+ name : "subpath-with-files" ,
545
+ prepare : func (base string ) ([]mount.MountPoint , error ) {
546
+ containerPath := filepath .Join (base , containerSubPathDirectoryName , testVol , "container1" )
547
+ if err := os .MkdirAll (containerPath , defaultPerm ); err != nil {
548
+ return nil , err
549
+ }
550
+
551
+ file0 := filepath .Join (containerPath , "0" )
552
+ if err := ioutil .WriteFile (file0 , []byte {}, defaultPerm ); err != nil {
553
+ return nil , err
554
+ }
555
+
556
+ dir1 := filepath .Join (containerPath , "1" )
557
+ if err := os .MkdirAll (filepath .Join (dir1 , "my-dir-1" ), defaultPerm ); err != nil {
558
+ return nil , err
559
+ }
560
+
561
+ dir2 := filepath .Join (containerPath , "2" )
562
+ if err := os .MkdirAll (filepath .Join (dir2 , "my-dir-2" ), defaultPerm ); err != nil {
563
+ return nil , err
564
+ }
565
+
566
+ file3 := filepath .Join (containerPath , "3" )
567
+ if err := ioutil .WriteFile (file3 , []byte {}, defaultPerm ); err != nil {
568
+ return nil , err
569
+ }
570
+
571
+ mounts := []mount.MountPoint {
572
+ {Device : "/dev/sdb" , Path : file0 },
573
+ {Device : "/dev/sdc" , Path : dir1 },
574
+ {Device : "/dev/sdd" , Path : dir2 },
575
+ {Device : "/dev/sde" , Path : file3 },
576
+ }
577
+ return mounts , nil
578
+ },
579
+ unmount : func (mountpath string ) error {
580
+ err := filepath .Walk (mountpath , func (path string , info os.FileInfo , err error ) error {
581
+ if path == mountpath {
582
+ // Skip top level directory
583
+ return nil
584
+ }
585
+
586
+ if err = os .Remove (path ); err != nil {
587
+ return err
588
+ }
589
+ return filepath .SkipDir
590
+ })
591
+ if err != nil {
592
+ return fmt .Errorf ("error processing %s: %s" , mountpath , err )
593
+ }
594
+
595
+ return nil
596
+ },
597
+ validate : func (base string ) error {
598
+ return validateDirNotExists (filepath .Join (base , containerSubPathDirectoryName ))
599
+ },
600
+ },
542
601
}
543
602
544
603
for _ , test := range tests {
@@ -553,7 +612,7 @@ func TestCleanSubPaths(t *testing.T) {
553
612
t .Fatalf ("failed to prepare test %q: %v" , test .name , err .Error ())
554
613
}
555
614
556
- fm := & mount.FakeMounter {MountPoints : mounts }
615
+ fm := & mount.FakeMounter {MountPoints : mounts , UnmountFunc : test . unmount }
557
616
558
617
err = doCleanSubPaths (fm , base , testVol )
559
618
if err != nil && ! test .expectError {
0 commit comments