github.com/jingleWang/moby@v1.13.1/integration-cli/docker_cli_rename_test.go (about) 1 package main 2 3 import ( 4 "strings" 5 6 "github.com/docker/docker/pkg/integration/checker" 7 icmd "github.com/docker/docker/pkg/integration/cmd" 8 "github.com/docker/docker/pkg/stringid" 9 "github.com/go-check/check" 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 c.Assert(result, icmd.Matches, icmd.Expected{ 67 ExitCode: 1, 68 Err: "No such container: first_name", 69 }) 70 } 71 72 func (s *DockerSuite) TestRenameInvalidName(c *check.C) { 73 runSleepingContainer(c, "--name", "myname") 74 75 out, _, err := dockerCmdWithError("rename", "myname", "new:invalid") 76 c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out)) 77 c.Assert(out, checker.Contains, "Invalid container name", check.Commentf("%v", err)) 78 79 out, _, err = dockerCmdWithError("rename", "myname") 80 c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out)) 81 c.Assert(out, checker.Contains, "requires exactly 2 argument(s).", check.Commentf("%v", err)) 82 83 out, _, err = dockerCmdWithError("rename", "myname", "") 84 c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out)) 85 c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err)) 86 87 out, _, err = dockerCmdWithError("rename", "", "newname") 88 c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out)) 89 c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err)) 90 91 out, _ = dockerCmd(c, "ps", "-a") 92 c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out)) 93 } 94 95 func (s *DockerSuite) TestRenameAnonymousContainer(c *check.C) { 96 testRequires(c, DaemonIsLinux) 97 98 dockerCmd(c, "network", "create", "network1") 99 out, _ := dockerCmd(c, "create", "-it", "--net", "network1", "busybox", "top") 100 101 anonymousContainerID := strings.TrimSpace(out) 102 103 dockerCmd(c, "rename", anonymousContainerID, "container1") 104 dockerCmd(c, "start", "container1") 105 106 count := "-c" 107 if daemonPlatform == "windows" { 108 count = "-n" 109 } 110 111 _, _, err := dockerCmdWithError("run", "--net", "network1", "busybox", "ping", count, "1", "container1") 112 c.Assert(err, check.IsNil, check.Commentf("Embedded DNS lookup fails after renaming anonymous container: %v", err)) 113 } 114 115 func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) { 116 out, _ := runSleepingContainer(c, "--name", "old") 117 ContainerID := strings.TrimSpace(out) 118 119 out, _, err := dockerCmdWithError("rename", "old", "old") 120 c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed")) 121 c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err)) 122 123 out, _, err = dockerCmdWithError("rename", ContainerID, "old") 124 c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed")) 125 c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err)) 126 } 127 128 // Test case for #23973 129 func (s *DockerSuite) TestRenameContainerWithLinkedContainer(c *check.C) { 130 testRequires(c, DaemonIsLinux) 131 132 db1, _ := dockerCmd(c, "run", "--name", "db1", "-d", "busybox", "top") 133 dockerCmd(c, "run", "--name", "app1", "-d", "--link", "db1:/mysql", "busybox", "top") 134 dockerCmd(c, "rename", "app1", "app2") 135 out, _, err := dockerCmdWithError("inspect", "--format={{ .Id }}", "app2/mysql") 136 c.Assert(err, checker.IsNil) 137 c.Assert(strings.TrimSpace(out), checker.Equals, strings.TrimSpace(db1)) 138 }