docker_cli_pull_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if out, err := exec.Command(dockerBinary, "push", repo).CombinedOutput(); err != nil {
  23. t.Fatalf("Failed to push image %v: error %v, output %q", err, string(out))
  24. }
  25. }
  26. // Clear local images store.
  27. args := append([]string{"rmi"}, repos...)
  28. if out, err := exec.Command(dockerBinary, args...).CombinedOutput(); err != nil {
  29. t.Fatalf("Failed to clean images: error %v, output %q", err, string(out))
  30. }
  31. // Pull a single tag and verify it doesn't bring down all aliases.
  32. pullCmd := exec.Command(dockerBinary, "pull", repos[0])
  33. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  34. t.Fatalf("Failed to pull %v: error %v, output %q", repoName, err, out)
  35. }
  36. if err := exec.Command(dockerBinary, "inspect", repos[0]).Run(); err != nil {
  37. t.Fatalf("Image %v was not pulled down", repos[0])
  38. }
  39. for _, repo := range repos[1:] {
  40. if err := exec.Command(dockerBinary, "inspect", repo).Run(); err == nil {
  41. t.Fatalf("Image %v shouldn't have been pulled down", repo)
  42. }
  43. }
  44. logDone("pull - image with aliases")
  45. }
  46. // pulling an image from the central registry should work
  47. func TestPullImageFromCentralRegistry(t *testing.T) {
  48. pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
  49. if out, _, err := runCommandWithOutput(pullCmd); err != nil {
  50. t.Fatalf("pulling the hello-world image from the registry has failed: %s, %v", out, err)
  51. }
  52. logDone("pull - pull hello-world")
  53. }
  54. // pulling a non-existing image from the central registry should return a non-zero exit code
  55. func TestPullNonExistingImage(t *testing.T) {
  56. pullCmd := exec.Command(dockerBinary, "pull", "fooblahblah1234")
  57. if out, _, err := runCommandWithOutput(pullCmd); err == nil {
  58. t.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
  59. }
  60. logDone("pull - pull fooblahblah1234 (non-existing image)")
  61. }
  62. // pulling an image from the central registry using official names should work
  63. // ensure all pulls result in the same image
  64. func TestPullImageOfficialNames(t *testing.T) {
  65. names := []string{
  66. "docker.io/hello-world",
  67. "index.docker.io/hello-world",
  68. "library/hello-world",
  69. "docker.io/library/hello-world",
  70. "index.docker.io/library/hello-world",
  71. }
  72. for _, name := range names {
  73. pullCmd := exec.Command(dockerBinary, "pull", name)
  74. out, exitCode, err := runCommandWithOutput(pullCmd)
  75. if err != nil || exitCode != 0 {
  76. t.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
  77. continue
  78. }
  79. // ensure we don't have multiple image names.
  80. imagesCmd := exec.Command(dockerBinary, "images")
  81. out, _, err = runCommandWithOutput(imagesCmd)
  82. if err != nil {
  83. t.Errorf("listing images failed with errors: %v", err)
  84. } else if strings.Contains(out, name) {
  85. t.Errorf("images should not have listed '%s'", name)
  86. }
  87. }
  88. logDone("pull - pull official names")
  89. }