docker_cli_pull_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "github.com/go-check/check"
  7. )
  8. // See issue docker/docker#8141
  9. func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
  10. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  11. repos := []string{}
  12. for _, tag := range []string{"recent", "fresh"} {
  13. repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
  14. }
  15. // Tag and push the same image multiple times.
  16. for _, repo := range repos {
  17. dockerCmd(c, "tag", "busybox", repo)
  18. dockerCmd(c, "push", repo)
  19. }
  20. // Clear local images store.
  21. args := append([]string{"rmi"}, repos...)
  22. dockerCmd(c, args...)
  23. // Pull a single tag and verify it doesn't bring down all aliases.
  24. dockerCmd(c, "pull", repos[0])
  25. dockerCmd(c, "inspect", repos[0])
  26. for _, repo := range repos[1:] {
  27. if _, _, err := dockerCmdWithError(c, "inspect", repo); err == nil {
  28. c.Fatalf("Image %v shouldn't have been pulled down", repo)
  29. }
  30. }
  31. }
  32. // pulling library/hello-world should show verified message
  33. func (s *DockerSuite) TestPullVerified(c *check.C) {
  34. c.Skip("Skipping hub dependent test")
  35. // Image must be pulled from central repository to get verified message
  36. // unless keychain is manually updated to contain the daemon's sign key.
  37. verifiedName := "hello-world"
  38. // pull it
  39. expected := "The image you are pulling has been verified"
  40. if out, exitCode, err := dockerCmdWithError(c, "pull", verifiedName); err != nil || !strings.Contains(out, expected) {
  41. if err != nil || exitCode != 0 {
  42. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  43. }
  44. c.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
  45. }
  46. // pull it again
  47. if out, exitCode, err := dockerCmdWithError(c, "pull", verifiedName); err != nil || strings.Contains(out, expected) {
  48. if err != nil || exitCode != 0 {
  49. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  50. }
  51. c.Fatalf("pulling a verified image failed. unexpected verify message\ngot: %s, %v", out, err)
  52. }
  53. }
  54. // pulling an image from the central registry should work
  55. func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
  56. testRequires(c, Network)
  57. dockerCmd(c, "pull", "hello-world")
  58. }
  59. // pulling a non-existing image from the central registry should return a non-zero exit code
  60. func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
  61. testRequires(c, Network)
  62. name := "sadfsadfasdf"
  63. out, _, err := dockerCmdWithError(c, "pull", name)
  64. if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
  65. c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
  66. }
  67. }
  68. // pulling an image from the central registry using official names should work
  69. // ensure all pulls result in the same image
  70. func (s *DockerSuite) TestPullImageOfficialNames(c *check.C) {
  71. testRequires(c, Network)
  72. names := []string{
  73. "library/hello-world",
  74. "docker.io/library/hello-world",
  75. "index.docker.io/library/hello-world",
  76. }
  77. for _, name := range names {
  78. out, exitCode, err := dockerCmdWithError(c, "pull", name)
  79. if err != nil || exitCode != 0 {
  80. c.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
  81. continue
  82. }
  83. // ensure we don't have multiple image names.
  84. out, _ = dockerCmd(c, "images")
  85. if strings.Contains(out, name) {
  86. c.Errorf("images should not have listed '%s'", name)
  87. }
  88. }
  89. }
  90. func (s *DockerSuite) TestPullScratchNotAllowed(c *check.C) {
  91. testRequires(c, Network)
  92. out, exitCode, err := dockerCmdWithError(c, "pull", "scratch")
  93. if err == nil {
  94. c.Fatal("expected pull of scratch to fail, but it didn't")
  95. }
  96. if exitCode != 1 {
  97. c.Fatalf("pulling scratch expected exit code 1, got %d", exitCode)
  98. }
  99. if strings.Contains(out, "Pulling repository scratch") {
  100. c.Fatalf("pulling scratch should not have begun: %s", out)
  101. }
  102. if !strings.Contains(out, "'scratch' is a reserved name") {
  103. c.Fatalf("unexpected output pulling scratch: %s", out)
  104. }
  105. }
  106. // pulling an image with --all-tags=true
  107. func (s *DockerSuite) TestPullImageWithAllTagFromCentralRegistry(c *check.C) {
  108. testRequires(c, Network)
  109. dockerCmd(c, "pull", "busybox")
  110. outImageCmd, _ := dockerCmd(c, "images", "busybox")
  111. dockerCmd(c, "pull", "--all-tags=true", "busybox")
  112. outImageAllTagCmd, _ := dockerCmd(c, "images", "busybox")
  113. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  114. c.Fatalf("Pulling with all tags should get more images")
  115. }
  116. // FIXME has probably no effect (tags already pushed)
  117. dockerCmd(c, "pull", "-a", "busybox")
  118. outImageAllTagCmd, _ = dockerCmd(c, "images", "busybox")
  119. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  120. c.Fatalf("Pulling with all tags should get more images")
  121. }
  122. }
  123. func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
  124. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  125. // tag the image and upload it to the private registry
  126. dockerCmd(c, "tag", "busybox", repoName)
  127. pushCmd := exec.Command(dockerBinary, "push", repoName)
  128. s.trustedCmd(pushCmd)
  129. out, _, err := runCommandWithOutput(pushCmd)
  130. if err != nil {
  131. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  132. }
  133. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  134. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  135. }
  136. dockerCmd(c, "rmi", repoName)
  137. // Try pull
  138. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  139. s.trustedCmd(pullCmd)
  140. out, _, err = runCommandWithOutput(pullCmd)
  141. if err != nil {
  142. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  143. }
  144. if !strings.Contains(string(out), "Tagging") {
  145. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  146. }
  147. }