docker_cli_push_test.go 5.2 KB

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