docker_cli_push_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // Try pull after push
  96. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  97. s.trustedCmd(pullCmd)
  98. out, _, err = runCommandWithOutput(pullCmd)
  99. c.Assert(err, check.IsNil, check.Commentf(out))
  100. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  101. }
  102. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  103. repoName := fmt.Sprintf("%v/dockerclienv/trusted:latest", privateRegistryURL)
  104. // tag the image and upload it to the private registry
  105. dockerCmd(c, "tag", "busybox", repoName)
  106. pushCmd := exec.Command(dockerBinary, "push", repoName)
  107. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  108. out, _, err := runCommandWithOutput(pushCmd)
  109. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  110. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  111. // Try pull after push
  112. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  113. s.trustedCmd(pullCmd)
  114. out, _, err = runCommandWithOutput(pullCmd)
  115. c.Assert(err, check.IsNil, check.Commentf(out))
  116. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  117. }
  118. // This test ensures backwards compatibility with old ENV variables. Should be
  119. // deprecated by 1.10
  120. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  121. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  122. // tag the image and upload it to the private registry
  123. dockerCmd(c, "tag", "busybox", repoName)
  124. pushCmd := exec.Command(dockerBinary, "push", repoName)
  125. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  126. out, _, err := runCommandWithOutput(pushCmd)
  127. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  128. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  129. }
  130. func (s *DockerTrustSuite) TestTrustedPushWithFailingServer(c *check.C) {
  131. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  132. // tag the image and upload it to the private registry
  133. dockerCmd(c, "tag", "busybox", repoName)
  134. pushCmd := exec.Command(dockerBinary, "push", repoName)
  135. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  136. out, _, err := runCommandWithOutput(pushCmd)
  137. c.Assert(err, check.NotNil, check.Commentf("Missing error while running trusted push w/ no server"))
  138. c.Assert(out, checker.Contains, "error contacting notary server", check.Commentf("Missing expected output on trusted push"))
  139. }
  140. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(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", "--disable-content-trust", repoName)
  145. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  146. out, _, err := runCommandWithOutput(pushCmd)
  147. c.Assert(err, check.IsNil, check.Commentf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out))
  148. 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:"))
  149. }
  150. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  151. repoName := fmt.Sprintf("%v/dockerclitag/trusted:latest", privateRegistryURL)
  152. // tag the image and upload it to the private registry
  153. dockerCmd(c, "tag", "busybox", repoName)
  154. dockerCmd(c, "push", repoName)
  155. pushCmd := exec.Command(dockerBinary, "push", repoName)
  156. s.trustedCmd(pushCmd)
  157. out, _, err := runCommandWithOutput(pushCmd)
  158. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  159. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  160. // Try pull after push
  161. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  162. s.trustedCmd(pullCmd)
  163. out, _, err = runCommandWithOutput(pullCmd)
  164. c.Assert(err, check.IsNil, check.Commentf(out))
  165. c.Assert(string(out), checker.Contains, "Status: Downloaded", check.Commentf(out))
  166. }
  167. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  168. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  169. // tag the image and upload it to the private registry
  170. dockerCmd(c, "tag", "busybox", repoName)
  171. // Do a trusted push
  172. pushCmd := exec.Command(dockerBinary, "push", repoName)
  173. s.trustedCmd(pushCmd)
  174. out, _, err := runCommandWithOutput(pushCmd)
  175. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  176. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with existing tag"))
  177. // Do another trusted push
  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 with existing tag"))
  183. dockerCmd(c, "rmi", repoName)
  184. // Try pull to ensure the double push did not break our ability to pull
  185. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  186. s.trustedCmd(pullCmd)
  187. out, _, err = runCommandWithOutput(pullCmd)
  188. c.Assert(err, check.IsNil, check.Commentf("Error running trusted pull: %s\n%s", err, out))
  189. c.Assert(out, checker.Contains, "Status: Downloaded", check.Commentf("Missing expected output on trusted pull with --disable-content-trust"))
  190. }
  191. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  192. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  193. // tag the image and upload it to the private registry
  194. dockerCmd(c, "tag", "busybox", repoName)
  195. // Push with default passphrases
  196. pushCmd := exec.Command(dockerBinary, "push", repoName)
  197. s.trustedCmd(pushCmd)
  198. out, _, err := runCommandWithOutput(pushCmd)
  199. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  200. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push:\n%s", out))
  201. // Push with wrong passphrases
  202. pushCmd = exec.Command(dockerBinary, "push", repoName)
  203. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  204. out, _, err = runCommandWithOutput(pushCmd)
  205. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  206. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  207. }
  208. // This test ensures backwards compatibility with old ENV variables. Should be
  209. // deprecated by 1.10
  210. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  211. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/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. // Push with wrong passphrases
  221. pushCmd = exec.Command(dockerBinary, "push", repoName)
  222. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  223. out, _, err = runCommandWithOutput(pushCmd)
  224. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with short targets passphrase: \n%s", out))
  225. c.Assert(out, checker.Contains, "could not find necessary signing keys", check.Commentf("Missing expected output on trusted push with short targets/snapsnot passphrase"))
  226. }
  227. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  228. c.Skip("Currently changes system time, causing instability")
  229. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  230. // tag the image and upload it to the private registry
  231. dockerCmd(c, "tag", "busybox", repoName)
  232. // Push with default passphrases
  233. pushCmd := exec.Command(dockerBinary, "push", repoName)
  234. s.trustedCmd(pushCmd)
  235. out, _, err := runCommandWithOutput(pushCmd)
  236. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  237. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  238. // Snapshots last for three years. This should be expired
  239. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  240. runAtDifferentDate(fourYearsLater, func() {
  241. // Push with wrong passphrases
  242. pushCmd = exec.Command(dockerBinary, "push", repoName)
  243. s.trustedCmd(pushCmd)
  244. out, _, err = runCommandWithOutput(pushCmd)
  245. c.Assert(err, check.NotNil, check.Commentf("Error missing from trusted push with expired snapshot: \n%s", out))
  246. c.Assert(out, checker.Contains, "repository out-of-date", check.Commentf("Missing expected output on trusted push with expired snapshot"))
  247. })
  248. }
  249. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  250. c.Skip("Currently changes system time, causing instability")
  251. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  252. // tag the image and upload it to the private registry
  253. dockerCmd(c, "tag", "busybox", repoName)
  254. // Push with default passphrases
  255. pushCmd := exec.Command(dockerBinary, "push", repoName)
  256. s.trustedCmd(pushCmd)
  257. out, _, err := runCommandWithOutput(pushCmd)
  258. c.Assert(err, check.IsNil, check.Commentf("trusted push failed: %s\n%s", err, out))
  259. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push"))
  260. // The timestamps expire in two weeks. Lets check three
  261. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  262. // Should succeed because the server transparently re-signs one
  263. runAtDifferentDate(threeWeeksLater, func() {
  264. pushCmd := exec.Command(dockerBinary, "push", repoName)
  265. s.trustedCmd(pushCmd)
  266. out, _, err := runCommandWithOutput(pushCmd)
  267. c.Assert(err, check.IsNil, check.Commentf("Error running trusted push: %s\n%s", err, out))
  268. c.Assert(out, checker.Contains, "Signing and pushing trust metadata", check.Commentf("Missing expected output on trusted push with expired timestamp"))
  269. })
  270. }