Browse code

Migrate some update restart tests to api tests

This fix migrates some update restart tests in
integration-cli to api tests in integration.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2018/02/12 07:32:15
Showing 2 changed files
1 1
deleted file mode 100644
... ...
@@ -1,43 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"strings"
5
-	"time"
6
-
7
-	"github.com/docker/docker/integration-cli/checker"
8
-	"github.com/docker/docker/integration-cli/cli"
9
-	"github.com/go-check/check"
10
-	"github.com/gotestyourself/gotestyourself/icmd"
11
-)
12
-
13
-func (s *DockerSuite) TestUpdateRestartPolicy(c *check.C) {
14
-	out := cli.DockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "sh", "-c", "sleep 1 && false").Combined()
15
-	timeout := 60 * time.Second
16
-	if testEnv.OSType == "windows" {
17
-		timeout = 180 * time.Second
18
-	}
19
-
20
-	id := strings.TrimSpace(string(out))
21
-
22
-	// update restart policy to on-failure:5
23
-	cli.DockerCmd(c, "update", "--restart=on-failure:5", id)
24
-
25
-	cli.WaitExited(c, id, timeout)
26
-
27
-	count := inspectField(c, id, "RestartCount")
28
-	c.Assert(count, checker.Equals, "5")
29
-
30
-	maximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
31
-	c.Assert(maximumRetryCount, checker.Equals, "5")
32
-}
33
-
34
-func (s *DockerSuite) TestUpdateRestartWithAutoRemoveFlag(c *check.C) {
35
-	out := runSleepingContainer(c, "--rm")
36
-	id := strings.TrimSpace(out)
37
-
38
-	// update restart policy for an AutoRemove container
39
-	cli.Docker(cli.Args("update", "--restart=always", id)).Assert(c, icmd.Expected{
40
-		ExitCode: 1,
41
-		Err:      "Restart policy cannot be updated because AutoRemove is enabled for the container",
42
-	})
43
-}
44 1
new file mode 100644
... ...
@@ -0,0 +1,65 @@
0
+package container // import "github.com/docker/docker/integration/container"
1
+
2
+import (
3
+	"context"
4
+	"testing"
5
+	"time"
6
+
7
+	containertypes "github.com/docker/docker/api/types/container"
8
+	"github.com/docker/docker/integration/internal/container"
9
+	"github.com/docker/docker/integration/internal/request"
10
+	"github.com/docker/docker/internal/testutil"
11
+	"github.com/gotestyourself/gotestyourself/poll"
12
+	"github.com/stretchr/testify/assert"
13
+	"github.com/stretchr/testify/require"
14
+)
15
+
16
+func TestUpdateRestartPolicy(t *testing.T) {
17
+	defer setupTest(t)()
18
+	client := request.NewAPIClient(t)
19
+	ctx := context.Background()
20
+
21
+	cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", "sleep 1 && false"), func(c *container.TestContainerConfig) {
22
+		c.HostConfig.RestartPolicy = containertypes.RestartPolicy{
23
+			Name:              "on-failure",
24
+			MaximumRetryCount: 3,
25
+		}
26
+	})
27
+
28
+	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
29
+		RestartPolicy: containertypes.RestartPolicy{
30
+			Name:              "on-failure",
31
+			MaximumRetryCount: 5,
32
+		},
33
+	})
34
+	require.NoError(t, err)
35
+
36
+	timeout := 60 * time.Second
37
+	if testEnv.OSType == "windows" {
38
+		timeout = 180 * time.Second
39
+	}
40
+
41
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(timeout))
42
+
43
+	inspect, err := client.ContainerInspect(ctx, cID)
44
+	require.NoError(t, err)
45
+	assert.Equal(t, inspect.RestartCount, 5)
46
+	assert.Equal(t, inspect.HostConfig.RestartPolicy.MaximumRetryCount, 5)
47
+}
48
+
49
+func TestUpdateRestartWithAutoRemove(t *testing.T) {
50
+	defer setupTest(t)()
51
+	client := request.NewAPIClient(t)
52
+	ctx := context.Background()
53
+
54
+	cID := container.Run(t, ctx, client, func(c *container.TestContainerConfig) {
55
+		c.HostConfig.AutoRemove = true
56
+	})
57
+
58
+	_, err := client.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
59
+		RestartPolicy: containertypes.RestartPolicy{
60
+			Name: "always",
61
+		},
62
+	})
63
+	testutil.ErrorContains(t, err, "Restart policy cannot be updated because AutoRemove is enabled for the container")
64
+}