github.com/gondor/docker@v1.9.0-rc1/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 testRequires(c, DaemonIsLinux) 13 errSubstr := "is using it" 14 15 // create a container 16 out, _, err := dockerCmdWithError("run", "-d", "busybox", "true") 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 out, _, err = dockerCmdWithError("rmi", "busybox") 25 if err == nil { 26 c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out) 27 } 28 if !strings.Contains(out, errSubstr) { 29 c.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out) 30 } 31 32 // make sure it didn't delete the busybox name 33 images, _ := dockerCmd(c, "images") 34 if !strings.Contains(images, "busybox") { 35 c.Fatalf("The name 'busybox' should not have been removed from images: %q", images) 36 } 37 } 38 39 func (s *DockerSuite) TestRmiTag(c *check.C) { 40 testRequires(c, DaemonIsLinux) 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 testRequires(c, DaemonIsLinux) 79 out, _, err := dockerCmdWithError("run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") 80 if err != nil { 81 c.Fatalf("failed to create a container:%s, %v", out, err) 82 } 83 84 containerID := strings.TrimSpace(out) 85 out, _, err = dockerCmdWithError("commit", containerID, "busybox-one") 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 = dockerCmdWithError("run", "-d", "busybox-one", "top") 104 if err != nil { 105 c.Fatalf("failed to create a container:%s, %v", out, err) 106 } 107 108 containerID = strings.TrimSpace(out) 109 110 // first checkout without force it fails 111 out, _, err = dockerCmdWithError("rmi", imgID) 112 expected := fmt.Sprintf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", imgID[:12], containerID[:12]) 113 if err == nil || !strings.Contains(out, expected) { 114 c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected) 115 } 116 117 dockerCmd(c, "stop", containerID) 118 dockerCmd(c, "rmi", "-f", imgID) 119 120 imagesAfter, _ = dockerCmd(c, "images", "-a") 121 if strings.Contains(imagesAfter, imgID[:12]) { 122 c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 123 } 124 } 125 126 func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { 127 testRequires(c, DaemonIsLinux) 128 out, _, err := dockerCmdWithError("run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") 129 if err != nil { 130 c.Fatalf("failed to create a container:%s, %v", out, err) 131 } 132 133 containerID := strings.TrimSpace(out) 134 out, _, err = dockerCmdWithError("commit", containerID, "busybox-test") 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 out, _, err = dockerCmdWithError("rmi", imgID) 155 if err == nil || !strings.Contains(out, "(must be forced) - image is referenced in one or more repositories") { 156 c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err) 157 } 158 159 dockerCmd(c, "rmi", "-f", imgID) 160 { 161 imagesAfter, _ := dockerCmd(c, "images", "-a") 162 if strings.Contains(imagesAfter, imgID[:12]) { 163 c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 164 } 165 } 166 } 167 168 // See https://github.com/docker/docker/issues/14116 169 func (s *DockerSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c *check.C) { 170 testRequires(c, DaemonIsLinux) 171 dockerfile := "FROM busybox\nRUN echo test 14116\n" 172 imgID, err := buildImage("test-14116", dockerfile, false) 173 c.Assert(err, check.IsNil) 174 175 newTag := "newtag" 176 dockerCmd(c, "tag", imgID, newTag) 177 dockerCmd(c, "run", "-d", imgID, "top") 178 179 out, _, err := dockerCmdWithError("rmi", "-f", imgID) 180 if err == nil || !strings.Contains(out, "(cannot be forced) - image is being used by running container") { 181 c.Log(out) 182 c.Fatalf("rmi -f should not delete image with running containers") 183 } 184 } 185 186 func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) { 187 testRequires(c, DaemonIsLinux) 188 container := "test-delete-tag" 189 newtag := "busybox:newtag" 190 bb := "busybox:latest" 191 if out, _, err := dockerCmdWithError("tag", bb, newtag); err != nil { 192 c.Fatalf("Could not tag busybox: %v: %s", err, out) 193 } 194 if out, _, err := dockerCmdWithError("run", "--name", container, bb, "/bin/true"); err != nil { 195 c.Fatalf("Could not run busybox: %v: %s", err, out) 196 } 197 out, _, err := dockerCmdWithError("rmi", newtag) 198 if err != nil { 199 c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out) 200 } 201 if d := strings.Count(out, "Untagged: "); d != 1 { 202 c.Fatalf("Expected 1 untagged entry got %d: %q", d, out) 203 } 204 } 205 206 func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) { 207 testRequires(c, DaemonIsLinux) 208 image := "busybox-clone" 209 210 cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") 211 cmd.Stdin = strings.NewReader(`FROM busybox 212 MAINTAINER foo`) 213 214 if out, _, err := runCommandWithOutput(cmd); err != nil { 215 c.Fatalf("Could not build %s: %s, %v", image, out, err) 216 } 217 218 if out, _, err := dockerCmdWithError("run", "--name", "test-force-rmi", image, "/bin/true"); err != nil { 219 c.Fatalf("Could not run container: %s, %v", out, err) 220 } 221 222 if out, _, err := dockerCmdWithError("rmi", "-f", image); err != nil { 223 c.Fatalf("Could not remove image %s: %s, %v", image, out, err) 224 } 225 } 226 227 func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) { 228 testRequires(c, DaemonIsLinux) 229 newRepo := "127.0.0.1:5000/busybox" 230 oldRepo := "busybox" 231 newTag := "busybox:test" 232 out, _, err := dockerCmdWithError("tag", oldRepo, newRepo) 233 if err != nil { 234 c.Fatalf("Could not tag busybox: %v: %s", err, out) 235 } 236 237 out, _, err = dockerCmdWithError("run", "--name", "test", oldRepo, "touch", "/home/abcd") 238 if err != nil { 239 c.Fatalf("failed to run container: %v, output: %s", err, out) 240 } 241 242 out, _, err = dockerCmdWithError("commit", "test", newTag) 243 if err != nil { 244 c.Fatalf("failed to commit container: %v, output: %s", err, out) 245 } 246 247 out, _, err = dockerCmdWithError("rmi", newTag) 248 if err != nil { 249 c.Fatalf("failed to remove image: %v, output: %s", err, out) 250 } 251 if !strings.Contains(out, "Untagged: "+newTag) { 252 c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err) 253 } 254 } 255 256 func (s *DockerSuite) TestRmiBlank(c *check.C) { 257 testRequires(c, DaemonIsLinux) 258 // try to delete a blank image name 259 out, _, err := dockerCmdWithError("rmi", "") 260 if err == nil { 261 c.Fatal("Should have failed to delete '' image") 262 } 263 if strings.Contains(out, "no such id") { 264 c.Fatalf("Wrong error message generated: %s", out) 265 } 266 if !strings.Contains(out, "image name cannot be blank") { 267 c.Fatalf("Expected error message not generated: %s", out) 268 } 269 270 out, _, err = dockerCmdWithError("rmi", " ") 271 if err == nil { 272 c.Fatal("Should have failed to delete '' image") 273 } 274 if !strings.Contains(out, "no such id") { 275 c.Fatalf("Expected error message not generated: %s", out) 276 } 277 } 278 279 func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) { 280 testRequires(c, DaemonIsLinux) 281 // Build 2 images for testing. 282 imageNames := []string{"test1", "test2"} 283 imageIds := make([]string, 2) 284 for i, name := range imageNames { 285 dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name) 286 id, err := buildImage(name, dockerfile, false) 287 c.Assert(err, check.IsNil) 288 imageIds[i] = id 289 } 290 291 // Create a long-running container. 292 dockerCmd(c, "run", "-d", imageNames[0], "top") 293 294 // Create a stopped container, and then force remove its image. 295 dockerCmd(c, "run", imageNames[1], "true") 296 dockerCmd(c, "rmi", "-f", imageIds[1]) 297 298 // Try to remove the image of the running container and see if it fails as expected. 299 out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0]) 300 if err == nil || !strings.Contains(out, "image is being used by running container") { 301 c.Log(out) 302 c.Fatal("The image of the running container should not be removed.") 303 } 304 } 305 306 // #13422 307 func (s *DockerSuite) TestRmiUntagHistoryLayer(c *check.C) { 308 testRequires(c, DaemonIsLinux) 309 image := "tmp1" 310 // Build a image for testing. 311 dockerfile := `FROM busybox 312 MAINTAINER foo 313 RUN echo 0 #layer0 314 RUN echo 1 #layer1 315 RUN echo 2 #layer2 316 ` 317 _, err := buildImage(image, dockerfile, false) 318 c.Assert(err, check.IsNil) 319 320 out, _ := dockerCmd(c, "history", "-q", image) 321 ids := strings.Split(out, "\n") 322 idToTag := ids[2] 323 324 // Tag layer0 to "tmp2". 325 newTag := "tmp2" 326 dockerCmd(c, "tag", idToTag, newTag) 327 // Create a container based on "tmp1". 328 dockerCmd(c, "run", "-d", image, "true") 329 330 // See if the "tmp2" can be untagged. 331 out, _ = dockerCmd(c, "rmi", newTag) 332 if d := strings.Count(out, "Untagged: "); d != 1 { 333 c.Log(out) 334 c.Fatalf("Expected 1 untagged entry got %d: %q", d, out) 335 } 336 337 // Now let's add the tag again and create a container based on it. 338 dockerCmd(c, "tag", idToTag, newTag) 339 out, _ = dockerCmd(c, "run", "-d", newTag, "true") 340 cid := strings.TrimSpace(out) 341 342 // At this point we have 2 containers, one based on layer2 and another based on layer0. 343 // Try to untag "tmp2" without the -f flag. 344 out, _, err = dockerCmdWithError("rmi", newTag) 345 if err == nil || !strings.Contains(out, cid[:12]) || !strings.Contains(out, "(must force)") { 346 c.Log(out) 347 c.Fatalf("%q should not be untagged without the -f flag", newTag) 348 } 349 350 // Add the -f flag and test again. 351 out, _ = dockerCmd(c, "rmi", "-f", newTag) 352 if !strings.Contains(out, fmt.Sprintf("Untagged: %s:latest", newTag)) { 353 c.Log(out) 354 c.Fatalf("%q should be allowed to untag with the -f flag", newTag) 355 } 356 }