tests: add functionality to skip a test if it's not running in CI and not with root user (#16222)

This commit is contained in:
Farbod Ahmadian 2023-03-02 22:08:27 +03:30 committed by GitHub
parent bb4880ec13
commit 629ac58763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 0 deletions

20
ci/skip_non_root.go Normal file
View File

@ -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())
}
}

View File

@ -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{}

View File

@ -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("."))))

View File

@ -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())