2014-10-04 22:47:54 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-23 10:11:20 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-10-04 22:47:54 -04:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "wait", cleanedContainerID)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
name, err := inspectField(cleanedContainerID, "Name")
|
2015-07-28 20:19:17 -04:00
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
name, err = inspectField(cleanedContainerID, "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
|
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-07-28 20:19:17 -04:00
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
name, err := inspectField(cleanedContainerID, "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
|
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-07-28 20:19:17 -04:00
|
|
|
newName := "new_name" + stringid.GenerateNonCryptoID()
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
2015-04-23 10:11:20 -04:00
|
|
|
name, err := inspectField(newName, "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
|
|
|
|
c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
name, err = inspectField("first_name", "Name")
|
2015-10-08 05:26:11 -04:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf(name))
|
|
|
|
c.Assert(err.Error(), checker.Contains, "No such image or container: first_name")
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
2015-01-13 20:30:49 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "run", "--name", "myname", "-d", "busybox", "top")
|
2015-01-13 20:30:49 -05:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _, err := dockerCmdWithError("rename", "myname", "new:invalid")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, "Invalid container name", check.Commentf("%v", err))
|
2015-01-13 20:30:49 -05:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _, err = dockerCmdWithError("rename", "myname", "")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
|
2015-09-18 14:05:04 -04:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _, err = dockerCmdWithError("rename", "", "newname")
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out))
|
|
|
|
c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
|
2015-09-18 14:05:04 -04:00
|
|
|
|
2015-10-08 05:26:11 -04:00
|
|
|
out, _ = dockerCmd(c, "ps", "-a")
|
|
|
|
c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
|
2015-01-13 20:30:49 -05:00
|
|
|
}
|