From 4f378124ff649b844de88c93f6ca70e6b3f5d7d7 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Mon, 29 Jan 2018 23:36:45 +0000 Subject: [PATCH] Migrate docker_cli_stop_test.go to api test This fix migrate docker_cli_stop_test.go to api test Signed-off-by: Yong Tang --- integration-cli/docker_cli_stop_test.go | 17 -------- integration/container/stop_test.go | 55 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 17 deletions(-) delete mode 100644 integration-cli/docker_cli_stop_test.go diff --git a/integration-cli/docker_cli_stop_test.go b/integration-cli/docker_cli_stop_test.go deleted file mode 100644 index 1be41203b3..0000000000 --- a/integration-cli/docker_cli_stop_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import ( - "github.com/docker/docker/integration-cli/checker" - "github.com/go-check/check" -) - -func (s *DockerSuite) TestStopContainerWithRestartPolicyAlways(c *check.C) { - dockerCmd(c, "run", "--name", "verifyRestart1", "-d", "--restart=always", "busybox", "false") - dockerCmd(c, "run", "--name", "verifyRestart2", "-d", "--restart=always", "busybox", "false") - - c.Assert(waitRun("verifyRestart1"), checker.IsNil) - c.Assert(waitRun("verifyRestart2"), checker.IsNil) - - dockerCmd(c, "stop", "verifyRestart1") - dockerCmd(c, "stop", "verifyRestart2") -} diff --git a/integration/container/stop_test.go b/integration/container/stop_test.go index feecc6901f..dadf768bb7 100644 --- a/integration/container/stop_test.go +++ b/integration/container/stop_test.go @@ -18,6 +18,46 @@ import ( "github.com/stretchr/testify/require" ) +func TestStopContainerWithRestartPolicyAlways(t *testing.T) { + defer setupTest(t)() + client := request.NewAPIClient(t) + ctx := context.Background() + + names := []string{"verifyRestart1", "verifyRestart2"} + for _, name := range names { + resp, err := client.ContainerCreate(ctx, + &container.Config{ + Cmd: []string{"false"}, + Image: "busybox", + }, + &container.HostConfig{ + RestartPolicy: container.RestartPolicy{ + Name: "always", + }, + }, + &network.NetworkingConfig{}, + name, + ) + require.NoError(t, err) + + err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}) + require.NoError(t, err) + } + + for _, name := range names { + poll.WaitOn(t, containerIsInState(ctx, client, name, "running", "restarting"), poll.WithDelay(100*time.Millisecond)) + } + + for _, name := range names { + err := client.ContainerStop(ctx, name, nil) + require.NoError(t, err) + } + + for _, name := range names { + poll.WaitOn(t, containerIsStopped(ctx, client, name), poll.WithDelay(100*time.Millisecond)) + } +} + func TestDeleteDevicemapper(t *testing.T) { skip.IfCondition(t, testEnv.DaemonInfo.Driver != "devicemapper") @@ -72,3 +112,18 @@ func containerIsStopped(ctx context.Context, client client.APIClient, containerI } } } + +func containerIsInState(ctx context.Context, client client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result { + return func(log poll.LogT) poll.Result { + inspect, err := client.ContainerInspect(ctx, containerID) + if err != nil { + return poll.Error(err) + } + for _, v := range state { + if inspect.State.Status == v { + return poll.Success() + } + } + return poll.Continue("waiting for container to be running, currently %s", inspect.State.Status) + } +}