docker_cli_pull_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
  28. c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
  29. } else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
  30. c.Fatal("invalid output for `docker images` (expected image and tag name")
  31. }
  32. }
  33. // TestPullNonExistingImage pulls non-existing images from the central registry, with different
  34. // combinations of implicit tag and library prefix.
  35. func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
  36. testRequires(c, DaemonIsLinux)
  37. for _, e := range []struct {
  38. Repo string
  39. Alias string
  40. }{
  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. out, err := s.CmdWithError("pull", e.Alias)
  49. c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", out))
  50. c.Assert(out, checker.Contains, fmt.Sprintf("Error: image %s not found", e.Repo), check.Commentf("expected image not found error messages"))
  51. }
  52. }
  53. // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
  54. // that pulling the same image with different combinations of implicit elements of the the image
  55. // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
  56. // multiple images.
  57. func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
  58. testRequires(c, DaemonIsLinux)
  59. s.Cmd(c, "pull", "hello-world")
  60. defer deleteImages("hello-world")
  61. for _, i := range []string{
  62. "hello-world",
  63. "hello-world:latest",
  64. "library/hello-world",
  65. "library/hello-world:latest",
  66. "docker.io/library/hello-world",
  67. "index.docker.io/library/hello-world",
  68. } {
  69. out := s.Cmd(c, "pull", i)
  70. c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
  71. }
  72. // We should have a single entry in images.
  73. img := strings.TrimSpace(s.Cmd(c, "images"))
  74. if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
  75. c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
  76. } else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
  77. c.Fatal("invalid output for `docker images` (expected image and tag name")
  78. }
  79. }
  80. // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
  81. func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
  82. testRequires(c, DaemonIsLinux)
  83. out, err := s.CmdWithError("pull", "scratch")
  84. c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
  85. c.Assert(out, checker.Contains, "'scratch' is a reserved name")
  86. c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
  87. }
  88. // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
  89. // results in more images than a naked pull.
  90. func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
  91. testRequires(c, DaemonIsLinux)
  92. s.Cmd(c, "pull", "busybox")
  93. outImageCmd := s.Cmd(c, "images", "busybox")
  94. splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
  95. c.Assert(splitOutImageCmd, checker.HasLen, 2, check.Commentf("expected a single entry in images\n%v", outImageCmd))
  96. s.Cmd(c, "pull", "--all-tags=true", "busybox")
  97. outImageAllTagCmd := s.Cmd(c, "images", "busybox")
  98. if linesCount := strings.Count(outImageAllTagCmd, "\n"); linesCount <= 2 {
  99. c.Fatalf("pulling all tags should provide more images, got %d", linesCount-1)
  100. }
  101. // Verify that the line for 'busybox:latest' is left unchanged.
  102. var latestLine string
  103. for _, line := range strings.Split(outImageAllTagCmd, "\n") {
  104. if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") {
  105. latestLine = line
  106. break
  107. }
  108. }
  109. c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags"))
  110. splitLatest := strings.Fields(latestLine)
  111. splitCurrent := strings.Fields(splitOutImageCmd[1])
  112. // Clear relative creation times, since these can easily change between
  113. // two invocations of "docker images". Without this, the test can fail
  114. // like this:
  115. // ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
  116. // ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
  117. splitLatest[3] = ""
  118. splitLatest[4] = ""
  119. splitLatest[5] = ""
  120. splitCurrent[3] = ""
  121. splitCurrent[4] = ""
  122. splitCurrent[5] = ""
  123. c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
  124. }
  125. // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
  126. // still succesfully completes on the daemon side.
  127. //
  128. // Ref: docker/docker#15589
  129. func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
  130. testRequires(c, DaemonIsLinux)
  131. repoName := "hello-world:latest"
  132. pullCmd := s.MakeCmd("pull", repoName)
  133. stdout, err := pullCmd.StdoutPipe()
  134. c.Assert(err, checker.IsNil)
  135. err = pullCmd.Start()
  136. c.Assert(err, checker.IsNil)
  137. // Cancel as soon as we get some output.
  138. buf := make([]byte, 10)
  139. _, err = stdout.Read(buf)
  140. c.Assert(err, checker.IsNil)
  141. err = pullCmd.Process.Kill()
  142. c.Assert(err, checker.IsNil)
  143. maxAttempts := 20
  144. for i := 0; ; i++ {
  145. if _, err := s.CmdWithError("inspect", repoName); err == nil {
  146. break
  147. }
  148. if i >= maxAttempts {
  149. c.Fatal("timeout reached: image was not pulled after client disconnected")
  150. }
  151. time.Sleep(500 * time.Millisecond)
  152. }
  153. }