docker_cli_pull_test.go 10 KB

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