mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
unit test not requiring priviledge
This commit is contained in:
parent
326d4ce072
commit
99d73dfae5
@ -24,11 +24,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
utilexec "k8s.io/utils/exec"
|
utilexec "k8s.io/utils/exec"
|
||||||
testexec "k8s.io/utils/exec/testing"
|
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) {
|
func TestFormat(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
formatCount = 5
|
formatCount = 5
|
||||||
@ -705,28 +751,3 @@ func makeFakeCommandAction(stdout string, err error, cmdFn func()) testexec.Fake
|
|||||||
return testexec.InitFakeCmd(&c, cmd, args...)
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user