Browse code

TestPullNonExistingImage: Use subtests

Rewrite the test to replace manual goroutines handling with paralell
subtests.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2023/10/05 21:00:01
Showing 1 changed files
... ...
@@ -5,7 +5,6 @@ import (
5 5
 	"fmt"
6 6
 	"regexp"
7 7
 	"strings"
8
-	"sync"
9 8
 	"testing"
10 9
 	"time"
11 10
 
... ...
@@ -56,70 +55,35 @@ func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *testing.T) {
56 56
 func (s *DockerHubPullSuite) TestPullNonExistingImage(c *testing.T) {
57 57
 	testRequires(c, DaemonIsLinux)
58 58
 
59
-	type entry struct {
60
-		repo  string
61
-		alias string
62
-		tag   string
63
-	}
64
-
65
-	entries := []entry{
66
-		{"asdfasdf", "asdfasdf", "foobar"},
67
-		{"asdfasdf", "library/asdfasdf", "foobar"},
68
-		{"asdfasdf", "asdfasdf", ""},
69
-		{"asdfasdf", "asdfasdf", "latest"},
70
-		{"asdfasdf", "library/asdfasdf", ""},
71
-		{"asdfasdf", "library/asdfasdf", "latest"},
72
-	}
73
-
74
-	// The option field indicates "-a" or not.
75
-	type record struct {
76
-		e      entry
77
-		option string
78
-		out    string
79
-		err    error
80
-	}
81
-
82
-	// Execute 'docker pull' in parallel, pass results (out, err) and
83
-	// necessary information ("-a" or not, and the image name) to channel.
84
-	var group sync.WaitGroup
85
-	recordChan := make(chan record, len(entries)*2)
86
-	for _, e := range entries {
87
-		group.Add(1)
88
-		go func(e entry) {
89
-			defer group.Done()
90
-			repoName := e.alias
91
-			if e.tag != "" {
92
-				repoName += ":" + e.tag
59
+	for _, ref := range []string{
60
+		"asdfasdf:foobar",
61
+		"library/asdfasdf:foobar",
62
+		"asdfasdf",
63
+		"asdfasdf:latest",
64
+		"library/asdfasdf",
65
+		"library/asdfasdf:latest",
66
+	} {
67
+		ref := ref
68
+		all := strings.Contains(ref, ":")
69
+		c.Run(ref, func(t *testing.T) {
70
+			t.Parallel()
71
+
72
+			var out string
73
+			var err error
74
+			if all {
75
+				out, err = s.CmdWithError("pull", "-a", repoName)
76
+			} else {
77
+				out, err = s.CmdWithError("pull", repoName)
93 78
 			}
94
-			out, err := s.CmdWithError("pull", repoName)
95
-			recordChan <- record{e, "", out, err}
96
-		}(e)
97
-		if e.tag == "" {
98
-			// pull -a on a nonexistent registry should fall back as well
99
-			group.Add(1)
100
-			go func(e entry) {
101
-				defer group.Done()
102
-				out, err := s.CmdWithError("pull", "-a", e.alias)
103
-				recordChan <- record{e, "-a", out, err}
104
-			}(e)
105
-		}
106
-	}
107 79
 
108
-	// Wait for completion
109
-	group.Wait()
110
-	close(recordChan)
111
-
112
-	// Process the results (out, err).
113
-	for record := range recordChan {
114
-		if len(record.option) == 0 {
115
-			assert.ErrorContains(c, record.err, "", "expected non-zero exit status when pulling non-existing image: %s", record.out)
116
-			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")
117
-		} else {
118
-			// pull -a on a nonexistent registry should fall back as well
119
-			assert.ErrorContains(c, record.err, "", "expected non-zero exit status when pulling non-existing image: %s", record.out)
120
-			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")
121
-			assert.Assert(c, !strings.Contains(record.out, "unauthorized"), `message should not contain "unauthorized"`)
122
-		}
80
+			expectedRepo := "asdfasdf"
81
+			assert.Check(t, is.ErrorContains(err, ""), "expected non-zero exit status when pulling non-existing image: %s", out)
82
+			assert.Check(t, is.Contains(out, fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", expectedRepo)))
83
+			if all {
84
+				// pull -a on a nonexistent registry should fall back as well
85
+				assert.Check(t, !strings.Contains(out, "unauthorized"), `message should not contain "unauthorized"`)
86
+			}
87
+		})
123 88
 	}
124 89
 }
125 90