github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/integration-cli/docker_cli_rm_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     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, DaemonIsLinux)
    13  	testRequires(c, SameHostDaemon)
    14  
    15  	dockerCmd(c, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
    16  
    17  	err := os.Remove("/tmp/testing")
    18  	c.Assert(err, check.IsNil)
    19  
    20  	dockerCmd(c, "rm", "-v", "losemyvolumes")
    21  }
    22  
    23  func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
    24  	testRequires(c, DaemonIsLinux)
    25  	dockerCmd(c, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
    26  
    27  	dockerCmd(c, "rm", "-v", "foo")
    28  }
    29  
    30  func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
    31  	testRequires(c, DaemonIsLinux)
    32  	createRunningContainer(c, "foo")
    33  
    34  	_, _, err := dockerCmdWithError("rm", "foo")
    35  	c.Assert(err, checker.NotNil, check.Commentf("Expected error, can't rm a running container"))
    36  }
    37  
    38  func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
    39  	testRequires(c, DaemonIsLinux)
    40  	createRunningContainer(c, "foo")
    41  
    42  	// Stop then remove with -s
    43  	dockerCmd(c, "rm", "-f", "foo")
    44  }
    45  
    46  func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
    47  	testRequires(c, DaemonIsLinux)
    48  	dockerfile1 := `FROM busybox:latest
    49  	ENTRYPOINT ["/bin/true"]`
    50  	img := "test-container-orphaning"
    51  	dockerfile2 := `FROM busybox:latest
    52  	ENTRYPOINT ["/bin/true"]
    53  	MAINTAINER Integration Tests`
    54  
    55  	// build first dockerfile
    56  	img1, err := buildImage(img, dockerfile1, true)
    57  	c.Assert(err, check.IsNil, check.Commentf("Could not build image %s", img))
    58  	// run container on first image
    59  	dockerCmd(c, "run", img)
    60  	// rebuild dockerfile with a small addition at the end
    61  	_, err = buildImage(img, dockerfile2, true)
    62  	c.Assert(err, check.IsNil, check.Commentf("Could not rebuild image %s", img))
    63  	// try to remove the image, should not error out.
    64  	out, _, err := dockerCmdWithError("rmi", img)
    65  	c.Assert(err, check.IsNil, check.Commentf("Expected to removing the image, but failed: %s", out))
    66  
    67  	// check if we deleted the first image
    68  	out, _ = dockerCmd(c, "images", "-q", "--no-trunc")
    69  	c.Assert(out, checker.Contains, img1, check.Commentf("Orphaned container (could not find %q in docker images): %s", img1, out))
    70  
    71  }
    72  
    73  func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
    74  	if out, _, err := dockerCmdWithError("rm", "unknown"); err == nil {
    75  		c.Fatal("Expected error on rm unknown container, got none")
    76  	} else if !strings.Contains(out, "Failed to remove container") {
    77  		c.Fatalf("Expected output to contain 'Failed to remove container', got %q", out)
    78  	}
    79  }
    80  
    81  func createRunningContainer(c *check.C, name string) {
    82  	dockerCmd(c, "run", "-dt", "--name", name, "busybox", "top")
    83  }