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