docker_cli_pull_test.go 8.5 KB

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