2014-10-04 22:47:54 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2015-04-23 10:11:20 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-12-30 12:23:00 -05:00
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-10-04 22:47:54 -04:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "wait", cleanedContainerID)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, cleanedContainerID, "Name")
|
2015-07-28 20:19:17 -04:00
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
name = inspectField(c, cleanedContainerID, "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-07-28 20:19:17 -04:00
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, cleanedContainerID, "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-10-26 20:49:50 -04:00
|
|
|
func (s *DockerSuite) TestRenameRunningContainerAndReuse(c *check.C) {
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "--name", "first_name")
|
2015-10-26 20:49:50 -04:00
|
|
|
c.Assert(waitRun("first_name"), check.IsNil)
|
|
|
|
|
|
|
|
newName := "new_name"
|
|
|
|
ContainerID := strings.TrimSpace(out)
|
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, ContainerID, "Name")
|
2015-10-26 20:49:50 -04:00
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container"))
|
|
|
|
|
2017-04-16 17:39:30 -04:00
|
|
|
out = runSleepingContainer(c, "--name", "first_name")
|
2015-10-26 20:49:50 -04:00
|
|
|
c.Assert(waitRun("first_name"), check.IsNil)
|
|
|
|
newContainerID := strings.TrimSpace(out)
|
2016-01-28 09:19:25 -05:00
|
|
|
name = inspectField(c, newContainerID, "Name")
|
2015-10-26 20:49:50 -04:00
|
|
|
c.Assert(name, checker.Equals, "/first_name", check.Commentf("Failed to reuse container name"))
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-07-28 20:19:17 -04:00
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, newName, "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2016-11-29 20:31:29 -05:00
|
|
|
result := dockerCmdWithResult("inspect", "-f={{.Name}}", "--type=container", "first_name")
|
2016-08-16 17:51:38 -04:00
|
|
|
c.Assert(result, icmd.Matches, icmd.Expected{
|
2016-08-04 12:57:34 -04:00
|
|
|
ExitCode: 1,
|
2016-11-29 20:31:29 -05:00
|
|
|
Err: "No such container: first_name",
|
2016-08-04 12:57:34 -04:00
|
|
|
})
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
2015-01-13 20:30:49 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
|
2016-01-26 23:16:36 -05:00
|
|
|
runSleepingContainer(c, "--name", "myname")
|
2015-01-13 20:30:49 -05:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _, err := dockerCmdWithError("rename", "myname", "new:invalid")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, "Invalid container name", check.Commentf("%v", err))
|
2015-01-13 20:30:49 -05:00
|
|
|
|
2016-06-07 09:42:48 -04:00
|
|
|
out, _, err = dockerCmdWithError("rename", "myname")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, "requires exactly 2 argument(s).", check.Commentf("%v", err))
|
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _, err = dockerCmdWithError("rename", "myname", "")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
|
2016-06-07 09:42:48 -04:00
|
|
|
c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
|
2015-09-18 14:05:04 -04:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _, err = dockerCmdWithError("rename", "", "newname")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out))
|
2016-06-07 09:42:48 -04:00
|
|
|
c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
|
2015-09-18 14:05:04 -04:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _ = dockerCmd(c, "ps", "-a")
|
|
|
|
c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
|
2015-01-13 20:30:49 -05:00
|
|
|
}
|
2016-05-12 22:45:42 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestRenameAnonymousContainer(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
dockerCmd(c, "network", "create", "network1")
|
|
|
|
out, _ := dockerCmd(c, "create", "-it", "--net", "network1", "busybox", "top")
|
|
|
|
|
|
|
|
anonymousContainerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
dockerCmd(c, "rename", anonymousContainerID, "container1")
|
|
|
|
dockerCmd(c, "start", "container1")
|
|
|
|
|
|
|
|
count := "-c"
|
2017-01-13 11:23:28 -05:00
|
|
|
if testEnv.DaemonPlatform() == "windows" {
|
2016-05-12 22:45:42 -04:00
|
|
|
count = "-n"
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := dockerCmdWithError("run", "--net", "network1", "busybox", "ping", count, "1", "container1")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Embedded DNS lookup fails after renaming anonymous container: %v", err))
|
|
|
|
}
|
2016-06-07 21:40:44 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) {
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "--name", "old")
|
2016-06-07 21:40:44 -04:00
|
|
|
ContainerID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
out, _, err := dockerCmdWithError("rename", "old", "old")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
|
|
|
|
c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
|
|
|
|
|
|
|
|
out, _, err = dockerCmdWithError("rename", ContainerID, "old")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
|
|
|
|
c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
|
|
|
|
}
|
2016-06-27 21:48:11 -04:00
|
|
|
|
|
|
|
// Test case for #23973
|
|
|
|
func (s *DockerSuite) TestRenameContainerWithLinkedContainer(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
db1, _ := dockerCmd(c, "run", "--name", "db1", "-d", "busybox", "top")
|
|
|
|
dockerCmd(c, "run", "--name", "app1", "-d", "--link", "db1:/mysql", "busybox", "top")
|
|
|
|
dockerCmd(c, "rename", "app1", "app2")
|
2016-06-23 12:24:28 -04:00
|
|
|
out, _, err := dockerCmdWithError("inspect", "--format={{ .Id }}", "app2/mysql")
|
2016-06-27 21:48:11 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(db1))
|
|
|
|
}
|