integration-cli/docker_cli_pull_test.go
6db32fde
 package main
 
 import (
9d6acbee
 	"fmt"
f324f485
 	"regexp"
568f86eb
 	"strings"
461976d2
 	"sync"
1406cb35
 	"time"
dc944ea7
 
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
3e90b12d
 	"github.com/go-check/check"
7a855799
 	"github.com/opencontainers/go-digest"
6db32fde
 )
 
f324f485
 // TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
 // prints all expected output.
 func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
f324f485
 	out := s.Cmd(c, "pull", "hello-world")
 	defer deleteImages("hello-world")
 
 	c.Assert(out, checker.Contains, "Using default tag: latest", check.Commentf("expected the 'latest' tag to be automatically assumed"))
 	c.Assert(out, checker.Contains, "Pulling from library/hello-world", check.Commentf("expected the 'library/' prefix to be automatically assumed"))
 	c.Assert(out, checker.Contains, "Downloaded newer image for hello-world:latest")
 
 	matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1)
 	c.Assert(len(matches), checker.Equals, 1, check.Commentf("expected exactly one image digest in the output"))
 	c.Assert(len(matches[0]), checker.Equals, 2, check.Commentf("unexpected number of submatches for the digest"))
7a855799
 	_, err := digest.Parse(matches[0][1])
f324f485
 	c.Check(err, checker.IsNil, check.Commentf("invalid digest %q in output", matches[0][1]))
 
 	// We should have a single entry in images.
 	img := strings.TrimSpace(s.Cmd(c, "images"))
1b010516
 	splitImg := strings.Split(img, "\n")
 	c.Assert(splitImg, checker.HasLen, 2)
 	c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
92d5eafe
 }
7d74be16
 
f324f485
 // TestPullNonExistingImage pulls non-existing images from the central registry, with different
 // combinations of implicit tag and library prefix.
 func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
461976d2
 
 	type entry struct {
745892a7
 		repo  string
 		alias string
 		tag   string
461976d2
 	}
 
 	entries := []entry{
19a93a6e
 		{"asdfasdf", "asdfasdf", "foobar"},
 		{"asdfasdf", "library/asdfasdf", "foobar"},
 		{"asdfasdf", "asdfasdf", ""},
 		{"asdfasdf", "asdfasdf", "latest"},
 		{"asdfasdf", "library/asdfasdf", ""},
 		{"asdfasdf", "library/asdfasdf", "latest"},
461976d2
 	}
 
 	// The option field indicates "-a" or not.
 	type record struct {
 		e      entry
 		option string
 		out    string
 		err    error
 	}
589a5226
 
