2015-03-18 15:59:26 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-06 10:18:01 -04:00
|
|
|
"bytes"
|
2015-03-18 15:59:26 -04:00
|
|
|
"os/exec"
|
2015-04-06 09:21:18 -04:00
|
|
|
"strings"
|
2015-03-18 15:59:26 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2015-03-18 15:59:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// non-blocking wait with 0 exit code
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "true")
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-10-01 12:18:52 -04:00
|
|
|
if err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second); err != nil {
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Fatal("Container should have stopped by now")
|
2015-03-18 15:59:26 -04:00
|
|
|
}
|
|
|
|
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
if strings.TrimSpace(out) != "0" {
|
|
|
|
c.Fatal("failed to set up container", out)
|
2015-03-18 15:59:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// blocking wait with 0 exit code
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
|
2015-10-01 12:18:52 -04:00
|
|
|
// Windows busybox does not support trap in this way, not sleep with sub-second
|
|
|
|
// granularity. It will always exit 0x40010004.
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-01 12:18:52 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do usleep 10; done")
|
2015-04-21 21:51:41 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(containerID), check.IsNil)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
chWait := make(chan string)
|
|
|
|
go func() {
|
|
|
|
out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
|
|
|
|
chWait <- out
|
|
|
|
}()
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
dockerCmd(c, "stop", containerID)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case status := <-chWait:
|
|
|
|
if strings.TrimSpace(status) != "0" {
|
|
|
|
c.Fatalf("expected exit 0, got %s", status)
|
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
c.Fatal("timeout waiting for `docker wait` to exit")
|
2015-03-18 15:59:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-blocking wait with random exit code
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99")
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-10-01 12:18:52 -04:00
|
|
|
if err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second); err != nil {
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Fatal("Container should have stopped by now")
|
2015-03-18 15:59:26 -04:00
|
|
|
}
|
|
|
|
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
|
|
|
if strings.TrimSpace(out) != "99" {
|
|
|
|
c.Fatal("failed to set up container", out)
|
2015-03-18 15:59:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// blocking wait with random exit code
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
|
2015-10-01 12:18:52 -04:00
|
|
|
// Cannot run on Windows as trap in Windows busybox does not support trap in this way.
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-10-01 12:18:52 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do usleep 10; done")
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(containerID), check.IsNil)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-05-06 10:18:01 -04:00
|
|
|
chWait := make(chan error)
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", containerID)
|
|
|
|
waitCmdOut := bytes.NewBuffer(nil)
|
|
|
|
waitCmd.Stdout = waitCmdOut
|
|
|
|
if err := waitCmd.Start(); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
go func() {
|
2015-05-06 10:18:01 -04:00
|
|
|
chWait <- waitCmd.Wait()
|
2015-04-21 21:51:41 -04:00
|
|
|
}()
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
dockerCmd(c, "stop", containerID)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
select {
|
2015-05-06 10:18:01 -04:00
|
|
|
case err := <-chWait:
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
status, err := waitCmdOut.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
2015-04-21 21:51:41 -04:00
|
|
|
if strings.TrimSpace(status) != "99" {
|
|
|
|
c.Fatalf("expected exit 99, got %s", status)
|
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
2015-05-06 10:18:01 -04:00
|
|
|
waitCmd.Process.Kill()
|
2015-04-21 21:51:41 -04:00
|
|
|
c.Fatal("timeout waiting for `docker wait` to exit")
|
|
|
|
}
|
2015-03-18 15:59:26 -04:00
|
|
|
}
|