docker_cli_push_test.go 5.3 KB

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