github.com/goern/docker@v1.9.0-rc1/integration-cli/docker_cli_rm_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/go-check/check"
     8  )
     9  
    10  func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
    11  	testRequires(c, DaemonIsLinux)
    12  	testRequires(c, SameHostDaemon)
    13  
    14  	dockerCmd(c, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
    15  
    16  	if err := os.Remove("/tmp/testing"); err != nil {
    17  		c.Fatal(err)
    18  	}
    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  	if _, _, err := dockerCmdWithError("rm", "foo"); err == nil {
    35  		c.Fatalf("Expected error, can't rm a running container")
    36  	}
    37  }
    38  
    39  func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
    40  	testRequires(c, DaemonIsLinux)
    41  	createRunningContainer(c, "foo")
    42  
    43  	// Stop then remove with -s
    44  	dockerCmd(c, "rm", "-f", "foo")
    45  }
    46  
    47  func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
    48  	testRequires(c, DaemonIsLinux)
    49  	dockerfile1 := `FROM busybox:latest
    50  	ENTRYPOINT ["/bin/true"]`
    51  	img := "test-container-orphaning"
    52  	dockerfile2 := `FROM busybox:latest
    53  	ENTRYPOINT ["/bin/true"]
    54  	MAINTAINER Integration Tests`
    55  
    56  	// build first dockerfile
    57  	img1, err := buildImage(img, dockerfile1, true)
    58  	if err != nil {
    59  		c.Fatalf("Could not build image %s: %v", img, err)
    60  	}
    61  	// run container on first image
    62  	if out, _, err := dockerCmdWithError("run", img); err != nil {
    63  		c.Fatalf("Could not run image %s: %v: %s", img, err, out)
    64  	}
    65  
    66  	// rebuild dockerfile with a small addition at the end
    67  	if _, err := buildImage(img, dockerfile2, true); err != nil {
    68  		c.Fatalf("Could not rebuild image %s: %v", img, err)
    69  	}
    70  	// try to remove the image, should error out.
    71  	if out, _, err := dockerCmdWithError("rmi", img); err == nil {
    72  		c.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
    73  	}
    74  
    75  	// check if we deleted the first image
    76  	out, _, err := dockerCmdWithError("images", "-q", "--no-trunc")
    77  	if err != nil {
    78  		c.Fatalf("%v: %s", err, out)
    79  	}
    80  	if !strings.Contains(out, img1) {
    81  		c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
    82  	}
    83  }
    84  
    85  func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
    86  	if out, _, err := dockerCmdWithError("rm", "unknown"); err == nil {
    87  		c.Fatal("Expected error on rm unknown container, got none")
    88  	} else if !strings.Contains(out, "failed to remove containers") {
    89  		c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out)
    90  	}
    91  }
    92  
    93  func createRunningContainer(c *check.C, name string) {
    94  	dockerCmd(c, "run", "-dt", "--name", name, "busybox", "top")
    95  }