summaryrefslogtreecommitdiffhomepage
path: root/runsc/container/multi_container_test.go
diff options
context:
space:
mode:
authorKevin Krakauer <krakauer@google.com>2018-09-06 10:40:53 -0700
committerShentubot <shentubot@google.com>2018-09-06 10:41:55 -0700
commitd95663a6b9831b56602c09f33a9679fa15175b97 (patch)
treed3b72f7719c5ebb51f0be0698934c3203f017907 /runsc/container/multi_container_test.go
parent8f0b6e7fc02919df034dea9e9c9dbab1b80de2be (diff)
runsc testing: Move TestMultiContainerSignal to multi_container_test.
PiperOrigin-RevId: 211831396 Change-Id: Id67f182cb43dccb696180ec967f5b96176f252e0
Diffstat (limited to 'runsc/container/multi_container_test.go')
-rw-r--r--runsc/container/multi_container_test.go100
1 files changed, 100 insertions, 0 deletions
diff --git a/runsc/container/multi_container_test.go b/runsc/container/multi_container_test.go
index 3bdfbaca3..84e0ec080 100644
--- a/runsc/container/multi_container_test.go
+++ b/runsc/container/multi_container_test.go
@@ -15,11 +15,13 @@
package container
import (
+ "fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
+ "syscall"
"testing"
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -237,3 +239,101 @@ func TestMultiContainerMount(t *testing.T) {
t.Error("container failed, waitStatus:", ws)
}
}
+
+// TestMultiContainerSignal checks that it is possible to signal individual
+// containers without killing the entire sandbox.
+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)
+ }
+
+ // Check via ps that multiple processes are running.
+ expectedPL := []*control.Process{
+ {PID: 1, Cmd: "sleep"},
+ {PID: 2, Cmd: "sleep"},
+ }
+
+ if err := waitForProcessList(containers[0], expectedPL); err != nil {
+ t.Errorf("failed to wait for sleep to start: %v", err)
+ }
+
+ // Kill process 2.
+ if err := containers[1].Signal(syscall.SIGKILL); err != nil {
+ t.Errorf("failed to kill process 2: %v", err)
+ }
+
+ // Make sure process 1 is still running.
+ if err := waitForProcessList(containers[0], expectedPL[:1]); err != nil {
+ t.Errorf("failed to wait for sleep to start: %v", err)
+ }
+
+ // Now that process 2 is gone, ensure we get an error trying to
+ // signal it again.
+ if err := containers[1].Signal(syscall.SIGKILL); err == nil {
+ t.Errorf("container %q shouldn't exist, but we were able to signal it", containers[1].ID)
+ }
+
+ // Kill process 1.
+ if err := containers[0].Signal(syscall.SIGKILL); err != nil {
+ t.Errorf("failed to kill process 1: %v", err)
+ }
+
+ if err := waitForSandboxExit(containers[0]); err != nil {
+ t.Errorf("failed to exit sandbox: %v", err)
+ }
+
+ // The sentry should be gone, so signaling should yield an
+ // error.
+ if err := containers[0].Signal(syscall.SIGKILL); err == nil {
+ t.Errorf("sandbox %q shouldn't exist, but we were able to signal it", containers[0].Sandbox.ID)
+ }
+ }
+}
+
+// waitForSandboxExit waits until both the sandbox and gofer processes of the
+// container have exited.
+func waitForSandboxExit(container *Container) error {
+ goferProc, _ := os.FindProcess(container.GoferPid)
+ state, err := goferProc.Wait()
+ if err != nil {
+ return err
+ }
+ if !state.Exited() {
+ return fmt.Errorf("gofer with PID %d failed to exit", container.GoferPid)
+ }
+ sandboxProc, _ := os.FindProcess(container.Sandbox.Pid)
+ state, err = sandboxProc.Wait()
+ if err != nil {
+ return err
+ }
+ if !state.Exited() {
+ return fmt.Errorf("sandbox with PID %d failed to exit", container.Sandbox.Pid)
+ }
+ return nil
+}