Skip to content

Commit

Permalink
unit test not requiring priviledge
Browse files Browse the repository at this point in the history
Kubernetes-commit: 99d73dfae5757a477fa40fa93d2f0f80e15db8bf
  • Loading branch information
mochizuki875 authored and k8s-publishing-bot committed Feb 17, 2023
1 parent 8e37d72 commit da8b256
Showing 1 changed file with 46 additions and 25 deletions.
71 changes: 46 additions & 25 deletions mount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit da8b256

Please sign in to comment.