docker_cli_push_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "time"
  9. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  10. "github.com/go-check/check"
  11. )
  12. // pulling an image from the central registry should work
  13. func (s *DockerSuite) TestPushBusyboxImage(c *check.C) {
  14. defer setupRegistry(c)()
  15. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  16. // tag the image to upload it to the private registry
  17. tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
  18. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  19. c.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. c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  25. }
  26. }
  27. // pushing an image without a prefix should throw an error
  28. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  29. pushCmd := exec.Command(dockerBinary, "push", "busybox")
  30. if out, _, err := runCommandWithOutput(pushCmd); err == nil {
  31. c.Fatalf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out)
  32. }
  33. }
  34. func (s *DockerSuite) TestPushUntagged(c *check.C) {
  35. defer setupRegistry(c)()
  36. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  37. expected := "Repository does not exist"
  38. pushCmd := exec.Command(dockerBinary, "push", repoName)
  39. if out, _, err := runCommandWithOutput(pushCmd); err == nil {
  40. c.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
  41. } else if !strings.Contains(out, expected) {
  42. c.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
  43. }
  44. }
  45. func (s *DockerSuite) TestPushBadTag(c *check.C) {
  46. defer setupRegistry(c)()
  47. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  48. expected := "does not exist"
  49. pushCmd := exec.Command(dockerBinary, "push", repoName)
  50. if out, _, err := runCommandWithOutput(pushCmd); err == nil {
  51. c.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
  52. } else if !strings.Contains(out, expected) {
  53. c.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
  54. }
  55. }
  56. func (s *DockerSuite) TestPushMultipleTags(c *check.C) {
  57. defer setupRegistry(c)()
  58. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  59. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  60. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  61. // tag the image to upload it tot he private registry
  62. tagCmd1 := exec.Command(dockerBinary, "tag", "busybox", repoTag1)
  63. if out, _, err := runCommandWithOutput(tagCmd1); err != nil {
  64. c.Fatalf("image tagging failed: %s, %v", out, err)
  65. }
  66. defer deleteImages(repoTag1)
  67. tagCmd2 := exec.Command(dockerBinary, "tag", "busybox", repoTag2)
  68. if out, _, err := runCommandWithOutput(tagCmd2); err != nil {
  69. c.Fatalf("image tagging failed: %s, %v", out, err)
  70. }
  71. defer deleteImages(repoTag2)
  72. pushCmd := exec.Command(dockerBinary, "push", repoName)
  73. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  74. c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  75. }
  76. }
  77. func (s *DockerSuite) TestPushInterrupt(c *check.C) {
  78. defer setupRegistry(c)()
  79. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  80. // tag the image to upload it tot he private registry
  81. tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
  82. if out, _, err := runCommandWithOutput(tagCmd); err != nil {
  83. c.Fatalf("image tagging failed: %s, %v", out, err)
  84. }
  85. defer deleteImages(repoName)
  86. pushCmd := exec.Command(dockerBinary, "push", repoName)
  87. if err := pushCmd.Start(); err != nil {
  88. c.Fatalf("Failed to start pushing to private registry: %v", err)
  89. }
  90. // Interrupt push (yes, we have no idea at what point it will get killed).
  91. time.Sleep(200 * time.Millisecond)
  92. if err := pushCmd.Process.Kill(); err != nil {
  93. c.Fatalf("Failed to kill push process: %v", err)
  94. }
  95. // Try agin
  96. pushCmd = exec.Command(dockerBinary, "push", repoName)
  97. if out, err := pushCmd.CombinedOutput(); err == nil {
  98. str := string(out)
  99. if !strings.Contains(str, "already in progress") {
  100. c.Fatalf("Push should be continued on daemon side, but seems ok: %v, %s", err, out)
  101. }
  102. }
  103. }
  104. func (s *DockerSuite) TestPushEmptyLayer(c *check.C) {
  105. defer setupRegistry(c)()
  106. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  107. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  108. if err != nil {
  109. c.Fatalf("Unable to create test file: %v", err)
  110. }
  111. tw := tar.NewWriter(emptyTarball)
  112. err = tw.Close()
  113. if err != nil {
  114. c.Fatalf("Error creating empty tarball: %v", err)
  115. }
  116. freader, err := os.Open(emptyTarball.Name())
  117. if err != nil {
  118. c.Fatalf("Could not open test tarball: %v", err)
  119. }
  120. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  121. importCmd.Stdin = freader
  122. out, _, err := runCommandWithOutput(importCmd)
  123. if err != nil {
  124. c.Errorf("import failed with errors: %v, output: %q", err, out)
  125. }
  126. // Now verify we can push it
  127. pushCmd := exec.Command(dockerBinary, "push", repoName)
  128. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  129. c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  130. }
  131. }