From 5571d5139b5c075dd5a99add00aa289a4ab10444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Mon, 4 Jul 2022 12:02:43 +0200 Subject: [PATCH] integration: TestWaitRestartedContainer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- integration/container/wait_test.go | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/integration/container/wait_test.go b/integration/container/wait_test.go index 6ac6b212997f6..12a9313caa031 100644 --- a/integration/container/wait_test.go +++ b/integration/container/wait_test.go @@ -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)) + } + } + }) + } + +}