docker_cli_pull_test.go 10 KB

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