forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_cli_wait_test.go
95 lines (76 loc) · 3.12 KB
/
docker_cli_wait_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"bytes"
"os/exec"
"strings"
"time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
// non-blocking wait with 0 exit code
func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "true")
containerID := strings.TrimSpace(out)
err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
c.Assert(err, checker.IsNil) //Container should have stopped by now
out, _ = dockerCmd(c, "wait", containerID)
c.Assert(strings.TrimSpace(out), checker.Equals, "0", check.Commentf("failed to set up container, %v", out))
}
// blocking wait with 0 exit code
func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
// Windows busybox does not support trap in this way, not sleep with sub-second
// granularity. It will always exit 0x40010004.
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do usleep 10; done")
containerID := strings.TrimSpace(out)
c.Assert(waitRun(containerID), checker.IsNil)
chWait := make(chan string)
go func() {
out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
chWait <- out
}()
time.Sleep(100 * time.Millisecond)
dockerCmd(c, "stop", containerID)
select {
case status := <-chWait:
c.Assert(strings.TrimSpace(status), checker.Equals, "0", check.Commentf("expected exit 0, got %s", status))
case <-time.After(2 * time.Second):
c.Fatal("timeout waiting for `docker wait` to exit")
}
}
// non-blocking wait with random exit code
func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99")
containerID := strings.TrimSpace(out)
err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
c.Assert(err, checker.IsNil) //Container should have stopped by now
out, _ = dockerCmd(c, "wait", containerID)
c.Assert(strings.TrimSpace(out), checker.Equals, "99", check.Commentf("failed to set up container, %v", out))
}
// blocking wait with random exit code
func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
// Cannot run on Windows as trap in Windows busybox does not support trap in this way.
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do usleep 10; done")
containerID := strings.TrimSpace(out)
c.Assert(waitRun(containerID), checker.IsNil)
chWait := make(chan error)
waitCmd := exec.Command(dockerBinary, "wait", containerID)
waitCmdOut := bytes.NewBuffer(nil)
waitCmd.Stdout = waitCmdOut
c.Assert(waitCmd.Start(), checker.IsNil)
go func() {
chWait <- waitCmd.Wait()
}()
dockerCmd(c, "stop", containerID)
select {
case err := <-chWait:
c.Assert(err, checker.IsNil)
status, err := waitCmdOut.ReadString('\n')
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(status), checker.Equals, "99", check.Commentf("expected exit 99, got %s", status))
case <-time.After(2 * time.Second):
waitCmd.Process.Kill()
c.Fatal("timeout waiting for `docker wait` to exit")
}
}