Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,98 +0,0 @@ |
| 1 |
-package main |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "bytes" |
|
| 5 |
- "os/exec" |
|
| 6 |
- "strings" |
|
| 7 |
- "time" |
|
| 8 |
- |
|
| 9 |
- "github.com/docker/docker/integration-cli/checker" |
|
| 10 |
- "github.com/go-check/check" |
|
| 11 |
- "gotest.tools/icmd" |
|
| 12 |
-) |
|
| 13 |
- |
|
| 14 |
-// non-blocking wait with 0 exit code |
|
| 15 |
-func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
|
|
| 16 |
- out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "true") |
|
| 17 |
- containerID := strings.TrimSpace(out) |
|
| 18 |
- |
|
| 19 |
- err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
|
|
| 20 |
- c.Assert(err, checker.IsNil) //Container should have stopped by now |
|
| 21 |
- |
|
| 22 |
- out, _ = dockerCmd(c, "wait", containerID) |
|
| 23 |
- c.Assert(strings.TrimSpace(out), checker.Equals, "0", check.Commentf("failed to set up container, %v", out))
|
|
| 24 |
- |
|
| 25 |
-} |
|
| 26 |
- |
|
| 27 |
-// blocking wait with 0 exit code |
|
| 28 |
-func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
|
|
| 29 |
- // Windows busybox does not support trap in this way, not sleep with sub-second |
|
| 30 |
- // granularity. It will always exit 0x40010004. |
|
| 31 |
- testRequires(c, DaemonIsLinux) |
|
| 32 |
- out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do usleep 10; done") |
|
| 33 |
- containerID := strings.TrimSpace(out) |
|
| 34 |
- |
|
| 35 |
- c.Assert(waitRun(containerID), checker.IsNil) |
|
| 36 |
- |
|
| 37 |
- chWait := make(chan string) |
|
| 38 |
- go func() {
|
|
| 39 |
- chWait <- "" |
|
| 40 |
- out := icmd.RunCommand(dockerBinary, "wait", containerID).Combined() |
|
| 41 |
- chWait <- out |
|
| 42 |
- }() |
|
| 43 |
- |
|
| 44 |
- <-chWait // make sure the goroutine is started |
|
| 45 |
- time.Sleep(100 * time.Millisecond) |
|
| 46 |
- dockerCmd(c, "stop", containerID) |
|
| 47 |
- |
|
| 48 |
- select {
|
|
| 49 |
- case status := <-chWait: |
|
| 50 |
- c.Assert(strings.TrimSpace(status), checker.Equals, "0", check.Commentf("expected exit 0, got %s", status))
|
|
| 51 |
- case <-time.After(2 * time.Second): |
|
| 52 |
- c.Fatal("timeout waiting for `docker wait` to exit")
|
|
| 53 |
- } |
|
| 54 |
- |
|
| 55 |
-} |
|
| 56 |
- |
|
| 57 |
-// non-blocking wait with random exit code |
|
| 58 |
-func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
|
|
| 59 |
- out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99") |
|
| 60 |
- containerID := strings.TrimSpace(out) |
|
| 61 |
- |
|
| 62 |
- err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
|
|
| 63 |
- c.Assert(err, checker.IsNil) //Container should have stopped by now |
|
| 64 |
- out, _ = dockerCmd(c, "wait", containerID) |
|
| 65 |
- c.Assert(strings.TrimSpace(out), checker.Equals, "99", check.Commentf("failed to set up container, %v", out))
|
|
| 66 |
- |
|
| 67 |
-} |
|
| 68 |
- |
|
| 69 |
-// blocking wait with random exit code |
|
| 70 |
-func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
|
|
| 71 |
- // Cannot run on Windows as trap in Windows busybox does not support trap in this way. |
|
| 72 |
- testRequires(c, DaemonIsLinux) |
|
| 73 |
- out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do usleep 10; done") |
|
| 74 |
- containerID := strings.TrimSpace(out) |
|
| 75 |
- c.Assert(waitRun(containerID), checker.IsNil) |
|
| 76 |
- |
|
| 77 |
- chWait := make(chan error) |
|
| 78 |
- waitCmd := exec.Command(dockerBinary, "wait", containerID) |
|
| 79 |
- waitCmdOut := bytes.NewBuffer(nil) |
|
| 80 |
- waitCmd.Stdout = waitCmdOut |
|
| 81 |
- c.Assert(waitCmd.Start(), checker.IsNil) |
|
| 82 |
- go func() {
|
|
| 83 |
- chWait <- waitCmd.Wait() |
|
| 84 |
- }() |
|
| 85 |
- |
|
| 86 |
- dockerCmd(c, "stop", containerID) |
|
| 87 |
- |
|
| 88 |
- select {
|
|
| 89 |
- case err := <-chWait: |
|
| 90 |
- c.Assert(err, checker.IsNil, check.Commentf(waitCmdOut.String())) |
|
| 91 |
- status, err := waitCmdOut.ReadString('\n')
|
|
| 92 |
- c.Assert(err, checker.IsNil) |
|
| 93 |
- c.Assert(strings.TrimSpace(status), checker.Equals, "99", check.Commentf("expected exit 99, got %s", status))
|
|
| 94 |
- case <-time.After(2 * time.Second): |
|
| 95 |
- waitCmd.Process.Kill() |
|
| 96 |
- c.Fatal("timeout waiting for `docker wait` to exit")
|
|
| 97 |
- } |
|
| 98 |
-} |
| 99 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,102 @@ |
| 0 |
+package container // import "github.com/docker/docker/integration/container" |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "context" |
|
| 4 |
+ "testing" |
|
| 5 |
+ "time" |
|
| 6 |
+ |
|
| 7 |
+ "github.com/docker/docker/integration/internal/container" |
|
| 8 |
+ "github.com/docker/docker/internal/test/request" |
|
| 9 |
+ "gotest.tools/assert" |
|
| 10 |
+ is "gotest.tools/assert/cmp" |
|
| 11 |
+ "gotest.tools/poll" |
|
| 12 |
+ "gotest.tools/skip" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+func TestWaitNonBlocked(t *testing.T) {
|
|
| 16 |
+ defer setupTest(t)() |
|
| 17 |
+ cli := request.NewAPIClient(t) |
|
| 18 |
+ |
|
| 19 |
+ testCases := []struct {
|
|
| 20 |
+ doc string |
|
| 21 |
+ cmd string |
|
| 22 |
+ expectedCode int64 |
|
| 23 |
+ }{
|
|
| 24 |
+ {
|
|
| 25 |
+ doc: "wait-nonblocking-exit-0", |
|
| 26 |
+ cmd: "exit 0", |
|
| 27 |
+ expectedCode: 0, |
|
| 28 |
+ }, |
|
| 29 |
+ {
|
|
| 30 |
+ doc: "wait-nonblocking-exit-random", |
|
| 31 |
+ cmd: "exit 99", |
|
| 32 |
+ expectedCode: 99, |
|
| 33 |
+ }, |
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ for _, tc := range testCases {
|
|
| 37 |
+ tc := tc |
|
| 38 |
+ t.Run(tc.doc, func(t *testing.T) {
|
|
| 39 |
+ t.Parallel() |
|
| 40 |
+ ctx := context.Background() |
|
| 41 |
+ containerID := container.Run(t, ctx, cli, container.WithCmd("sh", "-c", tc.cmd))
|
|
| 42 |
+ poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "exited"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond)) |
|
| 43 |
+ |
|
| 44 |
+ waitresC, errC := cli.ContainerWait(ctx, containerID, "") |
|
| 45 |
+ select {
|
|
| 46 |
+ case err := <-errC: |
|
| 47 |
+ assert.NilError(t, err) |
|
| 48 |
+ case waitres := <-waitresC: |
|
| 49 |
+ assert.Check(t, is.Equal(tc.expectedCode, waitres.StatusCode)) |
|
| 50 |
+ } |
|
| 51 |
+ }) |
|
| 52 |
+ } |
|
| 53 |
+} |
|
| 54 |
+ |
|
| 55 |
+func TestWaitBlocked(t *testing.T) {
|
|
| 56 |
+ // Windows busybox does not support trap in this way, not sleep with sub-second |
|
| 57 |
+ // granularity. It will always exit 0x40010004. |
|
| 58 |
+ skip.If(t, testEnv.DaemonInfo.OSType != "linux") |
|
| 59 |
+ defer setupTest(t)() |
|
| 60 |
+ cli := request.NewAPIClient(t) |
|
| 61 |
+ |
|
| 62 |
+ testCases := []struct {
|
|
| 63 |
+ doc string |
|
| 64 |
+ cmd string |
|
| 65 |
+ expectedCode int64 |
|
| 66 |
+ }{
|
|
| 67 |
+ {
|
|
| 68 |
+ doc: "test-wait-blocked-exit-zero", |
|
| 69 |
+ cmd: "trap 'exit 0' TERM; while true; do usleep 10; done", |
|
| 70 |
+ expectedCode: 0, |
|
| 71 |
+ }, |
|
| 72 |
+ {
|
|
| 73 |
+ doc: "test-wait-blocked-exit-random", |
|
| 74 |
+ cmd: "trap 'exit 99' TERM; while true; do usleep 10; done", |
|
| 75 |
+ expectedCode: 99, |
|
| 76 |
+ }, |
|
| 77 |
+ } |
|
| 78 |
+ for _, tc := range testCases {
|
|
| 79 |
+ tc := tc |
|
| 80 |
+ t.Run(tc.doc, func(t *testing.T) {
|
|
| 81 |
+ t.Parallel() |
|
| 82 |
+ ctx := context.Background() |
|
| 83 |
+ containerID := container.Run(t, ctx, cli, container.WithCmd("sh", "-c", tc.cmd))
|
|
| 84 |
+ poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "running"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond)) |
|
| 85 |
+ |
|
| 86 |
+ waitresC, errC := cli.ContainerWait(ctx, containerID, "") |
|
| 87 |
+ |
|
| 88 |
+ err := cli.ContainerStop(ctx, containerID, nil) |
|
| 89 |
+ assert.NilError(t, err) |
|
| 90 |
+ |
|
| 91 |
+ select {
|
|
| 92 |
+ case err := <-errC: |
|
| 93 |
+ assert.NilError(t, err) |
|
| 94 |
+ case waitres := <-waitresC: |
|
| 95 |
+ assert.Check(t, is.Equal(tc.expectedCode, waitres.StatusCode)) |
|
| 96 |
+ case <-time.After(2 * time.Second): |
|
| 97 |
+ t.Fatal("timeout waiting for `docker wait`")
|
|
| 98 |
+ } |
|
| 99 |
+ }) |
|
| 100 |
+ } |
|
| 101 |
+} |