461976d2
 	// Execute 'docker pull' in parallel, pass results (out, err) and
 	// necessary information ("-a" or not, and the image name) to channel.
 	var group sync.WaitGroup
 	recordChan := make(chan record, len(entries)*2)
 	for _, e := range entries {
 		group.Add(1)
 		go func(e entry) {
 			defer group.Done()
745892a7
 			repoName := e.alias
 			if e.tag != "" {
 				repoName += ":" + e.tag
 			}
 			out, err := s.CmdWithError("pull", repoName)
461976d2
 			recordChan <- record{e, "", out, err}
 		}(e)
745892a7
 		if e.tag == "" {
461976d2
 			// pull -a on a nonexistent registry should fall back as well
 			group.Add(1)
 			go func(e entry) {
 				defer group.Done()
745892a7
 				out, err := s.CmdWithError("pull", "-a", e.alias)
461976d2
 				recordChan <- record{e, "-a", out, err}
 			}(e)
 		}
 	}
 
 	// Wait for completion
 	group.Wait()
 	close(recordChan)
 
 	// Process the results (out, err).
 	for record := range recordChan {
 		if len(record.option) == 0 {
 			c.Assert(record.err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", record.out))
8d9f51ea
 			c.Assert(record.out, checker.Contains, fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", record.e.repo), check.Commentf("expected image not found error messages"))
461976d2
 		} else {
 			// pull -a on a nonexistent registry should fall back as well
 			c.Assert(record.err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", record.out))
8d9f51ea
 			c.Assert(record.out, checker.Contains, fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", record.e.repo), check.Commentf("expected image not found error messages"))
461976d2
 			c.Assert(record.out, checker.Not(checker.Contains), "unauthorized", check.Commentf(`message should not contain "unauthorized"`))
589a5226
 		}
6db32fde
 	}
589a5226
 
6db32fde
 }
568f86eb
 
f324f485
 // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
ea2dd4b5
 // that pulling the same image with different combinations of implicit elements of the image
f324f485
 // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
 // multiple images.
 func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
f324f485
 
0d270cad
 	// Pull hello-world from v2
 	pullFromV2 := func(ref string) (int, string) {
 		out := s.Cmd(c, "pull", "hello-world")
88420111
 		v1Retries := 0
 		for strings.Contains(out, "this image was pulled from a legacy registry") {
 			// Some network errors may cause fallbacks to the v1
 			// protocol, which would violate the test's assumption
 			// that it will get the same images. To make the test
 			// more robust against these network glitches, allow a
 			// few retries if we end up with a v1 pull.
 
 			if v1Retries > 2 {
0d270cad
 				c.Fatalf("too many v1 fallback incidents when pulling %s", ref)
88420111
 			}
 
0d270cad
 			s.Cmd(c, "rmi", ref)
 			out = s.Cmd(c, "pull", ref)
88420111
 
 			v1Retries++
 		}
0d270cad
 
 		return v1Retries, out
 	}
 
 	pullFromV2("hello-world")
 	defer deleteImages("hello-world")
 
 	s.Cmd(c, "tag", "hello-world", "hello-world-backup")
 
 	for _, ref := range []string{
 		"hello-world",
 		"hello-world:latest",
 		"library/hello-world",
 		"library/hello-world:latest",
 		"docker.io/library/hello-world",
 		"index.docker.io/library/hello-world",
 	} {
 		var out string
 		for {
 			var v1Retries int
 			v1Retries, out = pullFromV2(ref)
 
 			// Keep repeating the test case until we don't hit a v1
 			// fallback case. We won't get the right "Image is up
 			// to date" message if the local image was replaced
 			// with one pulled from v1.
 			if v1Retries == 0 {
 				break
 			}
 			s.Cmd(c, "rmi", ref)
 			s.Cmd(c, "tag", "hello-world-backup", "hello-world")
 		}
f324f485
 		c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
871d2b96
 	}
 
0d270cad
 	s.Cmd(c, "rmi", "hello-world-backup")
 
f324f485
 	// We should have a single entry in images.
 	img := strings.TrimSpace(s.Cmd(c, "images"))
1b010516
 	splitImg := strings.Split(img, "\n")
 	c.Assert(splitImg, checker.HasLen, 2)
 	c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
871d2b96
 }
 
f324f485
 // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
 func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
f324f485
 	out, err := s.CmdWithError("pull", "scratch")
 	c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
 	c.Assert(out, checker.Contains, "'scratch' is a reserved name")
 	c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
58a1de9b
 }
1406cb35
 
f324f485
 // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
 // results in more images than a naked pull.
 func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
5739ba1b
 	s.Cmd(c, "pull", "dockercore/engine-pull-all-test-fixture")
 	outImageCmd := s.Cmd(c, "images", "dockercore/engine-pull-all-test-fixture")
f324f485
 	splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
1b010516
 	c.Assert(splitOutImageCmd, checker.HasLen, 2)
f324f485
 
5739ba1b
 	s.Cmd(c, "pull", "--all-tags=true", "dockercore/engine-pull-all-test-fixture")
 	outImageAllTagCmd := s.Cmd(c, "images", "dockercore/engine-pull-all-test-fixture")
1b010516
 	linesCount := strings.Count(outImageAllTagCmd, "\n")
 	c.Assert(linesCount, checker.GreaterThan, 2, check.Commentf("pulling all tags should provide more than two images, got %s", outImageAllTagCmd))
f324f485
 
5739ba1b
 	// Verify that the line for 'dockercore/engine-pull-all-test-fixture:latest' is left unchanged.
f324f485
 	var latestLine string
 	for _, line := range strings.Split(outImageAllTagCmd, "\n") {
5739ba1b
 		if strings.HasPrefix(line, "dockercore/engine-pull-all-test-fixture") && strings.Contains(line, "latest") {
f324f485
 			latestLine = line
 			break
1406cb35
 		}
268fa5af
 	}
5739ba1b
 	c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for dockercore/engine-pull-all-test-fixture:latest found after pulling all tags"))
b291f5a3
 
f324f485
 	splitLatest := strings.Fields(latestLine)
 	splitCurrent := strings.Fields(splitOutImageCmd[1])
d1766999
 
 	// Clear relative creation times, since these can easily change between
 	// two invocations of "docker images". Without this, the test can fail
 	// like this:
 	// ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
 	// ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
 	splitLatest[3] = ""
 	splitLatest[4] = ""
 	splitLatest[5] = ""
 	splitCurrent[3] = ""
 	splitCurrent[4] = ""
 	splitCurrent[5] = ""
 
5739ba1b
 	c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("dockercore/engine-pull-all-test-fixture:latest was changed after pulling all tags"))
268fa5af
 }
3e90b12d
 
f324f485
 // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
572ce802
 // gets cancelled.
f324f485
 //
 // Ref: docker/docker#15589
 func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
8eeafa05
 	repoName := "hello-world:latest"
 
f324f485
 	pullCmd := s.MakeCmd("pull", repoName)
8eeafa05
 	stdout, err := pullCmd.StdoutPipe()
f324f485
 	c.Assert(err, checker.IsNil)
8eeafa05
 	err = pullCmd.Start()
f324f485
 	c.Assert(err, checker.IsNil)
617f89b9
 	go pullCmd.Wait()
8eeafa05
 
f324f485
 	// Cancel as soon as we get some output.
8eeafa05
 	buf := make([]byte, 10)
 	_, err = stdout.Read(buf)
f324f485
 	c.Assert(err, checker.IsNil)
8eeafa05
 
 	err = pullCmd.Process.Kill()
f324f485
 	c.Assert(err, checker.IsNil)
8eeafa05
 
572ce802
 	time.Sleep(2 * time.Second)
1b010516
 	_, err = s.CmdWithError("inspect", repoName)
 	c.Assert(err, checker.NotNil, check.Commentf("image was pulled after client disconnected"))
8eeafa05
 }
6d23c3c5
 
8437d0a3
 // Regression test for https://github.com/docker/docker/issues/26429
 func (s *DockerSuite) TestPullLinuxImageFailsOnWindows(c *check.C) {
 	testRequires(c, DaemonIsWindows, Network)
 	_, _, err := dockerCmdWithError("pull", "ubuntu")
b291f5a3
 	c.Assert(err.Error(), checker.Contains, "no matching manifest")
8437d0a3
 }
d5530406
 
 // Regression test for https://github.com/docker/docker/issues/28892
 func (s *DockerSuite) TestPullWindowsImageFailsOnLinux(c *check.C) {
 	testRequires(c, DaemonIsLinux, Network)
 	_, _, err := dockerCmdWithError("pull", "microsoft/nanoserver")
 	c.Assert(err.Error(), checker.Contains, "cannot be used on this platform")
 }