-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_cli_wait_test.go
142 lines (115 loc) · 3.37 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bytes"
"os/exec"
"strings"
"time"
"github.com/go-check/check"
)
// non-blocking wait with 0 exit code
func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
containerID := strings.TrimSpace(out)
status := "true"
for i := 0; status != "false"; i++ {
status, err = inspectField(containerID, "State.Running")
c.Assert(err, check.IsNil)
time.Sleep(time.Second)
if i >= 60 {
c.Fatal("Container should have stopped by now")
}
}
runCmd = exec.Command(dockerBinary, "wait", containerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil || strings.TrimSpace(out) != "0" {
c.Fatal("failed to set up container", out, err)
}
}
// blocking wait with 0 exit code
func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do sleep 0.01; done")
containerID := strings.TrimSpace(out)
if err := waitRun(containerID); err != nil {
c.Fatal(err)
}
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:
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")
}
}
// non-blocking wait with random exit code
func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
containerID := strings.TrimSpace(out)
status := "true"
for i := 0; status != "false"; i++ {
status, err = inspectField(containerID, "State.Running")
c.Assert(err, check.IsNil)
time.Sleep(time.Second)
if i >= 60 {
c.Fatal("Container should have stopped by now")
}
}
runCmd = exec.Command(dockerBinary, "wait", containerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil || strings.TrimSpace(out) != "99" {
c.Fatal("failed to set up container", out, err)
}
}
// blocking wait with random exit code
func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do sleep 0.01; done")
containerID := strings.TrimSpace(out)
if err := waitRun(containerID); err != nil {
c.Fatal(err)
}
if err := waitRun(containerID); err != nil {
c.Fatal(err)
}
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)
}
go func() {
chWait <- waitCmd.Wait()
}()
dockerCmd(c, "stop", containerID)
select {
case err := <-chWait:
if err != nil {
c.Fatal(err)
}
status, err := waitCmdOut.ReadString('\n')
if err != nil {
c.Fatal(err)
}
if strings.TrimSpace(status) != "99" {
c.Fatalf("expected exit 99, got %s", status)
}
case <-time.After(2 * time.Second):
waitCmd.Process.Kill()
c.Fatal("timeout waiting for `docker wait` to exit")
}
}