2018-07-23 17:37:08 -04:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-07-04 06:02:43 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2022-02-16 05:36:37 -05:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-07-23 17:37:08 -04:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-07-23 17:37:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWaitNonBlocked(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
cli := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
doc string
|
|
|
|
cmd string
|
|
|
|
expectedCode int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "wait-nonblocking-exit-0",
|
|
|
|
cmd: "exit 0",
|
|
|
|
expectedCode: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "wait-nonblocking-exit-random",
|
|
|
|
cmd: "exit 99",
|
|
|
|
expectedCode: 99,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
2019-06-06 07:15:31 -04:00
|
|
|
containerID := container.Run(ctx, t, cli, container.WithCmd("sh", "-c", tc.cmd))
|
2018-07-23 17:37:08 -04:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "exited"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
2019-08-28 12:42:16 -04:00
|
|
|
waitResC, errC := cli.ContainerWait(ctx, containerID, "")
|
2018-07-23 17:37:08 -04:00
|
|
|
select {
|
|
|
|
case err := <-errC:
|
|
|
|
assert.NilError(t, err)
|
2019-08-28 12:42:16 -04:00
|
|
|
case waitRes := <-waitResC:
|
|
|
|
assert.Check(t, is.Equal(tc.expectedCode, waitRes.StatusCode))
|
2018-07-23 17:37:08 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitBlocked(t *testing.T) {
|
|
|
|
// Windows busybox does not support trap in this way, not sleep with sub-second
|
|
|
|
// granularity. It will always exit 0x40010004.
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
|
|
|
defer setupTest(t)()
|
|
|
|
cli := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
doc string
|
|
|
|
cmd string
|
|
|
|
expectedCode int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "test-wait-blocked-exit-zero",
|
|
|
|
cmd: "trap 'exit 0' TERM; while true; do usleep 10; done",
|
|
|
|
expectedCode: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "test-wait-blocked-exit-random",
|
|
|
|
cmd: "trap 'exit 99' TERM; while true; do usleep 10; done",
|
|
|
|
expectedCode: 99,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
2019-06-06 07:15:31 -04:00
|
|
|
containerID := container.Run(ctx, t, cli, container.WithCmd("sh", "-c", tc.cmd))
|
2018-07-23 17:37:08 -04:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "running"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
2019-08-28 12:42:16 -04:00
|
|
|
waitResC, errC := cli.ContainerWait(ctx, containerID, "")
|
2018-07-23 17:37:08 -04:00
|
|
|
|
2022-02-16 05:36:37 -05:00
|
|
|
err := cli.ContainerStop(ctx, containerID, containertypes.StopOptions{})
|
2018-07-23 17:37:08 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errC:
|
|
|
|
assert.NilError(t, err)
|
2019-08-28 12:42:16 -04:00
|
|
|
case waitRes := <-waitResC:
|
|
|
|
assert.Check(t, is.Equal(tc.expectedCode, waitRes.StatusCode))
|
2018-07-23 17:37:08 -04:00
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for `docker wait`")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 08:26:35 -04:00
|
|
|
|
|
|
|
func TestWaitConditions(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
cli := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-07-25 17:51:33 -04:00
|
|
|
doc string
|
|
|
|
waitCond containertypes.WaitCondition
|
|
|
|
runOpts []func(*container.TestContainerConfig)
|
2022-06-27 08:26:35 -04:00
|
|
|
}{
|
|
|
|
{
|
2022-07-25 17:51:33 -04:00
|
|
|
doc: "default",
|
2022-06-27 08:26:35 -04:00
|
|
|
},
|
|
|
|
{
|
2022-07-25 17:51:33 -04:00
|
|
|
doc: "not-running",
|
|
|
|
waitCond: containertypes.WaitConditionNotRunning,
|
2022-06-27 08:26:35 -04:00
|
|
|
},
|
|
|
|
{
|
2022-07-25 17:51:33 -04:00
|
|
|
doc: "next-exit",
|
|
|
|
waitCond: containertypes.WaitConditionNextExit,
|
2022-06-27 08:26:35 -04:00
|
|
|
},
|
|
|
|
{
|
2022-07-25 17:51:33 -04:00
|
|
|
doc: "removed",
|
|
|
|
waitCond: containertypes.WaitConditionRemoved,
|
|
|
|
runOpts: []func(*container.TestContainerConfig){container.WithAutoRemove},
|
2022-06-27 08:26:35 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
2022-07-25 17:51:33 -04:00
|
|
|
opts := append([]func(*container.TestContainerConfig){
|
|
|
|
container.WithCmd("sh", "-c", "read -r; exit 99"),
|
|
|
|
func(tcc *container.TestContainerConfig) {
|
|
|
|
tcc.Config.AttachStdin = true
|
|
|
|
tcc.Config.OpenStdin = true
|
|
|
|
},
|
|
|
|
}, tc.runOpts...)
|
|
|
|
containerID := container.Create(ctx, t, cli, opts...)
|
|
|
|
t.Logf("ContainerID = %v", containerID)
|
|
|
|
|
|
|
|
streams, err := cli.ContainerAttach(ctx, containerID, types.ContainerAttachOptions{Stream: true, Stdin: true})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer streams.Close()
|
2022-06-27 08:26:35 -04:00
|
|
|
|
2022-07-25 17:51:33 -04:00
|
|
|
assert.NilError(t, cli.ContainerStart(ctx, containerID, types.ContainerStartOptions{}))
|
2022-06-27 08:26:35 -04:00
|
|
|
waitResC, errC := cli.ContainerWait(ctx, containerID, tc.waitCond)
|
2022-07-25 17:51:33 -04:00
|
|
|
select {
|
|
|
|
case err := <-errC:
|
|
|
|
t.Fatalf("ContainerWait() err = %v", err)
|
|
|
|
case res := <-waitResC:
|
|
|
|
t.Fatalf("ContainerWait() sent exit code (%v) before ContainerStart()", res)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
info, _ := cli.ContainerInspect(ctx, containerID)
|
|
|
|
assert.Equal(t, "running", info.State.Status)
|
|
|
|
|
|
|
|
_, err = streams.Conn.Write([]byte("\n"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2022-06-27 08:26:35 -04:00
|
|
|
select {
|
|
|
|
case err := <-errC:
|
|
|
|
assert.NilError(t, err)
|
|
|
|
case waitRes := <-waitResC:
|
2022-07-25 17:51:33 -04:00
|
|
|
assert.Check(t, is.Equal(int64(99), waitRes.StatusCode))
|
|
|
|
case <-time.After(15 * time.Second):
|
|
|
|
info, _ := cli.ContainerInspect(ctx, containerID)
|
|
|
|
t.Fatalf("Timed out waiting for container exit code (status = %q)", info.State.Status)
|
2022-06-27 08:26:35 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 06:02:43 -04:00
|
|
|
|
|
|
|
func TestWaitRestartedContainer(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
cli := request.NewAPIClient(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
doc string
|
|
|
|
waitCond containertypes.WaitCondition
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "default",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "not-running",
|
|
|
|
waitCond: containertypes.WaitConditionNotRunning,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "next-exit",
|
|
|
|
waitCond: containertypes.WaitConditionNextExit,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't catch the SIGTERM in the Windows based busybox image
|
|
|
|
isWindowDaemon := testEnv.DaemonInfo.OSType == "windows"
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
|
|
containerID := container.Run(ctx, t, cli,
|
|
|
|
container.WithCmd("sh", "-c", "trap 'exit 5' SIGTERM; while true; do sleep 0.1; done"),
|
|
|
|
)
|
|
|
|
defer cli.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{Force: true})
|
|
|
|
|
|
|
|
poll.WaitOn(t, container.IsInState(ctx, cli, containerID, "running"), poll.WithTimeout(30*time.Second), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
// Container is running now, wait for exit
|
|
|
|
waitResC, errC := cli.ContainerWait(ctx, containerID, tc.waitCond)
|
|
|
|
|
|
|
|
timeout := 5
|
|
|
|
// On Windows it will always timeout, because our process won't receive SIGTERM
|
|
|
|
// Skip to force killing immediately
|
|
|
|
if isWindowDaemon {
|
|
|
|
timeout = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cli.ContainerRestart(ctx, containerID, containertypes.StopOptions{Timeout: &timeout, Signal: "SIGTERM"})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errC:
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatalf("Wait should end after restart")
|
|
|
|
case waitRes := <-waitResC:
|
|
|
|
expectedCode := int64(5)
|
|
|
|
|
|
|
|
if !isWindowDaemon {
|
|
|
|
assert.Check(t, is.Equal(expectedCode, waitRes.StatusCode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|