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