Skip to content

Commit

Permalink
Merge pull request moby#10082 from jfrazelle/10081-fix-renaming
Browse files Browse the repository at this point in the history
Renaming a container with an invalid name should fail
  • Loading branch information
vieux committed Jan 14, 2015
2 parents 37b6940 + a922816 commit f67c6d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
22 changes: 13 additions & 9 deletions daemon/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ func (daemon *Daemon) ContainerRename(job *engine.Job) engine.Status {
if len(job.Args) != 2 {
return job.Errorf("usage: %s OLD_NAME NEW_NAME", job.Name)
}
old_name := job.Args[0]
new_name := job.Args[1]
oldName := job.Args[0]
newName := job.Args[1]

container := daemon.Get(old_name)
container := daemon.Get(oldName)
if container == nil {
return job.Errorf("No such container: %s", old_name)
return job.Errorf("No such container: %s", oldName)
}

oldName = container.Name

container.Lock()
defer container.Unlock()
if err := daemon.containerGraph.Delete(container.Name); err != nil {
return job.Errorf("Failed to delete container %q: %v", old_name, err)
}
if _, err := daemon.reserveName(container.ID, new_name); err != nil {
if _, err := daemon.reserveName(container.ID, newName); err != nil {
return job.Errorf("Error when allocating new name: %s", err)
}
container.Name = new_name

container.Name = newName

if err := daemon.containerGraph.Delete(oldName); err != nil {
return job.Errorf("Failed to delete container %q: %v", oldName, err)
}

return engine.StatusOK
}
20 changes: 20 additions & 0 deletions integration-cli/docker_cli_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,23 @@ func TestRenameCheckNames(t *testing.T) {

logDone("rename - running container")
}

func TestRenameInvalidName(t *testing.T) {
defer deleteAllContainers()
runCmd := exec.Command(dockerBinary, "run", "--name", "myname", "-d", "busybox", "top")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
t.Fatalf(out, err)
}

runCmd = exec.Command(dockerBinary, "rename", "myname", "new:invalid")
if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Invalid container name") {
t.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err)
}

runCmd = exec.Command(dockerBinary, "ps", "-a")
if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "myname") {
t.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err)
}

logDone("rename - invalid container name")
}

0 comments on commit f67c6d3

Please sign in to comment.