github.com/willmtemple/docker@v1.7.0-rc2/integration-cli/docker_cli_rmi_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 8 "github.com/go-check/check" 9 ) 10 11 func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) { 12 errSubstr := "is using it" 13 14 // create a container 15 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") 16 out, _, err := runCommandWithOutput(runCmd) 17 if err != nil { 18 c.Fatalf("failed to create a container: %s, %v", out, err) 19 } 20 21 cleanedContainerID := strings.TrimSpace(out) 22 23 // try to delete the image 24 runCmd = exec.Command(dockerBinary, "rmi", "busybox") 25 out, _, err = runCommandWithOutput(runCmd) 26 if err == nil { 27 c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out) 28 } 29 if !strings.Contains(out, errSubstr) { 30 c.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out) 31 } 32 33 // make sure it didn't delete the busybox name 34 images, _ := dockerCmd(c, "images") 35 if !strings.Contains(images, "busybox") { 36 c.Fatalf("The name 'busybox' should not have been removed from images: %q", images) 37 } 38 } 39 40 func (s *DockerSuite) TestRmiTag(c *check.C) { 41 imagesBefore, _ := dockerCmd(c, "images", "-a") 42 dockerCmd(c, "tag", "busybox", "utest:tag1") 43 dockerCmd(c, "tag", "busybox", "utest/docker:tag2") 44 dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3") 45 { 46 imagesAfter, _ := dockerCmd(c, "images", "-a") 47 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 { 48 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 49 } 50 } 51 dockerCmd(c, "rmi", "utest/docker:tag2") 52 { 53 imagesAfter, _ := dockerCmd(c, "images", "-a") 54 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 { 55 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 56 } 57 58 } 59 dockerCmd(c, "rmi", "utest:5000/docker:tag3") 60 { 61 imagesAfter, _ := dockerCmd(c, "images", "-a") 62 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 { 63 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 64 } 65 66 } 67 dockerCmd(c, "rmi", "utest:tag1") 68 { 69 imagesAfter, _ := dockerCmd(c, "images", "-a") 70 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 { 71 c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter) 72 } 73 74 } 75 } 76 77 func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) { 78 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") 79 out, _, err := runCommandWithOutput(runCmd) 80 if err != nil { 81 c.Fatalf("failed to create a container:%s, %v", out, err) 82 } 83 containerID := strings.TrimSpace(out) 84 runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-one") 85 out, _, err = runCommandWithOutput(runCmd) 86 if err != nil { 87 c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err) 88 } 89 90 imagesBefore, _ := dockerCmd(c, "images", "-a") 91 dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1") 92 dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2") 93 94 imagesAfter, _ := dockerCmd(c, "images", "-a") 95 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 { 96 c.Fatalf("tag busybox to create 2 more images with same imageID; docker images shows: %q\n", imagesAfter) 97 } 98 99 imgID, err := inspectField("busybox-one:tag1", "Id") 100 c.Assert(err, check.IsNil) 101 102 // run a container with the image 103 out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox-one", "top")) 104 if err != nil { 105 c.Fatalf("failed to create a container:%s, %v", out, err) 106 } 107 containerID = strings.TrimSpace(out) 108 109 // first checkout without force it fails 110 out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "rmi", imgID)) 111 expected := fmt.Sprintf("Conflict, cannot delete %s because the running container %s is using it, stop it and use -f to force", imgID[:12], containerID[:12]) 112 if err == nil || !strings.Contains(out, expected) { 113 c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected) 114 } 115 116 dockerCmd(c, "stop", containerID) 117 dockerCmd(c, "rmi", "-f", imgID) 118 119 imagesAfter, _ = dockerCmd(c, "images", "-a") 120 if strings.Contains(imagesAfter, imgID[:12]) { 121 c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 122 } 123 124 } 125 126 func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { 127 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") 128 out, _, err := runCommandWithOutput(runCmd) 129 if err != nil { 130 c.Fatalf("failed to create a container:%s, %v", out, err) 131 } 132 containerID := strings.TrimSpace(out) 133 runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test") 134 out, _, err = runCommandWithOutput(runCmd) 135 if err != nil { 136 c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err) 137 } 138 139 imagesBefore, _ := dockerCmd(c, "images", "-a") 140 dockerCmd(c, "tag", "busybox-test", "utest:tag1") 141 dockerCmd(c, "tag", "busybox-test", "utest:tag2") 142 dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3") 143 dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4") 144 { 145 imagesAfter, _ := dockerCmd(c, "images", "-a") 146 if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 { 147 c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter) 148 } 149 } 150 imgID, err := inspectField("busybox-test", "Id") 151 c.Assert(err, check.IsNil) 152 153 // first checkout without force it fails 154 runCmd = exec.Command(dockerBinary, "rmi", imgID) 155 out, _, err = runCommandWithOutput(runCmd) 156 if err == nil || !strings.Contains(out, fmt.Sprintf("Conflict, cannot delete image %s because it is tagged in multiple repositories, use -f to force", imgID)) { 157 c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err) 158 } 159 160 dockerCmd(c, "rmi", "-f", imgID) 161 { 162 imagesAfter, _ := dockerCmd(c, "images", "-a") 163 if strings.Contains(imagesAfter, imgID[:12]) { 164 c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 165 } 166 167 } 168 } 169 170 func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) { 171 container := "test-delete-tag" 172 newtag := "busybox:newtag" 173 bb := "busybox:latest" 174 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil { 175 c.Fatalf("Could not tag busybox: %v: %s", err, out) 176 } 177 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil { 178 c.Fatalf("Could not run busybox: %v: %s", err, out) 179 } 180 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag)) 181 if err != nil { 182 c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out) 183 } 184 if d := strings.Count(out, "Untagged: "); d != 1 { 185 c.Fatalf("Expected 1 untagged entry got %d: %q", d, out) 186 } 187 188 } 189 190 func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) { 191 192 image := "busybox-clone" 193 194 cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") 195 cmd.Stdin = strings.NewReader(`FROM busybox 196 MAINTAINER foo`) 197 198 if out, _, err := runCommandWithOutput(cmd); err != nil { 199 c.Fatalf("Could not build %s: %s, %v", image, out, err) 200 } 201 202 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil { 203 c.Fatalf("Could not run container: %s, %v", out, err) 204 } 205 206 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image)) 207 if err != nil { 208 c.Fatalf("Could not remove image %s: %s, %v", image, out, err) 209 } 210 211 } 212 213 func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) { 214 newRepo := "127.0.0.1:5000/busybox" 215 oldRepo := "busybox" 216 newTag := "busybox:test" 217 cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo) 218 out, _, err := runCommandWithOutput(cmd) 219 if err != nil { 220 c.Fatalf("Could not tag busybox: %v: %s", err, out) 221 } 222 cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd") 223 out, _, err = runCommandWithOutput(cmd) 224 if err != nil { 225 c.Fatalf("failed to run container: %v, output: %s", err, out) 226 } 227 cmd = exec.Command(dockerBinary, "commit", "test", newTag) 228 out, _, err = runCommandWithOutput(cmd) 229 if err != nil { 230 c.Fatalf("failed to commit container: %v, output: %s", err, out) 231 } 232 cmd = exec.Command(dockerBinary, "rmi", newTag) 233 out, _, err = runCommandWithOutput(cmd) 234 if err != nil { 235 c.Fatalf("failed to remove image: %v, output: %s", err, out) 236 } 237 if !strings.Contains(out, "Untagged: "+newTag) { 238 c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err) 239 } 240 241 } 242 243 func (s *DockerSuite) TestRmiBlank(c *check.C) { 244 // try to delete a blank image name 245 runCmd := exec.Command(dockerBinary, "rmi", "") 246 out, _, err := runCommandWithOutput(runCmd) 247 248 if err == nil { 249 c.Fatal("Should have failed to delete '' image") 250 } 251 252 if strings.Contains(out, "No such image") { 253 c.Fatalf("Wrong error message generated: %s", out) 254 } 255 }