integration-cli/docker_cli_rm_test.go
39103e72
 package main
 
 import (
 	"os"
 	"os/exec"
69f9d488
 	"strings"
39103e72
 	"testing"
 )
 
cc54b77f
 func TestRmContainerWithRemovedVolume(t *testing.T) {
b686b65c
 	testRequires(t, SameHostDaemon)
321874f3
 	defer deleteAllContainers()
70407ce4
 
39103e72
 	cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
 	if _, err := runCommand(cmd); err != nil {
 		t.Fatal(err)
 	}
 
 	if err := os.Remove("/tmp/testing"); err != nil {
 		t.Fatal(err)
 	}
 
 	cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
45407cf0
 	if out, _, err := runCommandWithOutput(cmd); err != nil {
 		t.Fatal(out, err)
39103e72
 	}
 
 	logDone("rm - removed volume")
 }
fcbc717f
 
cc54b77f
 func TestRmContainerWithVolume(t *testing.T) {
70407ce4
 	defer deleteAllContainers()
 
fcbc717f
 	cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
 	if _, err := runCommand(cmd); err != nil {
 		t.Fatal(err)
 	}
 
 	cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
 	if _, err := runCommand(cmd); err != nil {
 		t.Fatal(err)
 	}
 
 	logDone("rm - volume")
 }
 
cc54b77f
 func TestRmRunningContainer(t *testing.T) {
70407ce4
 	defer deleteAllContainers()
 
d689a5e1
 	createRunningContainer(t, "foo")
fcbc717f
 
 	// Test cannot remove running container
d689a5e1
 	cmd := exec.Command(dockerBinary, "rm", "foo")
fcbc717f
 	if _, err := runCommand(cmd); err == nil {
 		t.Fatalf("Expected error, can't rm a running container")
 	}
 
d689a5e1
 	logDone("rm - running container")
 }
 
6599aa33
 func TestRmRunningContainerCheckError409(t *testing.T) {
70407ce4
 	defer deleteAllContainers()
 
6599aa33
 	createRunningContainer(t, "foo")
 
 	endpoint := "/containers/foo"
 	_, err := sockRequest("DELETE", endpoint, nil)
 
 	if err == nil {
 		t.Fatalf("Expected error, can't rm a running container")
 	}
 	if !strings.Contains(err.Error(), "409 Conflict") {
a75b02fe
 		t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
6599aa33
 	}
 
 	logDone("rm - running container")
 }
 
cc54b77f
 func TestRmForceRemoveRunningContainer(t *testing.T) {
70407ce4
 	defer deleteAllContainers()
 
d689a5e1
 	createRunningContainer(t, "foo")
 
 	// Stop then remove with -s
95f86da6
 	cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
fcbc717f
 	if _, err := runCommand(cmd); err != nil {
 		t.Fatal(err)
 	}
 
95f86da6
 	logDone("rm - running container with --force=true")
d689a5e1
 }
 
cc54b77f
 func TestRmContainerOrphaning(t *testing.T) {
70407ce4
 	defer deleteAllContainers()
 
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)
70407ce4
 	defer deleteImages(img1)
69f9d488
 	if err != nil {
 		t.Fatalf("Could not build image %s: %v", img, err)
 	}
 	// run container on first image
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
 		t.Fatalf("Could not run image %s: %v: %s", img, err, out)
 	}
 	// rebuild dockerfile with a small addition at the end
 	if _, err := buildImage(img, dockerfile2, true); err != nil {
 		t.Fatalf("Could not rebuild image %s: %v", img, err)
 	}
 	// try to remove the image, should error out.
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
 		t.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
 	}
 	// check if we deleted the first image
 	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
 	if err != nil {
 		t.Fatalf("%v: %s", err, out)
 	}
 	if !strings.Contains(out, img1) {
be31a66b
 		t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
69f9d488
 	}
 
 	logDone("rm - container orphaning")
 }
 
c68e6b15
 func TestRmInvalidContainer(t *testing.T) {
3e473c08
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
c68e6b15
 		t.Fatal("Expected error on rm unknown container, got none")
3e473c08
 	} else if !strings.Contains(out, "failed to remove one or more containers") {
18d9f197
 		t.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
69f9d488
 	}
 
c68e6b15
 	logDone("rm - delete unknown container")
69f9d488
 }
 
d689a5e1
 func createRunningContainer(t *testing.T, name string) {
 	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
 	if _, err := runCommand(cmd); err != nil {
 		t.Fatal(err)
 	}
fcbc717f
 }