Skip to content

Commit

Permalink
integration: TestWaitRestartedContainer
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed Jul 20, 2022
1 parent 498803b commit 5571d51
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions integration/container/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}
})
}

}

0 comments on commit 5571d51

Please sign in to comment.