2014-04-14 12:43:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-14 08:00:46 +00:00
|
|
|
"net/http"
|
2014-04-14 12:43:01 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2014-08-05 23:11:09 +00:00
|
|
|
"strings"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-04-14 12:43:01 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
2015-02-20 06:56:02 +00:00
|
|
|
|
2014-04-14 12:43:01 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-04-14 12:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove("/tmp/testing"); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-04-14 12:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes")
|
2014-08-28 14:18:08 +00:00
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2014-04-14 12:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-04-18 17:40:12 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) {
|
2015-02-20 06:56:02 +00:00
|
|
|
|
2014-04-18 17:40:12 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-04-18 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "rm", "-v", "foo")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-04-18 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
|
2015-02-20 06:56:02 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
createRunningContainer(c, "foo")
|
2014-04-18 17:40:12 +00:00
|
|
|
|
|
|
|
// Test cannot remove running container
|
2014-07-06 20:44:56 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "rm", "foo")
|
2014-04-18 17:40:12 +00:00
|
|
|
if _, err := runCommand(cmd); err == nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected error, can't rm a running container")
|
2014-04-18 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 20:44:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmRunningContainerCheckError409(c *check.C) {
|
2015-02-20 06:56:02 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
createRunningContainer(c, "foo")
|
2014-12-09 20:04:33 +00:00
|
|
|
|
|
|
|
endpoint := "/containers/foo"
|
2015-04-14 08:00:46 +00:00
|
|
|
status, _, err := sockRequest("DELETE", endpoint, nil)
|
2015-04-20 21:03:56 +00:00
|
|
|
c.Assert(status, check.Equals, http.StatusConflict)
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-12-09 20:04:33 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
|
2015-02-20 06:56:02 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
createRunningContainer(c, "foo")
|
2014-07-06 20:44:56 +00:00
|
|
|
|
|
|
|
// Stop then remove with -s
|
2014-08-07 18:50:59 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "rm", "-f", "foo")
|
2014-04-18 17:40:12 +00:00
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-04-18 17:40:12 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 20:44:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
|
2015-02-20 06:56:02 +00:00
|
|
|
|
2014-08-05 23:11:09 +00: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)
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Could not build image %s: %v", img, err)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
// run container on first image
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Could not run image %s: %v: %s", img, err, out)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
// rebuild dockerfile with a small addition at the end
|
|
|
|
if _, err := buildImage(img, dockerfile2, true); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Could not rebuild image %s: %v", img, err)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
// try to remove the image, should error out.
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
// check if we deleted the first image
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc"))
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("%v: %s", err, out)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(out, img1) {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
|
2014-10-06 18:18:25 +00:00
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("Expected error on rm unknown container, got none")
|
2014-10-06 18:18:25 +00:00
|
|
|
} else if !strings.Contains(out, "failed to remove one or more containers") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
|
2014-08-05 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func createRunningContainer(c *check.C, name string) {
|
2014-07-06 20:44:56 +00:00
|
|
|
cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
|
|
|
|
if _, err := runCommand(cmd); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-07-06 20:44:56 +00:00
|
|
|
}
|
2014-04-18 17:40:12 +00:00
|
|
|
}
|