docker_cli_push_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/docker/docker/pkg/integration/checker"
  11. "github.com/go-check/check"
  12. )
  13. // Pushing an image to a private registry.
  14. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  15. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  16. // tag the image to upload it to the private registry
  17. dockerCmd(c, "tag", "busybox", repoName)
  18. // push the image to the registry
  19. dockerCmd(c, "push", repoName)
  20. }
  21. // pushing an image without a prefix should throw an error
  22. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  23. out, _, err := dockerCmdWithError("push", "busybox")
  24. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  25. }
  26. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  27. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  28. expected := "Repository does not exist"
  29. out, _, err := dockerCmdWithError("push", repoName)
  30. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  31. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  32. }
  33. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  34. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  35. expected := "does not exist"
  36. out, _, err := dockerCmdWithError("push", repoName)
  37. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  38. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  39. }
  40. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  41. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  42. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  43. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  44. // tag the image and upload it to the private registry
  45. dockerCmd(c, "tag", "busybox", repoTag1)
  46. dockerCmd(c, "tag", "busybox", repoTag2)
  47. dockerCmd(c, "push", repoName)
  48. // Ensure layer list is equivalent for repoTag1 and repoTag2
  49. out1, _ := dockerCmd(c, "pull", repoTag1)
  50. imageAlreadyExists := ": Image already exists"
  51. var out1Lines []string
  52. for _, outputLine := range strings.Split(out1, "\n") {
  53. if strings.Contains(outputLine, imageAlreadyExists) {
  54. out1Lines = append(out1Lines, outputLine)
  55. }
  56. }
  57. out2, _ := dockerCmd(c, "pull", repoTag2)
  58. var out2Lines []string
  59. for _, outputLine := range strings.Split(out2, "\n") {
  60. if strings.Contains(outputLine, imageAlreadyExists) {
  61. out1Lines = append(out1Lines, outputLine)
  62. }
  63. }
  64. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  65. for i := range out1Lines {
  66. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  67. }
  68. }
  69. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  70. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  71. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  72. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  73. tw := tar.NewWriter(emptyTarball)
  74. err = tw.Close()
  75. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  76. freader, err := os.Open(emptyTarball.Name())
  77. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  78. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  79. importCmd.Stdin = freader
  80. out, _, err := runCommandWithOutput(importCmd)
  81. c.Assert(err, check.IsNil, check.Commentf("import failed: %q", out))
  82. // Now verify we can push it
  83. out, _, err = dockerCmdWithError("push", repoName)
  84. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  85. }
  86. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  87. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  88. // tag the image and upload it to the private registry
  89. dockerCmd(c, "tag", "busybox", repoName)
  90. pushCmd := exec.Command(dockerBinary, "push", repoName)
  91. s.trustedCmd(pushCmd)
  92. out, _, err := runCommandWithOutput(pushCmd)
  93. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  94. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  95. }
  96. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  97. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  98. // tag the image and upload it to the private registry
  99. dockerCmd(c, "tag", "busybox", repoName)
  100. pushCmd := exec.Command(dockerBinary, "push", repoName)
  101. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  102. out, _, err := runCommandWithOutput(pushCmd)
  103. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  104. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  105. }
  106. // This test ensures backwards compatibility with old ENV variables. Should be
  107. // deprecated by 1.10
  108. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  109. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  110. // tag the image and upload it to the private registry
  111. dockerCmd(c, "tag", "busybox", repoName)
  112. pushCmd := exec.Command(dockerBinary, "push", repoName)
  113. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  114. out, _, err := runCommandWithOutput(pushCmd)
  115. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  116. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  117. }
  118. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  119. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  120. // tag the image and upload it to the private registry
  121. dockerCmd(c, "tag", "busybox", repoName)
  122. pushCmd := exec.Command(dockerBinary, "push", repoName)
  123. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  124. out, _, err := runCommandWithOutput(pushCmd)
  125. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  126. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  127. }
  128. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  129. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  130. // tag the image and upload it to the private registry
  131. dockerCmd(c, "tag", "busybox", repoName)
  132. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  133. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  134. out, _, err := runCommandWithOutput(pushCmd)
  135. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  136. c.Assert(out, check.Not(checker.Contains), "Error establishing connection to notary repository", check.Commentf("Missing expected output on trusted push with --disable-content-trust:"))
  137. }
  138. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  139. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  140. // tag the image and upload it to the private registry
  141. dockerCmd(c, "tag", "busybox", repoName)
  142. dockerCmd(c, "push", repoName)
  143. pushCmd := exec.Command(dockerBinary, "push", repoName)
  144. s.trustedCmd(pushCmd)
  145. out, _, err := runCommandWithOutput(pushCmd)
  146. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  147. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  148. }
  149. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  150. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  151. // tag the image and upload it to the private registry
  152. dockerCmd(c, "tag", "busybox", repoName)
  153. // Do a trusted push
  154. pushCmd := exec.Command(dockerBinary, "push", repoName)
  155. s.trustedCmd(pushCmd)
  156. out, _, err := runCommandWithOutput(pushCmd)
  157. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  158. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  159. // Do another trusted push
  160. pushCmd = exec.Command(dockerBinary, "push", repoName)
  161. s.trustedCmd(pushCmd)
  162. out, _, err = runCommandWithOutput(pushCmd)
  163. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  164. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  165. dockerCmd(c, "rmi", repoName)
  166. // Try pull to ensure the double push did not break our ability to pull
  167. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  168. s.trustedCmd(pullCmd)
  169. out, _, err = runCommandWithOutput(pullCmd)
  170. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  171. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  172. }
  173. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  174. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  175. // tag the image and upload it to the private registry
  176. dockerCmd(c, "tag", "busybox", repoName)
  177. // Push with default passphrases
  178. pushCmd := exec.Command(dockerBinary, "push", repoName)
  179. s.trustedCmd(pushCmd)
  180. out, _, err := runCommandWithOutput(pushCmd)
  181. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  182. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  183. // Push with wrong passphrases
  184. pushCmd = exec.Command(dockerBinary, "push", repoName)
  185. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  186. out, _, err = runCommandWithOutput(pushCmd)
  187. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  188. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  189. }
  190. // This test ensures backwards compatibility with old ENV variables. Should be
  191. // deprecated by 1.10
  192. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  193. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/trusted:latest", privateRegistryURL)
  194. // tag the image and upload it to the private registry
  195. dockerCmd(c, "tag", "busybox", repoName)
  196. // Push with default passphrases
  197. pushCmd := exec.Command(dockerBinary, "push", repoName)
  198. s.trustedCmd(pushCmd)
  199. out, _, err := runCommandWithOutput(pushCmd)
  200. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  201. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  202. // Push with wrong passphrases
  203. pushCmd = exec.Command(dockerBinary, "push", repoName)
  204. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  205. out, _, err = runCommandWithOutput(pushCmd)
  206. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  207. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  208. }
  209. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  210. c.Skip("Currently changes system time, causing instability")
  211. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  212. // tag the image and upload it to the private registry
  213. dockerCmd(c, "tag", "busybox", repoName)
  214. // Push with default passphrases
  215. pushCmd := exec.Command(dockerBinary, "push", repoName)
  216. s.trustedCmd(pushCmd)
  217. out, _, err := runCommandWithOutput(pushCmd)
  218. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  219. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  220. // Snapshots last for three years. This should be expired
  221. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  222. runAtDifferentDate(fourYearsLater, func() {
  223. // Push with wrong passphrases
  224. pushCmd = exec.Command(dockerBinary, "push", repoName)
  225. s.trustedCmd(pushCmd)
  226. out, _, err = runCommandWithOutput(pushCmd)
  227. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  228. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  229. })
  230. }
  231. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  232. c.Skip("Currently changes system time, causing instability")
  233. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  234. // tag the image and upload it to the private registry
  235. dockerCmd(c, "tag", "busybox", repoName)
  236. // Push with default passphrases
  237. pushCmd := exec.Command(dockerBinary, "push", repoName)
  238. s.trustedCmd(pushCmd)
  239. out, _, err := runCommandWithOutput(pushCmd)
  240. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  241. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  242. // The timestamps expire in two weeks. Lets check three
  243. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  244. // Should succeed because the server transparently re-signs one
  245. runAtDifferentDate(threeWeeksLater, func() {
  246. pushCmd := exec.Command(dockerBinary, "push", repoName)
  247. s.trustedCmd(pushCmd)
  248. out, _, err := runCommandWithOutput(pushCmd)
  249. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  250. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  251. })
  252. }