github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/integration-cli/docker_cli_rename_test.go (about) 1 package main 2 3 import ( 4 "os/exec" 5 "strings" 6 7 "github.com/docker/docker/pkg/stringid" 8 "github.com/go-check/check" 9 ) 10 11 func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) { 12 runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") 13 out, _, err := runCommandWithOutput(runCmd) 14 if err != nil { 15 c.Fatalf(out, err) 16 } 17 18 cleanedContainerID := strings.TrimSpace(out) 19 20 runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID) 21 out, _, err = runCommandWithOutput(runCmd) 22 if err != nil { 23 c.Fatalf(out, err) 24 } 25 26 name, err := inspectField(cleanedContainerID, "Name") 27 28 newName := "new_name" + stringid.GenerateRandomID() 29 runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) 30 out, _, err = runCommandWithOutput(runCmd) 31 if err != nil { 32 c.Fatalf(out, err) 33 } 34 35 name, err = inspectField(cleanedContainerID, "Name") 36 if err != nil { 37 c.Fatal(err) 38 } 39 if name != "/"+newName { 40 c.Fatal("Failed to rename container ", name) 41 } 42 43 } 44 45 func (s *DockerSuite) TestRenameRunningContainer(c *check.C) { 46 runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") 47 out, _, err := runCommandWithOutput(runCmd) 48 if err != nil { 49 c.Fatalf(out, err) 50 } 51 52 newName := "new_name" + stringid.GenerateRandomID() 53 cleanedContainerID := strings.TrimSpace(out) 54 runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) 55 out, _, err = runCommandWithOutput(runCmd) 56 if err != nil { 57 c.Fatalf(out, err) 58 } 59 60 name, err := inspectField(cleanedContainerID, "Name") 61 if err != nil { 62 c.Fatal(err) 63 } 64 if name != "/"+newName { 65 c.Fatal("Failed to rename container ") 66 } 67 } 68 69 func (s *DockerSuite) TestRenameCheckNames(c *check.C) { 70 runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") 71 out, _, err := runCommandWithOutput(runCmd) 72 if err != nil { 73 c.Fatalf(out, err) 74 } 75 76 newName := "new_name" + stringid.GenerateRandomID() 77 runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) 78 out, _, err = runCommandWithOutput(runCmd) 79 if err != nil { 80 c.Fatalf(out, err) 81 } 82 83 name, err := inspectField(newName, "Name") 84 if err != nil { 85 c.Fatal(err) 86 } 87 if name != "/"+newName { 88 c.Fatal("Failed to rename container ") 89 } 90 91 name, err = inspectField("first_name", "Name") 92 if err == nil && !strings.Contains(err.Error(), "No such image or container: first_name") { 93 c.Fatal(err) 94 } 95 } 96 97 func (s *DockerSuite) TestRenameInvalidName(c *check.C) { 98 runCmd := exec.Command(dockerBinary, "run", "--name", "myname", "-d", "busybox", "top") 99 if out, _, err := runCommandWithOutput(runCmd); err != nil { 100 c.Fatalf(out, err) 101 } 102 103 runCmd = exec.Command(dockerBinary, "rename", "myname", "new:invalid") 104 if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Invalid container name") { 105 c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err) 106 } 107 108 runCmd = exec.Command(dockerBinary, "ps", "-a") 109 if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "myname") { 110 c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err) 111 } 112 }