Browse code

Merge pull request #36213 from yongtang/02052018-rename_test

Migrate rename tests in integration-cli to api tests

Yong Tang authored on 2018/02/09 11:38:02
Showing 2 changed files
1 1
deleted file mode 100644
... ...
@@ -1,127 +0,0 @@
1
-package main
2
-
3
-import (
4
-	"strings"
5
-
6
-	"github.com/docker/docker/integration-cli/checker"
7
-	"github.com/docker/docker/pkg/stringid"
8
-	"github.com/go-check/check"
9
-	"github.com/gotestyourself/gotestyourself/icmd"
10
-)
11
-
12
-func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
13
-	out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
14
-
15
-	cleanedContainerID := strings.TrimSpace(out)
16
-	dockerCmd(c, "wait", cleanedContainerID)
17
-
18
-	name := inspectField(c, cleanedContainerID, "Name")
19
-	newName := "new_name" + stringid.GenerateNonCryptoID()
20
-	dockerCmd(c, "rename", "first_name", newName)
21
-
22
-	name = inspectField(c, cleanedContainerID, "Name")
23
-	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
24
-
25
-}
26
-
27
-func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
28
-	out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
29
-
30
-	newName := "new_name" + stringid.GenerateNonCryptoID()
31
-	cleanedContainerID := strings.TrimSpace(out)
32
-	dockerCmd(c, "rename", "first_name", newName)
33
-
34
-	name := inspectField(c, cleanedContainerID, "Name")
35
-	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
36
-}
37
-
38
-func (s *DockerSuite) TestRenameRunningContainerAndReuse(c *check.C) {
39
-	out := runSleepingContainer(c, "--name", "first_name")
40
-	c.Assert(waitRun("first_name"), check.IsNil)
41
-
42
-	newName := "new_name"
43
-	ContainerID := strings.TrimSpace(out)
44
-	dockerCmd(c, "rename", "first_name", newName)
45
-
46
-	name := inspectField(c, ContainerID, "Name")
47
-	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container"))
48
-
49
-	out = runSleepingContainer(c, "--name", "first_name")
50
-	c.Assert(waitRun("first_name"), check.IsNil)
51
-	newContainerID := strings.TrimSpace(out)
52
-	name = inspectField(c, newContainerID, "Name")
53
-	c.Assert(name, checker.Equals, "/first_name", check.Commentf("Failed to reuse container name"))
54
-}
55
-
56
-func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
57
-	dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
58
-
59
-	newName := "new_name" + stringid.GenerateNonCryptoID()
60
-	dockerCmd(c, "rename", "first_name", newName)
61
-
62
-	name := inspectField(c, newName, "Name")
63
-	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
64
-
65
-	result := dockerCmdWithResult("inspect", "-f={{.Name}}", "--type=container", "first_name")
66
-	result.Assert(c, icmd.Expected{
67
-		ExitCode: 1,
68
-		Err:      "No such container: first_name",
69
-	})
70
-}
71
-
72
-// TODO: move to unit test
73
-func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
74
-	runSleepingContainer(c, "--name", "myname")
75
-
76
-	out, _, err := dockerCmdWithError("rename", "myname", "new:invalid")
77
-	c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
78
-	c.Assert(out, checker.Contains, "Invalid container name", check.Commentf("%v", err))
79
-
80
-	out, _ = dockerCmd(c, "ps", "-a")
81
-	c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
82
-}
83
-
84
-func (s *DockerSuite) TestRenameAnonymousContainer(c *check.C) {
85
-	testRequires(c, DaemonIsLinux)
86
-
87
-	dockerCmd(c, "network", "create", "network1")
88
-	out, _ := dockerCmd(c, "create", "-it", "--net", "network1", "busybox", "top")
89
-
90
-	anonymousContainerID := strings.TrimSpace(out)
91
-
92
-	dockerCmd(c, "rename", anonymousContainerID, "container1")
93
-	dockerCmd(c, "start", "container1")
94
-
95
-	count := "-c"
96
-	if testEnv.OSType == "windows" {
97
-		count = "-n"
98
-	}
99
-
100
-	_, _, err := dockerCmdWithError("run", "--net", "network1", "busybox", "ping", count, "1", "container1")
101
-	c.Assert(err, check.IsNil, check.Commentf("Embedded DNS lookup fails after renaming anonymous container: %v", err))
102
-}
103
-
104
-func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) {
105
-	out := runSleepingContainer(c, "--name", "old")
106
-	ContainerID := strings.TrimSpace(out)
107
-
108
-	out, _, err := dockerCmdWithError("rename", "old", "old")
109
-	c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
110
-	c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
111
-
112
-	out, _, err = dockerCmdWithError("rename", ContainerID, "old")
113
-	c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
114
-	c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
115
-}
116
-
117
-// Test case for #23973
118
-func (s *DockerSuite) TestRenameContainerWithLinkedContainer(c *check.C) {
119
-	testRequires(c, DaemonIsLinux)
120
-
121
-	db1, _ := dockerCmd(c, "run", "--name", "db1", "-d", "busybox", "top")
122
-	dockerCmd(c, "run", "--name", "app1", "-d", "--link", "db1:/mysql", "busybox", "top")
123
-	dockerCmd(c, "rename", "app1", "app2")
124
-	out, _, err := dockerCmdWithError("inspect", "--format={{ .Id }}", "app2/mysql")
125
-	c.Assert(err, checker.IsNil)
126
-	c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(db1))
127
-}
... ...
@@ -3,11 +3,16 @@ package container // import "github.com/docker/docker/integration/container"
3 3
 import (
4 4
 	"context"
5 5
 	"testing"
6
+	"time"
6 7
 
7 8
 	"github.com/docker/docker/api/types"
8 9
 	"github.com/docker/docker/api/types/container"
9 10
 	"github.com/docker/docker/api/types/network"
10 11
 	"github.com/docker/docker/integration/util/request"
12
+	"github.com/docker/docker/internal/testutil"
13
+	"github.com/docker/docker/pkg/stringid"
14
+	"github.com/gotestyourself/gotestyourself/poll"
15
+	"github.com/gotestyourself/gotestyourself/skip"
11 16
 	"github.com/stretchr/testify/assert"
12 17
 	"github.com/stretchr/testify/require"
13 18
 )
