docker_cli_push_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 and upload it to the 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 and upload it to the private registry
  81. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", "busybox", repoName)); err != nil {
  82. c.Fatalf("image tagging failed: %s, %v", out, err)
  83. }
  84. defer deleteImages(repoName)
  85. pushCmd := exec.Command(dockerBinary, "push", repoName)
  86. if err := pushCmd.Start(); err != nil {
  87. c.Fatalf("Failed to start pushing to private registry: %v", err)
  88. }
  89. // Interrupt push (yes, we have no idea at what point it will get killed).
  90. time.Sleep(200 * time.Millisecond)
  91. if err := pushCmd.Process.Kill(); err != nil {
  92. c.Fatalf("Failed to kill push process: %v", err)
  93. }
  94. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "push", repoName)); err == nil {
  95. str := string(out)
  96. if !strings.Contains(str, "already in progress") {
  97. c.Fatalf("Push should be continued on daemon side, but seems ok: %v, %s", err, out)
  98. }
  99. }
  100. // now wait until all this pushes will complete
  101. // if it failed with timeout - there would be some error,
  102. // so no logic about it here
  103. for exec.Command(dockerBinary, "push", repoName).Run() != nil {
  104. }
  105. }
  106. func (s *DockerSuite) TestPushEmptyLayer(c *check.C) {
  107. defer setupRegistry(c)()
  108. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  109. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  110. if err != nil {
  111. c.Fatalf("Unable to create test file: %v", err)
  112. }
  113. tw := tar.NewWriter(emptyTarball)
  114. err = tw.Close()
  115. if err != nil {
  116. c.Fatalf("Error creating empty tarball: %v", err)
  117. }
  118. freader, err := os.Open(emptyTarball.Name())
  119. if err != nil {
  120. c.Fatalf("Could not open test tarball: %v", err)
  121. }
  122. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  123. importCmd.Stdin = freader
  124. out, _, err := runCommandWithOutput(importCmd)
  125. if err != nil {
  126. c.Errorf("import failed with errors: %v, output: %q", err, out)
  127. }
  128. // Now verify we can push it
  129. pushCmd := exec.Command(dockerBinary, "push", repoName)
  130. if out, _, err := runCommandWithOutput(pushCmd); err != nil {
  131. c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  132. }
  133. }