2015-03-19 01:29:26 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2015-04-06 09:21:18 -04:00
|
|
|
"strings"
|
2015-03-19 01:29:26 +05:30
|
|
|
"time"
|
2015-04-18 09:46:47 -07:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2015-03-19 01:29:26 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// non-blocking wait with 0 exit code
|
2015-04-18 09:46:47 -07:00
|
|
|
func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal(out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
status := "true"
|
|
|
|
for i := 0; status != "false"; i++ {
|
|
|
|
runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
|
|
|
|
status, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal(status, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
status = strings.TrimSpace(status)
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
if i >= 60 {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal("Container should have stopped by now")
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "wait", containerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
if err != nil || strings.TrimSpace(out) != "0" {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal("failed to set up container", out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// blocking wait with 0 exit code
|
2015-04-18 09:46:47 -07:00
|
|
|
func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal(out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "wait", containerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
if err != nil || strings.TrimSpace(out) != "0" {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal("failed to set up container", out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-blocking wait with random exit code
|
2015-04-18 09:46:47 -07:00
|
|
|
func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal(out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
status := "true"
|
|
|
|
for i := 0; status != "false"; i++ {
|
|
|
|
runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
|
|
|
|
status, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal(status, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
status = strings.TrimSpace(status)
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
if i >= 60 {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal("Container should have stopped by now")
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "wait", containerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
if err != nil || strings.TrimSpace(out) != "99" {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal("failed to set up container", out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// blocking wait with random exit code
|
2015-04-18 09:46:47 -07:00
|
|
|
func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal(out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
2015-04-06 09:21:18 -04:00
|
|
|
containerID := strings.TrimSpace(out)
|
2015-03-19 01:29:26 +05:30
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "wait", containerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
if err != nil || strings.TrimSpace(out) != "99" {
|
2015-04-18 09:46:47 -07:00
|
|
|
c.Fatal("failed to set up container", out, err)
|
2015-03-19 01:29:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|