Shaman: fail unit test when running as root user (linux) #104234
@ -190,26 +190,31 @@ func TestCheckSharedStoragePath(t *testing.T) {
|
|||||||
// that seems consistent.
|
// that seems consistent.
|
||||||
// FIXME: find another way to test with unwritable directories on Windows.
|
// FIXME: find another way to test with unwritable directories on Windows.
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
currentUser, err := user.Current()
|
|
||||||
|
|
||||||
if err != nil {
|
// Root can always create directories, so this test would fail with a
|
||||||
|
// confusing message. Instead it's better to refuse running as root at all.
|
||||||
|
currentUser, err := user.Current()
|
||||||
|
|||||||
|
require.NoError(t, err)
|
||||||
|
require.NotEqual(t, "0", currentUser.Uid,
|
||||||
|
"this test requires running as normal user, not %s (%s)", currentUser.Username, currentUser.Uid)
|
||||||
|
require.NotEqual(t, "root", currentUser.Username,
|
||||||
|
"this test requires running as normal user, not %s (%s)", currentUser.Username, currentUser.Uid)
|
||||||
|
|
||||||
|
parentPath := filepath.Join(mf.tempdir, "deep")
|
||||||
|
testPath := filepath.Join(parentPath, "nesting")
|
||||||
|
if err := os.Mkdir(parentPath, fs.ModePerm); !assert.NoError(t, err) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
} else if currentUser.Username != "root" {
|
|
||||||
parentPath := filepath.Join(mf.tempdir, "deep")
|
|
||||||
testPath := filepath.Join(parentPath, "nesting")
|
|
||||||
if err := os.Mkdir(parentPath, fs.ModePerm); !assert.NoError(t, err) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
if err := os.Mkdir(testPath, fs.FileMode(0)); !assert.NoError(t, err) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
echoCtx := doTest(testPath)
|
|
||||||
result := api.PathCheckResult{}
|
|
||||||
getResponseJSON(t, echoCtx, http.StatusOK, &result)
|
|
||||||
assert.Equal(t, testPath, result.Path)
|
|
||||||
assert.False(t, result.IsUsable)
|
|
||||||
assert.Contains(t, result.Cause, "Unable to create a file")
|
|
||||||
}
|
}
|
||||||
|
if err := os.Mkdir(testPath, fs.FileMode(0)); !assert.NoError(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
echoCtx := doTest(testPath)
|
||||||
|
result := api.PathCheckResult{}
|
||||||
|
getResponseJSON(t, echoCtx, http.StatusOK, &result)
|
||||||
|
assert.Equal(t, testPath, result.Path)
|
||||||
|
assert.False(t, result.IsUsable)
|
||||||
|
assert.Contains(t, result.Cause, "Unable to create a file")
|
||||||
|
|
||||||
Sybren A. Stüvel
commented
Please keep unrelated formatting changes out of a PR. I'll remove this before landing; this is just a request for other/future PRs. Please keep unrelated formatting changes out of a PR. I'll remove this before landing; this is just a request for other/future PRs.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user
I feel there's too much nesting going on here. Also
t.FailNow()
wouldn't show the actual content oferr
. Finally, the original code was written before I realised I could dorequire.NoError(...)
instead ofassert.NoError(...)
and it would immediately stop the test.This would be better: