docker_cli_pull_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", "busybox", repo)); err != nil {
  18. c.Fatalf("Failed to tag image %v: error %v, output %q", repos, err, out)
  19. }
  20. if out, err := exec.Command(dockerBinary, "push", repo).CombinedOutput(); err != nil {
  21. c.Fatalf("Failed to push image %v: error %v, output %q", repo, err, string(out))
  22. }
  23. }
  24. // Clear local images store.
  25. args := append([]string{"rmi"}, repos...)
  26. if out, err := exec.Command(dockerBinary, args...).CombinedOutput(); err != nil {
  27. c.Fatalf("Failed to clean images: error %v, output %q", err, string(out))
  28. }
  29. // Pull a single tag and verify it doesn't bring down all aliases.
  30. pullCmd := exec.Command(dockerBinary, "pull", repos[0])
  31. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  32. c.Fatalf("Failed to pull %v: error %v, output %q", repoName, err, out)
  33. }
  34. if err := exec.Command(dockerBinary, "inspect", repos[0]).Run(); err != nil {
  35. c.Fatalf("Image %v was not pulled down", repos[0])
  36. }
  37. for _, repo := range repos[1:] {
  38. if err := exec.Command(dockerBinary, "inspect", repo).Run(); err == nil {
  39. c.Fatalf("Image %v shouldn't have been pulled down", repo)
  40. }
  41. }
  42. }
  43. // pulling library/hello-world should show verified message
  44. func (s *DockerSuite) TestPullVerified(c *check.C) {
  45. c.Skip("Skipping hub dependent test")
  46. // Image must be pulled from central repository to get verified message
  47. // unless keychain is manually updated to contain the daemon's sign key.
  48. verifiedName := "hello-world"
  49. // pull it
  50. expected := "The image you are pulling has been verified"
  51. pullCmd := exec.Command(dockerBinary, "pull", verifiedName)
  52. if out, exitCode, err := runCommandWithOutput(pullCmd); err != nil || !strings.Contains(out, expected) {
  53. if err != nil || exitCode != 0 {
  54. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  55. }
  56. c.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
  57. }
  58. // pull it again
  59. pullCmd = exec.Command(dockerBinary, "pull", verifiedName)
  60. if out, exitCode, err := runCommandWithOutput(pullCmd); err != nil || strings.Contains(out, expected) {
  61. if err != nil || exitCode != 0 {
  62. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  63. }
  64. c.Fatalf("pulling a verified image failed. unexpected verify message\ngot: %s, %v", out, err)
  65. }
  66. }
  67. // pulling an image from the central registry should work
  68. func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
  69. testRequires(c, Network)
  70. pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
  71. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  72. c.Fatalf("pulling the hello-world image from the registry has failed: %s, %v", out, err)
  73. }
  74. }
  75. // pulling a non-existing image from the central registry should return a non-zero exit code
  76. func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
  77. testRequires(c, Network)
  78. name := "sadfsadfasdf"
  79. pullCmd := exec.Command(dockerBinary, "pull", name)
  80. out, _, err := runCommandWithOutput(pullCmd)
  81. if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
  82. c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
  83. }
  84. }
  85. // pulling an image from the central registry using official names should work
  86. // ensure all pulls result in the same image
  87. func (s *DockerSuite) TestPullImageOfficialNames(c *check.C) {
  88. testRequires(c, Network)
  89. names := []string{
  90. "docker.io/hello-world",
  91. "index.docker.io/hello-world",
  92. "library/hello-world",
  93. "docker.io/library/hello-world",
  94. "index.docker.io/library/hello-world",
  95. }
  96. for _, name := range names {
  97. pullCmd := exec.Command(dockerBinary, "pull", name)
  98. out, exitCode, err := runCommandWithOutput(pullCmd)
  99. if err != nil || exitCode != 0 {
  100. c.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
  101. continue
  102. }
  103. // ensure we don't have multiple image names.
  104. imagesCmd := exec.Command(dockerBinary, "images")
  105. out, _, err = runCommandWithOutput(imagesCmd)
  106. if err != nil {
  107. c.Errorf("listing images failed with errors: %v", err)
  108. } else if strings.Contains(out, name) {
  109. c.Errorf("images should not have listed '%s'", name)
  110. }
  111. }
  112. }
  113. func (s *DockerSuite) TestPullScratchNotAllowed(c *check.C) {
  114. testRequires(c, Network)
  115. pullCmd := exec.Command(dockerBinary, "pull", "scratch")
  116. out, exitCode, err := runCommandWithOutput(pullCmd)
  117. if err == nil {
  118. c.Fatal("expected pull of scratch to fail, but it didn't")
  119. }
  120. if exitCode != 1 {
  121. c.Fatalf("pulling scratch expected exit code 1, got %d", exitCode)
  122. }
  123. if strings.Contains(out, "Pulling repository scratch") {
  124. c.Fatalf("pulling scratch should not have begun: %s", out)
  125. }
  126. if !strings.Contains(out, "'scratch' is a reserved name") {
  127. c.Fatalf("unexpected output pulling scratch: %s", out)
  128. }
  129. }
  130. // pulling an image with --all-tags=true
  131. func (s *DockerSuite) TestPullImageWithAllTagFromCentralRegistry(c *check.C) {
  132. //testRequires(c, Network)
  133. pullCmd := exec.Command(dockerBinary, "pull", "busybox")
  134. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  135. c.Fatalf("pulling the busybox image from the registry has failed: %s, %v", out, err)
  136. }
  137. ImageCmd := exec.Command(dockerBinary, "images", "busybox")
  138. outImageCmd, _, err := runCommandWithOutput(ImageCmd)
  139. c.Assert(err, check.IsNil)
  140. pullAllTagCmd := exec.Command(dockerBinary, "pull", "--all-tags=true", "busybox")
  141. if out, _, err := runCommandWithOutput(pullAllTagCmd); err != nil {
  142. c.Fatalf("pulling the busybox image with all tags from the registry has failed: %s, %v", out, err)
  143. }
  144. ImageCmd1 := exec.Command(dockerBinary, "images", "busybox")
  145. outImageAllTagCmd, _, err := runCommandWithOutput(ImageCmd1)
  146. c.Assert(err, check.IsNil)
  147. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  148. c.Fatalf("Pulling with all tags should get more images")
  149. }
  150. pullAllTagCmd = exec.Command(dockerBinary, "pull", "-a", "busybox")
  151. if out, _, err := runCommandWithOutput(pullAllTagCmd); err != nil {
  152. c.Fatalf("pulling the busybox image with all tags from the registry has failed: %s, %v", out, err)
  153. }
  154. ImageCmd2 := exec.Command(dockerBinary, "images", "busybox")
  155. outImageAllTagCmd, _, err = runCommandWithOutput(ImageCmd2)
  156. c.Assert(err, check.IsNil)
  157. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  158. c.Fatalf("Pulling with all tags should get more images")
  159. }
  160. }