Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
tests: Update test code to use test constraints
Browse files Browse the repository at this point in the history
Updated the test code to use the new test constraints feature.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed May 2, 2019
1 parent 586efb1 commit 9053172
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 89 deletions.
21 changes: 11 additions & 10 deletions cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions cli/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cli/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 3 additions & 2 deletions cli/kata-check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("", "")
Expand Down
11 changes: 7 additions & 4 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions containerd-shim-v2/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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("", "")
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions containerd-shim-v2/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion netmon/netmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/katautils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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-")
Expand All @@ -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-")
Expand All @@ -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-")
Expand Down
15 changes: 11 additions & 4 deletions pkg/katautils/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 9053172

Please sign in to comment.