1401b8fe |
package main
import ( |
d2c4ee37 |
"bytes" |
1401b8fe |
"os/exec" |
475c6531 |
"strings" |
1401b8fe |
"time" |
dc944ea7 |
|
33968e6c |
"github.com/docker/docker/integration-cli/checker" |
dc944ea7 |
"github.com/go-check/check" |
92427b3a |
"github.com/gotestyourself/gotestyourself/icmd" |
1401b8fe |
)
// non-blocking wait with 0 exit code |
dc944ea7 |
func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) { |
eef6eda7 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "true") |
475c6531 |
containerID := strings.TrimSpace(out) |
1401b8fe |
|
19c30447 |
err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
c.Assert(err, checker.IsNil) //Container should have stopped by now |
1401b8fe |
|
eef6eda7 |
out, _ = dockerCmd(c, "wait", containerID) |
19c30447 |
c.Assert(strings.TrimSpace(out), checker.Equals, "0", check.Commentf("failed to set up container, %v", out)) |
1401b8fe |
}
// blocking wait with 0 exit code |
dc944ea7 |
func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) { |
e666dfff |
// Windows busybox does not support trap in this way, not sleep with sub-second
// granularity. It will always exit 0x40010004. |
f9a3558a |
testRequires(c, DaemonIsLinux) |
e666dfff |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do usleep 10; done") |
9e0ffae8 |
containerID := strings.TrimSpace(out) |
1401b8fe |
|
19c30447 |
c.Assert(waitRun(containerID), checker.IsNil) |
1401b8fe |
|
9e0ffae8 |
chWait := make(chan string)
go func() { |
42df9edc |
chWait <- "" |
87e3fcfe |
out := icmd.RunCommand(dockerBinary, "wait", containerID).Combined() |
9e0ffae8 |
chWait <- out
}() |
1401b8fe |
|
42df9edc |
<-chWait // make sure the goroutine is started |
9e0ffae8 |
time.Sleep(100 * time.Millisecond)
dockerCmd(c, "stop", containerID)
select {
case status := <-chWait: |
19c30447 |
c.Assert(strings.TrimSpace(status), checker.Equals, "0", check.Commentf("expected exit 0, got %s", status)) |
9e0ffae8 |
case <-time.After(2 * time.Second):
c.Fatal("timeout waiting for `docker wait` to exit") |
1401b8fe |
}
}
// non-blocking wait with random exit code |
dc944ea7 |
func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) { |
eef6eda7 |
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99") |
475c6531 |
containerID := strings.TrimSpace(out) |
1401b8fe |
|
19c30447 |
err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
c.Assert(err, checker.IsNil) //Container should have stopped by now |
eef6eda7 |
out, _ = dockerCmd(c, "wait", containerID) |
19c30447 |
c.Assert(strings.TrimSpace(out), checker.Equals, "99", check.Commentf("failed to set up container, %v", out)) |
1401b8fe |
}
// blocking wait with random exit code |
dc944ea7 |
func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) { |
e666dfff |
// Cannot run on Windows as trap in Windows busybox does not support trap in this way. |
f9a3558a |
testRequires(c, DaemonIsLinux) |
e666dfff |
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do usleep 10; done") |
475c6531 |
containerID := strings.TrimSpace(out) |
19c30447 |
c.Assert(waitRun(containerID), checker.IsNil) |
1401b8fe |
|
d2c4ee37 |
chWait := make(chan error)
waitCmd := exec.Command(dockerBinary, "wait", containerID)
waitCmdOut := bytes.NewBuffer(nil)
waitCmd.Stdout = waitCmdOut |
19c30447 |
c.Assert(waitCmd.Start(), checker.IsNil) |
9e0ffae8 |
go func() { |
d2c4ee37 |
chWait <- waitCmd.Wait() |
9e0ffae8 |
}() |
1401b8fe |
|
9e0ffae8 |
dockerCmd(c, "stop", containerID) |
1401b8fe |
|
9e0ffae8 |
select { |
d2c4ee37 |
case err := <-chWait: |
42df9edc |
c.Assert(err, checker.IsNil, check.Commentf(waitCmdOut.String())) |
d2c4ee37 |
status, err := waitCmdOut.ReadString('\n') |
19c30447 |
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(status), checker.Equals, "99", check.Commentf("expected exit 99, got %s", status)) |
9e0ffae8 |
case <-time.After(2 * time.Second): |
d2c4ee37 |
waitCmd.Process.Kill() |
9e0ffae8 |
c.Fatal("timeout waiting for `docker wait` to exit")
} |
1401b8fe |
} |