2014-04-14 08:43:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-08-05 19:11:09 -04:00
|
|
|
"strings"
|
2014-04-14 08:43:01 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-09-19 13:51:37 -04:00
|
|
|
func TestRmContainerWithRemovedVolume(t *testing.T) {
|
2015-02-18 23:06:23 -05:00
|
|
|
testRequires(t, SameHostDaemon)
|
2015-02-25 23:16:44 -05:00
|
|
|
defer deleteAllContainers()
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-04-14 08:43:01 -04:00
|
|
|
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")
|
2014-08-28 10:18:08 -04:00
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
|
|
|
t.Fatal(out, err)
|
2014-04-14 08:43:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("rm - removed volume")
|
|
|
|
}
|
2014-04-18 13:40:12 -04:00
|
|
|
|
2014-09-19 13:51:37 -04:00
|
|
|
func TestRmContainerWithVolume(t *testing.T) {
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-04-18 13:40:12 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-09-19 13:51:37 -04:00
|
|
|
func TestRmRunningContainer(t *testing.T) {
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-07-06 16:44:56 -04:00
|
|
|
createRunningContainer(t, "foo")
|
2014-04-18 13:40:12 -04:00
|
|
|
|
|
|
|
// Test cannot remove running container
|
2014-07-06 16:44:56 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "rm", "foo")
|
2014-04-18 13:40:12 -04:00
|
|
|
if _, err := runCommand(cmd); err == nil {
|
|
|
|
t.Fatalf("Expected error, can't rm a running container")
|
|
|
|
}
|
|
|
|
|
2014-07-06 16:44:56 -04:00
|
|
|
logDone("rm - running container")
|
|
|
|
}
|
|
|
|
|
2014-12-09 15:04:33 -05:00
|
|
|
func TestRmRunningContainerCheckError409(t *testing.T) {
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-12-09 15:04:33 -05:00
|
|
|
createRunningContainer(t, "foo")
|
|
|
|
|
|
|
|
endpoint := "/containers/foo"
|
2015-04-11 17:49:14 -04:00
|
|
|
_, _, err := sockRequest("DELETE", endpoint, nil)
|
2014-12-09 15:04:33 -05:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error, can't rm a running container")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "409 Conflict") {
|
2015-01-14 17:12:03 -05:00
|
|
|
t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
|
2014-12-09 15:04:33 -05:00
|
|
|
}
|
|
|
|
|
2015-04-13 20:32:25 -04:00
|
|
|
logDone("rm - running container with Error 409")
|
2014-12-09 15:04:33 -05:00
|
|
|
}
|
|
|
|
|
2014-09-19 13:51:37 -04:00
|
|
|
func TestRmForceRemoveRunningContainer(t *testing.T) {
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-07-06 16:44:56 -04:00
|
|
|
createRunningContainer(t, "foo")
|
|
|
|
|
|
|
|
// Stop then remove with -s
|
2014-08-07 14:50:59 -04:00
|
|
|
cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
|
2014-04-18 13:40:12 -04:00
|
|
|
if _, err := runCommand(cmd); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 14:50:59 -04:00
|
|
|
logDone("rm - running container with --force=true")
|
2014-07-06 16:44:56 -04:00
|
|
|
}
|
|
|
|
|
2014-09-19 13:51:37 -04:00
|
|
|
func TestRmContainerOrphaning(t *testing.T) {
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
|
|
|
|
2014-08-05 19:11:09 -04:00
|
|
|
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)
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteImages(img1)
|
2014-08-05 19:11:09 -04:00
|
|
|
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) {
|
2014-10-14 13:59:11 -04:00
|
|
|
t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
|
2014-08-05 19:11:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("rm - container orphaning")
|
|
|
|
}
|
|
|
|
|
2014-10-02 19:39:39 -04:00
|
|
|
func TestRmInvalidContainer(t *testing.T) {
|
2014-10-06 14:18:25 -04:00
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
|
2014-10-02 19:39:39 -04:00
|
|
|
t.Fatal("Expected error on rm unknown container, got none")
|
2014-10-06 14:18:25 -04:00
|
|
|
} else if !strings.Contains(out, "failed to remove one or more containers") {
|
2014-11-05 11:26:22 -05:00
|
|
|
t.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
|
2014-08-05 19:11:09 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 19:39:39 -04:00
|
|
|
logDone("rm - delete unknown container")
|
2014-08-05 19:11:09 -04:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:44:56 -04:00
|
|
|
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)
|
|
|
|
}
|
2014-04-18 13:40:12 -04:00
|
|
|
}
|