github.com/michael-k/docker@v1.7.0-rc2/integration-cli/docker_cli_rm_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"github.com/go-check/check"
     9  )
    10  
    11  func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
    12  	testRequires(c, SameHostDaemon)
    13  
    14  	cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
    15  	if _, err := runCommand(cmd); err != nil {
    16  		c.Fatal(err)
    17  	}
    18  
    19  	if err := os.Remove("/tmp/testing"); err != nil {
    20  		c.Fatal(err)
    21  	}
    22  
    23  	cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
    24  	if out, _, err := runCommandWithOutput(cmd); err != nil {
    25  		c.Fatal(out, err)
    26  	}
    27  
    28  }
    29  
    30  func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
    31  
    32  	cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
    33  	if _, err := runCommand(cmd); err != nil {
    34  		c.Fatal(err)
    35  	}
    36  
    37  	cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
    38  	if _, err := runCommand(cmd); err != nil {
    39  		c.Fatal(err)
    40  	}
    41  
    42  }
    43  
    44  func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
    45  
    46  	createRunningContainer(c, "foo")
    47  
    48  	// Test cannot remove running container
    49  	cmd := exec.Command(dockerBinary, "rm", "foo")
    50  	if _, err := runCommand(cmd); err == nil {
    51  		c.Fatalf("Expected error, can't rm a running container")
    52  	}
    53  
    54  }
    55  
    56  func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
    57  
    58  	createRunningContainer(c, "foo")
    59  
    60  	// Stop then remove with -s
    61  	cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
    62  	if _, err := runCommand(cmd); err != nil {
    63  		c.Fatal(err)
    64  	}
    65  
    66  }
    67  
    68  func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
    69  
    70  	dockerfile1 := `FROM busybox:latest
    71  	ENTRYPOINT ["/bin/true"]`
    72  	img := "test-container-orphaning"
    73  	dockerfile2 := `FROM busybox:latest
    74  	ENTRYPOINT ["/bin/true"]
    75  	MAINTAINER Integration Tests`
    76  
    77  	// build first dockerfile
    78  	img1, err := buildImage(img, dockerfile1, true)
    79  	if err != nil {
    80  		c.Fatalf("Could not build image %s: %v", img, err)
    81  	}
    82  	// run container on first image
    83  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
    84  		c.Fatalf("Could not run image %s: %v: %s", img, err, out)
    85  	}
    86  	// rebuild dockerfile with a small addition at the end
    87  	if _, err := buildImage(img, dockerfile2, true); err != nil {
    88  		c.Fatalf("Could not rebuild image %s: %v", img, err)
    89  	}
    90  	// try to remove the image, should error out.
    91  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
    92  		c.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
    93  	}
    94  	// check if we deleted the first image
    95  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
    96  	if err != nil {
    97  		c.Fatalf("%v: %s", err, out)
    98  	}
    99  	if !strings.Contains(out, img1) {
   100  		c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
   101  	}
   102  
   103  }
   104  
   105  func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
   106  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
   107  		c.Fatal("Expected error on rm unknown container, got none")
   108  	} else if !strings.Contains(out, "failed to remove containers") {
   109  		c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out)
   110  	}
   111  
   112  }
   113  
   114  func createRunningContainer(c *check.C, name string) {
   115  	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
   116  	if _, err := runCommand(cmd); err != nil {
   117  		c.Fatal(err)
   118  	}
   119  }