2014-10-04 22:47:54 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
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) {
|
2014-10-04 22:47:54 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-10-04 22:47:54 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
name, err := inspectField(cleanedContainerID, "Name")
|
|
|
|
|
2015-04-23 10:11:20 -04:00
|
|
|
newName := "new_name" + stringid.GenerateRandomID()
|
|
|
|
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
name, err = inspectField(cleanedContainerID, "Name")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
2015-04-23 10:11:20 -04:00
|
|
|
if name != "/"+newName {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Failed to rename container ", name)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
|
2014-10-04 22:47:54 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 10:11:20 -04:00
|
|
|
newName := "new_name" + stringid.GenerateRandomID()
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-04-23 10:11:20 -04:00
|
|
|
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
name, err := inspectField(cleanedContainerID, "Name")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
2015-04-23 10:11:20 -04:00
|
|
|
if name != "/"+newName {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Failed to rename container ")
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
|
2014-10-04 22:47:54 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 10:11:20 -04:00
|
|
|
newName := "new_name" + stringid.GenerateRandomID()
|
|
|
|
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName)
|
2014-10-04 22:47:54 -04:00
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
2015-04-23 10:11:20 -04:00
|
|
|
name, err := inspectField(newName, "Name")
|
2014-10-04 22:47:54 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
2015-04-23 10:11:20 -04:00
|
|
|
if name != "/"+newName {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Failed to rename container ")
|
2014-10-04 22:47:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
name, err = inspectField("first_name", "Name")
|
|
|
|
if err == nil && !strings.Contains(err.Error(), "No such image or container: first_name") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
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-01-13 20:30:49 -05:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", "myname", "-d", "busybox", "top")
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf(out, err)
|
2015-01-13 20:30:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "rename", "myname", "new:invalid")
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Invalid container name") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err)
|
2015-01-13 20:30:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "ps", "-a")
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "myname") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err)
|
2015-01-13 20:30:49 -05:00
|
|
|
}
|
|
|
|
}
|