Browse code

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 <yong.tang.github@outlook.com>

Yong Tang authored on 2018/01/30 08:36:45
Showing 2 changed files
1 1
deleted file mode 100644
... ...
@@ -1,17 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"github.com/docker/docker/integration-cli/checker"
5
-	"github.com/go-check/check"
6
-)
7
-
8
-func (s *DockerSuite) TestStopContainerWithRestartPolicyAlways(c *check.C) {
9
-	dockerCmd(c, "run", "--name", "verifyRestart1", "-d", "--restart=always", "busybox", "false")
10
-	dockerCmd(c, "run", "--name", "verifyRestart2", "-d", "--restart=always", "busybox", "false")
11
-
12
-	c.Assert(waitRun("verifyRestart1"), checker.IsNil)
13
-	c.Assert(waitRun("verifyRestart2"), checker.IsNil)
14
-
15
-	dockerCmd(c, "stop", "verifyRestart1")
16
-	dockerCmd(c, "stop", "verifyRestart2")
17
-}
... ...
@@ -18,6 +18,46 @@ import (
18 18
 	"github.com/stretchr/testify/require"
19 19
 )
20 20
 
21
+func TestStopContainerWithRestartPolicyAlways(t *testing.T) {
22
+	defer setupTest(t)()
23
+	client := request.NewAPIClient(t)
24
+	ctx := context.Background()
25
+
26
+	names := []string{"verifyRestart1", "verifyRestart2"}
27
+	for _, name := range names {
28
+		resp, err := client.ContainerCreate(ctx,
29
+			&container.Config{
30
+				Cmd:   []string{"false"},
31
+				Image: "busybox",
32
+			},
33
+			&container.HostConfig{
34
+				RestartPolicy: container.RestartPolicy{
35
+					Name: "always",
36
+				},
37
+			},
38
+			&network.NetworkingConfig{},
39
+			name,
40
+		)
41
+		require.NoError(t, err)
42
+
43
+		err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
44
+		require.NoError(t, err)
45
+	}
46
+
47
+	for _, name := range names {
48
+		poll.WaitOn(t, containerIsInState(ctx, client, name, "running", "restarting"), poll.WithDelay(100*time.Millisecond))
49
+	}
50
+
51
+	for _, name := range names {
52
+		err := client.ContainerStop(ctx, name, nil)
53
+		require.NoError(t, err)
54
+	}
55
+
56
+	for _, name := range names {
57
+		poll.WaitOn(t, containerIsStopped(ctx, client, name), poll.WithDelay(100*time.Millisecond))
58
+	}
59
+}
60
+
21 61
 func TestDeleteDevicemapper(t *testing.T) {
22 62
 	skip.IfCondition(t, testEnv.DaemonInfo.Driver != "devicemapper")
23 63
 
... ...
@@ -72,3 +112,18 @@ func containerIsStopped(ctx context.Context, client client.APIClient, containerI
72 72
 		}
73 73
 	}
74 74
 }
75
+
76
+func containerIsInState(ctx context.Context, client client.APIClient, containerID string, state ...string) func(log poll.LogT) poll.Result {
77
+	return func(log poll.LogT) poll.Result {
78
+		inspect, err := client.ContainerInspect(ctx, containerID)
79
+		if err != nil {
80
+			return poll.Error(err)
81
+		}
82
+		for _, v := range state {
83
+			if inspect.State.Status == v {
84
+				return poll.Success()
85
+			}
86
+		}
87
+		return poll.Continue("waiting for container to be running, currently %s", inspect.State.Status)
88
+	}
89
+}