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

@ -34,6 +34,7 @@ type FakeMounter struct {
// any golang's DATA RACE warnings. // any golang's DATA RACE warnings.
mutex sync.Mutex mutex sync.Mutex
UnmountFunc UnmountFunc UnmountFunc UnmountFunc
skipMountPointCheck bool
} }
// UnmountFunc is a function callback to be executed during the Unmount() call. // 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 // ResetLog clears all the log entries in FakeMounter
func (f *FakeMounter) ResetLog() { func (f *FakeMounter) ResetLog() {
f.mutex.Lock() f.mutex.Lock()
@ -212,9 +218,8 @@ func (f *FakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
return true, nil return true, nil
} }
// CanSafelySkipMountPointCheck always returns false for FakeMounter.
func (f *FakeMounter) CanSafelySkipMountPointCheck() bool { func (f *FakeMounter) CanSafelySkipMountPointCheck() bool {
return false return f.skipMountPointCheck
} }
// GetMountRefs finds all mount references to the path, returns a // 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. // the given base directory.
// Returns a fake MountPoint, a fake error for the mount point, // Returns a fake MountPoint, a fake error for the mount point,
// and error if the prepare function encountered a fatal error. // and error if the prepare function encountered a fatal error.
prepare func(base string) (MountPoint, error, error) 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 expectErr bool
}{ }{
"mount-ok": { "mount-ok": {
prepare: func(base string) (MountPoint, error, error) { prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount) path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil { if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{}, nil, err return MountPoint{}, nil, err
@ -54,13 +57,13 @@ func TestDoCleanupMountPoint(t *testing.T) {
}, },
}, },
"path-not-exist": { "path-not-exist": {
prepare: func(base string) (MountPoint, error, error) { prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount) path := filepath.Join(base, testMount)
return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil
}, },
}, },
"mount-corrupted": { "mount-corrupted": {
prepare: func(base string) (MountPoint, error, error) { prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount) path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil { if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{}, nil, err return MountPoint{}, nil, err
@ -70,7 +73,7 @@ func TestDoCleanupMountPoint(t *testing.T) {
corruptedMnt: true, corruptedMnt: true,
}, },
"mount-err-not-corrupted": { "mount-err-not-corrupted": {
prepare: func(base string) (MountPoint, error, error) { prepareMnt: func(base string) (MountPoint, error, error) {
path := filepath.Join(base, testMount) path := filepath.Join(base, testMount)
if err := os.MkdirAll(path, defaultPerm); err != nil { if err := os.MkdirAll(path, defaultPerm); err != nil {
return MountPoint{}, nil, err return MountPoint{}, nil, err
@ -79,6 +82,20 @@ func TestDoCleanupMountPoint(t *testing.T) {
}, },
expectErr: true, 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 { for name, tt := range tests {
@ -90,19 +107,22 @@ func TestDoCleanupMountPoint(t *testing.T) {
} }
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
if tt.prepare == nil { if tt.prepareMnt == nil {
t.Fatalf("prepare function required") t.Fatalf("prepareMnt function required")
} }
mountPoint, mountError, err := tt.prepare(tmpDir) mountPoint, mountError, err := tt.prepareMnt(tmpDir)
if err != nil { if err != nil {
t.Fatalf("failed to prepare test: %v", err) t.Fatalf("failed to prepareMnt for test: %v", err)
} }
fake := NewFakeMounter( fake := NewFakeMounter(
[]MountPoint{mountPoint}, []MountPoint{mountPoint},
) )
fake.MountCheckErrors = map[string]error{mountPoint.Path: mountError} fake.MountCheckErrors = map[string]error{mountPoint.Path: mountError}
if tt.prepareMntr != nil {
tt.prepareMntr(fake)
}
err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt) err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt)
if tt.expectErr { if tt.expectErr {