github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_rm_test.go (about)

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