integration-cli/docker_cli_rm_test.go
39103e72
 package main
 
 import (
 	"os"
 	"os/exec"
69f9d488
 	"strings"
dc944ea7
 
 	"github.com/go-check/check"
39103e72
 )
 
dc944ea7
 func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
 	testRequires(c, SameHostDaemon)
70407ce4
 
39103e72
 	cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
39103e72
 	}
 
 	if err := os.Remove("/tmp/testing"); err != nil {
dc944ea7
 		c.Fatal(err)
39103e72
 	}
 
 	cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
45407cf0
 	if out, _, err := runCommandWithOutput(cmd); err != nil {
dc944ea7
 		c.Fatal(out, err)
39103e72
 	}
 
 }
fcbc717f
 
dc944ea7
 func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
70407ce4
 
fcbc717f
 	cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
fcbc717f
 	}
 
 	cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
fcbc717f
 	}
 
 }
 
dc944ea7
 func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
70407ce4
 
dc944ea7
 	createRunningContainer(c, "foo")
fcbc717f
 
 	// Test cannot remove running container
d689a5e1
 	cmd := exec.Command(dockerBinary, "rm", "foo")
fcbc717f
 	if _, err := runCommand(cmd); err == nil {
dc944ea7
 		c.Fatalf("Expected error, can't rm a running container")
fcbc717f
 	}
 
d689a5e1
 }
 
dc944ea7
 func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
70407ce4
 
dc944ea7
 	createRunningContainer(c, "foo")
d689a5e1
 
 	// Stop then remove with -s
95f86da6
 	cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
fcbc717f
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
fcbc717f
 	}
 
d689a5e1
 }
 
dc944ea7
 func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
70407ce4
 
69f9d488
 	dockerfile1 := `FROM busybox:latest
 	ENTRYPOINT ["/bin/true"]`
 	img := "test-container-orphaning"
 	dockerfile2 := `FROM busybox:latest
 	ENTRYPOINT ["/bin/true"]
 	MAINTAINER Integration Tests`
 
 	// build first dockerfile
 	img1, err := buildImage(img, dockerfile1, true)
 	if err != nil {
dc944ea7
 		c.Fatalf("Could not build image %s: %v", img, err)
69f9d488
 	}
 	// run container on first image
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
dc944ea7
 		c.Fatalf("Could not run image %s: %v: %s", img, err, out)
69f9d488
 	}
 	// rebuild dockerfile with a small addition at the end
 	if _, err := buildImage(img, dockerfile2, true); err != nil {
dc944ea7
 		c.Fatalf("Could not rebuild image %s: %v", img, err)
69f9d488
 	}
 	// try to remove the image, should error out.
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
dc944ea7
 		c.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
69f9d488
 	}
 	// check if we deleted the first image
 	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
 	if err != nil {
dc944ea7
 		c.Fatalf("%v: %s", err, out)
69f9d488
 	}
 	if !strings.Contains(out, img1) {
dc944ea7
 		c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
69f9d488
 	}
 
 }
 
dc944ea7
 func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
3e473c08
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
dc944ea7
 		c.Fatal("Expected error on rm unknown container, got none")
5a6db4fd
 	} else if !strings.Contains(out, "failed to remove containers") {
 		c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out)
69f9d488
 	}
 
 }
 
dc944ea7
 func createRunningContainer(c *check.C, name string) {
d689a5e1
 	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
 	if _, err := runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
d689a5e1
 	}
fcbc717f
 }