Browse code

Update image pull tests

Update and migrate existing tests to the `DockerHubPullSuite`. Most
tests were preserved, but refactored and made more exhaustive. One test
was deliberately removed (`TestPullVerified`) as it is unreliable and
that the feature was obsoleted by content trust.

Move all trust related tests to `docker_cli_pull_trusted_test.go`.

Move tests depending on a local registry to `docker_cli_pull_local_test.go`.

Signed-off-by: Arnaud Porterie <arnaud.porterie@docker.com>

Arnaud Porterie authored on 2015/09/03 09:56:01
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,39 @@
0
+package main
1
+
2
+import (
3
+	"fmt"
4
+
5
+	"github.com/go-check/check"
6
+)
7
+
8
+// TestPullImageWithAliases pulls a specific image tag and verifies that any aliases (i.e., other
9
+// tags for the same image) are not also pulled down.
10
+//
11
+// Ref: docker/docker#8141
12
+func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
13
+	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
14
+
15
+	repos := []string{}
16
+	for _, tag := range []string{"recent", "fresh"} {
17
+		repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
18
+	}
19
+
20
+	// Tag and push the same image multiple times.
21
+	for _, repo := range repos {
22
+		dockerCmd(c, "tag", "busybox", repo)
23
+		dockerCmd(c, "push", repo)
24
+	}
25
+
26
+	// Clear local images store.
27
+	args := append([]string{"rmi"}, repos...)
28
+	dockerCmd(c, args...)
29
+
30
+	// Pull a single tag and verify it doesn't bring down all aliases.
31
+	dockerCmd(c, "pull", repos[0])
32
+	dockerCmd(c, "inspect", repos[0])
33
+	for _, repo := range repos[1:] {
34
+		if _, _, err := dockerCmdWithError("inspect", repo); err == nil {
35
+			c.Fatalf("Image %v shouldn't have been pulled down", repo)
36
+		}
37
+	}
38
+}
... ...
@@ -2,407 +2,154 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"os/exec"
5
+	"regexp"
6 6
 	"strings"
7 7
 	"time"
8 8
 
9
-	"io/ioutil"
10
-
9
+	"github.com/docker/distribution/digest"
10
+	"github.com/docker/docker/integration-cli/checker"
11 11
 	"github.com/go-check/check"
12 12
 )
13 13
 
14
-// See issue docker/docker#8141
15
-func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
16
-	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
17
-
18
-	repos := []string{}
19
-	for _, tag := range []string{"recent", "fresh"} {
20
-		repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
21
-	}
22
-
23
-	// Tag and push the same image multiple times.
24
-	for _, repo := range repos {
25
-		dockerCmd(c, "tag", "busybox", repo)
26
-		dockerCmd(c, "push", repo)
27
-	}
28
-
29
-	// Clear local images store.
30
-	args := append([]string{"rmi"}, repos...)
31
-	dockerCmd(c, args...)
32
-
33
-	// Pull a single tag and verify it doesn't bring down all aliases.
34
-	dockerCmd(c, "pull", repos[0])
35
-	dockerCmd(c, "inspect", repos[0])
36
-	for _, repo := range repos[1:] {
37
-		if _, _, err := dockerCmdWithError("inspect", repo); err == nil {
38
-			c.Fatalf("Image %v shouldn't have been pulled down", repo)
39
-		}
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
+	out := s.Cmd(c, "pull", "hello-world")
18
+	defer deleteImages("hello-world")
19
+
20
+	c.Assert(out, checker.Contains, "Using default tag: latest", check.Commentf("expected the 'latest' tag to be automatically assumed"))
21
+	c.Assert(out, checker.Contains, "Pulling from library/hello-world", check.Commentf("expected the 'library/' prefix to be automatically assumed"))
22
+	c.Assert(out, checker.Contains, "Downloaded newer image for hello-world:latest")
23
+
24
+	matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1)
25
+	c.Assert(len(matches), checker.Equals, 1, check.Commentf("expected exactly one image digest in the output"))
26
+	c.Assert(len(matches[0]), checker.Equals, 2, check.Commentf("unexpected number of submatches for the digest"))
27
+	_, err := digest.ParseDigest(matches[0][1])
28
+	c.Check(err, checker.IsNil, check.Commentf("invalid digest %q in output", matches[0][1]))
29
+
30
+	// We should have a single entry in images.
31
+	img := strings.TrimSpace(s.Cmd(c, "images"))
32
+	if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
33
+		c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
34
+	} else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
35
+		c.Fatal("invalid output for `docker images` (expected image and tag name")
40 36
 	}
