Unmount subpath should only scan the first level dir

This commit is contained in:
Janario Oliveira 2019-09-13 11:12:22 +02:00
parent 73b2c82b28
commit 67ec00d6b8
3 changed files with 45 additions and 3 deletions

View File

@ -32,9 +32,12 @@ type FakeMounter struct {
MountCheckErrors map[string]error
// Some tests run things in parallel, make sure the mounter does not produce
// any golang's DATA RACE warnings.
mutex sync.Mutex
mutex sync.Mutex
UmountFunc UmountFunc
}
type UmountFunc func(path string) error
var _ Interface = &FakeMounter{}
const (
@ -117,6 +120,12 @@ func (f *FakeMounter) Unmount(target string) error {
newMountpoints := []MountPoint{}
for _, mp := range f.MountPoints {
if mp.Path == absTarget {
if f.UmountFunc != nil {
err := f.UmountFunc(absTarget)
if err != nil {
return err
}
}
klog.V(5).Infof("Fake mounter: unmounted %s from %s", mp.Device, absTarget)
// Don't copy it to newMountpoints
continue

View File

@ -241,7 +241,8 @@ func doCleanSubPaths(mounter mount.Interface, podDir string, volumeName string)
if err = doCleanSubPath(mounter, fullContainerDirPath, filepath.Base(path)); err != nil {
return err
}
return nil
// skip subdirs of the volume: it only matters the first level to unmount, otherwise it would try to unmount subdir of the volume
return filepath.SkipDir
})
if err != nil {
return fmt.Errorf("error processing %s: %s", fullContainerDirPath, err)

View File

@ -409,6 +409,7 @@ func TestCleanSubPaths(t *testing.T) {
// Function that validates directory structure after the test
validate func(base string) error
expectError bool
umount func(path string) error
}{
{
name: "not-exists",
@ -539,6 +540,37 @@ func TestCleanSubPaths(t *testing.T) {
return validateDirExists(baseSubdir)
},
},
{
name: "subpath-with-files",
prepare: func(base string) ([]mount.MountPoint, error) {
path := filepath.Join(base, containerSubPathDirectoryName, testVol, "container1", "0")
path2 := filepath.Join(base, containerSubPathDirectoryName, testVol, "container1", "1")
if err := os.MkdirAll(filepath.Join(path, "my-dir-1"), defaultPerm); err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Join(path2, "my-dir-2"), defaultPerm); err != nil {
return nil, err
}
mounts := []mount.MountPoint{
{Device: "/dev/sdb", Path: path},
{Device: "/dev/sdc", Path: path2},
}
return mounts, nil
},
umount: func(mountpath string) error {
fileInfo, err := ioutil.ReadDir(mountpath)
for _, file := range fileInfo {
if err = os.RemoveAll(filepath.Join(mountpath, file.Name())); err != nil {
return err
}
}
return nil
},
validate: func(base string) error {
return validateDirNotExists(filepath.Join(base, containerSubPathDirectoryName))
},
},
}
for _, test := range tests {
@ -553,7 +585,7 @@ func TestCleanSubPaths(t *testing.T) {
t.Fatalf("failed to prepare test %q: %v", test.name, err.Error())
}
fm := &mount.FakeMounter{MountPoints: mounts}
fm := &mount.FakeMounter{MountPoints: mounts, UmountFunc: test.umount}
err = doCleanSubPaths(fm, base, testVol)
if err != nil && !test.expectError {