github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/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  	deleteContainer(cleanedContainerID)
    40  
    41  }
    42  
    43  func (s *DockerSuite) TestRmiTag(c *check.C) {
    44  	imagesBefore, _ := dockerCmd(c, "images", "-a")
    45  	dockerCmd(c, "tag", "busybox", "utest:tag1")
    46  	dockerCmd(c, "tag", "busybox", "utest/docker:tag2")
    47  	dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
    48  	{
    49  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    50  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
    51  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    52  		}
    53  	}
    54  	dockerCmd(c, "rmi", "utest/docker:tag2")
    55  	{
    56  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    57  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
    58  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    59  		}
    60  
    61  	}
    62  	dockerCmd(c, "rmi", "utest:5000/docker:tag3")
    63  	{
    64  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    65  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
    66  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    67  		}
    68  
    69  	}
    70  	dockerCmd(c, "rmi", "utest:tag1")
    71  	{
    72  		imagesAfter, _ := dockerCmd(c, "images", "-a")
    73  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
    74  			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
    75  		}
    76  
    77  	}
    78  }
    79  
    80  func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
    81  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
    82  	out, _, err := runCommandWithOutput(runCmd)
    83  	if err != nil {
    84  		c.Fatalf("failed to create a container:%s, %v", out, err)
    85  	}
    86  	containerID := strings.TrimSpace(out)
    87  	runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test")
    88  	out, _, err = runCommandWithOutput(runCmd)
    89  	if err != nil {
    90  		c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err)
    91  	}
    92  
    93  	imagesBefore, _ := dockerCmd(c, "images", "-a")
    94  	dockerCmd(c, "tag", "busybox-test", "utest:tag1")
    95  	dockerCmd(c, "tag", "busybox-test", "utest:tag2")
    96  	dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
    97  	dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
    98  	{
    99  		imagesAfter, _ := dockerCmd(c, "images", "-a")
   100  		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 {
   101  			c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
   102  		}
   103  	}
   104  	imgID, err := inspectField("busybox-test", "Id")
   105  	c.Assert(err, check.IsNil)
   106  
   107  	// first checkout without force it fails
   108  	runCmd = exec.Command(dockerBinary, "rmi", imgID)
   109  	out, _, err = runCommandWithOutput(runCmd)
   110  	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)) {
   111  		c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err)
   112  	}
   113  
   114  	dockerCmd(c, "rmi", "-f", imgID)
   115  	{
   116  		imagesAfter, _ := dockerCmd(c, "images", "-a")
   117  		if strings.Contains(imagesAfter, imgID[:12]) {
   118  			c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
   119  		}
   120  
   121  	}
   122  }
   123  
   124  func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
   125  
   126  	container := "test-delete-tag"
   127  	newtag := "busybox:newtag"
   128  	bb := "busybox:latest"
   129  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil {
   130  		c.Fatalf("Could not tag busybox: %v: %s", err, out)
   131  	}
   132  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil {
   133  		c.Fatalf("Could not run busybox: %v: %s", err, out)
   134  	}
   135  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag))
   136  	if err != nil {
   137  		c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
   138  	}
   139  	if d := strings.Count(out, "Untagged: "); d != 1 {
   140  		c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
   141  	}
   142  
   143  }
   144  
   145  func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
   146  
   147  	image := "busybox-clone"
   148  
   149  	cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
   150  	cmd.Stdin = strings.NewReader(`FROM busybox
   151  MAINTAINER foo`)
   152  
   153  	if out, _, err := runCommandWithOutput(cmd); err != nil {
   154  		c.Fatalf("Could not build %s: %s, %v", image, out, err)
   155  	}
   156  
   157  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil {
   158  		c.Fatalf("Could not run container: %s, %v", out, err)
   159  	}
   160  
   161  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image))
   162  	if err != nil {
   163  		c.Fatalf("Could not remove image %s:  %s, %v", image, out, err)
   164  	}
   165  
   166  }
   167  
   168  func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
   169  	newRepo := "127.0.0.1:5000/busybox"
   170  	oldRepo := "busybox"
   171  	newTag := "busybox:test"
   172  	cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo)
   173  	out, _, err := runCommandWithOutput(cmd)
   174  	if err != nil {
   175  		c.Fatalf("Could not tag busybox: %v: %s", err, out)
   176  	}
   177  	cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
   178  	out, _, err = runCommandWithOutput(cmd)
   179  	if err != nil {
   180  		c.Fatalf("failed to run container: %v, output: %s", err, out)
   181  	}
   182  	cmd = exec.Command(dockerBinary, "commit", "test", newTag)
   183  	out, _, err = runCommandWithOutput(cmd)
   184  	if err != nil {
   185  		c.Fatalf("failed to commit container: %v, output: %s", err, out)
   186  	}
   187  	cmd = exec.Command(dockerBinary, "rmi", newTag)
   188  	out, _, err = runCommandWithOutput(cmd)
   189  	if err != nil {
   190  		c.Fatalf("failed to remove image: %v, output: %s", err, out)
   191  	}
   192  	if !strings.Contains(out, "Untagged: "+newTag) {
   193  		c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
   194  	}
   195  
   196  }
   197  
   198  func (s *DockerSuite) TestRmiBlank(c *check.C) {
   199  	// try to delete a blank image name
   200  	runCmd := exec.Command(dockerBinary, "rmi", "")
   201  	out, _, err := runCommandWithOutput(runCmd)
   202  
   203  	if err == nil {
   204  		c.Fatal("Should have failed to delete '' image")
   205  	}
   206  
   207  	if strings.Contains(out, "No such image") {
   208  		c.Fatalf("Wrong error message generated: %s", out)
   209  	}
   210  }