41 37
 }
42 38
 
43
-// pulling library/hello-world should show verified message
44
-func (s *DockerSuite) TestPullVerified(c *check.C) {
45
-	c.Skip("Skipping hub dependent test")
46
-
47
-	// Image must be pulled from central repository to get verified message
48
-	// unless keychain is manually updated to contain the daemon's sign key.
49
-
50
-	verifiedName := "hello-world"
51
-
52
-	// pull it
53
-	expected := "The image you are pulling has been verified"
54
-	if out, exitCode, err := dockerCmdWithError("pull", verifiedName); err != nil || !strings.Contains(out, expected) {
55
-		if err != nil || exitCode != 0 {
56
-			c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
57
-		}
58
-		c.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
59
-	}
60
-
61
-	// pull it again
62
-	if out, exitCode, err := dockerCmdWithError("pull", verifiedName); err != nil || strings.Contains(out, expected) {
63
-		if err != nil || exitCode != 0 {
64
-			c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
65
-		}
66
-		c.Fatalf("pulling a verified image failed. unexpected verify message\ngot: %s, %v", out, err)
67
-	}
68
-
69
-}
70
-
71
-// pulling an image from the central registry should work
72
-func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
73
-	testRequires(c, Network)
74
-
75
-	dockerCmd(c, "pull", "hello-world")
76
-}
77
-
78
-// pulling a non-existing image from the central registry should return a non-zero exit code
79
-func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
80
-	testRequires(c, Network)
81
-
82
-	name := "sadfsadfasdf"
83
-	out, _, err := dockerCmdWithError("pull", name)
84
-
85
-	if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
86
-		c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
39
+// TestPullNonExistingImage pulls non-existing images from the central registry, with different
40
+// combinations of implicit tag and library prefix.
41
+func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
42
+	for _, e := range []struct {
43
+		Image string
44
+		Alias string
45
+	}{
46
+		{"library/asdfasdf:foobar", "asdfasdf:foobar"},
47
+		{"library/asdfasdf:foobar", "library/asdfasdf:foobar"},
48
+		{"library/asdfasdf:latest", "asdfasdf"},
49
+		{"library/asdfasdf:latest", "asdfasdf:latest"},
50
+		{"library/asdfasdf:latest", "library/asdfasdf"},
51
+		{"library/asdfasdf:latest", "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
+		c.Assert(out, checker.Contains, fmt.Sprintf("Error: image %s not found", e.Image), check.Commentf("expected image not found error messages"))
87 56
 	}
88 57
 }
89 58
 
