docker_cli_pull_test.go 7.9 KB

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