From fbd0dcbe9b9bbbf393a03de54b787d825434be32 Mon Sep 17 00:00:00 2001 From: Farbod Ahmadian Date: Thu, 2 Mar 2023 22:08:27 +0330 Subject: [PATCH] tests: add functionality to skip a test if it's not running in CI and not with root user (#16222) --- ci/skip_non_root.go | 20 +++++++++++++++++++ .../taskrunner/artifact_hook_test.go | 1 + .../taskrunner/task_runner_test.go | 1 + drivers/exec/driver_unix_test.go | 1 + 4 files changed, 23 insertions(+) create mode 100644 ci/skip_non_root.go diff --git a/ci/skip_non_root.go b/ci/skip_non_root.go new file mode 100644 index 000000000..7f161f04b --- /dev/null +++ b/ci/skip_non_root.go @@ -0,0 +1,20 @@ +package ci + +import ( + "os" + "strconv" + "syscall" + "testing" +) + +// SkipTestWithoutRootAccess will skip test t if it's not running in CI environment +// and test is not running with Root access. +func SkipTestWithoutRootAccess(t *testing.T) { + ciVar := os.Getenv("CI") + isCI, err := strconv.ParseBool(ciVar) + isCI = isCI && err == nil + + if !isCI && syscall.Getuid() != 0 { + t.Skipf("Skipping test %s. To run this test, you should run it as root user", t.Name()) + } +} diff --git a/client/allocrunner/taskrunner/artifact_hook_test.go b/client/allocrunner/taskrunner/artifact_hook_test.go index 917e0411a..01a87a582 100644 --- a/client/allocrunner/taskrunner/artifact_hook_test.go +++ b/client/allocrunner/taskrunner/artifact_hook_test.go @@ -161,6 +161,7 @@ func TestTaskRunner_ArtifactHook_PartialDone(t *testing.T) { // TestTaskRunner_ArtifactHook_ConcurrentDownloadSuccess asserts that the artifact hook // download multiple files concurrently. this is a successful test without any errors. func TestTaskRunner_ArtifactHook_ConcurrentDownloadSuccess(t *testing.T) { + ci.SkipTestWithoutRootAccess(t) t.Parallel() me := &mockEmitter{} diff --git a/client/allocrunner/taskrunner/task_runner_test.go b/client/allocrunner/taskrunner/task_runner_test.go index 65de74e94..b3064f3af 100644 --- a/client/allocrunner/taskrunner/task_runner_test.go +++ b/client/allocrunner/taskrunner/task_runner_test.go @@ -1720,6 +1720,7 @@ func TestTaskRunner_DeriveToken_Unrecoverable(t *testing.T) { // TestTaskRunner_Download_RawExec asserts that downloaded artifacts may be // executed in a driver without filesystem isolation. func TestTaskRunner_Download_RawExec(t *testing.T) { + ci.SkipTestWithoutRootAccess(t) ci.Parallel(t) ts := httptest.NewServer(http.FileServer(http.Dir(filepath.Dir(".")))) diff --git a/drivers/exec/driver_unix_test.go b/drivers/exec/driver_unix_test.go index ce765835d..e81eeeaa1 100644 --- a/drivers/exec/driver_unix_test.go +++ b/drivers/exec/driver_unix_test.go @@ -88,6 +88,7 @@ func TestExecDriver_StartWaitStop(t *testing.T) { } func TestExec_ExecTaskStreaming(t *testing.T) { + ci.SkipTestWithoutRootAccess(t) ci.Parallel(t) ctx, cancel := context.WithCancel(context.Background())