1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_pull_test.go
Sebastiaan van Stijn 08d01be870
integration-cli: fix empty-lines (revive)
integration-cli/docker_cli_pull_test.go:55:69: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_exec_test.go:46:64: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_service_health_test.go:86:65: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_images_test.go:128:66: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_swarm_node_test.go:79:69: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_health_test.go:51:57: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_health_test.go:159:73: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_swarm_unix_test.go:60:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_inspect_test.go:30:33: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_build_test.go:429:71: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_attach_unix_test.go:19:78: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_build_test.go:470:70: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_history_test.go:29:64: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_links_test.go:93:86: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_create_test.go:33:61: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_links_test.go:145:78: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_create_test.go:114:70: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_attach_test.go:226:153: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_by_digest_test.go:239:71: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_create_test.go:135:49: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_create_test.go:143:75: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_create_test.go:181:71: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_inspect_test.go:72:65: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_swarm_service_test.go:98:77: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_swarm_service_test.go:144:69: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_rmi_test.go:63:2: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_swarm_service_test.go:199:79: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_rmi_test.go:69:2: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_swarm_service_test.go:300:75: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_prune_unix_test.go:35:25: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_events_unix_test.go:393:60: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_events_unix_test.go:441:71: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_ps_test.go:33:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_ps_test.go:559:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_events_test.go:117:75: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_containers_test.go:547:74: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_api_containers_test.go:1054:84: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_containers_test.go:1076:87: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_containers_test.go:1232:72: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_api_containers_test.go:1801:21: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_network_unix_test.go:58:95: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_network_unix_test.go:750:75: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_network_unix_test.go:765:76: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_swarm_test.go:617:100: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_swarm_test.go:892:72: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_daemon_test.go:119:74: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_daemon_test.go:981:68: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_daemon_test.go:1951:87: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_run_test.go:83:66: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_run_test.go:357:72: empty-lines: extra empty line at the start of a block (revive)
    integration-cli/docker_cli_build_test.go:89:83: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:114:83: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:183:80: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:290:71: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:314:65: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:331:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:366:76: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:403:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:648:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:708:72: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:938:66: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1018:72: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1097:2: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1182:62: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1244:66: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1524:69: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1546:80: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1716:70: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:1730:65: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:2162:74: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:2270:71: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:2288:70: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3206:65: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3392:66: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3433:72: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3678:76: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3732:67: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3759:69: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3802:61: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:3898:66: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:4107:9: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:4791:74: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:4821:73: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:4854:70: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:5341:74: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_cli_build_test.go:5593:81: empty-lines: extra empty line at the end of a block (revive)
    integration-cli/docker_api_containers_test.go:2145:11: empty-lines: extra empty line at the start of a block (revive)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit dc0c2340b8)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-30 23:59:29 +02:00

288 lines
10 KiB
Go