... ...
@@ -43,3 +48,159 @@ func TestRenameLinkedContainer(t *testing.T) {
43 43
 	require.NoError(t, err)
44 44
 	assert.Equal(t, []string{"/a0:/b0/a0"}, inspect.HostConfig.Links)
45 45
 }
46
+
47
+func TestRenameStoppedContainer(t *testing.T) {
48
+	defer setupTest(t)()
49
+	ctx := context.Background()
50
+	client := request.NewAPIClient(t)
51
+
52
+	oldName := "first_name"
53
+	cID := runSimpleContainer(ctx, t, client, oldName, func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
54
+		config.Cmd = []string{"sh"}
55
+	})
56
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
57
+
58
+	inspect, err := client.ContainerInspect(ctx, cID)
59
+	require.NoError(t, err)
60
+	assert.Equal(t, inspect.Name, "/"+oldName)
61
+
62
+	newName := "new_name" + stringid.GenerateNonCryptoID()
63
+	err = client.ContainerRename(ctx, oldName, newName)
64
+	require.NoError(t, err)
65
+
66
+	inspect, err = client.ContainerInspect(ctx, cID)
67
+	require.NoError(t, err)
68
+	assert.Equal(t, inspect.Name, "/"+newName)
69
+}
70
+
71
+func TestRenameRunningContainerAndReuse(t *testing.T) {
72
+	defer setupTest(t)()
73
+	ctx := context.Background()
74
+	client := request.NewAPIClient(t)
75
+
76
+	oldName := "first_name"
77
+	cID := runSimpleContainer(ctx, t, client, oldName)
78
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
79
+
80
+	newName := "new_name" + stringid.GenerateNonCryptoID()
81
+	err := client.ContainerRename(ctx, oldName, newName)
82
+	require.NoError(t, err)
83
+
84
+	inspect, err := client.ContainerInspect(ctx, cID)
85
+	require.NoError(t, err)
86
+	assert.Equal(t, inspect.Name, "/"+newName)
87
+
88
+	_, err = client.ContainerInspect(ctx, oldName)
89
+	testutil.ErrorContains(t, err, "No such container: "+oldName)
90
+
91
+	cID = runSimpleContainer(ctx, t, client, oldName)
92
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
93
+
94
+	inspect, err = client.ContainerInspect(ctx, cID)
95
+	require.NoError(t, err)
96
+	assert.Equal(t, inspect.Name, "/"+oldName)
97
+}
98
+
99
+func TestRenameInvalidName(t *testing.T) {
100
+	defer setupTest(t)()
101
+	ctx := context.Background()
102
+	client := request.NewAPIClient(t)
103
+
104
+	oldName := "first_name"
105
+	cID := runSimpleContainer(ctx, t, client, oldName)
106
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
107
+
108
+	err := client.ContainerRename(ctx, oldName, "new:invalid")
109
+	testutil.ErrorContains(t, err, "Invalid container name")
110
+
111
+	inspect, err := client.ContainerInspect(ctx, oldName)
112
+	require.NoError(t, err)
113
+	assert.Equal(t, inspect.ID, cID)
114
+}
115
+
116
+// Test case for GitHub issue 22466
117
+// Docker's service discovery works for named containers so
118
+// ping to a named container should work, and an anonymous
119
+// container without a name does not work with service discovery.
120
+// However, an anonymous could be renamed to a named container.
121
+// This test is to make sure once the container has been renamed,
122
+// the service discovery for the (re)named container works.
123
+func TestRenameAnonymousContainer(t *testing.T) {
124
+	defer setupTest(t)()
125
+	ctx := context.Background()
126
+	client := request.NewAPIClient(t)
127
+
128
+	_, err := client.NetworkCreate(ctx, "network1", types.NetworkCreate{})
129
+	require.NoError(t, err)
130
+	cID := createSimpleContainer(ctx, t, client, "", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
131
+		networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
132
+			"network1": {},
133
+		}
134
+		hostConfig.NetworkMode = "network1"
135
+	})
136
+	err = client.ContainerRename(ctx, cID, "container1")
137
+	require.NoError(t, err)
138
+	err = client.ContainerStart(ctx, "container1", types.ContainerStartOptions{})
139
+	require.NoError(t, err)
140
+
141
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
142
+
143
+	count := "-c"
144
+	if testEnv.OSType == "windows" {
145
+		count = "-n"
146
+	}
147
+	cID = runSimpleContainer(ctx, t, client, "", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
148
+		networkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{
149
+			"network1": {},
150
+		}
151
+		hostConfig.NetworkMode = "network1"
152
+		config.Cmd = []string{"ping", count, "1", "container1"}
153
+	})
154
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "exited"), poll.WithDelay(100*time.Millisecond))
155
+
156
+	inspect, err := client.ContainerInspect(ctx, cID)
157
+	require.NoError(t, err)
158
+	assert.Equal(t, inspect.State.ExitCode, 0)
159
+}
160
+
161
+// TODO: should be a unit test
162
+func TestRenameContainerWithSameName(t *testing.T) {
163
+	defer setupTest(t)()
164
+	ctx := context.Background()
165
+	client := request.NewAPIClient(t)
166
+
167
+	cID := runSimpleContainer(ctx, t, client, "old")
168
+	poll.WaitOn(t, containerIsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
169
+	err := client.ContainerRename(ctx, "old", "old")
170
+	testutil.ErrorContains(t, err, "Renaming a container with the same name")
171
+	err = client.ContainerRename(ctx, cID, "old")
172
+	testutil.ErrorContains(t, err, "Renaming a container with the same name")
173
+}
174
+
175
+// Test case for GitHub issue 23973
176
+// When a container is being renamed, the container might
177
+// be linked to another container. In that case, the meta data
178
+// of the linked container should be updated so that the other
179
+// container could still reference to the container that is renamed.
180
+func TestRenameContainerWithLinkedContainer(t *testing.T) {
181
+	skip.If(t, !testEnv.IsLocalDaemon())
182
+
183
+	defer setupTest(t)()
184
+	ctx := context.Background()
185
+	client := request.NewAPIClient(t)
186
+
187
+	db1ID := runSimpleContainer(ctx, t, client, "db1")
188
+	poll.WaitOn(t, containerIsInState(ctx, client, db1ID, "running"), poll.WithDelay(100*time.Millisecond))
189
+
190
+	app1ID := runSimpleContainer(ctx, t, client, "app1", func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig) {
191
+		hostConfig.Links = []string{"db1:/mysql"}
192
+	})
193
+	poll.WaitOn(t, containerIsInState(ctx, client, app1ID, "running"), poll.WithDelay(100*time.Millisecond))
194
+
195
+	err := client.ContainerRename(ctx, "app1", "app2")
196
+	require.NoError(t, err)
197
+
198
+	inspect, err := client.ContainerInspect(ctx, "app2/mysql")
199
+	require.NoError(t, err)
200
+	assert.Equal(t, inspect.ID, db1ID)
201
+}