docker_cli_push_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 not pass")
  35. }
  36. func TestPushUntagged(t *testing.T) {
  37. defer setupRegistry(t)()
  38. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  39. expected := "No tags to push"
  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 TestPushBadTag(t *testing.T) {
  49. defer setupRegistry(t)()
  50. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  51. expected := "does not exist"
  52. pushCmd := exec.Command(dockerBinary, "push", repoName)
  53. if out, _, err := runCommandWithOutput(pushCmd); err == nil {
  54. t.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
  55. } else if !strings.Contains(out, expected) {
  56. t.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
  57. }
  58. logDone("push - image with bad tag")
  59. }
  60. func TestPushMultipleTags(t *testing.T) {
  61. defer setupRegistry(t)()
  62. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  63. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  64. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  65. // tag the image to upload it tot he private registry
  66. tagCmd1 := exec.Command(dockerBinary, "tag", "busybox", repoTag1)
  67. if out, _, err := runCommandWithOutput(tagCmd1); err != nil {
  68. t.Fatalf("image tagging failed: %s, %v", out, err)
  69. }
  70. defer deleteImages(repoTag1)
  71. tagCmd2 := exec.Command(dockerBinary, "tag", "busybox", repoTag2)
  72. if out, _, err := runCommandWithOutput(tagCmd2); err != nil {
  73. t.Fatalf("image tagging failed: %s, %v", out, err)
  74. }
  75. defer deleteImages(repoTag2)
  76. pushCmd := exec.Command(dockerBinary, "push", repoName)
  77. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  78. t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  79. }
  80. logDone("push - multiple tags to private registry")
  81. }
  82. func TestPushInterrupt(t *testing.T) {
  83. defer setupRegistry(t)()
  84. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  85. // tag the image to upload it tot he private registry
  86. tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
  87. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  88. t.Fatalf("image tagging failed: %s, %v", out, err)
  89. }
  90. defer deleteImages(repoName)
  91. pushCmd := exec.Command(dockerBinary, "push", repoName)
  92. if err := pushCmd.Start(); err != nil {
  93. t.Fatalf("Failed to start pushing to private registry: %v", err)
  94. }
  95. // Interrupt push (yes, we have no idea at what point it will get killed).
  96. time.Sleep(200 * time.Millisecond)
  97. if err := pushCmd.Process.Kill(); err != nil {
  98. t.Fatalf("Failed to kill push process: %v", err)
  99. }
  100. // Try agin
  101. pushCmd = exec.Command(dockerBinary, "push", repoName)
  102. if err := pushCmd.Start(); err != nil {
  103. t.Fatalf("Failed to start pushing to private registry: %v", err)
  104. }
  105. logDone("push - interrupted")
  106. }
  107. func TestPushEmptyLayer(t *testing.T) {
  108. defer setupRegistry(t)()
  109. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  110. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  111. if err != nil {
  112. t.Fatalf("Unable to create test file: %v", err)
  113. }
  114. tw := tar.NewWriter(emptyTarball)
  115. err = tw.Close()
  116. if err != nil {
  117. t.Fatalf("Error creating empty tarball: %v", err)
  118. }
  119. freader, err := os.Open(emptyTarball.Name())
  120. if err != nil {
  121. t.Fatalf("Could not open test tarball: %v", err)
  122. }
  123. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  124. importCmd.Stdin = freader
  125. out, _, err := runCommandWithOutput(importCmd)
  126. if err != nil {
  127. t.Errorf("import failed with errors: %v, output: %q", err, out)
  128. }
  129. // Now verify we can push it
  130. pushCmd := exec.Command(dockerBinary, "push", repoName)
  131. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  132. t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  133. }
  134. logDone("push - empty layer config to private registry")
  135. }