docker_cli_pull_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "time"
  7. "github.com/docker/distribution/digest"
  8. "github.com/docker/docker/integration-cli/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. 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. if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
  27. c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
  28. } else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
  29. c.Fatal("invalid output for `docker images` (expected image and tag name")
  30. }
  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. for _, e := range []struct {
  36. Image string
  37. Alias string
  38. }{
  39. {"library/asdfasdf:foobar", "asdfasdf:foobar"},
  40. {"library/asdfasdf:foobar", "library/asdfasdf:foobar"},
  41. {"library/asdfasdf:latest", "asdfasdf"},
  42. {"library/asdfasdf:latest", "asdfasdf:latest"},
  43. {"library/asdfasdf:latest", "library/asdfasdf"},
  44. {"library/asdfasdf:latest", "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. c.Assert(out, checker.Contains, fmt.Sprintf("Error: image %s not found", e.Image), check.Commentf("expected image not found error messages"))
  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. s.Cmd(c, "pull", "hello-world")
  57. defer deleteImages("hello-world")
  58. for _, i := range []string{
  59. "hello-world",
  60. "hello-world:latest",
  61. "library/hello-world",
  62. "library/hello-world:latest",
  63. "docker.io/library/hello-world",
  64. "index.docker.io/library/hello-world",
  65. } {
  66. out := s.Cmd(c, "pull", i)
  67. c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
  68. }
  69. // We should have a single entry in images.
  70. img := strings.TrimSpace(s.Cmd(c, "images"))
  71. if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
  72. c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
  73. } else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
  74. c.Fatal("invalid output for `docker images` (expected image and tag name")
  75. }
  76. }
  77. // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
  78. func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
  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. s.Cmd(c, "pull", "busybox")
  88. outImageCmd := s.Cmd(c, "images", "busybox")
  89. splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
  90. c.Assert(splitOutImageCmd, checker.HasLen, 2, check.Commentf("expected a single entry in images\n%v", outImageCmd))
  91. s.Cmd(c, "pull", "--all-tags=true", "busybox")
  92. outImageAllTagCmd := s.Cmd(c, "images", "busybox")
  93. if linesCount := strings.Count(outImageAllTagCmd, "\n"); linesCount <= 2 {
  94. c.Fatalf("pulling all tags should provide more images, got %d", linesCount-1)
  95. }
  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. c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
  108. }
  109. // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
  110. // still succesfully completes on the daemon side.
  111. //
  112. // Ref: docker/docker#15589
  113. func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
  114. repoName := "hello-world:latest"
  115. pullCmd := s.MakeCmd("pull", repoName)
  116. stdout, err := pullCmd.StdoutPipe()
  117. c.Assert(err, checker.IsNil)
  118. err = pullCmd.Start()
  119. c.Assert(err, checker.IsNil)
  120. // Cancel as soon as we get some output.
  121. buf := make([]byte, 10)
  122. _, err = stdout.Read(buf)
  123. c.Assert(err, checker.IsNil)
  124. err = pullCmd.Process.Kill()
  125. c.Assert(err, checker.IsNil)
  126. maxAttempts := 20
  127. for i := 0; ; i++ {
  128. if _, err := s.CmdWithError("inspect", repoName); err == nil {
  129. break
  130. }
  131. if i >= maxAttempts {
  132. c.Fatal("timeout reached: image was not pulled after client disconnected")
  133. }
  134. time.Sleep(500 * time.Millisecond)
  135. }
  136. }