github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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  	out, _, err := dockerCmdWithError("run", "-d", "busybox", "true")
    16  	if err != nil {
    17  		c.Fatalf("failed to create a container: %s, %v", out, err)
    18  	}
    19  
    20  	cleanedContainerID := strings.TrimSpace(out)
    21  
    22  	// try to delete the image
    23  	out, _, err = dockerCmdWithError("rmi", "busybox")
    24  	if err == nil {
    25  		c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out)
    26  	}
    27  	if !strings.Contains(out, errSubstr) {
    28  		c.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out)
    29  	}
    30  
    31  	// make sure it didn't delete the busybox name
    32  	images, _ := dockerCmd(c, "images")
    33  	if !strings.Contains(images, "busybox") {
    34  		c.Fatalf("The name 'busybox' should not have been removed from images: %q", images)
    35  	}
    36  }
    37  
    38  func (s *DockerSuite) TestRmiTag(c *check.C) {
    39  	imagesBefore, _ := dockerCmd(c, "images", "-a")
    40  	dockerCmd(c, "tag", "busybox", "utest:tag1")
    41  	dockerCmd(c, "tag", "busybox", "utest/docker:tag2")
    42  	dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
    43  	{
    44  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    45  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
    46  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    47  		}
    48  	}
    49  	dockerCmd(c, "rmi", "utest/docker:tag2")
    50  	{
    51  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    52  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
    53  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    54  		}
    55  
    56  	}
    57  	dockerCmd(c, "rmi", "utest:5000/docker:tag3")
    58  	{
    59  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    60  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
    61  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    62  		}
    63  
    64  	}
    65  	dockerCmd(c, "rmi", "utest:tag1")
    66  	{
    67  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    68  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
    69  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    70  		}
    71  
    72  	}
    73  }
    74  
    75  func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) {
    76  	out, _, err := dockerCmdWithError("run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
    77  	if err != nil {
    78  		c.Fatalf("failed to create a container:%s, %v", out, err)
    79  	}
    80  
    81  	containerID := strings.TrimSpace(out)
    82  	out, _, err = dockerCmdWithError("commit", containerID, "busybox-one")
    83  	if err != nil {
    84  		c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err)
    85  	}
    86  
    87  	imagesBefore, _ := dockerCmd(c, "images", "-a")
    88  	dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
    89  	dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
    90  
    91  	imagesAfter, _ := dockerCmd(c, "images", "-a")
    92  	if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
    93  		c.Fatalf("tag busybox to create 2 more images with same imageID; docker images shows: %q\n", imagesAfter)
    94  	}
    95  
    96  	imgID, err := inspectField("busybox-one:tag1", "Id")
    97  	c.Assert(err, check.IsNil)
    98  
    99  	// run a container with the image
   100  	out, _, err = dockerCmdWithError("run", "-d", "busybox-one", "top")
   101  	if err != nil {
   102  		c.Fatalf("failed to create a container:%s, %v", out, err)
   103  	}
   104  
   105  	containerID = strings.TrimSpace(out)
   106  
   107  	// first checkout without force it fails
   108  	out, _, err = dockerCmdWithError("rmi", imgID)
   109  	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])
   110  	if err == nil || !strings.Contains(out, expected) {
   111  		c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected)
   112  	}
   113  
   114  	dockerCmd(c, "stop", containerID)
   115  	dockerCmd(c, "rmi", "-f", imgID)
   116  
   117  	imagesAfter, _ = dockerCmd(c, "images", "-a")
   118  	if strings.Contains(imagesAfter, imgID[:12]) {
   119  		c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
   120  	}
   121  }
   122  
   123  func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
   124  	out, _, err := dockerCmdWithError("run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
   125  	if err != nil {
   126  		c.Fatalf("failed to create a container:%s, %v", out, err)
   127  	}
   128  
   129  	containerID := strings.TrimSpace(out)
   130  	out, _, err = dockerCmdWithError("commit", containerID, "busybox-test")
   131  	if err != nil {
   132  		c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err)
   133  	}
   134  
   135  	imagesBefore, _ := dockerCmd(c, "images", "-a")
   136  	dockerCmd(c, "tag", "busybox-test", "utest:tag1")
   137  	dockerCmd(c, "tag", "busybox-test", "utest:tag2")
   138  	dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
   139  	dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
   140  	{
   141  		imagesAfter, _ := dockerCmd(c, "images", "-a")
   142  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 {
   143  			c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
   144  		}
   145  	}
   146  	imgID, err := inspectField("busybox-test", "Id")
   147  	c.Assert(err, check.IsNil)
   148  
   149  	// first checkout without force it fails
   150  	out, _, err = dockerCmdWithError("rmi", imgID)
   151  	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)) {
   152  		c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err)
   153  	}
   154  
   155  	dockerCmd(c, "rmi", "-f", imgID)
   156  	{
   157  		imagesAfter, _ := dockerCmd(c, "images", "-a")
   158  		if strings.Contains(imagesAfter, imgID[:12]) {
   159  			c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
   160  		}
   161  	}
   162  }
   163  
   164  // See https://github.com/docker/docker/issues/14116
   165  func (s *DockerSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c *check.C) {
   166  	dockerfile := "FROM busybox\nRUN echo test 14116\n"
   167  	imgID, err := buildImage("test-14116", dockerfile, false)
   168  	c.Assert(err, check.IsNil)
   169  
   170  	newTag := "newtag"
   171  	dockerCmd(c, "tag", imgID, newTag)
   172  	dockerCmd(c, "run", "-d", imgID, "top")
   173  
   174  	out, _, err := dockerCmdWithError("rmi", "-f", imgID)
   175  	if err == nil || !strings.Contains(out, "stop it and retry") {
   176  		c.Log(out)
   177  		c.Fatalf("rmi -f should not delete image with running containers")
   178  	}
   179  }
   180  
   181  func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
   182  	container := "test-delete-tag"
   183  	newtag := "busybox:newtag"
   184  	bb := "busybox:latest"
   185  	if out, _, err := dockerCmdWithError("tag", bb, newtag); err != nil {
   186  		c.Fatalf("Could not tag busybox: %v: %s", err, out)
   187  	}
   188  	if out, _, err := dockerCmdWithError("run", "--name", container, bb, "/bin/true"); err != nil {
   189  		c.Fatalf("Could not run busybox: %v: %s", err, out)
   190  	}
   191  	out, _, err := dockerCmdWithError("rmi", newtag)
   192  	if err != nil {
   193  		c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
   194  	}
   195  	if d := strings.Count(out, "Untagged: "); d != 1 {
   196  		c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
   197  	}
   198  }
   199  
   200  func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
   201  	image := "busybox-clone"
   202  
   203  	cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
   204  	cmd.Stdin = strings.NewReader(`FROM busybox
   205  MAINTAINER foo`)
   206  
   207  	if out, _, err := runCommandWithOutput(cmd); err != nil {
   208  		c.Fatalf("Could not build %s: %s, %v", image, out, err)
   209  	}
   210  
   211  	if out, _, err := dockerCmdWithError("run", "--name", "test-force-rmi", image, "/bin/true"); err != nil {
   212  		c.Fatalf("Could not run container: %s, %v", out, err)
   213  	}
   214  
   215  	if out, _, err := dockerCmdWithError("rmi", "-f", image); err != nil {
   216  		c.Fatalf("Could not remove image %s:  %s, %v", image, out, err)
   217  	}
   218  }
   219  
   220  func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
   221  	newRepo := "127.0.0.1:5000/busybox"
   222  	oldRepo := "busybox"
   223  	newTag := "busybox:test"
   224  	out, _, err := dockerCmdWithError("tag", oldRepo, newRepo)
   225  	if err != nil {
   226  		c.Fatalf("Could not tag busybox: %v: %s", err, out)
   227  	}
   228  
   229  	out, _, err = dockerCmdWithError("run", "--name", "test", oldRepo, "touch", "/home/abcd")
   230  	if err != nil {
   231  		c.Fatalf("failed to run container: %v, output: %s", err, out)
   232  	}
   233  
   234  	out, _, err = dockerCmdWithError("commit", "test", newTag)
   235  	if err != nil {
   236  		c.Fatalf("failed to commit container: %v, output: %s", err, out)
   237  	}
   238  
   239  	out, _, err = dockerCmdWithError("rmi", newTag)
   240  	if err != nil {
   241  		c.Fatalf("failed to remove image: %v, output: %s", err, out)
   242  	}
   243  	if !strings.Contains(out, "Untagged: "+newTag) {
   244  		c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
   245  	}
   246  }
   247  
   248  func (s *DockerSuite) TestRmiBlank(c *check.C) {
   249  	// try to delete a blank image name
   250  	out, _, err := dockerCmdWithError("rmi", "")
   251  	if err == nil {
   252  		c.Fatal("Should have failed to delete '' image")
   253  	}
   254  	if strings.Contains(out, "No such image") {
   255  		c.Fatalf("Wrong error message generated: %s", out)
   256  	}
   257  	if !strings.Contains(out, "Image name can not be blank") {
   258  		c.Fatalf("Expected error message not generated: %s", out)
   259  	}
   260  
   261  	out, _, err = dockerCmdWithError("rmi", " ")
   262  	if err == nil {
   263  		c.Fatal("Should have failed to delete '' image")
   264  	}
   265  	if !strings.Contains(out, "No such image") {
   266  		c.Fatalf("Expected error message not generated: %s", out)
   267  	}
   268  }
   269  
   270  func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
   271  	// Build 2 images for testing.
   272  	imageNames := []string{"test1", "test2"}
   273  	imageIds := make([]string, 2)
   274  	for i, name := range imageNames {
   275  		dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
   276  		id, err := buildImage(name, dockerfile, false)
   277  		c.Assert(err, check.IsNil)
   278  		imageIds[i] = id
   279  	}
   280  
   281  	// Create a long-running container.
   282  	dockerCmd(c, "run", "-d", imageNames[0], "top")
   283  
   284  	// Create a stopped container, and then force remove its image.
   285  	dockerCmd(c, "run", imageNames[1], "true")
   286  	dockerCmd(c, "rmi", "-f", imageIds[1])
   287  
   288  	// Try to remove the image of the running container and see if it fails as expected.
   289  	out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
   290  	if err == nil || !strings.Contains(out, "is using it") {
   291  		c.Log(out)
   292  		c.Fatal("The image of the running container should not be removed.")
   293  	}
   294  }