github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_rename_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/pkg/integration/checker"
     7  	"github.com/docker/docker/pkg/stringid"
     8  	"github.com/go-check/check"
     9  )
    10  
    11  func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
    12  	testRequires(c, DaemonIsLinux)
    13  	out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
    14  
    15  	cleanedContainerID := strings.TrimSpace(out)
    16  	dockerCmd(c, "wait", cleanedContainerID)
    17  
    18  	name, err := inspectField(cleanedContainerID, "Name")
    19  	newName := "new_name" + stringid.GenerateNonCryptoID()
    20  	dockerCmd(c, "rename", "first_name", newName)
    21  
    22  	name, err = inspectField(cleanedContainerID, "Name")
    23  	c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
    24  	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
    25  
    26  }
    27  
    28  func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
    29  	testRequires(c, DaemonIsLinux)
    30  	out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
    31  
    32  	newName := "new_name" + stringid.GenerateNonCryptoID()
    33  	cleanedContainerID := strings.TrimSpace(out)
    34  	dockerCmd(c, "rename", "first_name", newName)
    35  
    36  	name, err := inspectField(cleanedContainerID, "Name")
    37  	c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
    38  	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
    39  }
    40  
    41  func (s *DockerSuite) TestRenameRunningContainerAndReuse(c *check.C) {
    42  	testRequires(c, DaemonIsLinux)
    43  	out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "top")
    44  	c.Assert(waitRun("first_name"), check.IsNil)
    45  
    46  	newName := "new_name"
    47  	ContainerID := strings.TrimSpace(out)
    48  	dockerCmd(c, "rename", "first_name", newName)
    49  
    50  	name, err := inspectField(ContainerID, "Name")
    51  	c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
    52  	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container"))
    53  
    54  	out, _ = dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "top")
    55  	c.Assert(waitRun("first_name"), check.IsNil)
    56  	newContainerID := strings.TrimSpace(out)
    57  	name, err = inspectField(newContainerID, "Name")
    58  	c.Assert(err, checker.IsNil, check.Commentf("Failed to reuse container name"))
    59  	c.Assert(name, checker.Equals, "/first_name", check.Commentf("Failed to reuse container name"))
    60  }
    61  
    62  func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
    63  	testRequires(c, DaemonIsLinux)
    64  	dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
    65  
    66  	newName := "new_name" + stringid.GenerateNonCryptoID()
    67  	dockerCmd(c, "rename", "first_name", newName)
    68  
    69  	name, err := inspectField(newName, "Name")
    70  	c.Assert(err, checker.IsNil, check.Commentf("Failed to rename container %s", name))
    71  	c.Assert(name, checker.Equals, "/"+newName, check.Commentf("Failed to rename container %s", name))
    72  
    73  	name, err = inspectField("first_name", "Name")
    74  	c.Assert(err, checker.NotNil, check.Commentf(name))
    75  	c.Assert(err.Error(), checker.Contains, "No such image or container: first_name")
    76  }
    77  
    78  func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
    79  	testRequires(c, DaemonIsLinux)
    80  	dockerCmd(c, "run", "--name", "myname", "-d", "busybox", "top")
    81  
    82  	out, _, err := dockerCmdWithError("rename", "myname", "new:invalid")
    83  	c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
    84  	c.Assert(out, checker.Contains, "Invalid container name", check.Commentf("%v", err))
    85  
    86  	out, _, err = dockerCmdWithError("rename", "myname", "")
    87  	c.Assert(err, checker.NotNil, check.Commentf("Renaming container to invalid name should have failed: %s", out))
    88  	c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
    89  
    90  	out, _, err = dockerCmdWithError("rename", "", "newname")
    91  	c.Assert(err, checker.NotNil, check.Commentf("Renaming container with empty name should have failed: %s", out))
    92  	c.Assert(out, checker.Contains, "may be empty", check.Commentf("%v", err))
    93  
    94  	out, _ = dockerCmd(c, "ps", "-a")
    95  	c.Assert(out, checker.Contains, "myname", check.Commentf("Output of docker ps should have included 'myname': %s", out))
    96  }