90
-// pulling an image from the central registry using official names should work
91
-// ensure all pulls result in the same image
92
-func (s *DockerSuite) TestPullImageOfficialNames(c *check.C) {
93
-	testRequires(c, Network)
94
-
95
-	names := []string{
59
+// TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
60
+// that pulling the same image with different combinations of implicit elements of the the image
61
+// reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
62
+// multiple images.
63
+func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
64
+	s.Cmd(c, "pull", "hello-world")
65
+	defer deleteImages("hello-world")
66
+
67
+	for _, i := range []string{
68
+		"hello-world",
69
+		"hello-world:latest",
96 70
 		"library/hello-world",
71
+		"library/hello-world:latest",
97 72
 		"docker.io/library/hello-world",
98 73
 		"index.docker.io/library/hello-world",
99
-	}
100
-	for _, name := range names {
101
-		out, exitCode, err := dockerCmdWithError("pull", name)
102
-		if err != nil || exitCode != 0 {
103
-			c.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
104
-			continue
105
-		}
106
-
107
-		// ensure we don't have multiple image names.
108
-		out, _ = dockerCmd(c, "images")
109
-		if strings.Contains(out, name) {
110
-			c.Errorf("images should not have listed '%s'", name)
111
-		}
112
-	}
113
-}
114
-
115
-func (s *DockerSuite) TestPullScratchNotAllowed(c *check.C) {
116
-	testRequires(c, Network)
117
-
118
-	out, exitCode, err := dockerCmdWithError("pull", "scratch")
119
-	if err == nil {
120
-		c.Fatal("expected pull of scratch to fail, but it didn't")
121
-	}
122
-	if exitCode != 1 {
123
-		c.Fatalf("pulling scratch expected exit code 1, got %d", exitCode)
124
-	}
125
-	if strings.Contains(out, "Pulling repository scratch") {
126
-		c.Fatalf("pulling scratch should not have begun: %s", out)
127
-	}
128
-	if !strings.Contains(out, "'scratch' is a reserved name") {
129
-		c.Fatalf("unexpected output pulling scratch: %s", out)
130
-	}
131
-}
132
-
133
-// pulling an image with --all-tags=true
134
-func (s *DockerSuite) TestPullImageWithAllTagFromCentralRegistry(c *check.C) {
135
-	testRequires(c, Network)
136
-
137
-	dockerCmd(c, "pull", "busybox")
138
-
139
-	outImageCmd, _ := dockerCmd(c, "images", "busybox")
140
-
141
-	dockerCmd(c, "pull", "--all-tags=true", "busybox")
142
-
143
-	outImageAllTagCmd, _ := dockerCmd(c, "images", "busybox")
144
-
145
-	if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
146
-		c.Fatalf("Pulling with all tags should get more images")
147
-	}
148
-
149
-	// FIXME has probably no effect (tags already pushed)
150
-	dockerCmd(c, "pull", "-a", "busybox")
151
-
152
-	outImageAllTagCmd, _ = dockerCmd(c, "images", "busybox")
153
-
154
-	if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
155
-		c.Fatalf("Pulling with all tags should get more images")
156
-	}
157
-}
158
-
159
-func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
160
-	repoName := s.setupTrustedImage(c, "trusted-pull")
161
-
162
-	// Try pull
163
-	pullCmd := exec.Command(dockerBinary, "pull", repoName)
164
-	s.trustedCmd(pullCmd)
165
-	out, _, err := runCommandWithOutput(pullCmd)
166
-	if err != nil {
167
-		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
168
-	}
169
-
170
-	if !strings.Contains(string(out), "Tagging") {
171
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
172
-	}
173
-
174
-	dockerCmd(c, "rmi", repoName)
175
-
176
-	// Try untrusted pull to ensure we pushed the tag to the registry
177
-	pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
178
-	s.trustedCmd(pullCmd)
179
-	out, _, err = runCommandWithOutput(pullCmd)
180
-	if err != nil {
181
-		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
182
-	}
183
-
184
-	if !strings.Contains(string(out), "Status: Downloaded") {
185
-		c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
186
-	}
187
-}
188
-
189
-func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
190
-	repoName := s.setupTrustedImage(c, "trusted-isolatd-pull")
191
-
192
-	// Try pull (run from isolated directory without trust information)
193
-	pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
194
-	s.trustedCmd(pullCmd)
195
-	out, _, err := runCommandWithOutput(pullCmd)
196
-	if err != nil {
197
-		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
74
+	} {
75
+		out := s.Cmd(c, "pull", i)
76
+		c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
198 77
 	}
199 78
 
200
-	if !strings.Contains(string(out), "Tagging") {
201
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
79
+	// We should have a single entry in images.
80
+	img := strings.TrimSpace(s.Cmd(c, "images"))
81
+	if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
82
+		c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
83
+	} else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
84
+		c.Fatal("invalid output for `docker images` (expected image and tag name")
202 85
 	}
203
-
204
-	dockerCmd(c, "rmi", repoName)
205 86
 }
206 87
 
