diff --git a/staging/src/k8s.io/mount-utils/mount_linux_test.go b/staging/src/k8s.io/mount-utils/mount_linux_test.go index dec91c72050..c34aa0be539 100644 --- a/staging/src/k8s.io/mount-utils/mount_linux_test.go +++ b/staging/src/k8s.io/mount-utils/mount_linux_test.go @@ -24,11 +24,13 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "reflect" "strings" "sync" "testing" "time" + "unsafe" utilexec "k8s.io/utils/exec" testexec "k8s.io/utils/exec/testing" @@ -607,6 +609,50 @@ func TestDetectSafeNotMountedBehavior(t *testing.T) { } } +func TestCheckUmountError(t *testing.T) { + target := "/test/path" + withSafeNotMountedBehavior := true + + pState := &os.ProcessState{} + var ref_pState reflect.Value = reflect.ValueOf(pState).Elem() + var ref_status reflect.Value = ref_pState.FieldByName("status") + status := (*uint32)(unsafe.Pointer(ref_status.UnsafeAddr())) + *status = 0 + + command := &exec.Cmd{ProcessState: pState} // dummy command return status 0 + + testcases := []struct { + output []byte + err error + expected bool + }{ + { + err: errors.New("wait: no child processes"), + expected: true, + }, + { + output: []byte("umount: /test/path: not mounted."), + err: errors.New("exit status 1"), + expected: true, + }, + { + output: []byte("umount: /test/path: No such file or directory"), + err: errors.New("exit status 1"), + expected: false, + }, + } + + for _, v := range testcases { + if err := checkUmountError(target, command, v.output, v.err, withSafeNotMountedBehavior); (err == nil) != v.expected { + if v.expected { + t.Errorf("Expected to return nil, but did not. err: %s", err) + } else { + t.Errorf("Expected to return error, but did not.") + } + } + } +} + func TestFormat(t *testing.T) { const ( formatCount = 5 @@ -705,28 +751,3 @@ func makeFakeCommandAction(stdout string, err error, cmdFn func()) testexec.Fake return testexec.InitFakeCmd(&c, cmd, args...) } } - -func TestNotMountedBehaviorOfUnmount(t *testing.T) { - target, err := ioutil.TempDir("", "kubelet-umount") - if err != nil { - t.Errorf("Cannot create temp dir: %v", err) - } - - defer os.RemoveAll(target) - - m := Mounter{withSafeNotMountedBehavior: true} - if err = m.Unmount(target); err != nil { - t.Errorf(`Expect complete Unmount(), but it dose not: %v`, err) - } - - if err = tryUnmount(target, m.withSafeNotMountedBehavior, time.Minute); err != nil { - t.Errorf(`Expect complete tryUnmount(), but it does not: %v`, err) - } - - // forceUmount exec "umount -f", so skip this case if user is not root. - if os.Getuid() == 0 { - if err = forceUmount(target, m.withSafeNotMountedBehavior); err != nil { - t.Errorf(`Expect complete forceUnmount(), but it does not: %v`, err) - } - } -}