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