Renaming a container with an invalid name should fail

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)
This commit is contained in:
Jessica Frazelle 2015-01-13 17:30:49 -08:00
Родитель 8ac075b846
Коммит a92281637f
2 изменённых файлов: 33 добавлений и 9 удалений

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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")
}