docker_cli_push_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. }
  126. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  127. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  128. // tag the image and upload it to the private registry
  129. dockerCmd(c, "tag", "busybox", repoName)
  130. pushCmd := exec.Command(dockerBinary, "push", repoName)
  131. s.trustedCmd(pushCmd)
  132. out, _, err := runCommandWithOutput(pushCmd)
  133. if err != nil {
  134. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  135. }
  136. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  137. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  138. }
  139. }
  140. func (s *DockerTrustSuite) TestTrustedPushWithoutServer(c *check.C) {
  141. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  142. // tag the image and upload it to the private registry
  143. dockerCmd(c, "tag", "busybox", repoName)
  144. pushCmd := exec.Command(dockerBinary, "push", repoName)
  145. s.trustedCmdWithServer(pushCmd, "example/")
  146. out, _, err := runCommandWithOutput(pushCmd)
  147. if err == nil {
  148. c.Fatalf("Missing error while running trusted push w/ no server")
  149. }
  150. if !strings.Contains(string(out), "Error establishing connection to notary repository") {
  151. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  152. }
  153. }
  154. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  155. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  156. // tag the image and upload it to the private registry
  157. dockerCmd(c, "tag", "busybox", repoName)
  158. pushCmd := exec.Command(dockerBinary, "push", "--untrusted", repoName)
  159. s.trustedCmdWithServer(pushCmd, "example/")
  160. out, _, err := runCommandWithOutput(pushCmd)
  161. if err != nil {
  162. c.Fatalf("trusted push with no server and --untrusted failed: %s\n%s", err, out)
  163. }
  164. if strings.Contains(string(out), "Error establishing connection to notary repository") {
  165. c.Fatalf("Missing expected output on trusted push with --untrusted:\n%s", out)
  166. }
  167. }
  168. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  169. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  170. // tag the image and upload it to the private registry
  171. dockerCmd(c, "tag", "busybox", repoName)
  172. dockerCmd(c, "push", repoName)
  173. pushCmd := exec.Command(dockerBinary, "push", repoName)
  174. s.trustedCmd(pushCmd)
  175. out, _, err := runCommandWithOutput(pushCmd)
  176. if err != nil {
  177. c.Fatalf("trusted push failed: %s\n%s", err, out)
  178. }
  179. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  180. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  181. }
  182. }
  183. func (s *DockerTrustSuite) TestTrustedPushWithShortRootPassphrase(c *check.C) {
  184. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  185. // tag the image and upload it to the private registry
  186. dockerCmd(c, "tag", "busybox", repoName)
  187. pushCmd := exec.Command(dockerBinary, "push", repoName)
  188. s.trustedCmdWithPassphrases(pushCmd, "rootPwd", "", "")
  189. out, _, err := runCommandWithOutput(pushCmd)
  190. if err == nil {
  191. c.Fatalf("Error missing from trusted push with short root passphrase")
  192. }
  193. if !strings.Contains(string(out), "tuf: insufficient signatures for Cryptoservice") {
  194. c.Fatalf("Missing expected output on trusted push with short root passphrase:\n%s", out)
  195. }
  196. }
  197. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectRootPassphrase(c *check.C) {
  198. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  199. // tag the image and upload it to the private registry
  200. dockerCmd(c, "tag", "busybox", repoName)
  201. // Push with default passphrase
  202. pushCmd := exec.Command(dockerBinary, "push", "--untrusted", repoName)
  203. s.trustedCmd(pushCmd)
  204. out, _, _ := runCommandWithOutput(pushCmd)
  205. fmt.Println("OUTPUT: ", out)
  206. // Push with incorrect passphrase
  207. pushCmd = exec.Command(dockerBinary, "push", "--untrusted", repoName)
  208. s.trustedCmd(pushCmd)
  209. // s.trustedCmdWithPassphrases(pushCmd, "87654321", "", "")
  210. out, _, _ = runCommandWithOutput(pushCmd)
  211. fmt.Println("OUTPUT2:", out)
  212. //c.Fail()
  213. }
  214. func (s *DockerTrustSuite) TestTrustedPushWithShortPassphraseForNonRoot(c *check.C) {
  215. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  216. // tag the image and upload it to the private registry
  217. dockerCmd(c, "tag", "busybox", repoName)
  218. pushCmd := exec.Command(dockerBinary, "push", repoName)
  219. s.trustedCmdWithPassphrases(pushCmd, "12345678", "short", "short")
  220. out, _, err := runCommandWithOutput(pushCmd)
  221. if err == nil {
  222. c.Fatalf("Error missing from trusted push with short targets passphrase")
  223. }
  224. if !strings.Contains(string(out), "tuf: insufficient signatures for Cryptoservice") {
  225. c.Fatalf("Missing expected output on trusted push with short targets/snapsnot passphrase:\n%s", out)
  226. }
  227. }