207
-func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
208
-	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
209
-	// tag the image and upload it to the private registry
210
-	dockerCmd(c, "tag", "busybox", repoName)
211
-	dockerCmd(c, "push", repoName)
212
-	dockerCmd(c, "rmi", repoName)
213
-
214
-	// Try trusted pull on untrusted tag
215
-	pullCmd := exec.Command(dockerBinary, "pull", repoName)
216
-	s.trustedCmd(pullCmd)
217
-	out, _, err := runCommandWithOutput(pullCmd)
218
-	if err == nil {
219
-		c.Fatalf("Error expected when running trusted pull with:\n%s", out)
220
-	}
221
-
222
-	if !strings.Contains(string(out), "no trust data available") {
223
-		c.Fatalf("Missing expected output on trusted pull:\n%s", out)
224
-	}
88
+// TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
89
+func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
90
+	out, err := s.CmdWithError("pull", "scratch")
91
+	c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
92
+	c.Assert(out, checker.Contains, "'scratch' is a reserved name")
93
+	c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
225 94
 }
226 95
 
227
-func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
228
-	c.Skip("Currently changes system time, causing instability")
229
-	repoName := s.setupTrustedImage(c, "trusted-cert-expired")
230
-
231
-	// Certificates have 10 years of expiration
232
-	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
233
-
234
-	runAtDifferentDate(elevenYearsFromNow, func() {
235
-		// Try pull
236
-		pullCmd := exec.Command(dockerBinary, "pull", repoName)
237
-		s.trustedCmd(pullCmd)
238
-		out, _, err := runCommandWithOutput(pullCmd)
239
-		if err == nil {
240
-			c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
241
-		}
242
-
243
-		if !strings.Contains(string(out), "could not validate the path to a trusted root") {
244
-			c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
245
-		}
246
-	})
247
-
248
-	runAtDifferentDate(elevenYearsFromNow, func() {
249
-		// Try pull
250
-		pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
251
-		s.trustedCmd(pullCmd)
252
-		out, _, err := runCommandWithOutput(pullCmd)
253
-		if err != nil {
254
-			c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
255
-		}
256
-
257
-		if !strings.Contains(string(out), "Status: Downloaded") {
258
-			c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
96
+// TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
97
+// results in more images than a naked pull.
98
+func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
99
+	s.Cmd(c, "pull", "busybox")
100
+	outImageCmd := s.Cmd(c, "images", "busybox")
101
+	splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
102
+	c.Assert(splitOutImageCmd, checker.HasLen, 2, check.Commentf("expected a single entry in images\n%v", outImageCmd))
103
+
104
+	s.Cmd(c, "pull", "--all-tags=true", "busybox")
105
+	outImageAllTagCmd := s.Cmd(c, "images", "busybox")
106
+	if linesCount := strings.Count(outImageAllTagCmd, "\n"); linesCount <= 2 {
107
+		c.Fatalf("pulling all tags should provide more images, got %d", linesCount-1)
108
+	}
109
+
110
+	// Verify that the line for 'busybox:latest' is left unchanged.
111
+	var latestLine string
112
+	for _, line := range strings.Split(outImageAllTagCmd, "\n") {
113
+		if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") {
114
+			latestLine = line
115
+			break
259 116
 		}
260
-	})
261
-}
262
-
263
-func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
264
-	repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
265
-	evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
266
-	if err != nil {
267
-		c.Fatalf("Failed to create local temp dir")
268
-	}
269
-
270
-	// tag the image and upload it to the private registry
271
-	dockerCmd(c, "tag", "busybox", repoName)
272
-
273
-	pushCmd := exec.Command(dockerBinary, "push", repoName)
274
-	s.trustedCmd(pushCmd)
275
-	out, _, err := runCommandWithOutput(pushCmd)
276
-	if err != nil {
277
-		c.Fatalf("Error running trusted push: %s\n%s", err, out)
278
-	}
279
-	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
280
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
281
-	}
282
-
283
-	dockerCmd(c, "rmi", repoName)
284
-
285
-	// Try pull
286
-	pullCmd := exec.Command(dockerBinary, "pull", repoName)
287
-	s.trustedCmd(pullCmd)
288
-	out, _, err = runCommandWithOutput(pullCmd)
289
-	if err != nil {
290
-		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
291
-	}
292
-
293
-	if !strings.Contains(string(out), "Tagging") {
294
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
295
-	}
296
-
297
-	dockerCmd(c, "rmi", repoName)
298
-
299
-	// Kill the notary server, start a new "evil" one.
300
-	s.not.Close()
301
-	s.not, err = newTestNotary(c)
302
-	if err != nil {
303
-		c.Fatalf("Restarting notary server failed.")
304
-	}
305
-
306
-	// In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
307
-	// tag an image and upload it to the private registry
308
-	dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
309
-
310
-	// Push up to the new server
311
-	pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
312
-	s.trustedCmd(pushCmd)
313
-	out, _, err = runCommandWithOutput(pushCmd)
314
-	if err != nil {
315
-		c.Fatalf("Error running trusted push: %s\n%s", err, out)
316
-	}
317
-	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
318
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
319
-	}
320
-
321
-	// Now, try pulling with the original client from this new trust server. This should fail.
322
-	pullCmd = exec.Command(dockerBinary, "pull", repoName)
323
-	s.trustedCmd(pullCmd)
324
-	out, _, err = runCommandWithOutput(pullCmd)
325
-	if err == nil {
326
-		c.Fatalf("Expected to fail on this pull due to different remote data: %s\n%s", err, out)
327
-	}
328
-
329
-	if !strings.Contains(string(out), "failed to validate data with current trusted certificates") {
330
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
331 117
 	}
