github.com/crquan/docker@v1.8.1/integration-cli/docker_cli_rm_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "strings" 6 7 "github.com/go-check/check" 8 ) 9 10 func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) { 11 testRequires(c, SameHostDaemon) 12 13 dockerCmd(c, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true") 14 15 if err := os.Remove("/tmp/testing"); err != nil { 16 c.Fatal(err) 17 } 18 19 dockerCmd(c, "rm", "-v", "losemyvolumes") 20 } 21 22 func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) { 23 dockerCmd(c, "run", "--name", "foo", "-v", "/srv", "busybox", "true") 24 25 dockerCmd(c, "rm", "-v", "foo") 26 } 27 28 func (s *DockerSuite) TestRmRunningContainer(c *check.C) { 29 createRunningContainer(c, "foo") 30 31 if _, _, err := dockerCmdWithError(c, "rm", "foo"); err == nil { 32 c.Fatalf("Expected error, can't rm a running container") 33 } 34 } 35 36 func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) { 37 createRunningContainer(c, "foo") 38 39 // Stop then remove with -s 40 dockerCmd(c, "rm", "-f", "foo") 41 } 42 43 func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) { 44 45 dockerfile1 := `FROM busybox:latest 46 ENTRYPOINT ["/bin/true"]` 47 img := "test-container-orphaning" 48 dockerfile2 := `FROM busybox:latest 49 ENTRYPOINT ["/bin/true"] 50 MAINTAINER Integration Tests` 51 52 // build first dockerfile 53 img1, err := buildImage(img, dockerfile1, true) 54 if err != nil { 55 c.Fatalf("Could not build image %s: %v", img, err) 56 } 57 // run container on first image 58 if out, _, err := dockerCmdWithError(c, "run", img); err != nil { 59 c.Fatalf("Could not run image %s: %v: %s", img, err, out) 60 } 61 62 // rebuild dockerfile with a small addition at the end 63 if _, err := buildImage(img, dockerfile2, true); err != nil { 64 c.Fatalf("Could not rebuild image %s: %v", img, err) 65 } 66 // try to remove the image, should error out. 67 if out, _, err := dockerCmdWithError(c, "rmi", img); err == nil { 68 c.Fatalf("Expected to error out removing the image, but succeeded: %s", out) 69 } 70 71 // check if we deleted the first image 72 out, _, err := dockerCmdWithError(c, "images", "-q", "--no-trunc") 73 if err != nil { 74 c.Fatalf("%v: %s", err, out) 75 } 76 if !strings.Contains(out, img1) { 77 c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out) 78 } 79 } 80 81 func (s *DockerSuite) TestRmInvalidContainer(c *check.C) { 82 if out, _, err := dockerCmdWithError(c, "rm", "unknown"); err == nil { 83 c.Fatal("Expected error on rm unknown container, got none") 84 } else if !strings.Contains(out, "failed to remove containers") { 85 c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out) 86 } 87 } 88 89 func createRunningContainer(c *check.C, name string) { 90 dockerCmd(c, "run", "-dt", "--name", name, "busybox", "top") 91 }