github.com/dinever/docker@v1.11.1/integration-cli/docker_cli_pull_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "regexp" 6 "strings" 7 "sync" 8 "time" 9 10 "github.com/docker/distribution/digest" 11 "github.com/docker/docker/pkg/integration/checker" 12 "github.com/go-check/check" 13 ) 14 15 // TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client 16 // prints all expected output. 17 func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *check.C) { 18 testRequires(c, DaemonIsLinux) 19 out := s.Cmd(c, "pull", "hello-world") 20 defer deleteImages("hello-world") 21 22 c.Assert(out, checker.Contains, "Using default tag: latest", check.Commentf("expected the 'latest' tag to be automatically assumed")) 23 c.Assert(out, checker.Contains, "Pulling from library/hello-world", check.Commentf("expected the 'library/' prefix to be automatically assumed")) 24 c.Assert(out, checker.Contains, "Downloaded newer image for hello-world:latest") 25 26 matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1) 27 c.Assert(len(matches), checker.Equals, 1, check.Commentf("expected exactly one image digest in the output")) 28 c.Assert(len(matches[0]), checker.Equals, 2, check.Commentf("unexpected number of submatches for the digest")) 29 _, err := digest.ParseDigest(matches[0][1]) 30 c.Check(err, checker.IsNil, check.Commentf("invalid digest %q in output", matches[0][1])) 31 32 // We should have a single entry in images. 33 img := strings.TrimSpace(s.Cmd(c, "images")) 34 splitImg := strings.Split(img, "\n") 35 c.Assert(splitImg, checker.HasLen, 2) 36 c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name")) 37 } 38 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 testRequires(c, DaemonIsLinux) 43 44 type entry struct { 45 Repo string 46 Alias string 47 } 48 49 entries := []entry{ 50 {"library/asdfasdf", "asdfasdf:foobar"}, 51 {"library/asdfasdf", "library/asdfasdf:foobar"}, 52 {"library/asdfasdf", "asdfasdf"}, 53 {"library/asdfasdf", "asdfasdf:latest"}, 54 {"library/asdfasdf", "library/asdfasdf"}, 55 {"library/asdfasdf", "library/asdfasdf:latest"}, 56 } 57 58 // The option field indicates "-a" or not. 59 type record struct { 60 e entry 61 option string 62 out string 63 err error 64 } 65 66 // Execute 'docker pull' in parallel, pass results (out, err) and 67 // necessary information ("-a" or not, and the image name) to channel. 68 var group sync.WaitGroup 69 recordChan := make(chan record, len(entries)*2) 70 for _, e := range entries { 71 group.Add(1) 72 go func(e entry) { 73 defer group.Done() 74 out, err := s.CmdWithError("pull", e.Alias) 75 recordChan <- record{e, "", out, err} 76 }(e) 77 if !strings.ContainsRune(e.Alias, ':') { 78 // pull -a on a nonexistent registry should fall back as well 79 group.Add(1) 80 go func(e entry) { 81 defer group.Done() 82 out, err := s.CmdWithError("pull", "-a", e.Alias) 83 recordChan <- record{e, "-a", out, err} 84 }(e) 85 } 86 } 87 88 // Wait for completion 89 group.Wait() 90 close(recordChan) 91 92 // Process the results (out, err). 93 for record := range recordChan { 94 if len(record.option) == 0 { 95 c.Assert(record.err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", record.out)) 96 // Hub returns 401 rather than 404 for nonexistent repos over 97 // the v2 protocol - but we should end up falling back to v1, 98 // which does return a 404. 99 c.Assert(record.out, checker.Contains, fmt.Sprintf("Error: image %s not found", record.e.Repo), check.Commentf("expected image not found error messages")) 100 } else { 101 // pull -a on a nonexistent registry should fall back as well 102 c.Assert(record.err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", record.out)) 103 c.Assert(record.out, checker.Contains, fmt.Sprintf("Error: image %s not found", record.e.Repo), check.Commentf("expected image not found error messages")) 104 c.Assert(record.out, checker.Not(checker.Contains), "unauthorized", check.Commentf(`message should not contain "unauthorized"`)) 105 } 106 } 107 108 } 109 110 // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies 111 // that pulling the same image with different combinations of implicit elements of the the image 112 // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to 113 // multiple images. 114 func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) { 115 testRequires(c, DaemonIsLinux) 116 117 // Pull hello-world from v2 118 pullFromV2 := func(ref string) (int, string) { 119 out := s.Cmd(c, "pull", "hello-world") 120 v1Retries := 0 121 for strings.Contains(out, "this image was pulled from a legacy registry") { 122 // Some network errors may cause fallbacks to the v1 123 // protocol, which would violate the test's assumption 124 // that it will get the same images. To make the test 125 // more robust against these network glitches, allow a 126 // few retries if we end up with a v1 pull. 127 128 if v1Retries > 2 { 129 c.Fatalf("too many v1 fallback incidents when pulling %s", ref) 130 } 131 132 s.Cmd(c, "rmi", ref) 133 out = s.Cmd(c, "pull", ref) 134 135 v1Retries++ 136 } 137 138 return v1Retries, out 139 } 140 141 pullFromV2("hello-world") 142 defer deleteImages("hello-world") 143 144 s.Cmd(c, "tag", "hello-world", "hello-world-backup") 145 146 for _, ref := range []string{ 147 "hello-world", 148 "hello-world:latest", 149 "library/hello-world", 150 "library/hello-world:latest", 151 "docker.io/library/hello-world", 152 "index.docker.io/library/hello-world", 153 } { 154 var out string 155 for { 156 var v1Retries int 157 v1Retries, out = pullFromV2(ref) 158 159 // Keep repeating the test case until we don't hit a v1 160 // fallback case. We won't get the right "Image is up 161 // to date" message if the local image was replaced 162 // with one pulled from v1. 163 if v1Retries == 0 { 164 break 165 } 166 s.Cmd(c, "rmi", ref) 167 s.Cmd(c, "tag", "hello-world-backup", "hello-world") 168 } 169 c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest") 170 } 171 172 s.Cmd(c, "rmi", "hello-world-backup") 173 174 // We should have a single entry in images. 175 img := strings.TrimSpace(s.Cmd(c, "images")) 176 splitImg := strings.Split(img, "\n") 177 c.Assert(splitImg, checker.HasLen, 2) 178 c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name")) 179 } 180 181 // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected. 182 func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) { 183 testRequires(c, DaemonIsLinux) 184 out, err := s.CmdWithError("pull", "scratch") 185 c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail")) 186 c.Assert(out, checker.Contains, "'scratch' is a reserved name") 187 c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch") 188 } 189 190 // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it 191 // results in more images than a naked pull. 192 func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) { 193 testRequires(c, DaemonIsLinux) 194 s.Cmd(c, "pull", "busybox") 195 outImageCmd := s.Cmd(c, "images", "busybox") 196 splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n") 197 c.Assert(splitOutImageCmd, checker.HasLen, 2) 198 199 s.Cmd(c, "pull", "--all-tags=true", "busybox") 200 outImageAllTagCmd := s.Cmd(c, "images", "busybox") 201 linesCount := strings.Count(outImageAllTagCmd, "\n") 202 c.Assert(linesCount, checker.GreaterThan, 2, check.Commentf("pulling all tags should provide more than two images, got %s", outImageAllTagCmd)) 203 204 // Verify that the line for 'busybox:latest' is left unchanged. 205 var latestLine string 206 for _, line := range strings.Split(outImageAllTagCmd, "\n") { 207 if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") { 208 latestLine = line 209 break 210 } 211 } 212 c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags")) 213 splitLatest := strings.Fields(latestLine) 214 splitCurrent := strings.Fields(splitOutImageCmd[1]) 215 216 // Clear relative creation times, since these can easily change between 217 // two invocations of "docker images". Without this, the test can fail 218 // like this: 219 // ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"} 220 // ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"} 221 splitLatest[3] = "" 222 splitLatest[4] = "" 223 splitLatest[5] = "" 224 splitCurrent[3] = "" 225 splitCurrent[4] = "" 226 splitCurrent[5] = "" 227 228 c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags")) 229 } 230 231 // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation 232 // gets cancelled. 233 // 234 // Ref: docker/docker#15589 235 func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) { 236 testRequires(c, DaemonIsLinux) 237 repoName := "hello-world:latest" 238 239 pullCmd := s.MakeCmd("pull", repoName) 240 stdout, err := pullCmd.StdoutPipe() 241 c.Assert(err, checker.IsNil) 242 err = pullCmd.Start() 243 c.Assert(err, checker.IsNil) 244 245 // Cancel as soon as we get some output. 246 buf := make([]byte, 10) 247 _, err = stdout.Read(buf) 248 c.Assert(err, checker.IsNil) 249 250 err = pullCmd.Process.Kill() 251 c.Assert(err, checker.IsNil) 252 253 time.Sleep(2 * time.Second) 254 _, err = s.CmdWithError("inspect", repoName) 255 c.Assert(err, checker.NotNil, check.Commentf("image was pulled after client disconnected")) 256 } 257 258 func (s *DockerRegistryAuthHtpasswdSuite) TestPullNoCredentialsNotFound(c *check.C) { 259 // we don't care about the actual image, we just want to see image not found 260 // because that means v2 call returned 401 and we fell back to v1 which usually 261 // gives a 404 (in this case the test registry doesn't handle v1 at all) 262 out, _, err := dockerCmdWithError("pull", privateRegistryURL+"/busybox") 263 c.Assert(err, check.NotNil, check.Commentf(out)) 264 c.Assert(out, checker.Contains, "Error: image busybox not found") 265 }