docker_cli_push_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  11. )
  12. // pulling an image from the central registry should work
  13. func TestPushBusyboxImage(t *testing.T) {
  14. defer setupRegistry(t)()
  15. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  16. // tag the image to upload it tot he private registry
  17. tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
  18. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  19. t.Fatalf("image tagging failed: %s, %v", out, err)
  20. }
  21. defer deleteImages(repoName)
  22. pushCmd := exec.Command(dockerBinary, "push", repoName)
  23. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  24. t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  25. }
  26. logDone("push - busybox to private registry")
  27. }
  28. // pushing an image without a prefix should throw an error
  29. func TestPushUnprefixedRepo(t *testing.T) {
  30. pushCmd := exec.Command(dockerBinary, "push", "busybox")
  31. if out, _, err := runCommandWithOutput(pushCmd); err == nil {
  32. t.Fatalf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out)
  33. }
  34. logDone("push - unprefixed busybox repo must fail")
  35. }
  36. func TestPushUntagged(t *testing.T) {
  37. defer setupRegistry(t)()
  38. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  39. expected := "does not exist"
  40. pushCmd := exec.Command(dockerBinary, "push", repoName)
  41. if out, _, err := runCommandWithOutput(pushCmd); err == nil {
  42. t.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
  43. } else if !strings.Contains(out, expected) {
  44. t.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
  45. }
  46. logDone("push - untagged image")
  47. }
  48. func TestPushInterrupt(t *testing.T) {
  49. defer setupRegistry(t)()
  50. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  51. // tag the image to upload it tot he private registry
  52. tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
  53. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  54. t.Fatalf("image tagging failed: %s, %v", out, err)
  55. }
  56. defer deleteImages(repoName)
  57. pushCmd := exec.Command(dockerBinary, "push", repoName)
  58. if err := pushCmd.Start(); err != nil {
  59. t.Fatalf("Failed to start pushing to private registry: %v", err)
  60. }
  61. // Interrupt push (yes, we have no idea at what point it will get killed).
  62. time.Sleep(200 * time.Millisecond)
  63. if err := pushCmd.Process.Kill(); err != nil {
  64. t.Fatalf("Failed to kill push process: %v", err)
  65. }
  66. // Try agin
  67. pushCmd = exec.Command(dockerBinary, "push", repoName)
  68. if err := pushCmd.Start(); err != nil {
  69. t.Fatalf("Failed to start pushing to private registry: %v", err)
  70. }
  71. logDone("push - interrupted")
  72. }
  73. func TestPushEmptyLayer(t *testing.T) {
  74. defer setupRegistry(t)()
  75. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  76. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  77. if err != nil {
  78. t.Fatalf("Unable to create test file: %v", err)
  79. }
  80. tw := tar.NewWriter(emptyTarball)
  81. err = tw.Close()
  82. if err != nil {
  83. t.Fatalf("Error creating empty tarball: %v", err)
  84. }
  85. freader, err := os.Open(emptyTarball.Name())
  86. if err != nil {
  87. t.Fatalf("Could not open test tarball: %v", err)
  88. }
  89. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  90. importCmd.Stdin = freader
  91. out, _, err := runCommandWithOutput(importCmd)
  92. if err != nil {
  93. t.Errorf("import failed with errors: %v, output: %q", err, out)
  94. }
  95. // Now verify we can push it
  96. pushCmd := exec.Command(dockerBinary, "push", repoName)
  97. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  98. t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  99. }
  100. logDone("push - empty layer config to private registry")
  101. }