docker_cli_pull_test.go 10 KB

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