github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/integration-cli/docker_cli_pull_test.go (about)

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