github.com/uriddle/docker@v0.0.0-20210926094723-4072e6aeb013/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  			c.Assert(out, checker.Not(checker.Contains), "unauthorized", check.Commentf(`message should not contain "unauthorized"`))
    66  		}
    67  	}
    68  
    69  }
    70  
    71  // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
    72  // that pulling the same image with different combinations of implicit elements of the the image
    73  // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
    74  // multiple images.
    75  func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
    76  	testRequires(c, DaemonIsLinux)
    77  	s.Cmd(c, "pull", "hello-world")
    78  	defer deleteImages("hello-world")
    79  
    80  	for _, i := range []string{
    81  		"hello-world",
    82  		"hello-world:latest",
    83  		"library/hello-world",
    84  		"library/hello-world:latest",
    85  		"docker.io/library/hello-world",
    86  		"index.docker.io/library/hello-world",
    87  	} {
    88  		out := s.Cmd(c, "pull", i)
    89  		c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
    90  	}
    91  
    92  	// We should have a single entry in images.
    93  	img := strings.TrimSpace(s.Cmd(c, "images"))
    94  	splitImg := strings.Split(img, "\n")
    95  	c.Assert(splitImg, checker.HasLen, 2)
    96  	c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
    97  }
    98  
    99  // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
   100  func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
   101  	testRequires(c, DaemonIsLinux)
   102  	out, err := s.CmdWithError("pull", "scratch")
   103  	c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
   104  	c.Assert(out, checker.Contains, "'scratch' is a reserved name")
   105  	c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
   106  }
   107  
   108  // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
   109  // results in more images than a naked pull.
   110  func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
   111  	testRequires(c, DaemonIsLinux)
   112  	s.Cmd(c, "pull", "busybox")
   113  	outImageCmd := s.Cmd(c, "images", "busybox")
   114  	splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
   115  	c.Assert(splitOutImageCmd, checker.HasLen, 2)
   116  
   117  	s.Cmd(c, "pull", "--all-tags=true", "busybox")
   118  	outImageAllTagCmd := s.Cmd(c, "images", "busybox")
   119  	linesCount := strings.Count(outImageAllTagCmd, "\n")
   120  	c.Assert(linesCount, checker.GreaterThan, 2, check.Commentf("pulling all tags should provide more than two images, got %s", outImageAllTagCmd))
   121  
   122  	// Verify that the line for 'busybox:latest' is left unchanged.
   123  	var latestLine string
   124  	for _, line := range strings.Split(outImageAllTagCmd, "\n") {
   125  		if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") {
   126  			latestLine = line
   127  			break
   128  		}
   129  	}
   130  	c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags"))
   131  	splitLatest := strings.Fields(latestLine)
   132  	splitCurrent := strings.Fields(splitOutImageCmd[1])
   133  
   134  	// Clear relative creation times, since these can easily change between
   135  	// two invocations of "docker images". Without this, the test can fail
   136  	// like this:
   137  	// ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
   138  	// ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
   139  	splitLatest[3] = ""
   140  	splitLatest[4] = ""
   141  	splitLatest[5] = ""
   142  	splitCurrent[3] = ""
   143  	splitCurrent[4] = ""
   144  	splitCurrent[5] = ""
   145  
   146  	c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
   147  }
   148  
   149  // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
   150  // gets cancelled.
   151  //
   152  // Ref: docker/docker#15589
   153  func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
   154  	testRequires(c, DaemonIsLinux)
   155  	repoName := "hello-world:latest"
   156  
   157  	pullCmd := s.MakeCmd("pull", repoName)
   158  	stdout, err := pullCmd.StdoutPipe()
   159  	c.Assert(err, checker.IsNil)
   160  	err = pullCmd.Start()
   161  	c.Assert(err, checker.IsNil)
   162  
   163  	// Cancel as soon as we get some output.
   164  	buf := make([]byte, 10)
   165  	_, err = stdout.Read(buf)
   166  	c.Assert(err, checker.IsNil)
   167  
   168  	err = pullCmd.Process.Kill()
   169  	c.Assert(err, checker.IsNil)
   170  
   171  	time.Sleep(2 * time.Second)
   172  	_, err = s.CmdWithError("inspect", repoName)
   173  	c.Assert(err, checker.NotNil, check.Commentf("image was pulled after client disconnected"))
   174  }