From 40c393732085af2e62e4e29138e079dde8b6eb03 Mon Sep 17 00:00:00 2001 From: Yecheng Fu Date: Tue, 29 May 2018 15:36:54 +0800 Subject: [PATCH] Clean up fake mounters. --- .../cm/container_manager_linux_test.go | 110 ++---------------- pkg/util/mount/exec_mount_test.go | 73 +----------- pkg/util/mount/fake.go | 9 +- pkg/util/removeall/removeall_test.go | 87 +------------- pkg/volume/host_path/host_path_test.go | 93 +-------------- 5 files changed, 29 insertions(+), 343 deletions(-) diff --git a/pkg/kubelet/cm/container_manager_linux_test.go b/pkg/kubelet/cm/container_manager_linux_test.go index 127140e751..882a3ea7bd 100644 --- a/pkg/kubelet/cm/container_manager_linux_test.go +++ b/pkg/kubelet/cm/container_manager_linux_test.go @@ -19,8 +19,6 @@ limitations under the License. package cm import ( - "errors" - "fmt" "io/ioutil" "os" "path" @@ -32,101 +30,9 @@ import ( "k8s.io/kubernetes/pkg/util/mount" ) -type fakeMountInterface struct { - mountPoints []mount.MountPoint -} - -func (mi *fakeMountInterface) Mount(source string, target string, fstype string, options []string) error { - return fmt.Errorf("unsupported") -} - -func (mi *fakeMountInterface) Unmount(target string) error { - return fmt.Errorf("unsupported") -} - -func (mi *fakeMountInterface) List() ([]mount.MountPoint, error) { - return mi.mountPoints, nil -} - -func (mi *fakeMountInterface) IsMountPointMatch(mp mount.MountPoint, dir string) bool { - return (mp.Path == dir) -} - -func (mi *fakeMountInterface) IsNotMountPoint(dir string) (bool, error) { - return false, fmt.Errorf("unsupported") -} - -func (mi *fakeMountInterface) IsLikelyNotMountPoint(file string) (bool, error) { - return false, fmt.Errorf("unsupported") -} -func (mi *fakeMountInterface) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) { - return "", nil -} - -func (mi *fakeMountInterface) DeviceOpened(pathname string) (bool, error) { - for _, mp := range mi.mountPoints { - if mp.Device == pathname { - return true, nil - } - } - return false, nil -} - -func (mi *fakeMountInterface) PathIsDevice(pathname string) (bool, error) { - return true, nil -} - -func (mi *fakeMountInterface) MakeRShared(path string) error { - return nil -} - -func (mi *fakeMountInterface) GetFileType(pathname string) (mount.FileType, error) { - return mount.FileType("fake"), nil -} - -func (mi *fakeMountInterface) MakeDir(pathname string) error { - return nil -} - -func (mi *fakeMountInterface) MakeFile(pathname string) error { - return nil -} - -func (mi *fakeMountInterface) ExistsPath(pathname string) (bool, error) { - return true, errors.New("not implemented") -} - -func (mi *fakeMountInterface) PrepareSafeSubpath(subPath mount.Subpath) (newHostPath string, cleanupAction func(), err error) { - return "", nil, nil -} - -func (mi *fakeMountInterface) CleanSubPaths(_, _ string) error { - return nil -} - -func (mi *fakeMountInterface) SafeMakeDir(_, _ string, _ os.FileMode) error { - return nil -} - -func (mi *fakeMountInterface) GetMountRefs(pathname string) ([]string, error) { - return nil, errors.New("not implemented") -} - -func (mi *fakeMountInterface) GetFSGroup(pathname string) (int64, error) { - return -1, errors.New("not implemented") -} - -func (mi *fakeMountInterface) GetSELinuxSupport(pathname string) (bool, error) { - return false, errors.New("not implemented") -} - -func (mi *fakeMountInterface) GetMode(pathname string) (os.FileMode, error) { - return 0, errors.New("not implemented") -} - func fakeContainerMgrMountInt() mount.Interface { - return &fakeMountInterface{ - []mount.MountPoint{ + return &mount.FakeMounter{ + MountPoints: []mount.MountPoint{ { Device: "cgroup", Type: "cgroup", @@ -158,8 +64,8 @@ func TestCgroupMountValidationSuccess(t *testing.T) { } func TestCgroupMountValidationMemoryMissing(t *testing.T) { - mountInt := &fakeMountInterface{ - []mount.MountPoint{ + mountInt := &mount.FakeMounter{ + MountPoints: []mount.MountPoint{ { Device: "cgroup", Type: "cgroup", @@ -182,8 +88,8 @@ func TestCgroupMountValidationMemoryMissing(t *testing.T) { } func TestCgroupMountValidationMultipleSubsystem(t *testing.T) { - mountInt := &fakeMountInterface{ - []mount.MountPoint{ + mountInt := &mount.FakeMounter{ + MountPoints: []mount.MountPoint{ { Device: "cgroup", Type: "cgroup", @@ -212,8 +118,8 @@ func TestSoftRequirementsValidationSuccess(t *testing.T) { defer os.RemoveAll(tempDir) req.NoError(ioutil.WriteFile(path.Join(tempDir, "cpu.cfs_period_us"), []byte("0"), os.ModePerm)) req.NoError(ioutil.WriteFile(path.Join(tempDir, "cpu.cfs_quota_us"), []byte("0"), os.ModePerm)) - mountInt := &fakeMountInterface{ - []mount.MountPoint{ + mountInt := &mount.FakeMounter{ + MountPoints: []mount.MountPoint{ { Device: "cgroup", Type: "cgroup", diff --git a/pkg/util/mount/exec_mount_test.go b/pkg/util/mount/exec_mount_test.go index 9619356f71..d6ccd84489 100644 --- a/pkg/util/mount/exec_mount_test.go +++ b/pkg/util/mount/exec_mount_test.go @@ -19,9 +19,7 @@ limitations under the License. package mount import ( - "errors" "fmt" - "os" "reflect" "strings" "testing" @@ -47,7 +45,7 @@ func TestMount(t *testing.T) { return nil, nil }) - wrappedMounter := &fakeMounter{t} + wrappedMounter := &fakeMounter{FakeMounter: &FakeMounter{}, t: t} mounter := NewExecMounter(exec, wrappedMounter) mounter.Mount(sourcePath, destinationPath, fsType, mountOptions) @@ -75,7 +73,7 @@ func TestBindMount(t *testing.T) { return nil, nil }) - wrappedMounter := &fakeMounter{t} + wrappedMounter := &fakeMounter{FakeMounter: &FakeMounter{}, t: t} mounter := NewExecMounter(exec, wrappedMounter) bindOptions := append(mountOptions, "bind") mounter.Mount(sourcePath, destinationPath, fsType, bindOptions) @@ -94,7 +92,7 @@ func TestUnmount(t *testing.T) { return nil, nil }) - wrappedMounter := &fakeMounter{t} + wrappedMounter := &fakeMounter{&FakeMounter{}, t} mounter := NewExecMounter(exec, wrappedMounter) mounter.Unmount(destinationPath) @@ -102,6 +100,7 @@ func TestUnmount(t *testing.T) { /* Fake wrapped mounter */ type fakeMounter struct { + *FakeMounter t *testing.T } @@ -116,67 +115,3 @@ func (fm *fakeMounter) Unmount(target string) error { fm.t.Errorf("Unexpected wrapped mount call") return fmt.Errorf("Unexpected wrapped mount call") } - -func (fm *fakeMounter) List() ([]MountPoint, error) { - return nil, nil -} -func (fm *fakeMounter) IsMountPointMatch(mp MountPoint, dir string) bool { - return false -} -func (fm *fakeMounter) IsNotMountPoint(file string) (bool, error) { - return false, nil -} -func (fm *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) { - return false, nil -} -func (fm *fakeMounter) DeviceOpened(pathname string) (bool, error) { - return false, nil -} -func (fm *fakeMounter) PathIsDevice(pathname string) (bool, error) { - return false, nil -} -func (fm *fakeMounter) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) { - return "", nil -} -func (fm *fakeMounter) MakeRShared(path string) error { - return nil -} -func (fm *fakeMounter) MakeFile(pathname string) error { - return nil -} -func (fm *fakeMounter) MakeDir(pathname string) error { - return nil -} -func (fm *fakeMounter) ExistsPath(pathname string) (bool, error) { - return false, errors.New("not implemented") -} -func (fm *fakeMounter) GetFileType(pathname string) (FileType, error) { - return FileTypeFile, nil -} -func (fm *fakeMounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) { - return subPath.Path, nil, nil -} - -func (fm *fakeMounter) CleanSubPaths(podDir string, volumeName string) error { - return nil -} - -func (fm *fakeMounter) SafeMakeDir(pathname string, base string, perm os.FileMode) error { - return nil -} - -func (fm *fakeMounter) GetMountRefs(pathname string) ([]string, error) { - return nil, errors.New("not implemented") -} - -func (fm *fakeMounter) GetFSGroup(pathname string) (int64, error) { - return -1, errors.New("not implemented") -} - -func (fm *fakeMounter) GetSELinuxSupport(pathname string) (bool, error) { - return false, errors.New("not implemented") -} - -func (fm *fakeMounter) GetMode(pathname string) (os.FileMode, error) { - return 0, errors.New("not implemented") -} diff --git a/pkg/util/mount/fake.go b/pkg/util/mount/fake.go index 10832fd321..55c99c9fe3 100644 --- a/pkg/util/mount/fake.go +++ b/pkg/util/mount/fake.go @@ -29,6 +29,7 @@ import ( type FakeMounter struct { MountPoints []MountPoint Log []FakeAction + Filesystem map[string]FileType // Some tests run things in parallel, make sure the mounter does not produce // any golang's DATA RACE warnings. mutex sync.Mutex @@ -190,6 +191,9 @@ func (f *FakeMounter) MakeRShared(path string) error { } func (f *FakeMounter) GetFileType(pathname string) (FileType, error) { + if t, ok := f.Filesystem[pathname]; ok { + return t, nil + } return FileType("fake"), nil } @@ -202,7 +206,10 @@ func (f *FakeMounter) MakeFile(pathname string) error { } func (f *FakeMounter) ExistsPath(pathname string) (bool, error) { - return false, errors.New("not implemented") + if _, ok := f.Filesystem[pathname]; ok { + return true, nil + } + return false, nil } func (f *FakeMounter) PrepareSafeSubpath(subPath Subpath) (newHostPath string, cleanupAction func(), err error) { diff --git a/pkg/util/removeall/removeall_test.go b/pkg/util/removeall/removeall_test.go index 1b67cc981f..07db7718ed 100644 --- a/pkg/util/removeall/removeall_test.go +++ b/pkg/util/removeall/removeall_test.go @@ -27,87 +27,12 @@ import ( "k8s.io/kubernetes/pkg/util/mount" ) -type fakeMounter struct{} - -var _ mount.Interface = &fakeMounter{} - -func (mounter *fakeMounter) Mount(source string, target string, fstype string, options []string) error { - return errors.New("not implemented") +type fakeMounter struct { + mount.FakeMounter } -func (mounter *fakeMounter) Unmount(target string) error { - return errors.New("not implemented") -} - -func (mounter *fakeMounter) List() ([]mount.MountPoint, error) { - return nil, errors.New("not implemented") -} - -func (mounter fakeMounter) DeviceOpened(pathname string) (bool, error) { - return false, errors.New("not implemented") -} - -func (mounter *fakeMounter) PathIsDevice(pathname string) (bool, error) { - return false, errors.New("not implemented") -} - -func (mounter *fakeMounter) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) { - return "", errors.New("not implemented") -} - -func (mounter *fakeMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool { - return mp.Path == dir -} - -func (mounter *fakeMounter) IsNotMountPoint(dir string) (bool, error) { - return mount.IsNotMountPoint(mounter, dir) -} - -func (mounter *fakeMounter) GetFileType(pathname string) (mount.FileType, error) { - return mount.FileType("fake"), errors.New("not implemented") -} - -func (mounter *fakeMounter) MakeDir(pathname string) error { - return nil -} - -func (mounter *fakeMounter) MakeFile(pathname string) error { - return nil -} - -func (mounter *fakeMounter) ExistsPath(pathname string) (bool, error) { - return true, errors.New("not implemented") -} - -func (mounter *fakeMounter) PrepareSafeSubpath(subPath mount.Subpath) (newHostPath string, cleanupAction func(), err error) { - return "", nil, nil -} - -func (mounter *fakeMounter) CleanSubPaths(_, _ string) error { - return nil -} - -func (mounter *fakeMounter) SafeMakeDir(_, _ string, _ os.FileMode) error { - return nil -} - -func (mounter *fakeMounter) GetMountRefs(pathname string) ([]string, error) { - return nil, errors.New("not implemented") -} - -func (mounter *fakeMounter) GetFSGroup(pathname string) (int64, error) { - return -1, errors.New("not implemented") -} - -func (mounter *fakeMounter) GetSELinuxSupport(pathname string) (bool, error) { - return false, errors.New("not implemented") -} - -func (mounter *fakeMounter) GetMode(pathname string) (os.FileMode, error) { - return 0, errors.New("not implemented") -} - -func (mounter *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) { +// IsLikelyNotMountPoint overrides mount.FakeMounter.IsLikelyNotMountPoint for our use. +func (f *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) { name := path.Base(file) if strings.HasPrefix(name, "mount") { return false, nil @@ -118,10 +43,6 @@ func (mounter *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) { return true, nil } -func (mounter *fakeMounter) MakeRShared(path string) error { - return nil -} - func TestRemoveAllOneFilesystem(t *testing.T) { tests := []struct { name string diff --git a/pkg/volume/host_path/host_path_test.go b/pkg/volume/host_path/host_path_test.go index 39696d765d..ef4bfc7ab3 100644 --- a/pkg/volume/host_path/host_path_test.go +++ b/pkg/volume/host_path/host_path_test.go @@ -17,7 +17,6 @@ limitations under the License. package host_path import ( - "errors" "fmt" "os" "testing" @@ -319,92 +318,6 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) { } } -type fakeFileTypeChecker struct { - desiredType string -} - -func (fftc *fakeFileTypeChecker) Mount(source string, target string, fstype string, options []string) error { - return nil -} - -func (fftc *fakeFileTypeChecker) Unmount(target string) error { - return nil -} - -func (fftc *fakeFileTypeChecker) List() ([]utilmount.MountPoint, error) { - return nil, nil -} -func (fftc *fakeFileTypeChecker) IsMountPointMatch(mp utilmount.MountPoint, dir string) bool { - return false -} - -func (fftc *fakeFileTypeChecker) IsNotMountPoint(file string) (bool, error) { - return false, nil -} - -func (fftc *fakeFileTypeChecker) IsLikelyNotMountPoint(file string) (bool, error) { - return false, nil -} - -func (fftc *fakeFileTypeChecker) DeviceOpened(pathname string) (bool, error) { - return false, nil -} -func (fftc *fakeFileTypeChecker) PathIsDevice(pathname string) (bool, error) { - return false, nil -} - -func (fftc *fakeFileTypeChecker) GetDeviceNameFromMount(mountPath, pluginDir string) (string, error) { - return "fake", nil -} - -func (fftc *fakeFileTypeChecker) MakeRShared(path string) error { - return nil -} - -func (fftc *fakeFileTypeChecker) MakeFile(pathname string) error { - return nil -} - -func (fftc *fakeFileTypeChecker) MakeDir(pathname string) error { - return nil -} - -func (fftc *fakeFileTypeChecker) ExistsPath(pathname string) (bool, error) { - return true, nil -} - -func (fftc *fakeFileTypeChecker) GetFileType(_ string) (utilmount.FileType, error) { - return utilmount.FileType(fftc.desiredType), nil -} - -func (fftc *fakeFileTypeChecker) PrepareSafeSubpath(subPath utilmount.Subpath) (newHostPath string, cleanupAction func(), err error) { - return "", nil, nil -} - -func (fftc *fakeFileTypeChecker) CleanSubPaths(_, _ string) error { - return nil -} - -func (fftc *fakeFileTypeChecker) SafeMakeDir(_, _ string, _ os.FileMode) error { - return nil -} - -func (fftc *fakeFileTypeChecker) GetMountRefs(pathname string) ([]string, error) { - return nil, errors.New("not implemented") -} - -func (fftc *fakeFileTypeChecker) GetFSGroup(pathname string) (int64, error) { - return -1, errors.New("not implemented") -} - -func (fftc *fakeFileTypeChecker) GetSELinuxSupport(pathname string) (bool, error) { - return false, errors.New("not implemented") -} - -func (fftc *fakeFileTypeChecker) GetMode(pathname string) (os.FileMode, error) { - return 0, errors.New("not implemented") -} - func setUp() error { err := os.MkdirAll("/tmp/ExistingFolder", os.FileMode(0755)) if err != nil { @@ -473,7 +386,11 @@ func TestOSFileTypeChecker(t *testing.T) { } for i, tc := range testCases { - fakeFTC := &fakeFileTypeChecker{desiredType: tc.desiredType} + fakeFTC := &utilmount.FakeMounter{ + Filesystem: map[string]utilmount.FileType{ + tc.path: utilmount.FileType(tc.desiredType), + }, + } oftc := newFileTypeChecker(tc.path, fakeFTC) path := oftc.GetPath()