Shaman: fail unit test when running as root user (linux) #104234

Manually merged
Sybren A. Stüvel merged 2 commits from michael-2/flamenco:mgr_api_meta_test_TestCheckSharedStoragePath into main 2023-07-07 16:07:17 +02:00
Showing only changes of commit 4deef05cf9 - Show all commits

View File

@ -6,6 +6,7 @@ import (
"io/fs"
"net/http"
"os"
"os/user"
"path/filepath"
"runtime"
"testing"
@ -189,6 +190,11 @@ func TestCheckSharedStoragePath(t *testing.T) {
// that seems consistent.
// FIXME: find another way to test with unwritable directories on Windows.
if runtime.GOOS != "windows" {
currentUser, err := user.Current()
if err != nil {
t.FailNow()

I feel there's too much nesting going on here. Also t.FailNow() wouldn't show the actual content of err. Finally, the original code was written before I realised I could do require.NoError(...) instead of assert.NoError(...) and it would immediately stop the test.

This would be better:

// 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)
I feel there's too much nesting going on here. Also `t.FailNow()` wouldn't show the actual content of `err`. Finally, the original code was written before I realised I could do `require.NoError(...)` instead of `assert.NoError(...)` and it would immediately stop the test. This would be better: ```go // 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) ```
} 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) {
@ -204,6 +210,7 @@ func TestCheckSharedStoragePath(t *testing.T) {
assert.False(t, result.IsUsable)
assert.Contains(t, result.Cause, "Unable to create a file")
}
}
}
func TestSaveSetupAssistantConfig(t *testing.T) {