docker_cli_pull_test.go 10 KB

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