github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_rm_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/docker/docker/integration-cli/checker"
     8  	"github.com/docker/docker/integration-cli/cli/build"
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
    13  	testRequires(c, SameHostDaemon)
    14  
    15  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
    16  
    17  	tempDir, err := ioutil.TempDir("", "test-rm-container-with-removed-volume-")
    18  	if err != nil {
    19  		c.Fatalf("failed to create temporary directory: %s", tempDir)
    20  	}
    21  	defer os.RemoveAll(tempDir)
    22  
    23  	dockerCmd(c, "run", "--name", "losemyvolumes", "-v", tempDir+":"+prefix+slash+"test", "busybox", "true")
    24  
    25  	err = os.RemoveAll(tempDir)
    26  	c.Assert(err, check.IsNil)
    27  
    28  	dockerCmd(c, "rm", "-v", "losemyvolumes")
    29  }
    30  
    31  func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
    32  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
    33  
    34  	dockerCmd(c, "run", "--name", "foo", "-v", prefix+slash+"srv", "busybox", "true")
    35  
    36  	dockerCmd(c, "rm", "-v", "foo")
    37  }
    38  
    39  func (s *DockerSuite) TestRmContainerRunning(c *check.C) {
    40  	createRunningContainer(c, "foo")
    41  
    42  	res, _, err := dockerCmdWithError("rm", "foo")
    43  	c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
    44  	c.Assert(res, checker.Contains, "cannot remove a running container")
    45  }
    46  
    47  func (s *DockerSuite) TestRmContainerForceRemoveRunning(c *check.C) {
    48  	createRunningContainer(c, "foo")
    49  
    50  	// Stop then remove with -f
    51  	dockerCmd(c, "rm", "-f", "foo")
    52  }
    53  
    54  func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
    55  	dockerfile1 := `FROM busybox:latest
    56  	ENTRYPOINT ["true"]`
    57  	img := "test-container-orphaning"
    58  	dockerfile2 := `FROM busybox:latest
    59  	ENTRYPOINT ["true"]
    60  	MAINTAINER Integration Tests`
    61  
    62  	// build first dockerfile
    63  	buildImageSuccessfully(c, img, build.WithDockerfile(dockerfile1))
    64  	img1 := getIDByName(c, img)
    65  	// run container on first image
    66  	dockerCmd(c, "run", img)
    67  	// rebuild dockerfile with a small addition at the end
    68  	buildImageSuccessfully(c, img, build.WithDockerfile(dockerfile2))
    69  	// try to remove the image, should not error out.
    70  	out, _, err := dockerCmdWithError("rmi", img)
    71  	c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
    72  
    73  	// check if we deleted the first image
    74  	out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
    75  	c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
    76  
    77  }
    78  
    79  func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
    80  	out, _, err := dockerCmdWithError("rm", "unknown")
    81  	c.Assert(err, checker.NotNil, check.Commentf("Expected error on rm unknown container, got none"))
    82  	c.Assert(out, checker.Contains, "No such container")
    83  }
    84  
    85  func createRunningContainer(c *check.C, name string) {
    86  	runSleepingContainer(c, "-dt", "--name", name)
    87  }