docker_cli_push_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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) TestTrustedPushWithFaillingServer(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", "--disable-content-trust", repoName)
  159. s.trustedCmdWithServer(pushCmd, "example/")
  160. out, _, err := runCommandWithOutput(pushCmd)
  161. if err != nil {
  162. c.Fatalf("trusted push with no server and --disable-content-trust 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 --disable-content-trust:\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) TestTrustedPushWithExistingSignedTag(c *check.C) {
  184. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  185. // tag the image and upload it to the private registry
  186. dockerCmd(c, "tag", "busybox", repoName)
  187. // Do a trusted push
  188. pushCmd := exec.Command(dockerBinary, "push", repoName)
  189. s.trustedCmd(pushCmd)
  190. out, _, err := runCommandWithOutput(pushCmd)
  191. if err != nil {
  192. c.Fatalf("trusted push failed: %s\n%s", err, out)
  193. }
  194. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  195. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  196. }
  197. // Do another trusted push
  198. pushCmd = exec.Command(dockerBinary, "push", repoName)
  199. s.trustedCmd(pushCmd)
  200. out, _, err = runCommandWithOutput(pushCmd)
  201. if err != nil {
  202. c.Fatalf("trusted push failed: %s\n%s", err, out)
  203. }
  204. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  205. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  206. }
  207. dockerCmd(c, "rmi", repoName)
  208. // Try pull to ensure the double push did not break our ability to pull
  209. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  210. s.trustedCmd(pullCmd)
  211. out, _, err = runCommandWithOutput(pullCmd)
  212. if err != nil {
  213. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  214. }
  215. if !strings.Contains(string(out), "Status: Downloaded") {
  216. c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
  217. }
  218. }
  219. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  220. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  221. // tag the image and upload it to the private registry
  222. dockerCmd(c, "tag", "busybox", repoName)
  223. // Push with default passphrases
  224. pushCmd := exec.Command(dockerBinary, "push", repoName)
  225. s.trustedCmd(pushCmd)
  226. out, _, err := runCommandWithOutput(pushCmd)
  227. if err != nil {
  228. c.Fatalf("trusted push failed: %s\n%s", err, out)
  229. }
  230. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  231. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  232. }
  233. // Push with wrong passphrases
  234. pushCmd = exec.Command(dockerBinary, "push", repoName)
  235. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321", "87654321")
  236. out, _, err = runCommandWithOutput(pushCmd)
  237. if err == nil {
  238. c.Fatalf("Error missing from trusted push with short targets passphrase: \n%s", out)
  239. }
  240. if !strings.Contains(string(out), "password invalid, operation has failed") {
  241. c.Fatalf("Missing expected output on trusted push with short targets/snapsnot passphrase:\n%s", out)
  242. }
  243. }
  244. func (s *DockerTrustSuite) TestTrustedPushWithExpiredSnapshot(c *check.C) {
  245. repoName := fmt.Sprintf("%v/dockercliexpiredsnapshot/trusted:latest", privateRegistryURL)
  246. // tag the image and upload it to the private registry
  247. dockerCmd(c, "tag", "busybox", repoName)
  248. // Push with default passphrases
  249. pushCmd := exec.Command(dockerBinary, "push", repoName)
  250. s.trustedCmd(pushCmd)
  251. out, _, err := runCommandWithOutput(pushCmd)
  252. if err != nil {
  253. c.Fatalf("trusted push failed: %s\n%s", err, out)
  254. }
  255. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  256. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  257. }
  258. // Snapshots last for three years. This should be expired
  259. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  260. runAtDifferentDate(fourYearsLater, func() {
  261. // Push with wrong passphrases
  262. pushCmd = exec.Command(dockerBinary, "push", repoName)
  263. s.trustedCmd(pushCmd)
  264. out, _, err = runCommandWithOutput(pushCmd)
  265. if err == nil {
  266. c.Fatalf("Error missing from trusted push with expired snapshot: \n%s", out)
  267. }
  268. if !strings.Contains(string(out), "repository out-of-date") {
  269. c.Fatalf("Missing expected output on trusted push with expired snapshot:\n%s", out)
  270. }
  271. })
  272. }
  273. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  274. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  275. // tag the image and upload it to the private registry
  276. dockerCmd(c, "tag", "busybox", repoName)
  277. // Push with default passphrases
  278. pushCmd := exec.Command(dockerBinary, "push", repoName)
  279. s.trustedCmd(pushCmd)
  280. out, _, err := runCommandWithOutput(pushCmd)
  281. if err != nil {
  282. c.Fatalf("trusted push failed: %s\n%s", err, out)
  283. }
  284. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  285. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  286. }
  287. // The timestamps expire in two weeks. Lets check three
  288. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  289. // Should succeed because the server transparently re-signs one
  290. runAtDifferentDate(threeWeeksLater, func() {
  291. pushCmd := exec.Command(dockerBinary, "push", repoName)
  292. s.trustedCmd(pushCmd)
  293. out, _, err := runCommandWithOutput(pushCmd)
  294. if err != nil {
  295. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  296. }
  297. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  298. c.Fatalf("Missing expected output on trusted push with expired timestamp:\n%s", out)
  299. }
  300. })
  301. }