1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_rename_test.go
Doug Davis 848792c42e Fix 'rename' error msg and error checking
`docker rename foo ''` would result in:
```
usage: docker rename OLD_NAME NEW_NAME
```
which is the old engine's way of return errors - yes that's in the
daemon code.  So I fixed that error msg to just be normal.

While doing that I noticed that using an empty string for the
source container name failed but didn't print any error message at all.
This is because we would generate a URL like: ../containers//rename/..
which would cause a 301 redirect to ../containers/rename/..
however the CLI code doesn't actually deal with 301's - it just ignores
them and returns back to the CLI code/caller.

Rather than changing the CLI to deal with 3xx error codes, which would
probably be a good thing to do in a follow-on PR, for this immediate
issue I just added a cli-side check for empty strings for both old and
new names. This way we catch it even before we hit the daemon.

API callers will get a 404, assuming they follow the 301, for the
case of the src being empty, and the new error msg when the destination
is empty - so we should be good now.

Add tests for both cases too.

Signed-off-by: Doug Davis <dug@us.ibm.com>
2015-09-18 11:12:22 -07:00

88 lines
2.7 KiB
Go

package main
import (
"strings"
"github.com/docker/docker/pkg/stringid"
"github.com/go-check/check"
)
func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "wait", cleanedContainerID)
name, err := inspectField(cleanedContainerID, "Name")
newName := "new_name" + stringid.GenerateNonCryptoID()
dockerCmd(c, "rename", "first_name", newName)
name, err = inspectField(cleanedContainerID, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ", name)
}
}
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
newName := "new_name" + stringid.GenerateNonCryptoID()
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "rename", "first_name", newName)
name, err := inspectField(cleanedContainerID, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ")
}
}
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
newName := "new_name" + stringid.GenerateNonCryptoID()
dockerCmd(c, "rename", "first_name", newName)
name, err := inspectField(newName, "Name")
if err != nil {
c.Fatal(err)
}
if name != "/"+newName {
c.Fatal("Failed to rename container ")
}
name, err = inspectField("first_name", "Name")
if err == nil && !strings.Contains(err.Error(), "No such image or container: first_name") {
c.Fatal(err)
}
}
func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "myname", "-d", "busybox", "top")
if out, _, err := dockerCmdWithError("rename", "myname", "new:invalid"); err == nil || !strings.Contains(out, "Invalid container name") {
c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err)
}
if out, _, err := dockerCmdWithError("rename", "myname", ""); err == nil || !strings.Contains(out, "may be empty") {
c.Fatalf("Renaming container to empty name should have failed: %s\n%v", out, err)
}
if out, _, err := dockerCmdWithError("rename", "", "newname"); err == nil || !strings.Contains(out, "may be empty") {
c.Fatalf("Renaming container to empty name should have failed: %s\n%v", out, err)
}
if out, _, err := dockerCmdWithError("ps", "-a"); err != nil || !strings.Contains(out, "myname") {
c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err)
}
}