github.com/vincentwoo/docker@v0.7.3-0.20160116130405-82401a4b13c0/integration-cli/docker_cli_pull_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/distribution/digest"
    10  	"github.com/docker/docker/pkg/integration/checker"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  // TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
    15  // prints all expected output.
    16  func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *check.C) {
    17  	testRequires(c, DaemonIsLinux)
    18  	out := s.Cmd(c, "pull", "hello-world")
    19  	defer deleteImages("hello-world")
    20  
    21  	c.Assert(out, checker.Contains, "Using default tag: latest", check.Commentf("expected the 'latest' tag to be automatically assumed"))
    22  	c.Assert(out, checker.Contains, "Pulling from library/hello-world", check.Commentf("expected the 'library/' prefix to be automatically assumed"))
    23  	c.Assert(out, checker.Contains, "Downloaded newer image for hello-world:latest")
    24  
    25  	matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1)
    26  	c.Assert(len(matches), checker.Equals, 1, check.Commentf("expected exactly one image digest in the output"))
    27  	c.Assert(len(matches[0]), checker.Equals, 2, check.Commentf("unexpected number of submatches for the digest"))
    28  	_, err := digest.ParseDigest(matches[0][1])
    29  	c.Check(err, checker.IsNil, check.Commentf("invalid digest %q in output", matches[0][1]))
    30  
    31  	// We should have a single entry in images.
    32  	img := strings.TrimSpace(s.Cmd(c, "images"))
    33  	splitImg := strings.Split(img, "\n")
    34  	c.Assert(splitImg, checker.HasLen, 2)
    35  	c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
    36  }
    37  
    38  // TestPullNonExistingImage pulls non-existing images from the central registry, with different
    39  // combinations of implicit tag and library prefix.
    40  func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
    41  	testRequires(c, DaemonIsLinux)
    42  	for _, e := range []struct {
    43  		Repo  string
    44  		Alias string
    45  	}{
    46  		{"library/asdfasdf", "asdfasdf:foobar"},
    47  		{"library/asdfasdf", "library/asdfasdf:foobar"},
    48  		{"library/asdfasdf", "asdfasdf"},
    49  		{"library/asdfasdf", "asdfasdf:latest"},
    50  		{"library/asdfasdf", "library/asdfasdf"},
    51  		{"library/asdfasdf", "library/asdfasdf:latest"},
    52  	} {
    53  		out, err := s.CmdWithError("pull", e.Alias)
    54  		c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", out))
    55  		// Hub returns 401 rather than 404 for nonexistent repos over
    56  		// the v2 protocol - but we should end up falling back to v1,
    57  		// which does return a 404.
    58  		c.Assert(out, checker.Contains, fmt.Sprintf("Error: image %s not found", e.Repo), check.Commentf("expected image not found error messages"))
    59  
    60  		// pull -a on a nonexistent registry should fall back as well
    61  		if !strings.ContainsRune(e.Alias, ':') {
    62  			out, err := s.CmdWithError("pull", "-a", e.Alias)
    63  			c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", out))
    64  			c.Assert(out, checker.Contains, fmt.Sprintf("Error: image %s not found", e.Repo), check.Commentf("expected image not found error messages"))
    65  		}
    66  	}
    67  
    68  }
    69  
    70  // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
    71  // that pulling the same image with different combinations of implicit elements of the the image
    72  // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
    73  // multiple images.
    74  func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
    75  	testRequires(c, DaemonIsLinux)
    76  	s.Cmd(c, "pull", "hello-world")
    77  	defer deleteImages("hello-world")
    78  
    79  	for _, i := range []string{
    80  		"hello-world",
    81  		"hello-world:latest",
    82  		"library/hello-world",
    83  		"library/hello-world:latest",
    84  		"docker.io/library/hello-world",
    85  		"index.docker.io/library/hello-world",
    86  	} {
    87  		out := s.Cmd(c, "pull", i)
    88  		c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
    89  	}
    90  
    91  	// We should have a single entry in images.
    92  	img := strings.TrimSpace(s.Cmd(c, "images"))
    93  	splitImg := strings.Split(img, "\n")
    94  	c.Assert(splitImg, checker.HasLen, 2)
    95  	c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
    96  }
    97  
    98  // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
    99  func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
   100  	testRequires(c, DaemonIsLinux)
   101  	out, err := s.CmdWithError("pull", "scratch")
   102  	c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
   103  	c.Assert(out, checker.Contains, "'scratch' is a reserved name")
   104  	c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
   105  }
   106  
   107  // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
   108  // results in more images than a naked pull.
   109  func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
   110  	testRequires(c, DaemonIsLinux)
   111  	s.Cmd(c, "pull", "busybox")
   112  	outImageCmd := s.Cmd(c, "images", "busybox")
   113  	splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
   114  	c.Assert(splitOutImageCmd, checker.HasLen, 2)
   115  
   116  	s.Cmd(c, "pull", "--all-tags=true", "busybox")
   117  	outImageAllTagCmd := s.Cmd(c, "images", "busybox")
   118  	linesCount := strings.Count(outImageAllTagCmd, "\n")
   119  	c.Assert(linesCount, checker.GreaterThan, 2, check.Commentf("pulling all tags should provide more than two images, got %s", outImageAllTagCmd))
   120  
   121  	// Verify that the line for 'busybox:latest' is left unchanged.
   122  	var latestLine string
   123  	for _, line := range strings.Split(outImageAllTagCmd, "\n") {
   124  		if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") {
   125  			latestLine = line
   126  			break
   127  		}
   128  	}
   129  	c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags"))
   130  	splitLatest := strings.Fields(latestLine)
   131  	splitCurrent := strings.Fields(splitOutImageCmd[1])
   132  
   133  	// Clear relative creation times, since these can easily change between
   134  	// two invocations of "docker images". Without this, the test can fail
   135  	// like this:
   136  	// ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
   137  	// ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
   138  	splitLatest[3] = ""
   139  	splitLatest[4] = ""
   140  	splitLatest[5] = ""
   141  	splitCurrent[3] = ""
   142  	splitCurrent[4] = ""
   143  	splitCurrent[5] = ""
   144  
   145  	c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
   146  }
   147  
   148  // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
   149  // gets cancelled.
   150  //
   151  // Ref: docker/docker#15589
   152  func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
   153  	testRequires(c, DaemonIsLinux)
   154  	repoName := "hello-world:latest"
   155  
   156  	pullCmd := s.MakeCmd("pull", repoName)
   157  	stdout, err := pullCmd.StdoutPipe()
   158  	c.Assert(err, checker.IsNil)
   159  	err = pullCmd.Start()
   160  	c.Assert(err, checker.IsNil)
   161  
   162  	// Cancel as soon as we get some output.
   163  	buf := make([]byte, 10)
   164  	_, err = stdout.Read(buf)
   165  	c.Assert(err, checker.IsNil)
   166  
   167  	err = pullCmd.Process.Kill()
   168  	c.Assert(err, checker.IsNil)
   169  
   170  	time.Sleep(2 * time.Second)
   171  	_, err = s.CmdWithError("inspect", repoName)
   172  	c.Assert(err, checker.NotNil, check.Commentf("image was pulled after client disconnected"))
   173  }