docker_cli_push_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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("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("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("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. dockerCmd(c, "push", repoName)
  52. // Ensure layer list is equivalent for repoTag1 and repoTag2
  53. out1, _ := dockerCmd(c, "pull", repoTag1)
  54. if strings.Contains(out1, "Tag t1 not found") {
  55. c.Fatalf("Unable to pull pushed image: %s", out1)
  56. }
  57. imageAlreadyExists := ": Image already exists"
  58. var out1Lines []string
  59. for _, outputLine := range strings.Split(out1, "\n") {
  60. if strings.Contains(outputLine, imageAlreadyExists) {
  61. out1Lines = append(out1Lines, outputLine)
  62. }
  63. }
  64. out2, _ := dockerCmd(c, "pull", repoTag2)
  65. if strings.Contains(out2, "Tag t2 not found") {
  66. c.Fatalf("Unable to pull pushed image: %s", out1)
  67. }
  68. var out2Lines []string
  69. for _, outputLine := range strings.Split(out2, "\n") {
  70. if strings.Contains(outputLine, imageAlreadyExists) {
  71. out1Lines = append(out1Lines, outputLine)
  72. }
  73. }
  74. if len(out1Lines) != len(out2Lines) {
  75. c.Fatalf("Mismatched output length:\n%s\n%s", out1, out2)
  76. }
  77. for i := range out1Lines {
  78. if out1Lines[i] != out2Lines[i] {
  79. c.Fatalf("Mismatched output line:\n%s\n%s", out1Lines[i], out2Lines[i])
  80. }
  81. }
  82. }
  83. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  84. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  85. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  86. if err != nil {
  87. c.Fatalf("Unable to create test file: %v", err)
  88. }
  89. tw := tar.NewWriter(emptyTarball)
  90. err = tw.Close()
  91. if err != nil {
  92. c.Fatalf("Error creating empty tarball: %v", err)
  93. }
  94. freader, err := os.Open(emptyTarball.Name())
  95. if err != nil {
  96. c.Fatalf("Could not open test tarball: %v", err)
  97. }
  98. importCmd := exec.Command(dockerBinary, "import", "-", repoName)
  99. importCmd.Stdin = freader
  100. out, _, err := runCommandWithOutput(importCmd)
  101. if err != nil {
  102. c.Errorf("import failed with errors: %v, output: %q", err, out)
  103. }
  104. // Now verify we can push it
  105. if out, _, err := dockerCmdWithError("push", repoName); err != nil {
  106. c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
  107. }
  108. }
  109. func (s *DockerTrustSuite) TestTrustedPush(c *check.C) {
  110. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  111. // tag the image and upload it to the private registry
  112. dockerCmd(c, "tag", "busybox", repoName)
  113. pushCmd := exec.Command(dockerBinary, "push", repoName)
  114. s.trustedCmd(pushCmd)
  115. out, _, err := runCommandWithOutput(pushCmd)
  116. if err != nil {
  117. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  118. }
  119. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  120. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  121. }
  122. }
  123. func (s *DockerTrustSuite) TestTrustedPushWithEnvPasswords(c *check.C) {
  124. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  125. // tag the image and upload it to the private registry
  126. dockerCmd(c, "tag", "busybox", repoName)
  127. pushCmd := exec.Command(dockerBinary, "push", repoName)
  128. s.trustedCmdWithPassphrases(pushCmd, "12345678", "12345678")
  129. out, _, err := runCommandWithOutput(pushCmd)
  130. if err != nil {
  131. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  132. }
  133. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  134. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  135. }
  136. }
  137. // This test ensures backwards compatibility with old ENV variables. Should be
  138. // deprecated by 1.10
  139. func (s *DockerTrustSuite) TestTrustedPushWithDeprecatedEnvPasswords(c *check.C) {
  140. repoName := fmt.Sprintf("%v/dockercli/trusteddeprecated:latest", privateRegistryURL)
  141. // tag the image and upload it to the private registry
  142. dockerCmd(c, "tag", "busybox", repoName)
  143. pushCmd := exec.Command(dockerBinary, "push", repoName)
  144. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "12345678")
  145. out, _, err := runCommandWithOutput(pushCmd)
  146. if err != nil {
  147. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  148. }
  149. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  150. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  151. }
  152. }
  153. func (s *DockerTrustSuite) TestTrustedPushWithFaillingServer(c *check.C) {
  154. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  155. // tag the image and upload it to the private registry
  156. dockerCmd(c, "tag", "busybox", repoName)
  157. pushCmd := exec.Command(dockerBinary, "push", repoName)
  158. s.trustedCmdWithServer(pushCmd, "https://example.com:81/")
  159. out, _, err := runCommandWithOutput(pushCmd)
  160. if err == nil {
  161. c.Fatalf("Missing error while running trusted push w/ no server")
  162. }
  163. if !strings.Contains(string(out), "error contacting notary server") {
  164. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  165. }
  166. }
  167. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  168. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  169. // tag the image and upload it to the private registry
  170. dockerCmd(c, "tag", "busybox", repoName)
  171. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  172. s.trustedCmdWithServer(pushCmd, "https://example.com/")
  173. out, _, err := runCommandWithOutput(pushCmd)
  174. if err != nil {
  175. c.Fatalf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out)
  176. }
  177. if strings.Contains(string(out), "Error establishing connection to notary repository") {
  178. c.Fatalf("Missing expected output on trusted push with --disable-content-trust:\n%s", out)
  179. }
  180. }
  181. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  182. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  183. // tag the image and upload it to the private registry
  184. dockerCmd(c, "tag", "busybox", repoName)
  185. dockerCmd(c, "push", repoName)
  186. pushCmd := exec.Command(dockerBinary, "push", repoName)
  187. s.trustedCmd(pushCmd)
  188. out, _, err := runCommandWithOutput(pushCmd)
  189. if err != nil {
  190. c.Fatalf("trusted push failed: %s\n%s", err, out)
  191. }
  192. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  193. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  194. }
  195. }
  196. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  197. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  198. // tag the image and upload it to the private registry
  199. dockerCmd(c, "tag", "busybox", repoName)
  200. // Do a trusted push
  201. pushCmd := exec.Command(dockerBinary, "push", repoName)
  202. s.trustedCmd(pushCmd)
  203. out, _, err := runCommandWithOutput(pushCmd)
  204. if err != nil {
  205. c.Fatalf("trusted push failed: %s\n%s", err, out)
  206. }
  207. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  208. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  209. }
  210. // Do another trusted push
  211. pushCmd = exec.Command(dockerBinary, "push", repoName)
  212. s.trustedCmd(pushCmd)
  213. out, _, err = runCommandWithOutput(pushCmd)
  214. if err != nil {
  215. c.Fatalf("trusted push failed: %s\n%s", err, out)
  216. }
  217. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  218. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  219. }
  220. dockerCmd(c, "rmi", repoName)
  221. // Try pull to ensure the double push did not break our ability to pull
  222. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  223. s.trustedCmd(pullCmd)
  224. out, _, err = runCommandWithOutput(pullCmd)
  225. if err != nil {
  226. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  227. }
  228. if !strings.Contains(string(out), "Status: Downloaded") {
  229. c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
  230. }
  231. }
  232. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  233. repoName := fmt.Sprintf("%v/dockercliincorretpwd/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. if err != nil {
  241. c.Fatalf("trusted push failed: %s\n%s", err, out)
  242. }
  243. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  244. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  245. }
  246. // Push with wrong passphrases
  247. pushCmd = exec.Command(dockerBinary, "push", repoName)
  248. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  249. out, _, err = runCommandWithOutput(pushCmd)
  250. if err == nil {
  251. c.Fatalf("Error missing from trusted push with short targets passphrase: \n%s", out)
  252. }
  253. if !strings.Contains(string(out), "password invalid, operation has failed") {
  254. c.Fatalf("Missing expected output on trusted push with short targets/snapsnot passphrase:\n%s", out)
  255. }
  256. }
  257. // This test ensures backwards compatibility with old ENV variables. Should be
  258. // deprecated by 1.10
  259. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectDeprecatedPassphraseForNonRoot(c *check.C) {
  260. repoName := fmt.Sprintf("%v/dockercliincorretdeprecatedpwd/trusted:latest", privateRegistryURL)
  261. // tag the image and upload it to the private registry
  262. dockerCmd(c, "tag", "busybox", repoName)
  263. // Push with default passphrases
  264. pushCmd := exec.Command(dockerBinary, "push", repoName)
  265. s.trustedCmd(pushCmd)
  266. out, _, err := runCommandWithOutput(pushCmd)
  267. if err != nil {
  268. c.Fatalf("trusted push failed: %s\n%s", err, out)
  269. }
  270. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  271. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  272. }
  273. // Push with wrong passphrases
  274. pushCmd = exec.Command(dockerBinary, "push", repoName)
  275. s.trustedCmdWithDeprecatedEnvPassphrases(pushCmd, "12345678", "87654321")
  276. out, _, err = runCommandWithOutput(pushCmd)
  277. if err == nil {
  278. c.Fatalf("Error missing from trusted push with short targets passphrase: \n%s", out)
  279. }
  280. if !strings.Contains(string(out), "password invalid, operation has failed") {
  281. c.Fatalf("Missing expected output on trusted push with short targets/snapsnot passphrase:\n%s", out)
  282. }
  283. }
  284. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  285. c.Skip("Currently changes system time, causing instability")
  286. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  287. // tag the image and upload it to the private registry
  288. dockerCmd(c, "tag", "busybox", repoName)
  289. // Push with default passphrases
  290. pushCmd := exec.Command(dockerBinary, "push", repoName)
  291. s.trustedCmd(pushCmd)
  292. out, _, err := runCommandWithOutput(pushCmd)
  293. if err != nil {
  294. c.Fatalf("trusted push failed: %s\n%s", err, out)
  295. }
  296. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  297. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  298. }
  299. // Snapshots last for three years. This should be expired
  300. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  301. runAtDifferentDate(fourYearsLater, func() {
  302. // Push with wrong passphrases
  303. pushCmd = exec.Command(dockerBinary, "push", repoName)
  304. s.trustedCmd(pushCmd)
  305. out, _, err = runCommandWithOutput(pushCmd)
  306. if err == nil {
  307. c.Fatalf("Error missing from trusted push with expired snapshot: \n%s", out)
  308. }
  309. if !strings.Contains(string(out), "repository out-of-date") {
  310. c.Fatalf("Missing expected output on trusted push with expired snapshot:\n%s", out)
  311. }
  312. })
  313. }
  314. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  315. c.Skip("Currently changes system time, causing instability")
  316. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  317. // tag the image and upload it to the private registry
  318. dockerCmd(c, "tag", "busybox", repoName)
  319. // Push with default passphrases
  320. pushCmd := exec.Command(dockerBinary, "push", repoName)
  321. s.trustedCmd(pushCmd)
  322. out, _, err := runCommandWithOutput(pushCmd)
  323. if err != nil {
  324. c.Fatalf("trusted push failed: %s\n%s", err, out)
  325. }
  326. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  327. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  328. }
  329. // The timestamps expire in two weeks. Lets check three
  330. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  331. // Should succeed because the server transparently re-signs one
  332. runAtDifferentDate(threeWeeksLater, func() {
  333. pushCmd := exec.Command(dockerBinary, "push", repoName)
  334. s.trustedCmd(pushCmd)
  335. out, _, err := runCommandWithOutput(pushCmd)
  336. if err != nil {
  337. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  338. }
  339. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  340. c.Fatalf("Missing expected output on trusted push with expired timestamp:\n%s", out)
  341. }
  342. })
  343. }