浏览代码

Merge pull request #46602 from vvoland/integration-TestPullNonExistingImage

TestPullNonExistingImage: Simplify and move to integration
Sebastiaan van Stijn 1 年之前
父节点
当前提交
949f8c9f2a
共有 2 个文件被更改,包括 40 次插入74 次删除
  1. 0 74
      integration-cli/docker_cli_pull_test.go
  2. 40 0
      integration/image/pull_test.go

+ 0 - 74
integration-cli/docker_cli_pull_test.go

@@ -2,10 +2,8 @@ package main
 
 import (
 	"context"
-	"fmt"
 	"regexp"
 	"strings"
-	"sync"
 	"testing"
 	"time"
 
@@ -51,78 +49,6 @@ func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *testing.T) {
 	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

+ 40 - 0
integration/image/pull_test.go

@@ -3,9 +3,11 @@ package image
 import (
 	"context"
 	"encoding/json"
+	"fmt"
 	"io"
 	"os"
 	"path"
+	"strings"
 	"testing"
 
 	"github.com/containerd/containerd"
@@ -21,6 +23,7 @@ import (
 	"github.com/opencontainers/image-spec/specs-go"
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
 	"gotest.tools/v3/assert"
+	is "gotest.tools/v3/assert/cmp"
 	"gotest.tools/v3/skip"
 )
 
@@ -155,3 +158,40 @@ func TestImagePullStoredfDigestForOtherRepo(t *testing.T) {
 	assert.Assert(t, err != nil, "Expected error, got none: %v", err)
 	assert.Assert(t, errdefs.IsNotFound(err), err)
 }
+
+// TestImagePullNonExisting pulls non-existing images from the central registry, with different
+// combinations of implicit tag and library prefix.
+func TestImagePullNonExisting(t *testing.T) {
+	ctx := setupTest(t)
+
+	for _, ref := range []string{
+		"asdfasdf:foobar",
+		"library/asdfasdf:foobar",
+		"asdfasdf",
+		"asdfasdf:latest",
+		"library/asdfasdf",
+		"library/asdfasdf:latest",
+	} {
+		ref := ref
+		all := strings.Contains(ref, ":")
+		t.Run(ref, func(t *testing.T) {
+			t.Parallel()
+
+			client := testEnv.APIClient()
+			rdr, err := client.ImagePull(ctx, ref, types.ImagePullOptions{
+				All: all,
+			})
+			if err == nil {
+				rdr.Close()
+			}
+
+			expectedMsg := fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", "asdfasdf")
+			assert.Assert(t, is.ErrorContains(err, expectedMsg))
+			assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
+			if all {
+				// pull -a on a nonexistent registry should fall back as well
+				assert.Check(t, !strings.Contains(err.Error(), "unauthorized"), `message should not contain "unauthorized"`)
+			}
+		})
+	}
+}