Browse code

docker rename fix to address the issue of renaming with the same name issue #23319

Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com>
(cherry picked from commit 3e8c16ef6d5e6b451996722d99f5d646ed8a0e56)

Sainath Grandhi authored on 2016/06/08 10:40:44
Showing 2 changed files
... ...
@@ -21,6 +21,10 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
21 21
 		return fmt.Errorf("Neither old nor new names may be empty")
22 22
 	}
23 23
 
24
+	if newName[0] != '/' {
25
+		newName = "/" + newName
26
+	}
27
+
24 28
 	container, err := daemon.GetContainer(oldName)
25 29
 	if err != nil {
26 30
 		return err
... ...
@@ -31,6 +35,11 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
31 31
 
32 32
 	container.Lock()
33 33
 	defer container.Unlock()
34
+
35
+	if oldName == newName {
36
+		return fmt.Errorf("Renaming a container with the same name as its current name")
37
+	}
38
+
34 39
 	if newName, err = daemon.reserveName(container.ID, newName); err != nil {
35 40
 		return fmt.Errorf("Error when allocating new name: %v", err)
36 41
 	}
... ...
@@ -108,3 +108,16 @@ func (s *DockerSuite) TestRenameAnonymousContainer(c *check.C) {
108 108
 	_, _, err := dockerCmdWithError("run", "--net", "network1", "busybox", "ping", count, "1", "container1")
109 109
 	c.Assert(err, check.IsNil, check.Commentf("Embedded DNS lookup fails after renaming anonymous container: %v", err))
110 110
 }
111
+
112
+func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) {
113
+	out, _ := runSleepingContainer(c, "--name", "old")
114
+	ContainerID := strings.TrimSpace(out)
115
+
116
+	out, _, err := dockerCmdWithError("rename", "old", "old")
117
+	c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
118
+	c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
119
+
120
+	out, _, err = dockerCmdWithError("rename", ContainerID, "old")
121
+	c.Assert(err, checker.NotNil, check.Commentf("Renaming a container with the same name should have failed"))
122
+	c.Assert(out, checker.Contains, "Renaming a container with the same name", check.Commentf("%v", err))
123
+}