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
|
|
|
|
|
|
|
status := "true"
|
2015-07-20 02:44:22 -04:00
|
|
|
var err error
|
2015-03-18 15:59:26 -04:00
|
|
|
for i := 0; status != "false"; i++ {
|
2015-05-17 22:06:13 -04:00
|
|
|
status, err = inspectField(containerID, "State.Running")
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
if i >= 60 {
|
2015-04-18 12:46:47 -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-05-06 10:18:01 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do sleep 0.01; done")
|
2015-04-21 21:51:41 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
2015-04-21 21:51:41 -04:00
|
|
|
if err := waitRun(containerID); err != nil {
|
|
|
|
c.Fatal(err)
|
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
|
|
|
|
|
|
|
status := "true"
|
2015-07-20 02:44:22 -04:00
|
|
|
var err error
|
2015-03-18 15:59:26 -04:00
|
|
|
for i := 0; status != "false"; i++ {
|
2015-05-17 22:06:13 -04:00
|
|
|
status, err = inspectField(containerID, "State.Running")
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-18 15:59:26 -04:00
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
if i >= 60 {
|
2015-04-18 12:46:47 -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-05-06 10:18:01 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do sleep 0.01; done")
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-04-21 21:51:41 -04:00
|
|
|
if err := waitRun(containerID); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := waitRun(containerID); err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
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
|
|
|
}
|