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