docker_cli_pull_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "regexp"
  4. "strings"
  5. "time"
  6. "github.com/docker/distribution/digest"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/go-check/check"
  9. )
  10. // TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
  11. // prints all expected output.
  12. func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *check.C) {
  13. testRequires(c, DaemonIsLinux)
  14. out := s.Cmd(c, "pull", "hello-world")
  15. defer deleteImages("hello-world")
  16. c.Assert(out, checker.Contains, "Using default tag: latest", check.Commentf("expected the 'latest' tag to be automatically assumed"))
  17. c.Assert(out, checker.Contains, "Pulling from library/hello-world", check.Commentf("expected the 'library/' prefix to be automatically assumed"))
  18. c.Assert(out, checker.Contains, "Downloaded newer image for hello-world:latest")
  19. matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1)
  20. c.Assert(len(matches), checker.Equals, 1, check.Commentf("expected exactly one image digest in the output"))
  21. c.Assert(len(matches[0]), checker.Equals, 2, check.Commentf("unexpected number of submatches for the digest"))
  22. _, err := digest.ParseDigest(matches[0][1])
  23. c.Check(err, checker.IsNil, check.Commentf("invalid digest %q in output", matches[0][1]))
  24. // We should have a single entry in images.
  25. img := strings.TrimSpace(s.Cmd(c, "images"))
  26. splitImg := strings.Split(img, "\n")
  27. c.Assert(splitImg, checker.HasLen, 2)
  28. c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
  29. }
  30. // TestPullNonExistingImage pulls non-existing images from the central registry, with different
  31. // combinations of implicit tag and library prefix.
  32. func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
  33. testRequires(c, DaemonIsLinux)
  34. for _, e := range []struct {
  35. Repo string
  36. Alias string
  37. }{
  38. {"library/asdfasdf", "asdfasdf:foobar"},
  39. {"library/asdfasdf", "library/asdfasdf:foobar"},
  40. {"library/asdfasdf", "asdfasdf"},
  41. {"library/asdfasdf", "asdfasdf:latest"},
  42. {"library/asdfasdf", "library/asdfasdf"},
  43. {"library/asdfasdf", "library/asdfasdf:latest"},
  44. } {
  45. out, err := s.CmdWithError("pull", e.Alias)
  46. c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", out))
  47. // Hub returns 401 rather than 404 for nonexistent library/ repos.
  48. c.Assert(out, checker.Contains, "unauthorized: access to the requested resource is not authorized", check.Commentf("expected unauthorized error message"))
  49. }
  50. }
  51. // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
  52. // that pulling the same image with different combinations of implicit elements of the the image
  53. // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
  54. // multiple images.
  55. func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
  56. testRequires(c, DaemonIsLinux)
  57. s.Cmd(c, "pull", "hello-world")
  58. defer deleteImages("hello-world")
  59. for _, i := range []string{
  60. "hello-world",
  61. "hello-world:latest",
  62. "library/hello-world",
  63. "library/hello-world:latest",
  64. "docker.io/library/hello-world",
  65. "index.docker.io/library/hello-world",
  66. } {
  67. out := s.Cmd(c, "pull", i)
  68. c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
  69. }
  70. // We should have a single entry in images.
  71. img := strings.TrimSpace(s.Cmd(c, "images"))
  72. splitImg := strings.Split(img, "\n")
  73. c.Assert(splitImg, checker.HasLen, 2)
  74. c.Assert(splitImg[1], checker.Matches, `hello-world\s+latest.*?`, check.Commentf("invalid output for `docker images` (expected image and tag name"))
  75. }
  76. // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
  77. func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
  78. testRequires(c, DaemonIsLinux)
  79. out, err := s.CmdWithError("pull", "scratch")
  80. c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
  81. c.Assert(out, checker.Contains, "'scratch' is a reserved name")
  82. c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
  83. }
  84. // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
  85. // results in more images than a naked pull.
  86. func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
  87. testRequires(c, DaemonIsLinux)
  88. s.Cmd(c, "pull", "busybox")
  89. outImageCmd := s.Cmd(c, "images", "busybox")
  90. splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
  91. c.Assert(splitOutImageCmd, checker.HasLen, 2)
  92. s.Cmd(c, "pull", "--all-tags=true", "busybox")
  93. outImageAllTagCmd := s.Cmd(c, "images", "busybox")
  94. linesCount := strings.Count(outImageAllTagCmd, "\n")
  95. c.Assert(linesCount, checker.GreaterThan, 2, check.Commentf("pulling all tags should provide more than two images, got %s", outImageAllTagCmd))
  96. // Verify that the line for 'busybox:latest' is left unchanged.
  97. var latestLine string
  98. for _, line := range strings.Split(outImageAllTagCmd, "\n") {
  99. if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") {
  100. latestLine = line
  101. break
  102. }
  103. }
  104. c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags"))
  105. splitLatest := strings.Fields(latestLine)
  106. splitCurrent := strings.Fields(splitOutImageCmd[1])
  107. // Clear relative creation times, since these can easily change between
  108. // two invocations of "docker images". Without this, the test can fail
  109. // like this:
  110. // ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
  111. // ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
  112. splitLatest[3] = ""
  113. splitLatest[4] = ""
  114. splitLatest[5] = ""
  115. splitCurrent[3] = ""
  116. splitCurrent[4] = ""
  117. splitCurrent[5] = ""
  118. c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
  119. }
  120. // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
  121. // gets cancelled.
  122. //
  123. // Ref: docker/docker#15589
  124. func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
  125. testRequires(c, DaemonIsLinux)
  126. repoName := "hello-world:latest"
  127. pullCmd := s.MakeCmd("pull", repoName)
  128. stdout, err := pullCmd.StdoutPipe()
  129. c.Assert(err, checker.IsNil)
  130. err = pullCmd.Start()
  131. c.Assert(err, checker.IsNil)
  132. // Cancel as soon as we get some output.
  133. buf := make([]byte, 10)
  134. _, err = stdout.Read(buf)
  135. c.Assert(err, checker.IsNil)
  136. err = pullCmd.Process.Kill()
  137. c.Assert(err, checker.IsNil)
  138. time.Sleep(2 * time.Second)
  139. _, err = s.CmdWithError("inspect", repoName)
  140. c.Assert(err, checker.NotNil, check.Commentf("image was pulled after client disconnected"))
  141. }