docker_cli_push_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. "time"
  10. "github.com/go-check/check"
  11. )
  12. // Pushing an image to a private registry.
  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. dockerCmd(c, "tag", "busybox", repoName)
  17. // push the image to the registry
  18. dockerCmd(c, "push", repoName)
  19. }
  20. // pushing an image without a prefix should throw an error
  21. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  22. if out, _, err := dockerCmdWithError(c, "push", "busybox"); err == nil {
  23. c.Fatalf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out)
  24. }
  25. }
  26. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  27. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  28. expected := "Repository does not exist"
  29. if out, _, err := dockerCmdWithError(c, "push", repoName); err == nil {
  30. c.Fatalf("pushing the image to the private registry should have failed: output %q", out)
  31. } else if !strings.Contains(out, expected) {
  32. c.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
  33. }
  34. }
  35. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  36. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  37. expected := "does not exist"
  38. if out, _, err := dockerCmdWithError(c, "push", repoName); err == nil {
  39. c.Fatalf("pushing the image to the private registry should have failed: output %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 *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  45. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  46. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  47. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  48. // tag the image and upload it to the private registry
  49. dockerCmd(c, "tag", "busybox", repoTag1)
  50. dockerCmd(c, "tag", "busybox", repoTag2)
  51. out, _ := dockerCmd(c, "push", repoName)
  52. // There should be no duplicate hashes in the output
  53. imageSuccessfullyPushed := ": Image successfully pushed"
  54. imageAlreadyExists := ": Image already exists"
  55. imagePushHashes := make(map[string]struct{})
  56. outputLines := strings.Split(out, "\n")
  57. for _, outputLine := range outputLines {
  58. if strings.Contains(outputLine, imageSuccessfullyPushed) {
  59. hash := strings.TrimSuffix(outputLine, imageSuccessfullyPushed)
  60. if _, present := imagePushHashes[hash]; present {
  61. c.Fatalf("Duplicate image push: %s", outputLine)
  62. }
  63. imagePushHashes[hash] = struct{}{}
  64. } else if strings.Contains(outputLine, imageAlreadyExists) {
  65. hash := strings.TrimSuffix(outputLine, imageAlreadyExists)
  66. if _, present := imagePushHashes[hash]; present {
  67. c.Fatalf("Duplicate image push: %s", outputLine)
  68. }
  69. imagePushHashes[hash] = struct{}{}
  70. }
  71. }
  72. if len(imagePushHashes) == 0 {
  73. c.Fatal(`Expected at least one line containing "Image successfully pushed"`)
  74. }
  75. }
  76. func (s *DockerRegistrySuite) TestPushInterrupt(c *check.C) {
  77. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  78. // tag the image and upload it to the private registry
  79. dockerCmd(c, "tag", "busybox", repoName)
  80. pushCmd := exec.Command(dockerBinary, "push", repoName)
  81. if err := pushCmd.Start(); err != nil {
  82. c.Fatalf("Failed to start pushing to private registry: %v", err)
  83. }
  84. // Interrupt push (yes, we have no idea at what point it will get killed).
  85. time.Sleep(200 * time.Millisecond)
  86. if err := pushCmd.Process.Kill(); err != nil {
  87. c.Fatalf("Failed to kill push process: %v", err)
  88. }
  89. if out, _, err := dockerCmdWithError(c, "push", repoName); err == nil {
  90. if !strings.Contains(out, "already in progress") {
  91. c.Fatalf("Push should be continued on daemon side, but seems ok: %v, %s", err, out)
  92. }
  93. }
  94. // now wait until all this pushes will complete
  95. // if it failed with timeout - there would be some error,
  96. // so no logic about it here
  97. for exec.Command(dockerBinary, "push", repoName).Run() != nil {
  98. }
  99. }
  100. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  101. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  102. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  103. if err != nil {
  104. c.Fatalf("Unable to create test file: %v", err)
  105. }
  106. tw := tar.NewWriter(emptyTarball)
  107. err = tw.Close()
  108. if err != nil {
  109. c.Fatalf("Error creating empty tarball: %v", err)
  110. }
  111. freader, err := os.Open(emptyTarball.Name())
  112. if err != nil {
  113. c.Fatalf("Could not open test tarball: %v", err)
  114. }
  115. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  116. importCmd.Stdin = freader
  117. out, _, err := runCommandWithOutput(importCmd)
  118. if err != nil {
  119. c.Errorf("import failed with errors: %v, output: %q", err, out)
  120. }
  121. // Now verify we can push it
  122. if out, _, err := dockerCmdWithError(c, "push", repoName); err != nil {
  123. c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  124. }
  125. }