118
+	c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags"))
119
+	splitLatest := strings.Fields(latestLine)
120
+	splitCurrent := strings.Fields(splitOutImageCmd[1])
121
+	c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
332 122
 }
333 123
 
334
-func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
335
-	c.Skip("Currently changes system time, causing instability")
336
-	repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
337
-	// tag the image and upload it to the private registry
338
-	dockerCmd(c, "tag", "busybox", repoName)
339
-
340
-	// Push with default passphrases
341
-	pushCmd := exec.Command(dockerBinary, "push", repoName)
342
-	s.trustedCmd(pushCmd)
343
-	out, _, err := runCommandWithOutput(pushCmd)
344
-	if err != nil {
345
-		c.Fatalf("trusted push failed: %s\n%s", err, out)
346
-	}
347
-
348
-	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
349
-		c.Fatalf("Missing expected output on trusted push:\n%s", out)
350
-	}
351
-
352
-	dockerCmd(c, "rmi", repoName)
353
-
354
-	// Snapshots last for three years. This should be expired
355
-	fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
356
-
357
-	// Should succeed because the server transparently re-signs one
358
-	runAtDifferentDate(fourYearsLater, func() {
359
-		// Try pull
360
-		pullCmd := exec.Command(dockerBinary, "pull", repoName)
361
-		s.trustedCmd(pullCmd)
362
-		out, _, err = runCommandWithOutput(pullCmd)
363
-		if err == nil {
364
-			c.Fatalf("Missing expected error running trusted pull with expired snapshots")
365
-		}
366
-
367
-		if !strings.Contains(string(out), "repository out-of-date") {
368
-			c.Fatalf("Missing expected output on trusted pull with expired snapshot:\n%s", out)
369
-		}
370
-	})
371
-}
372
-
373
-// Test that pull continues after client has disconnected. #15589
374
-func (s *DockerSuite) TestPullClientDisconnect(c *check.C) {
375
-	testRequires(c, Network)
376
-
124
+// TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
125
+// still succesfully completes on the daemon side.
126
+//
127
+// Ref: docker/docker#15589
128
+func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
377 129
 	repoName := "hello-world:latest"
378 130
 
379
-	dockerCmdWithError("rmi", repoName) // clean just in case
380
-
381
-	pullCmd := exec.Command(dockerBinary, "pull", repoName)
382
-
131
+	pullCmd := s.MakeCmd("pull", repoName)
383 132
 	stdout, err := pullCmd.StdoutPipe()
384
-	c.Assert(err, check.IsNil)
385
-
133
+	c.Assert(err, checker.IsNil)
386 134
 	err = pullCmd.Start()
387
-	c.Assert(err, check.IsNil)
135
+	c.Assert(err, checker.IsNil)
388 136
 
389
-	// cancel as soon as we get some output
137
+	// Cancel as soon as we get some output.
390 138
 	buf := make([]byte, 10)
391 139
 	_, err = stdout.Read(buf)
392
-	c.Assert(err, check.IsNil)
140
+	c.Assert(err, checker.IsNil)
393 141
 
394 142
 	err = pullCmd.Process.Kill()
395
-	c.Assert(err, check.IsNil)
143
+	c.Assert(err, checker.IsNil)
396 144
 
397 145
 	maxAttempts := 20
398 146
 	for i := 0; ; i++ {
399
-		if _, _, err := dockerCmdWithError("inspect", repoName); err == nil {
147
+		if _, err := s.CmdWithError("inspect", repoName); err == nil {
400 148
 			break
401 149
 		}
402 150
 		if i >= maxAttempts {
403
-			c.Fatal("Timeout reached. Image was not pulled after client disconnected.")
151
+			c.Fatal("timeout reached: image was not pulled after client disconnected")
404 152
 		}
405 153
 		time.Sleep(500 * time.Millisecond)
406 154
 	}
407
-
408 155
 }
409 156
new file mode 100644
... ...
@@ -0,0 +1,225 @@
0
+package main
1
+
2
+import (
3
+	"fmt"
4
+	"io/ioutil"
5
+	"os/exec"
6
+	"strings"
7
+	"time"
8
+
9
+	"github.com/go-check/check"
10
+)
11
+
12
+func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
13
+	repoName := s.setupTrustedImage(c, "trusted-pull")
14
+
15
+	// Try pull
16
+	pullCmd := exec.Command(dockerBinary, "pull", repoName)
17
+	s.trustedCmd(pullCmd)
18
+	out, _, err := runCommandWithOutput(pullCmd)
19
+	if err != nil {
20
+		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
21
+	}
22
+
23
+	if !strings.Contains(string(out), "Tagging") {
24
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
25
+	}
26
+
27
+	dockerCmd(c, "rmi", repoName)
28
+
29
+	// Try untrusted pull to ensure we pushed the tag to the registry
30
+	pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
31
+	s.trustedCmd(pullCmd)
32
+	out, _, err = runCommandWithOutput(pullCmd)
33
+	if err != nil {
34
+		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
35
+	}
36
+
37
+	if !strings.Contains(string(out), "Status: Downloaded") {
38
+		c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
39
+	}
40
+}
41
+
42
+func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
43
+	repoName := s.setupTrustedImage(c, "trusted-isolatd-pull")
44
+
45
+	// Try pull (run from isolated directory without trust information)
46
+	pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
47
+	s.trustedCmd(pullCmd)
48
+	out, _, err := runCommandWithOutput(pullCmd)
49
+	if err != nil {
50
+		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
51
+	}
52
+
53
+	if !strings.Contains(string(out), "Tagging") {
54
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
55
+	}
56
+
57
+	dockerCmd(c, "rmi", repoName)
58
+}
59
+
60
+func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
61
+	repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
62
+	// tag the image and upload it to the private registry
63
+	dockerCmd(c, "tag", "busybox", repoName)
64
+	dockerCmd(c, "push", repoName)
65
+	dockerCmd(c, "rmi", repoName)
66
+
67
+	// Try trusted pull on untrusted tag
68
+	pullCmd := exec.Command(dockerBinary, "pull", repoName)
69
+	s.trustedCmd(pullCmd)
70
+	out, _, err := runCommandWithOutput(pullCmd)
71
+	if err == nil {
72
+		c.Fatalf("Error expected when running trusted pull with:\n%s", out)
73
+	}
74
+
75
+	if !strings.Contains(string(out), "no trust data available") {
76
+		c.Fatalf("Missing expected output on trusted pull:\n%s", out)
77
+	}
78
+}
79
+
80
+func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
81
+	c.Skip("Currently changes system time, causing instability")
82
+	repoName := s.setupTrustedImage(c, "trusted-cert-expired")
83
+
84
+	// Certificates have 10 years of expiration
85
+	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
86
+
87
+	runAtDifferentDate(elevenYearsFromNow, func() {
88
+		// Try pull
89
+		pullCmd := exec.Command(dockerBinary, "pull", repoName)
90
+		s.trustedCmd(pullCmd)
91
+		out, _, err := runCommandWithOutput(pullCmd)
92
+		if err == nil {
93
+			c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
94
+		}
95
+
96
+		if !strings.Contains(string(out), "could not validate the path to a trusted root") {
97
+			c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
98
+		}
99
+	})
100
+
101
+	runAtDifferentDate(elevenYearsFromNow, func() {
102
+		// Try pull
103
+		pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
104
+		s.trustedCmd(pullCmd)
105
+		out, _, err := runCommandWithOutput(pullCmd)
106
+		if err != nil {
107
+			c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
108
+		}
109
+
110
+		if !strings.Contains(string(out), "Status: Downloaded") {
111
+			c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
112
+		}
113
+	})
114
+}
115
+
116
+func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
117
+	repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
118
+	evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
119
+	if err != nil {
120
+		c.Fatalf("Failed to create local temp dir")
121
+	}
122
+
123
+	// tag the image and upload it to the private registry
124
+	dockerCmd(c, "tag", "busybox", repoName)
125
+
126
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
127
+	s.trustedCmd(pushCmd)
128
+	out, _, err := runCommandWithOutput(pushCmd)
129
+	if err != nil {
130
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
131
+	}
132
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
133
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
134
+	}
135
+
136
+	dockerCmd(c, "rmi", repoName)
137
+
138
+	// Try pull
139
+	pullCmd := exec.Command(dockerBinary, "pull", repoName)
140
+	s.trustedCmd(pullCmd)
141
+	out, _, err = runCommandWithOutput(pullCmd)
142
+	if err != nil {
143
+		c.Fatalf("Error running trusted pull: %s\n%s", err, out)
144
+	}
145
+
146
+	if !strings.Contains(string(out), "Tagging") {
147
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
148
+	}
149
+
150
+	dockerCmd(c, "rmi", repoName)
151
+
152
+	// Kill the notary server, start a new "evil" one.
153
+	s.not.Close()
154
+	s.not, err = newTestNotary(c)
155
+	if err != nil {
156
+		c.Fatalf("Restarting notary server failed.")
157
+	}
158
+
159
+	// In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
160
+	// tag an image and upload it to the private registry
161
+	dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
162
+
163
+	// Push up to the new server
164
+	pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
165
+	s.trustedCmd(pushCmd)
166
+	out, _, err = runCommandWithOutput(pushCmd)
167
+	if err != nil {
168
+		c.Fatalf("Error running trusted push: %s\n%s", err, out)
169
+	}
170
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
171
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
172
+	}
173
+
174
+	// Now, try pulling with the original client from this new trust server. This should fail.
175
+	pullCmd = exec.Command(dockerBinary, "pull", repoName)
176
+	s.trustedCmd(pullCmd)
177
+	out, _, err = runCommandWithOutput(pullCmd)
178
+	if err == nil {
179
+		c.Fatalf("Expected to fail on this pull due to different remote data: %s\n%s", err, out)
180
+	}
181
+
182
+	if !strings.Contains(string(out), "failed to validate data with current trusted certificates") {
183
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
184
+	}
185
+}
186
+
187
+func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
188
+	c.Skip("Currently changes system time, causing instability")
189
+	repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
190
+	// tag the image and upload it to the private registry
191
+	dockerCmd(c, "tag", "busybox", repoName)
192
+
193
+	// Push with default passphrases
194
+	pushCmd := exec.Command(dockerBinary, "push", repoName)
195
+	s.trustedCmd(pushCmd)
196
+	out, _, err := runCommandWithOutput(pushCmd)
197
+	if err != nil {
198
+		c.Fatalf("trusted push failed: %s\n%s", err, out)
199
+	}
200
+
201
+	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
202
+		c.Fatalf("Missing expected output on trusted push:\n%s", out)
203
+	}
204
+
205
+	dockerCmd(c, "rmi", repoName)
206
+
207
+	// Snapshots last for three years. This should be expired
208
+	fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
209
+
210
+	// Should succeed because the server transparently re-signs one
211
+	runAtDifferentDate(fourYearsLater, func() {
212
+		// Try pull
213
+		pullCmd := exec.Command(dockerBinary, "pull", repoName)
214
+		s.trustedCmd(pullCmd)
215
+		out, _, err = runCommandWithOutput(pullCmd)
216
+		if err == nil {
217
+			c.Fatalf("Missing expected error running trusted pull with expired snapshots")
218
+		}
219
+
220
+		if !strings.Contains(string(out), "repository out-of-date") {
221
+			c.Fatalf("Missing expected output on trusted pull with expired snapshot:\n%s", out)
222
+		}
223
+	})
224
+}