diff --git a/cli/create_test.go b/cli/create_test.go index 9e212af99..0428c3eaa 100644 --- a/cli/create_test.go +++ b/cli/create_test.go @@ -15,6 +15,7 @@ import ( "regexp" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/pkg/katautils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" @@ -70,9 +71,9 @@ func TestCreatePIDFileEmptyPathSuccessful(t *testing.T) { } func TestCreatePIDFileUnableToRemove(t *testing.T) { - if os.Geteuid() == 0 { + if tc.NotValid(ktu.NeedNonRoot()) { // The os.FileMode(0000) trick doesn't work for root. - t.Skip(testDisabledNeedNonRoot) + t.Skip(ktu.TestDisabledNeedNonRoot) } assert := assert.New(t) @@ -387,8 +388,8 @@ func TestCreateContainerInvalid(t *testing.T) { } func TestCreateProcessCgroupsPathSuccessful(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -489,9 +490,9 @@ func TestCreateProcessCgroupsPathSuccessful(t *testing.T) { } func TestCreateCreateCgroupsFilesFail(t *testing.T) { - if os.Geteuid() == 0 { + if tc.NotValid(ktu.NeedNonRoot()) { // The os.FileMode(0000) trick doesn't work for root. - t.Skip(testDisabledNeedNonRoot) + t.Skip(ktu.TestDisabledNeedNonRoot) } assert := assert.New(t) @@ -575,9 +576,9 @@ func TestCreateCreateCgroupsFilesFail(t *testing.T) { } func TestCreateCreateCreatePidFileFail(t *testing.T) { - if os.Geteuid() == 0 { + if tc.NotValid(ktu.NeedNonRoot()) { // The os.FileMode(0000) trick doesn't work for root. - t.Skip(testDisabledNeedNonRoot) + t.Skip(ktu.TestDisabledNeedNonRoot) } assert := assert.New(t) @@ -652,8 +653,8 @@ func TestCreateCreateCreatePidFileFail(t *testing.T) { } func TestCreate(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) diff --git a/cli/delete_test.go b/cli/delete_test.go index 8590e4bd6..97768e1b4 100644 --- a/cli/delete_test.go +++ b/cli/delete_test.go @@ -13,6 +13,7 @@ import ( "path/filepath" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" "github.com/kata-containers/runtime/virtcontainers/pkg/vcmock" @@ -597,8 +598,8 @@ func TestDeleteCLIFunctionSuccess(t *testing.T) { } func TestRemoveCGroupsPath(t *testing.T) { - if os.Geteuid() == 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedNonRoot()) { + t.Skip(ktu.TestDisabledNeedNonRoot) } assert := assert.New(t) diff --git a/cli/factory_test.go b/cli/factory_test.go index 1af43bd74..9e968d40f 100644 --- a/cli/factory_test.go +++ b/cli/factory_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/urfave/cli" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" ) @@ -65,7 +66,7 @@ func TestFactoryCLIFunctionInit(t *testing.T) { assert.Nil(err) // With template - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } diff --git a/cli/kata-check_test.go b/cli/kata-check_test.go index c9dbd1cbb..418a6635b 100644 --- a/cli/kata-check_test.go +++ b/cli/kata-check_test.go @@ -15,6 +15,7 @@ import ( "path/filepath" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/pkg/katautils" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" @@ -553,8 +554,8 @@ func TestCheckCheckKernelModules(t *testing.T) { func TestCheckCheckKernelModulesUnreadableFile(t *testing.T) { assert := assert.New(t) - if os.Geteuid() == 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedNonRoot()) { + t.Skip(ktu.TestDisabledNeedNonRoot) } dir, err := ioutil.TempDir("", "") diff --git a/cli/main_test.go b/cli/main_test.go index 72ef8290d..462d22b15 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/dlespiau/covertool/pkg/cover" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/pkg/katautils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" @@ -34,10 +35,9 @@ import ( ) const ( - testDisabledNeedNonRoot = "Test disabled as requires non-root user" - testDirMode = os.FileMode(0750) - testFileMode = os.FileMode(0640) - testExeFileMode = os.FileMode(0750) + testDirMode = os.FileMode(0750) + testFileMode = os.FileMode(0640) + testExeFileMode = os.FileMode(0750) // small docker image used to create root filesystems from testDockerImage = "busybox" @@ -51,6 +51,7 @@ var ( // package variables set by calling TestMain() testDir = "" testBundleDir = "" + tc ktu.TestConstraint ) // testingImpl is a concrete mock RVC implementation used for testing @@ -122,6 +123,8 @@ func init() { if err != nil { panic(fmt.Sprintf("ERROR: failed to create OCI bundle: %v", err)) } + + tc = ktu.NewTestConstraint(false) } // resetCLIGlobals undoes the effects of setCLIGlobals(), restoring the original values diff --git a/cli/run_test.go b/cli/run_test.go index 2154f9db1..4f9640bc8 100644 --- a/cli/run_test.go +++ b/cli/run_test.go @@ -16,6 +16,7 @@ import ( "regexp" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" @@ -223,8 +224,8 @@ func testRunContainerSetup(t *testing.T) runContainerData { } func TestRunContainerSuccessful(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -332,8 +333,8 @@ func TestRunContainerSuccessful(t *testing.T) { } func TestRunContainerDetachSuccessful(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) diff --git a/containerd-shim-v2/create_test.go b/containerd-shim-v2/create_test.go index fa8a82a35..53be90d50 100644 --- a/containerd-shim-v2/create_test.go +++ b/containerd-shim-v2/create_test.go @@ -19,14 +19,15 @@ import ( vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/vcmock" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/pkg/katautils" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/stretchr/testify/assert" ) func TestCreateSandboxSuccess(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -97,9 +98,10 @@ func TestCreateSandboxSuccess(t *testing.T) { } func TestCreateSandboxFail(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } + assert := assert.New(t) tmpdir, err := ioutil.TempDir("", "") @@ -142,8 +144,8 @@ func TestCreateSandboxFail(t *testing.T) { } func TestCreateSandboxConfigFail(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) diff --git a/containerd-shim-v2/utils_test.go b/containerd-shim-v2/utils_test.go index 4ae413ef9..9da6ce519 100644 --- a/containerd-shim-v2/utils_test.go +++ b/containerd-shim-v2/utils_test.go @@ -17,6 +17,7 @@ import ( "path/filepath" "strings" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/pkg/katautils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" @@ -41,8 +42,6 @@ const ( testBundle = "bundle" testConsole = "/dev/pts/888" - testDisabledNeedRoot = "Test disabled as requires root user" - testContainerTypeAnnotation = "io.kubernetes.cri.container-type" testSandboxIDAnnotation = "io.kubernetes.cri.sandbox-id" testContainerTypeSandbox = "sandbox" @@ -53,6 +52,7 @@ var ( // package variables set by calling TestMain() testDir = "" testBundleDir = "" + tc ktu.TestConstraint ) // testingImpl is a concrete mock RVC implementation used for testing @@ -108,6 +108,8 @@ func init() { if err != nil { panic(fmt.Sprintf("ERROR: failed to create OCI bundle: %v", err)) } + + tc = ktu.NewTestConstraint(false) } // createOCIConfig creates an OCI configuration (spec) file in diff --git a/netmon/netmon_test.go b/netmon/netmon_test.go index 294ec4617..98dede6eb 100644 --- a/netmon/netmon_test.go +++ b/netmon/netmon_test.go @@ -16,6 +16,7 @@ import ( "runtime" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" @@ -42,7 +43,9 @@ const ( ) func skipUnlessRoot(t *testing.T) { - if os.Getuid() != 0 { + tc := ktu.NewTestConstraint(false) + + if tc.NotValid(ktu.NeedRoot()) { t.Skip("Test disabled as requires root user") } } diff --git a/pkg/katautils/config_test.go b/pkg/katautils/config_test.go index 44d434893..24b7511ab 100644 --- a/pkg/katautils/config_test.go +++ b/pkg/katautils/config_test.go @@ -19,7 +19,7 @@ import ( "syscall" "testing" - "github.com/kata-containers/runtime/pkg/katatestutils" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" "github.com/kata-containers/runtime/virtcontainers/utils" @@ -84,7 +84,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf hotplugVFIOOnRootBus := true disableNewNetNs := false - configFileOptions := katatestutils.RuntimeConfigOptions{ + configFileOptions := ktu.RuntimeConfigOptions{ Hypervisor: "qemu", HypervisorPath: hypervisorPath, KernelPath: kernelPath, @@ -115,7 +115,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf AgentTrace: agentTrace, } - runtimeConfigFileData := katatestutils.MakeRuntimeConfigFileData(configFileOptions) + runtimeConfigFileData := ktu.MakeRuntimeConfigFileData(configFileOptions) configPath := path.Join(dir, "runtime.toml") err = createConfig(configPath, runtimeConfigFileData) @@ -422,8 +422,8 @@ func TestConfigLoadConfigurationFailMissingShim(t *testing.T) { } func TestConfigLoadConfigurationFailUnreadableConfig(t *testing.T) { - if os.Geteuid() == 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedNonRoot()) { + t.Skip(ktu.TestDisabledNeedNonRoot) } tmpdir, err := ioutil.TempDir(testDir, "runtime-config-") @@ -445,8 +445,8 @@ func TestConfigLoadConfigurationFailUnreadableConfig(t *testing.T) { } func TestConfigLoadConfigurationFailTOMLConfigFileInvalidContents(t *testing.T) { - if os.Geteuid() == 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedNonRoot()) { + t.Skip(ktu.TestDisabledNeedNonRoot) } tmpdir, err := ioutil.TempDir(testDir, "runtime-config-") @@ -471,8 +471,8 @@ func TestConfigLoadConfigurationFailTOMLConfigFileInvalidContents(t *testing.T) } func TestConfigLoadConfigurationFailTOMLConfigFileDuplicatedData(t *testing.T) { - if os.Geteuid() == 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedNonRoot()) { + t.Skip(ktu.TestDisabledNeedNonRoot) } tmpdir, err := ioutil.TempDir(testDir, "runtime-config-") diff --git a/pkg/katautils/create_test.go b/pkg/katautils/create_test.go index 0acb8121e..8fc928334 100644 --- a/pkg/katautils/create_test.go +++ b/pkg/katautils/create_test.go @@ -19,6 +19,7 @@ import ( "syscall" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" "github.com/kata-containers/runtime/virtcontainers/pkg/vcmock" @@ -38,8 +39,14 @@ var ( // testingImpl is a concrete mock RVC implementation used for testing testingImpl = &vcmock.VCMock{} + + tc ktu.TestConstraint ) +func init() { + tc = ktu.NewTestConstraint(false) +} + // readOCIConfig returns an OCI spec. func readOCIConfigFile(configPath string) (oci.CompatOCISpec, error) { if configPath == "" { @@ -178,8 +185,8 @@ func findLastParam(key string, params []vc.Param) (string, error) { } func TestSetEphemeralStorageType(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -313,8 +320,8 @@ func TestCreateSandboxConfigFail(t *testing.T) { } func TestCreateSandboxFail(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) diff --git a/pkg/katautils/hook_test.go b/pkg/katautils/hook_test.go index 8bc09142e..83dc65585 100644 --- a/pkg/katautils/hook_test.go +++ b/pkg/katautils/hook_test.go @@ -11,6 +11,7 @@ import ( "os" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" . "github.com/kata-containers/runtime/virtcontainers/pkg/mock" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" "github.com/opencontainers/runtime-spec/specs-go" @@ -55,8 +56,8 @@ func createWrongHook() specs.Hook { } func TestRunHook(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -86,8 +87,8 @@ func TestRunHook(t *testing.T) { } func TestPreStartHooks(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -134,8 +135,8 @@ func TestPreStartHooks(t *testing.T) { } func TestPostStartHooks(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) @@ -182,8 +183,8 @@ func TestPostStartHooks(t *testing.T) { } func TestPostStopHooks(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) diff --git a/pkg/katautils/network_test.go b/pkg/katautils/network_test.go index 857568ce5..3b157dde1 100644 --- a/pkg/katautils/network_test.go +++ b/pkg/katautils/network_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/containernetworking/plugins/pkg/ns" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/stretchr/testify/assert" "golang.org/x/sys/unix" @@ -64,8 +65,8 @@ func TestGetNetNsFromBindMount(t *testing.T) { func TestHostNetworkingRequested(t *testing.T) { assert := assert.New(t) - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } // Network namespace same as the host @@ -101,8 +102,8 @@ func TestHostNetworkingRequested(t *testing.T) { } func TestSetupNetworkNamespace(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } assert := assert.New(t) diff --git a/pkg/katautils/utils_test.go b/pkg/katautils/utils_test.go index af2238894..c7365eeb3 100644 --- a/pkg/katautils/utils_test.go +++ b/pkg/katautils/utils_test.go @@ -26,9 +26,6 @@ const ( testDirMode = os.FileMode(0750) testFileMode = os.FileMode(0640) - testDisabledNeedRoot = "Test disabled as requires root user" - testDisabledNeedNonRoot = "Test disabled as requires non-root user" - // small docker image used to create root filesystems from testDockerImage = "busybox" @@ -38,7 +35,9 @@ const ( specConfig = "config.json" ) -var testDir = "" +var ( + testDir = "" +) func init() { var err error diff --git a/virtcontainers/api_test.go b/virtcontainers/api_test.go index b8e3c35bf..a3895a885 100644 --- a/virtcontainers/api_test.go +++ b/virtcontainers/api_test.go @@ -15,6 +15,7 @@ import ( "syscall" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" "github.com/kata-containers/runtime/virtcontainers/pkg/mock" vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" @@ -132,7 +133,7 @@ func TestCreateSandboxNoopAgentSuccessful(t *testing.T) { } func TestCreateSandboxKataAgentSuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -212,7 +213,7 @@ func TestDeleteSandboxNoopAgentSuccessful(t *testing.T) { } func TestDeleteSandboxKataAgentSuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -287,7 +288,7 @@ func TestStartSandboxNoopAgentSuccessful(t *testing.T) { } func TestStartSandboxKataAgentSuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -421,7 +422,7 @@ func TestPauseThenResumeSandboxNoopAgentSuccessful(t *testing.T) { } func TestStopSandboxKataAgentSuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -491,7 +492,7 @@ func TestRunSandboxNoopAgentSuccessful(t *testing.T) { } func TestRunSandboxKataAgentSuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -1705,7 +1706,7 @@ func TestReleaseSandbox(t *testing.T) { } func TestUpdateContainer(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -1758,7 +1759,7 @@ func TestUpdateContainer(t *testing.T) { } func TestPauseResumeContainer(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -1800,7 +1801,7 @@ func TestPauseResumeContainer(t *testing.T) { } func TestNetworkOperation(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } diff --git a/virtcontainers/container_test.go b/virtcontainers/container_test.go index 333d2e488..3c5600053 100644 --- a/virtcontainers/container_test.go +++ b/virtcontainers/container_test.go @@ -16,6 +16,7 @@ import ( "syscall" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/virtcontainers/device/api" "github.com/kata-containers/runtime/virtcontainers/device/config" "github.com/kata-containers/runtime/virtcontainers/device/drivers" @@ -138,7 +139,7 @@ func TestContainerRemoveDrive(t *testing.T) { } func testSetupFakeRootfs(t *testing.T) (testRawFile, loopDev, mntDir string, err error) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -206,7 +207,7 @@ func cleanupFakeRootfsSetup(testRawFile, loopDev, mntDir string) { } func TestContainerAddDriveDir(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } diff --git a/virtcontainers/mount_test.go b/virtcontainers/mount_test.go index 71d109483..64cbe61b6 100644 --- a/virtcontainers/mount_test.go +++ b/virtcontainers/mount_test.go @@ -9,7 +9,6 @@ import ( "bytes" "context" "fmt" - "github.com/stretchr/testify/assert" "io/ioutil" "os" "os/exec" @@ -18,13 +17,21 @@ import ( "strings" "syscall" "testing" + + ktu "github.com/kata-containers/runtime/pkg/katatestutils" + "github.com/stretchr/testify/assert" ) const ( - testDisabledNeedRoot = "Test disabled as requires root user" - testDirMode = os.FileMode(0750) + testDirMode = os.FileMode(0750) ) +var tc ktu.TestConstraint + +func init() { + tc = ktu.NewTestConstraint(false) +} + func TestIsSystemMount(t *testing.T) { tests := []struct { mnt string @@ -70,8 +77,8 @@ func TestIsHostDevice(t *testing.T) { } func TestIsHostDeviceCreateFile(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledAsNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } // Create regular file in /dev @@ -196,8 +203,8 @@ func TestGetDeviceForPath(t *testing.T) { } func TestGetDeviceForPathBindMount(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledAsNonRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } source := filepath.Join(testDir, "testDeviceDirSrc") @@ -301,8 +308,8 @@ func TestIsDockerVolume(t *testing.T) { } func TestIsEphemeralStorage(t *testing.T) { - if os.Geteuid() != 0 { - t.Skip(testDisabledNeedRoot) + if tc.NotValid(ktu.NeedRoot()) { + t.Skip(ktu.TestDisabledNeedRoot) } dir, err := ioutil.TempDir(testDir, "foo") diff --git a/virtcontainers/network_test.go b/virtcontainers/network_test.go index c1d4804f4..cc9ccacd3 100644 --- a/virtcontainers/network_test.go +++ b/virtcontainers/network_test.go @@ -11,13 +11,14 @@ import ( "reflect" "testing" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types" "github.com/stretchr/testify/assert" "github.com/vishvananda/netlink" ) func TestCreateDeleteNetNS(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -175,7 +176,7 @@ func TestGenerateRandomPrivateMacAdd(t *testing.T) { } func TestCreateGetBridgeLink(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -199,7 +200,7 @@ func TestCreateGetBridgeLink(t *testing.T) { } func TestCreateGetTunTapLink(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -224,7 +225,7 @@ func TestCreateGetTunTapLink(t *testing.T) { } func TestCreateMacVtap(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -268,7 +269,7 @@ func TestCreateMacVtap(t *testing.T) { } func TestTcRedirectNetwork(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } diff --git a/virtcontainers/physical_endpoint_test.go b/virtcontainers/physical_endpoint_test.go index f22a8f152..12aa39ed9 100644 --- a/virtcontainers/physical_endpoint_test.go +++ b/virtcontainers/physical_endpoint_test.go @@ -7,10 +7,10 @@ package virtcontainers import ( "net" - "os" "testing" "github.com/containernetworking/plugins/pkg/ns" + ktu "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/stretchr/testify/assert" "github.com/vishvananda/netlink" "github.com/vishvananda/netns" @@ -43,7 +43,7 @@ func TestPhysicalEndpoint_HotDetach(t *testing.T) { } func TestIsPhysicalIface(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } diff --git a/virtcontainers/syscall_test.go b/virtcontainers/syscall_test.go index b13608843..685f79d7e 100644 --- a/virtcontainers/syscall_test.go +++ b/virtcontainers/syscall_test.go @@ -12,6 +12,8 @@ import ( "path/filepath" "syscall" "testing" + + ktu "github.com/kata-containers/runtime/pkg/katatestutils" ) func TestBindMountInvalidSourceSymlink(t *testing.T) { @@ -47,7 +49,7 @@ func TestBindMountFailingMount(t *testing.T) { } func TestBindMountSuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) } @@ -76,7 +78,7 @@ func TestBindMountSuccessful(t *testing.T) { } func TestBindMountReadonlySuccessful(t *testing.T) { - if os.Geteuid() != 0 { + if tc.NotValid(ktu.NeedRoot()) { t.Skip(testDisabledAsNonRoot) }