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

Commit

Permalink
tests: Stop bundle creation timing out tests
Browse files Browse the repository at this point in the history
Previously, makeOCIBundle() would create a fresh bundle every time it
was called. However, since it uses docker to generate the rootfs, that
could take a while on slow systems meaning the tests could timeout.

To overcome this issue, the tests now create a full pristine bundle at
startup and makeOCIBundle() now just copies the pristine bundle to the
location requested by the calling test.

This change also removes the requirement that the bundle directory exist
before calling makeOCIBundle().

Fixes #461.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Aug 25, 2017
1 parent 438daca commit de7cb39
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 68 deletions.
42 changes: 0 additions & 42 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ func TestCreateInvalidArgs(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -329,9 +326,6 @@ func TestCreateInvalidConfigJSON(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -376,9 +370,6 @@ func TestCreateInvalidContainerType(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -426,9 +417,6 @@ func TestCreateContainerInvalid(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -489,9 +477,6 @@ func TestCreateProcessCgroupsPathFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -582,9 +567,6 @@ func TestCreateCreateCgroupsFilesFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -670,9 +652,6 @@ func TestCreateCreateCreatePidFileFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -744,9 +723,6 @@ func TestCreate(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -800,9 +776,6 @@ func TestCreateInvalidKernelParams(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -864,9 +837,6 @@ func TestCreateCreatePodPodConfigFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -914,9 +884,6 @@ func TestCreateCreatePodFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -949,9 +916,6 @@ func TestCreateCreateContainerContainerConfigFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -996,9 +960,6 @@ func TestCreateCreateContainerFail(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down Expand Up @@ -1047,9 +1008,6 @@ func TestCreateCreateContainer(t *testing.T) {

bundlePath := filepath.Join(tmpdir, "bundle")

err = os.MkdirAll(bundlePath, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundlePath)
assert.NoError(err)

Expand Down
86 changes: 60 additions & 26 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ const (
NoopAgentType vc.AgentType = "noop"
)

// package variables set in TestMain
var testDir = ""
var (
// package variables set by calling TestMain()
testDir = ""
testBundleDir = ""
)

// testingImpl is a concrete mock RVC implementation used for testing
var testingImpl = &vcMock.VCMock{}
Expand All @@ -66,10 +69,32 @@ func init() {
fmt.Printf("INFO: switching to fake virtcontainers implementation for testing\n")
vci = testingImpl

var err error

fmt.Printf("INFO: creating test directory\n")
testDir, err = ioutil.TempDir("", fmt.Sprintf("%s-", name))
if err != nil {
panic(err)
}

fmt.Printf("INFO: test directory is %v\n", testDir)

// Do this now to avoid hitting the test timeout value due to
// slow network response.
fmt.Printf("INFO: ensuring required docker image (%v) is available\n", testDockerImage)
_, err := runCommand([]string{"docker", "pull", testDockerImage})
_, err = runCommand([]string{"docker", "pull", testDockerImage})
if err != nil {
panic(err)
}

testBundleDir = filepath.Join(testDir, testBundle)
err = os.MkdirAll(testBundleDir, testDirMode)
if err != nil {
panic(err)
}

fmt.Printf("INFO: creating OCI bundle in %v for tests to use\n", testBundleDir)
err = realMakeOCIBundle(testBundleDir)
if err != nil {
panic(err)
}
Expand All @@ -86,19 +111,6 @@ var testContainerAnnotations = map[string]string{
}

func runUnitTests(m *testing.M) {
var err error

testDir, err = ioutil.TempDir("", fmt.Sprintf("%s-", name))
if err != nil {
panic(err)
}

err = os.MkdirAll(testDir, testDirMode)
if err != nil {
fmt.Printf("Could not create test directory %s: %s\n", testDir, err)
os.Exit(1)
}

ret := m.Run()

os.RemoveAll(testDir)
Expand Down Expand Up @@ -328,7 +340,7 @@ func createRootfs(dir string) error {

// makeOCIBundle will create an OCI bundle (including the "config.json"
// config file) in the directory specified (which must already exist).
func makeOCIBundle(bundleDir string) error {
func realMakeOCIBundle(bundleDir string) error {
if bundleDir == "" {
return errors.New("BUG: Need bundle directory")
}
Expand All @@ -355,8 +367,7 @@ func makeOCIBundle(bundleDir string) error {
rootfsDir := filepath.Join(bundleDir, ociRootPath)

if strings.HasPrefix(ociRootPath, "/") {
// Absolute path
rootfsDir = ociRootPath
return fmt.Errorf("Cannot handle absolute rootfs as bundle must be unique to each test")
}

err = createRootfs(rootfsDir)
Expand All @@ -367,6 +378,36 @@ func makeOCIBundle(bundleDir string) error {
return nil
}

// Create an OCI bundle in the specified directory.
//
// Note that the directory will be created, but it's parent is expected to exist.
//
// This function works by copying the already-created test bundle. Ideally,
// the bundle would be recreated for each test, but createRootfs() uses
// docker which on some systems is too slow, resulting in the tests timing
// out.
func makeOCIBundle(bundleDir string) error {
from := testBundleDir
to := bundleDir

// only the basename of bundleDir needs to exist as bundleDir
// will get created by cp(1).
base := filepath.Dir(bundleDir)

for _, dir := range []string{from, base} {
if !fileExists(dir) {
return fmt.Errorf("BUG: directory %v should exist", dir)
}
}

output, err := runCommandFull([]string{"cp", "-a", from, to}, true)
if err != nil {
return fmt.Errorf("failed to copy test OCI bundle from %v to %v: %v (output: %v)", from, to, err, output)
}

return nil
}

// readOCIConfig returns an OCI spec.
func readOCIConfigFile(configPath string) (oci.CompatOCISpec, error) {
if configPath == "" {
Expand Down Expand Up @@ -408,13 +449,6 @@ func TestMakeOCIBundle(t *testing.T) {

bundleDir := filepath.Join(tmpdir, "bundle")

err = makeOCIBundle(bundleDir)
// ENOENT
assert.Error(err)

err = os.MkdirAll(bundleDir, testDirMode)
assert.NoError(err)

err = makeOCIBundle(bundleDir)
assert.NoError(err)

Expand Down

0 comments on commit de7cb39

Please sign in to comment.