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