docker_cli_push_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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) TestTrustedPushWithFaillingServer(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.trustedCmdWithServer(pushCmd, "example/")
  129. out, _, err := runCommandWithOutput(pushCmd)
  130. if err == nil {
  131. c.Fatalf("Missing error while running trusted push w/ no server")
  132. }
  133. if !strings.Contains(string(out), "error contacting notary server") {
  134. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  135. }
  136. }
  137. func (s *DockerTrustSuite) TestTrustedPushWithoutServerAndUntrusted(c *check.C) {
  138. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  139. // tag the image and upload it to the private registry
  140. dockerCmd(c, "tag", "busybox", repoName)
  141. pushCmd := exec.Command(dockerBinary, "push", "--disable-content-trust", repoName)
  142. s.trustedCmdWithServer(pushCmd, "example/")
  143. out, _, err := runCommandWithOutput(pushCmd)
  144. if err != nil {
  145. c.Fatalf("trusted push with no server and --disable-content-trust failed: %s\n%s", err, out)
  146. }
  147. if strings.Contains(string(out), "Error establishing connection to notary repository") {
  148. c.Fatalf("Missing expected output on trusted push with --disable-content-trust:\n%s", out)
  149. }
  150. }
  151. func (s *DockerTrustSuite) TestTrustedPushWithExistingTag(c *check.C) {
  152. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  153. // tag the image and upload it to the private registry
  154. dockerCmd(c, "tag", "busybox", repoName)
  155. dockerCmd(c, "push", repoName)
  156. pushCmd := exec.Command(dockerBinary, "push", repoName)
  157. s.trustedCmd(pushCmd)
  158. out, _, err := runCommandWithOutput(pushCmd)
  159. if err != nil {
  160. c.Fatalf("trusted push failed: %s\n%s", err, out)
  161. }
  162. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  163. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  164. }
  165. }
  166. func (s *DockerTrustSuite) TestTrustedPushWithExistingSignedTag(c *check.C) {
  167. repoName := fmt.Sprintf("%v/dockerclipushpush/trusted:latest", privateRegistryURL)
  168. // tag the image and upload it to the private registry
  169. dockerCmd(c, "tag", "busybox", repoName)
  170. // Do a trusted push
  171. pushCmd := exec.Command(dockerBinary, "push", repoName)
  172. s.trustedCmd(pushCmd)
  173. out, _, err := runCommandWithOutput(pushCmd)
  174. if err != nil {
  175. c.Fatalf("trusted push failed: %s\n%s", err, out)
  176. }
  177. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  178. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  179. }
  180. // Do another trusted push
  181. pushCmd = exec.Command(dockerBinary, "push", repoName)
  182. s.trustedCmd(pushCmd)
  183. out, _, err = runCommandWithOutput(pushCmd)
  184. if err != nil {
  185. c.Fatalf("trusted push failed: %s\n%s", err, out)
  186. }
  187. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  188. c.Fatalf("Missing expected output on trusted push with existing tag:\n%s", out)
  189. }
  190. dockerCmd(c, "rmi", repoName)
  191. // Try pull to ensure the double push did not break our ability to pull
  192. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  193. s.trustedCmd(pullCmd)
  194. out, _, err = runCommandWithOutput(pullCmd)
  195. if err != nil {
  196. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  197. }
  198. if !strings.Contains(string(out), "Status: Downloaded") {
  199. c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
  200. }
  201. }
  202. func (s *DockerTrustSuite) TestTrustedPushWithIncorrectPassphraseForNonRoot(c *check.C) {
  203. repoName := fmt.Sprintf("%v/dockercliincorretpwd/trusted:latest", privateRegistryURL)
  204. // tag the image and upload it to the private registry
  205. dockerCmd(c, "tag", "busybox", repoName)
  206. // Push with default passphrases
  207. pushCmd := exec.Command(dockerBinary, "push", repoName)
  208. s.trustedCmd(pushCmd)
  209. out, _, err := runCommandWithOutput(pushCmd)
  210. if err != nil {
  211. c.Fatalf("trusted push failed: %s\n%s", err, out)
  212. }
  213. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  214. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  215. }
  216. // Push with wrong passphrases
  217. pushCmd = exec.Command(dockerBinary, "push", repoName)
  218. s.trustedCmdWithPassphrases(pushCmd, "12345678", "87654321")
  219. out, _, err = runCommandWithOutput(pushCmd)
  220. if err == nil {
  221. c.Fatalf("Error missing from trusted push with short targets passphrase: \n%s", out)
  222. }
  223. if !strings.Contains(string(out), "password invalid, operation has failed") {
  224. c.Fatalf("Missing expected output on trusted push with short targets/snapsnot passphrase:\n%s", out)
  225. }
  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. if err != nil {
  237. c.Fatalf("trusted push failed: %s\n%s", err, out)
  238. }
  239. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  240. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  241. }
  242. // Snapshots last for three years. This should be expired
  243. fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
  244. runAtDifferentDate(fourYearsLater, func() {
  245. // Push with wrong passphrases
  246. pushCmd = exec.Command(dockerBinary, "push", repoName)
  247. s.trustedCmd(pushCmd)
  248. out, _, err = runCommandWithOutput(pushCmd)
  249. if err == nil {
  250. c.Fatalf("Error missing from trusted push with expired snapshot: \n%s", out)
  251. }
  252. if !strings.Contains(string(out), "repository out-of-date") {
  253. c.Fatalf("Missing expected output on trusted push with expired snapshot:\n%s", out)
  254. }
  255. })
  256. }
  257. func (s *DockerTrustSuite) TestTrustedPushWithExpiredTimestamp(c *check.C) {
  258. c.Skip("Currently changes system time, causing instability")
  259. repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppush/trusted:latest", privateRegistryURL)
  260. // tag the image and upload it to the private registry
  261. dockerCmd(c, "tag", "busybox", repoName)
  262. // Push with default passphrases
  263. pushCmd := exec.Command(dockerBinary, "push", repoName)
  264. s.trustedCmd(pushCmd)
  265. out, _, err := runCommandWithOutput(pushCmd)
  266. if err != nil {
  267. c.Fatalf("trusted push failed: %s\n%s", err, out)
  268. }
  269. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  270. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  271. }
  272. // The timestamps expire in two weeks. Lets check three
  273. threeWeeksLater := time.Now().Add(time.Hour * 24 * 21)
  274. // Should succeed because the server transparently re-signs one
  275. runAtDifferentDate(threeWeeksLater, func() {
  276. pushCmd := exec.Command(dockerBinary, "push", repoName)
  277. s.trustedCmd(pushCmd)
  278. out, _, err := runCommandWithOutput(pushCmd)
  279. if err != nil {
  280. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  281. }
  282. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  283. c.Fatalf("Missing expected output on trusted push with expired timestamp:\n%s", out)
  284. }
  285. })
  286. }