docker_cli_pull_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. // See issue docker/docker#8141
  9. func TestPullImageWithAliases(t *testing.T) {
  10. defer setupRegistry(t)()
  11. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  12. defer deleteImages(repoName)
  13. repos := []string{}
  14. for _, tag := range []string{"recent", "fresh"} {
  15. repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
  16. }
  17. // Tag and push the same image multiple times.
  18. for _, repo := range repos {
  19. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", "busybox", repo)); err != nil {
  20. t.Fatalf("Failed to tag image %v: error %v, output %q", repos, err, out)
  21. }
  22. defer deleteImages(repo)
  23. if out, err := exec.Command(dockerBinary, "push", repo).CombinedOutput(); err != nil {
  24. t.Fatalf("Failed to push image %v: error %v, output %q", repo, err, string(out))
  25. }
  26. }
  27. // Clear local images store.
  28. args := append([]string{"rmi"}, repos...)
  29. if out, err := exec.Command(dockerBinary, args...).CombinedOutput(); err != nil {
  30. t.Fatalf("Failed to clean images: error %v, output %q", err, string(out))
  31. }
  32. // Pull a single tag and verify it doesn't bring down all aliases.
  33. pullCmd := exec.Command(dockerBinary, "pull", repos[0])
  34. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  35. t.Fatalf("Failed to pull %v: error %v, output %q", repoName, err, out)
  36. }
  37. if err := exec.Command(dockerBinary, "inspect", repos[0]).Run(); err != nil {
  38. t.Fatalf("Image %v was not pulled down", repos[0])
  39. }
  40. for _, repo := range repos[1:] {
  41. if err := exec.Command(dockerBinary, "inspect", repo).Run(); err == nil {
  42. t.Fatalf("Image %v shouldn't have been pulled down", repo)
  43. }
  44. }
  45. logDone("pull - image with aliases")
  46. }
  47. // pulling busybox should show verified message
  48. func TestPullVerified(t *testing.T) {
  49. defer setupRegistry(t)()
  50. repo := fmt.Sprintf("%v/dockercli/busybox:verified", privateRegistryURL)
  51. defer deleteImages(repo)
  52. // tag the image
  53. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", "busybox", repo)); err != nil {
  54. t.Fatalf("Failed to tag image verifiedTest: error %v, output %q", err, out)
  55. }
  56. // push it
  57. if out, err := exec.Command(dockerBinary, "push", repo).CombinedOutput(); err != nil {
  58. t.Fatalf("Failed to push image %v: error %v, output %q", repo, err, string(out))
  59. }
  60. // remove it locally
  61. if out, err := exec.Command(dockerBinary, "rmi", repo).CombinedOutput(); err != nil {
  62. t.Fatalf("Failed to clean images: error %v, output %q", err, string(out))
  63. }
  64. // pull it
  65. expected := "The image you are pulling has been verified"
  66. pullCmd := exec.Command(dockerBinary, "pull", repo)
  67. if out, _, err := runCommandWithOutput(pullCmd); err != nil || !strings.Contains(out, expected) {
  68. t.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
  69. }
  70. // pull it again
  71. pullCmd = exec.Command(dockerBinary, "pull", repo)
  72. if out, _, err := runCommandWithOutput(pullCmd); err != nil || !strings.Contains(out, expected) {
  73. t.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
  74. }
  75. logDone("pull - pull verified")
  76. }
  77. // pulling an image from the central registry should work
  78. func TestPullImageFromCentralRegistry(t *testing.T) {
  79. pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
  80. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  81. t.Fatalf("pulling the hello-world image from the registry has failed: %s, %v", out, err)
  82. }
  83. logDone("pull - pull hello-world")
  84. }
  85. // pulling a non-existing image from the central registry should return a non-zero exit code
  86. func TestPullNonExistingImage(t *testing.T) {
  87. pullCmd := exec.Command(dockerBinary, "pull", "fooblahblah1234")
  88. if out, _, err := runCommandWithOutput(pullCmd); err == nil {
  89. t.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
  90. }
  91. logDone("pull - pull fooblahblah1234 (non-existing image)")
  92. }
  93. // pulling an image from the central registry using official names should work
  94. // ensure all pulls result in the same image
  95. func TestPullImageOfficialNames(t *testing.T) {
  96. names := []string{
  97. "docker.io/hello-world",
  98. "index.docker.io/hello-world",
  99. "library/hello-world",
  100. "docker.io/library/hello-world",
  101. "index.docker.io/library/hello-world",
  102. }
  103. for _, name := range names {
  104. pullCmd := exec.Command(dockerBinary, "pull", name)
  105. out, exitCode, err := runCommandWithOutput(pullCmd)
  106. if err != nil || exitCode != 0 {
  107. t.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
  108. continue
  109. }
  110. // ensure we don't have multiple image names.
  111. imagesCmd := exec.Command(dockerBinary, "images")
  112. out, _, err = runCommandWithOutput(imagesCmd)
  113. if err != nil {
  114. t.Errorf("listing images failed with errors: %v", err)
  115. } else if strings.Contains(out, name) {
  116. t.Errorf("images should not have listed '%s'", name)
  117. }
  118. }
  119. logDone("pull - pull official names")
  120. }