|
@@ -5,6 +5,7 @@ import (
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/docker/docker/api/types"
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
"github.com/docker/docker/integration/internal/container"
|
|
|
"github.com/docker/docker/testutil/request"
|
|
@@ -156,3 +157,69 @@ func TestWaitConditions(t *testing.T) {
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+}
|