docker_cli_pull_test.go 5.3 KB

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