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
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
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-09 13:03:24 -04:00
|
|
|
err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil) //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)
|
2015-10-09 13:03:24 -04:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "0", check.Commentf("failed to set up container, %v", 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-10-09 13:03:24 -04:00
|
|
|
c.Assert(waitRun(containerID), checker.IsNil)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
chWait := make(chan string)
|
|
|
|
go func() {
|
2016-03-24 12:43:04 -04:00
|
|
|
chWait <- ""
|
2015-04-21 21:51:41 -04:00
|
|
|
out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
|
|
|
|
chWait <- out
|
|
|
|
}()
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2016-03-24 12:43:04 -04:00
|
|
|
<-chWait // make sure the goroutine is started
|
2015-04-21 21:51:41 -04:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
dockerCmd(c, "stop", containerID)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case status := <-chWait:
|
2015-10-09 13:03:24 -04:00
|
|
|
c.Assert(strings.TrimSpace(status), checker.Equals, "0", check.Commentf("expected exit 0, got %s", status))
|
2015-04-21 21:51:41 -04:00
|
|
|
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-09 13:03:24 -04:00
|
|
|
err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil) //Container should have stopped by now
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ = dockerCmd(c, "wait", containerID)
|
2015-10-09 13:03:24 -04:00
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, "99", check.Commentf("failed to set up container, %v", 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-10-09 13:03:24 -04:00
|
|
|
c.Assert(waitRun(containerID), checker.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
|
2015-10-09 13:03:24 -04:00
|
|
|
c.Assert(waitCmd.Start(), checker.IsNil)
|
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:
|
2016-03-24 12:43:04 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(waitCmdOut.String()))
|
2015-05-06 10:18:01 -04:00
|
|
|
status, err := waitCmdOut.ReadString('\n')
|
2015-10-09 13:03:24 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(status), checker.Equals, "99", check.Commentf("expected exit 99, got %s", status))
|
2015-04-21 21:51:41 -04:00
|
|
|
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
|
|
|
}
|