docker_cli_pull_test.go 9.9 KB

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