diff options
author | Fabricio Voznika <fvoznika@google.com> | 2018-09-27 22:52:25 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-09-27 22:53:18 -0700 |
commit | 1166c088fc51c83af3198e25d5e774103ae976fc (patch) | |
tree | e1785f3b783ee36c9e08c9669d1f77e0a6c30d78 /runsc/container | |
parent | b709d239870143102cf4e44b65cc26cea78a6ccb (diff) |
Move common test code to function
PiperOrigin-RevId: 214890335
Change-Id: I42743f0ce46a5a42834133bce2f32d187194fc87
Diffstat (limited to 'runsc/container')
-rw-r--r-- | runsc/container/container_test.go | 6 | ||||
-rw-r--r-- | runsc/container/multi_container_test.go | 256 |
2 files changed, 80 insertions, 182 deletions
diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index de1e50a3f..c71bcc46d 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -62,7 +62,8 @@ func waitForProcessList(cont *Container, want []*control.Process) error { } return nil } - return testutil.Poll(cb, 5*time.Second) + // Gives plenty of time as tests can run slow under --race. + return testutil.Poll(cb, 30*time.Second) } func waitForProcessCount(cont *Container, want int) error { @@ -77,7 +78,8 @@ func waitForProcessCount(cont *Container, want int) error { } return nil } - return testutil.Poll(cb, 5*time.Second) + // Gives plenty of time as tests can run slow under --race. + return testutil.Poll(cb, 30*time.Second) } // procListsEqual is used to check whether 2 Process lists are equal for all diff --git a/runsc/container/multi_container_test.go b/runsc/container/multi_container_test.go index dc938066b..8c98bed22 100644 --- a/runsc/container/multi_container_test.go +++ b/runsc/container/multi_container_test.go @@ -15,6 +15,7 @@ package container import ( + "fmt" "io/ioutil" "math" "os" @@ -56,38 +57,60 @@ func createSpecs(cmds ...[]string) ([]*specs.Spec, []string) { return specs, ids } +func startContainers(conf *boot.Config, specs []*specs.Spec, ids []string) ([]*Container, func(), error) { + rootDir, err := testutil.SetupRootDir() + if err != nil { + return nil, nil, fmt.Errorf("error creating root dir: %v", err) + } + + var containers []*Container + var bundles []string + cleanup := func() { + for _, c := range containers { + c.Destroy() + } + for _, b := range bundles { + os.RemoveAll(b) + } + os.RemoveAll(rootDir) + } + for i, spec := range specs { + bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) + if err != nil { + cleanup() + return nil, nil, fmt.Errorf("error setting up container: %v", err) + } + bundles = append(bundles, bundleDir) + + cont, err := Create(ids[i], spec, conf, bundleDir, "", "") + if err != nil { + cleanup() + return nil, nil, fmt.Errorf("error creating container: %v", err) + } + containers = append(containers, cont) + + if err := cont.Start(conf); err != nil { + cleanup() + return nil, nil, fmt.Errorf("error starting container: %v", err) + } + } + return containers, cleanup, nil +} + // TestMultiContainerSanity checks that it is possible to run 2 dead-simple // containers in the same sandbox. func TestMultiContainerSanity(t *testing.T) { for _, conf := range configs(all...) { t.Logf("Running test with conf: %+v", conf) - rootDir, err := testutil.SetupRootDir() - if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - // Setup the containers. sleep := []string{"sleep", "100"} specs, ids := createSpecs(sleep, sleep) - var containers []*Container - for i, spec := range specs { - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + containers, cleanup, err := startContainers(conf, specs, ids) + if err != nil { + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Check via ps that multiple processes are running. expectedPL := []*control.Process{ @@ -106,37 +129,18 @@ func TestMultiContainerSanity(t *testing.T) { } func TestMultiContainerWait(t *testing.T) { - rootDir, err := testutil.SetupRootDir() - if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - // The first container should run the entire duration of the test. cmd1 := []string{"sleep", "100"} // We'll wait on the second container, which is much shorter lived. cmd2 := []string{"sleep", "1"} specs, ids := createSpecs(cmd1, cmd2) - // Setup the containers. - var containers []*Container - for i, spec := range specs { - conf := testutil.TestConfig() - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + conf := testutil.TestConfig() + containers, cleanup, err := startContainers(conf, specs, ids) + if err != nil { + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Check via ps that multiple processes are running. expectedPL := []*control.Process{ @@ -206,26 +210,12 @@ func TestExecWait(t *testing.T) { // We'll wait on the second container, which is much shorter lived. cmd2 := []string{"sleep", "1"} specs, ids := createSpecs(cmd1, cmd2) - - // Setup the containers. - var containers []*Container - for i, spec := range specs { - conf := testutil.TestConfig() - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + conf := testutil.TestConfig() + containers, cleanup, err := startContainers(conf, specs, ids) + if err != nil { + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Check via ps that process is running. expectedPL := []*control.Process{ @@ -284,12 +274,6 @@ func TestExecWait(t *testing.T) { // TestMultiContainerMount tests that bind mounts can be used with multiple // containers. func TestMultiContainerMount(t *testing.T) { - rootDir, err := testutil.SetupRootDir() - if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - cmd1 := []string{"sleep", "100"} // 'src != dst' ensures that 'dst' doesn't exist in the host and must be @@ -309,24 +293,12 @@ func TestMultiContainerMount(t *testing.T) { }) // Setup the containers. - var containers []*Container - for i, spec := range sps { - conf := testutil.TestConfig() - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + conf := testutil.TestConfig() + containers, cleanup, err := startContainers(conf, sps, ids) + if err != nil { + t.Fatalf("error starting containers: %v", err) } + defer cleanup() ws, err := containers[1].Wait() if err != nil { @@ -343,32 +315,14 @@ func TestMultiContainerSignal(t *testing.T) { for _, conf := range configs(all...) { t.Logf("Running test with conf: %+v", conf) - rootDir, err := testutil.SetupRootDir() - if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - // Setup the containers. sleep := []string{"sleep", "100"} specs, ids := createSpecs(sleep, sleep) - var containers []*Container - for i, spec := range specs { - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + containers, cleanup, err := startContainers(conf, specs, ids) + if err != nil { + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Check via ps that container 1 process is running. expectedPL := []*control.Process{ @@ -452,34 +406,14 @@ func TestMultiContainerDestroy(t *testing.T) { for _, conf := range configs(all...) { t.Logf("Running test with conf: %+v", conf) - rootDir, err := testutil.SetupRootDir() - if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - // Two containers that will run for a long time. We will // destroy the second one. specs, ids := createSpecs([]string{"sleep", "100"}, []string{"sleep", "100"}) - - // Setup the containers. - var containers []*Container - for i, spec := range specs { - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + containers, cleanup, err := startContainers(conf, specs, ids) + if err != nil { + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Exec in the root container to check for the existence of the // second containers root filesystem directory. @@ -519,31 +453,12 @@ func TestMultiContainerProcesses(t *testing.T) { specs, ids := createSpecs( []string{"sleep", "100"}, []string{"sh", "-c", "while true; do sleep 100; done"}) - - rootDir, err := testutil.SetupRootDir() + conf := testutil.TestConfig() + containers, cleanup, err := startContainers(conf, specs, ids) if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - - var containers []*Container - for i, spec := range specs { - conf := testutil.TestConfig() - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Check root's container process list doesn't include other containers. expectedPL0 := []*control.Process{ @@ -592,31 +507,12 @@ func TestMultiContainerKillAll(t *testing.T) { specs, ids := createSpecs( []string{app, "task-tree", "--depth=2", "--width=2"}, []string{app, "task-tree", "--depth=4", "--width=2"}) - - rootDir, err := testutil.SetupRootDir() + conf := testutil.TestConfig() + containers, cleanup, err := startContainers(conf, specs, ids) if err != nil { - t.Fatalf("error creating root dir: %v", err) - } - defer os.RemoveAll(rootDir) - - var containers []*Container - for i, spec := range specs { - conf := testutil.TestConfig() - bundleDir, err := testutil.SetupContainerInRoot(rootDir, spec, conf) - if err != nil { - t.Fatalf("error setting up container: %v", err) - } - defer os.RemoveAll(bundleDir) - cont, err := Create(ids[i], spec, conf, bundleDir, "", "") - if err != nil { - t.Fatalf("error creating container: %v", err) - } - defer cont.Destroy() - if err := cont.Start(conf); err != nil { - t.Fatalf("error starting container: %v", err) - } - containers = append(containers, cont) + t.Fatalf("error starting containers: %v", err) } + defer cleanup() // Wait until all processes are created. rootProcCount := int(math.Pow(2, 3) - 1) |