Add test for CanSafelySkipMountPointCheck

This commit is contained in:
Carter McKinnon 2022-05-02 19:07:39 -07:00
parent 6b44c0debb
commit 3eee8a12ca
2 changed files with 39 additions and 14 deletions

View File

@ -32,8 +32,9 @@ 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
UnmountFunc UnmountFunc
mutex sync.Mutex
UnmountFunc UnmountFunc
skipMountPointCheck bool
}
// UnmountFunc is a function callback to be executed during the Unmount() call.
@ -64,6 +65,11 @@ func NewFakeMounter(mps []MountPoint) *FakeMounter {
}
}
func (f *FakeMounter) WithSkipMountPointCheck() *FakeMounter {
f.skipMountPointCheck = true
return f
}
// ResetLog clears all the log entries in FakeMounter
func (f *FakeMounter) ResetLog() {
f.mutex.Lock()
@ -212,9 +218,8 @@ func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
return true, nil
}
// CanSafelySkipMountPointCheck always returns false for FakeMounter.
func (f *FakeMounter) CanSafelySkipMountPointCheck() bool {
return false
return f.skipMountPointCheck
}
// GetMountRefs finds all mount references to the path, returns a

View File

@ -41,11 +41,14 @@ func TestDoCleanupMountPoint(t *testing.T) {
// 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
prepareMnt func(base string) (MountPoint, error, error)
// Function that prepares the FakeMounter for the test.
// Returns error if prepareMntr function encountered a fatal error.
prepareMntr func(mntr *FakeMounter) error
expectErr bool
}{
"mount-ok": {
prepare: func(base string) (MountPoint, error, error) {
prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{}, nil, err
@ -54,13 +57,13 @@ func TestDoCleanupMountPoint(t *testing.T) {
},
},
"path-not-exist": {
prepare: func(base string) (MountPoint, error, error) {
prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount)
return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil
},
},
"mount-corrupted": {
prepare: func(base string) (MountPoint, error, error) {
prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{}, nil, err
@ -70,7 +73,7 @@ func TestDoCleanupMountPoint(t *testing.T) {
corruptedMnt: true,
},
"mount-err-not-corrupted": {
prepare: func(base string) (MountPoint, error, error) {
prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{}, nil, err
@ -79,6 +82,20 @@ func TestDoCleanupMountPoint(t *testing.T) {
},
expectErr: true,
},
"skip-mount-point-check": {
prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{Device: "/dev/sdb", Path: path}, nil, err
}
return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil
},
prepareMntr: func(mntr *FakeMounter) error {
mntr.WithSkipMountPointCheck()
return nil
},
expectErr: false,
},
}
for name, tt := range tests {
@ -90,19 +107,22 @@ func TestDoCleanupMountPoint(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
if tt.prepare == nil {
t.Fatalf("prepare function required")
if tt.prepareMnt == nil {
t.Fatalf("prepareMnt function required")
}
mountPoint, mountError, err := tt.prepare(tmpDir)
mountPoint, mountError, err := tt.prepareMnt(tmpDir)
if err != nil {
t.Fatalf("failed to prepare test: %v", err)
t.Fatalf("failed to prepareMnt for test: %v", err)
}
fake := NewFakeMounter(
[]MountPoint{mountPoint},
)
fake.MountCheckErrors = map[string]error{mountPoint.Path: mountError}
if tt.prepareMntr != nil {
tt.prepareMntr(fake)
}
err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt)
if tt.expectErr {