package main
import (
"fmt"
"regexp"
"strings"
"sync"
"testing"
"time"
"github.com/opencontainers/go-digest"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
type DockerCLIPullSuite struct {
ds *DockerSuite
}
func (s *DockerCLIPullSuite) TearDownTest(c *testing.T) {
s.ds.TearDownTest(c)
}
func (s *DockerCLIPullSuite) OnTimeout(c *testing.T) {
s.ds.OnTimeout(c)
}
// TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
// prints all expected output.
func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *testing.T) {
testRequires(c, DaemonIsLinux)
out := s.Cmd(c, "pull", "hello-world")
defer deleteImages("hello-world")
assert.Assert(c, strings.Contains(out, "Using default tag: latest"), "expected the 'latest' tag to be automatically assumed")
assert.Assert(c, strings.Contains(out, "Pulling from library/hello-world"), "expected the 'library/' prefix to be automatically assumed")
assert.Assert(c, strings.Contains(out, "Downloaded newer image for hello-world:latest"))
matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1)
assert.Equal(c, len(matches), 1, "expected exactly one image digest in the output")
assert.Equal(c, len(matches[0]), 2, "unexpected number of submatches for the digest")
_, err := digest.Parse(matches[0][1])
assert.NilError(c, err, "invalid digest %q in output", matches[0][1])
// We should have a single entry in images.
img := strings.TrimSpace(s.Cmd(c, "images"))
splitImg := strings.Split(img, "\n")
assert.Equal(c, len(splitImg), 2)
match, _ := regexp.MatchString(`hello-world\s+latest.*?`, splitImg[1])
assert.Assert(c, match, "invalid output for `docker images` (expected image and tag name)")
}
// TestPullNonExistingImage pulls non-existing images from the central registry, with different
// combinations of implicit tag and library prefix.
func (s *DockerHubPullSuite) TestPullNonExistingImage(c *testing.T) {
testRequires(c, DaemonIsLinux)
type entry struct {
repo string
alias string
tag string
}
entries := []entry{
{"asdfasdf", "asdfasdf", "foobar"},
{"asdfasdf", "library/asdfasdf", "foobar"},
{"asdfasdf", "asdfasdf", ""},
{"asdfasdf", "asdfasdf", "latest"},
{"asdfasdf", "library/asdfasdf", ""},
{"asdfasdf", "library/asdfasdf", "latest"},
}
// The option field indicates "-a" or not.
type record struct {
e entry
option string
out string
err error
}
// 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()
repoName := e.alias
if e.tag != "" {
repoName += ":" + e.tag
}
out, err := s.CmdWithError("pull", repoName)
recordChan <- record{e, "", out, err}
}(e)
if e.tag == "" {
// pull -a on a nonexistent registry should fall back as well
group.Add(1)
go func(e entry) {
defer group.Done()
out, err := s.CmdWithError("pull", "-a", e.alias)
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 {
assert.ErrorContains(c, record.err, "", "expected non-zero exit status when pulling non-existing image: %s", record.out)
assert.Assert(c, strings.Contains(record.out, fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", record.e.repo)), "expected image not found error messages")
} else {
// pull -a on a nonexistent registry should fall back as well
assert.ErrorContains(c, record.err, "", "expected non-zero exit status when pulling non-existing image: %s", record.out)
assert.Assert(c, strings.Contains(record.out, fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", record.e.repo)), "expected image not found error messages")
assert.Assert(c, !strings.Contains(record.out, "unauthorized"), `message should not contain "unauthorized"`)
}
}
}
// TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
// that pulling the same image with different combinations of implicit elements of the image
// reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
// multiple images.
func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *testing.T) {
testRequires(c, DaemonIsLinux)
// Pull hello-world from v2
pullFromV2 := func(ref string) (int, string) {
out := s.Cmd(c, "pull", "hello-world")
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 {
c.Fatalf("too many v1 fallback incidents when pulling %s", ref)
}
s.Cmd(c, "rmi", ref)
out = s.Cmd(c, "pull", ref)
v1Retries++
}
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")
}
assert.Assert(c, strings.Contains(out, "Image is up to date for hello-world:latest"))
}
s.Cmd(c, "rmi", "hello-world-backup")
// We should have a single entry in images.
img := strings.TrimSpace(s.Cmd(c, "images"))
splitImg := strings.Split(img, "\n")
assert.Equal(c, len(splitImg), 2)
match, _ := regexp.MatchString(`hello-world\s+latest.*?`, splitImg[1])
assert.Assert(c, match, "invalid output for `docker images` (expected image and tag name)")
}
// TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, err := s.CmdWithError("pull", "scratch")
assert.ErrorContains(c, err, "", "expected pull of scratch to fail")
assert.Assert(c, strings.Contains(out, "'scratch' is a reserved name"))
assert.Assert(c, !strings.Contains(out, "Pulling repository scratch"))
}
// 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 *testing.T) {
testRequires(c, DaemonIsLinux)
s.Cmd(c, "pull", "dockercore/engine-pull-all-test-fixture")
outImageCmd := s.Cmd(c, "images", "dockercore/engine-pull-all-test-fixture")
splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
assert.Equal(c, len(splitOutImageCmd), 2)
s.Cmd(c, "pull", "--all-tags=true", "dockercore/engine-pull-all-test-fixture")
outImageAllTagCmd := s.Cmd(c, "images", "dockercore/engine-pull-all-test-fixture")
linesCount := strings.Count(outImageAllTagCmd, "\n")
assert.Assert(c, linesCount > 2, "pulling all tags should provide more than two images, got %s", outImageAllTagCmd)
// Verify that the line for 'dockercore/engine-pull-all-test-fixture:latest' is left unchanged.
var latestLine string
for _, line := range strings.Split(outImageAllTagCmd, "\n") {
if strings.HasPrefix(line, "dockercore/engine-pull-all-test-fixture") && strings.Contains(line, "latest") {
latestLine = line
break
}
}
assert.Assert(c, latestLine != "", "no entry for dockercore/engine-pull-all-test-fixture:latest found after pulling all tags")
splitLatest := strings.Fields(latestLine)
splitCurrent := strings.Fields(splitOutImageCmd[1])
// 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] = ""
assert.Assert(c, is.DeepEqual(splitLatest, splitCurrent), "dockercore/engine-pull-all-test-fixture:latest was changed after pulling all tags")
}
// TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
// gets cancelled.
//
// Ref: docker/docker#15589
func (s *DockerHubPullSuite) TestPullClientDisconnect(c *testing.T) {
testRequires(c, DaemonIsLinux)
repoName := "hello-world:latest"
pullCmd := s.MakeCmd("pull", repoName)
stdout, err := pullCmd.StdoutPipe()
assert.NilError(c, err)
err = pullCmd.Start()
assert.NilError(c, err)
go pullCmd.Wait()
// Cancel as soon as we get some output.
buf := make([]byte, 10)
_, err = stdout.Read(buf)
assert.NilError(c, err)
err = pullCmd.Process.Kill()
assert.NilError(c, err)
time.Sleep(2 * time.Second)
_, err = s.CmdWithError("inspect", repoName)
assert.ErrorContains(c, err, "", "image was pulled after client disconnected")
}
// Regression test for https://github.com/docker/docker/issues/26429
func (s *DockerCLIPullSuite) TestPullLinuxImageFailsOnWindows(c *testing.T) {
testRequires(c, DaemonIsWindows, Network)
_, _, err := dockerCmdWithError("pull", "ubuntu")
assert.ErrorContains(c, err, "no matching manifest for windows")
}
// Regression test for https://github.com/docker/docker/issues/28892
func (s *DockerCLIPullSuite) TestPullWindowsImageFailsOnLinux(c *testing.T) {
testRequires(c, DaemonIsLinux, Network)
_, _, err := dockerCmdWithError("pull", "mcr.microsoft.com/windows/servercore:ltsc2022")
assert.ErrorContains(c, err, "no matching manifest for linux")
}