فهرست منبع

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

Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com>
Sainath Grandhi 9 سال پیش
والد
کامیت
3e8c16ef6d
2فایلهای تغییر یافته به همراه22 افزوده شده و 0 حذف شده
  1. 9 0
      daemon/rename.go
  2. 13 0
      integration-cli/docker_cli_rename_test.go

+ 9 - 0
daemon/rename.go

@@ -21,6 +21,10 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
 		return fmt.Errorf("Neither old nor new names may be empty")
 		return fmt.Errorf("Neither old nor new names may be empty")
 	}
 	}
 
 
+	if newName[0] != '/' {
+		newName = "/" + newName
+	}
+
 	container, err := daemon.GetContainer(oldName)
 	container, err := daemon.GetContainer(oldName)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
@@ -31,6 +35,11 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
 
 
 	container.Lock()
 	container.Lock()
 	defer container.Unlock()
 	defer container.Unlock()
+
+	if oldName == newName {
+		return fmt.Errorf("Renaming a container with the same name as its current name")
+	}
+
 	if newName, err = daemon.reserveName(container.ID, newName); err != nil {
 	if newName, err = daemon.reserveName(container.ID, newName); err != nil {
 		return fmt.Errorf("Error when allocating new name: %v", err)
 		return fmt.Errorf("Error when allocating new name: %v", err)
 	}
 	}

+ 13 - 0
integration-cli/docker_cli_rename_test.go

@@ -108,3 +108,16 @@ func (s *DockerSuite) TestRenameAnonymousContainer(c *check.C) {
 	_, _, err := dockerCmdWithError("run", "--net", "network1", "busybox", "ping", count, "1", "container1")
 	_, _, 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))
 	c.Assert(err, check.IsNil, check.Commentf("Embedded DNS lookup fails after renaming anonymous container: %v", err))
 }
 }
+
+func (s *DockerSuite) TestRenameContainerWithSameName(c *check.C) {
+	out, _ := runSleepingContainer(c, "--name", "old")
+	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))
+}