diff --git a/pkg/util/mount/fake.go b/pkg/util/mount/fake.go index 06e0fcccdc..0e2952f3e0 100644 --- a/pkg/util/mount/fake.go +++ b/pkg/util/mount/fake.go @@ -30,6 +30,8 @@ type FakeMounter struct { MountPoints []MountPoint Log []FakeAction Filesystem map[string]FileType + // Error to return for a path when calling IsLikelyNotMountPoint + 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 @@ -119,6 +121,7 @@ func (f *FakeMounter) Unmount(target string) error { } f.MountPoints = newMountpoints f.Log = append(f.Log, FakeAction{Action: FakeActionUnmount, Target: absTarget}) + delete(f.MountCheckErrors, target) return nil } @@ -141,7 +144,12 @@ func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) { f.mutex.Lock() defer f.mutex.Unlock() - _, err := os.Stat(file) + err := f.MountCheckErrors[file] + if err != nil { + return false, err + } + + _, err = os.Stat(file) if err != nil { return true, err } diff --git a/pkg/util/mount/mount_helper_test.go b/pkg/util/mount/mount_helper_test.go index 2897d33afa..092f99b948 100644 --- a/pkg/util/mount/mount_helper_test.go +++ b/pkg/util/mount/mount_helper_test.go @@ -17,47 +17,97 @@ limitations under the License. package mount import ( + "io/ioutil" "os" + "path/filepath" + "syscall" "testing" - - utiltesting "k8s.io/client-go/util/testing" ) func TestDoUnmountMountPoint(t *testing.T) { + const testMount = "test-mount" + const defaultPerm = 0750 - tmpDir1, err1 := utiltesting.MkTmpdir("umount_test1") - if err1 != nil { - t.Fatalf("error creating temp dir: %v", err1) - } - defer os.RemoveAll(tmpDir1) - - tmpDir2, err2 := utiltesting.MkTmpdir("umount_test2") - if err2 != nil { - t.Fatalf("error creating temp dir: %v", err2) - } - defer os.RemoveAll(tmpDir2) - - // Second part: want no error - tests := []struct { - mountPath string + tests := map[string]struct { corruptedMnt bool + // Function that prepares the directory structure for the test under + // the given base directory. + // Returns a fake MountPoint, a fake error for the mount point, + // and error if the prepare function encountered a fatal error. + prepare func(base string) (MountPoint, error, error) + expectErr bool }{ - { - mountPath: tmpDir1, + "mount-ok": { + prepare: func(base string) (MountPoint, error, error) { + path := filepath.Join(base, testMount) + if err := os.MkdirAll(path, defaultPerm); err != nil { + return MountPoint{}, nil, err + } + return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil + }, + }, + "mount-corrupted": { + prepare: func(base string) (MountPoint, error, error) { + path := filepath.Join(base, testMount) + if err := os.MkdirAll(path, defaultPerm); err != nil { + return MountPoint{}, nil, err + } + return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ESTALE), nil + }, corruptedMnt: true, }, - { - mountPath: tmpDir2, - corruptedMnt: false, + "mount-err-not-corrupted": { + prepare: func(base string) (MountPoint, error, error) { + path := filepath.Join(base, testMount) + if err := os.MkdirAll(path, defaultPerm); err != nil { + return MountPoint{}, nil, err + } + return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ETIMEDOUT), nil + }, + expectErr: true, }, } - fake := &FakeMounter{} + for name, tt := range tests { + t.Run(name, func(t *testing.T) { - for _, tt := range tests { - err := doUnmountMountPoint(tt.mountPath, fake, false, tt.corruptedMnt) - if err != nil { - t.Errorf("err Expected nil, but got: %v", err) - } + tmpDir, err := ioutil.TempDir("", "unmount-mount-point-test") + if err != nil { + t.Fatalf("failed to create tmpdir: %v", err) + } + defer os.RemoveAll(tmpDir) + + if tt.prepare == nil { + t.Fatalf("prepare function required") + } + + mountPoint, mountError, err := tt.prepare(tmpDir) + if err != nil { + t.Fatalf("failed to prepare test: %v", err) + } + + fake := &FakeMounter{ + MountPoints: []MountPoint{mountPoint}, + MountCheckErrors: map[string]error{mountPoint.Path: mountError}, + } + + err = doUnmountMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt) + if tt.expectErr { + if err == nil { + t.Errorf("test %s failed, expected error, got none", name) + } + if err := validateDirExists(mountPoint.Path); err != nil { + t.Errorf("test %s failed, mount path doesn't exist: %v", name, err) + } + } + if !tt.expectErr { + if err != nil { + t.Errorf("test %s failed: %v", name, err) + } + if err := validateDirNotExists(mountPoint.Path); err != nil { + t.Errorf("test %s failed, mount path still exists: %v", name, err) + } + } + }) } }