docker_cli_push_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package main
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "strings"
  10. "sync"
  11. "github.com/docker/distribution/reference"
  12. "github.com/docker/docker/integration-cli/checker"
  13. "github.com/docker/docker/integration-cli/cli/build"
  14. "github.com/go-check/check"
  15. "github.com/gotestyourself/gotestyourself/icmd"
  16. )
  17. // Pushing an image to a private registry.
  18. func testPushBusyboxImage(c *check.C) {
  19. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  20. // tag the image to upload it to the private registry
  21. dockerCmd(c, "tag", "busybox", repoName)
  22. // push the image to the registry
  23. dockerCmd(c, "push", repoName)
  24. }
  25. func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
  26. testPushBusyboxImage(c)
  27. }
  28. func (s *DockerSchema1RegistrySuite) TestPushBusyboxImage(c *check.C) {
  29. testPushBusyboxImage(c)
  30. }
  31. // pushing an image without a prefix should throw an error
  32. func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
  33. out, _, err := dockerCmdWithError("push", "busybox")
  34. c.Assert(err, check.NotNil, check.Commentf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out))
  35. }
  36. func testPushUntagged(c *check.C) {
  37. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  38. expected := "An image does not exist locally with the tag"
  39. out, _, err := dockerCmdWithError("push", repoName)
  40. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  41. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  42. }
  43. func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
  44. testPushUntagged(c)
  45. }
  46. func (s *DockerSchema1RegistrySuite) TestPushUntagged(c *check.C) {
  47. testPushUntagged(c)
  48. }
  49. func testPushBadTag(c *check.C) {
  50. repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
  51. expected := "does not exist"
  52. out, _, err := dockerCmdWithError("push", repoName)
  53. c.Assert(err, check.NotNil, check.Commentf("pushing the image to the private registry should have failed: output %q", out))
  54. c.Assert(out, checker.Contains, expected, check.Commentf("pushing the image failed"))
  55. }
  56. func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
  57. testPushBadTag(c)
  58. }
  59. func (s *DockerSchema1RegistrySuite) TestPushBadTag(c *check.C) {
  60. testPushBadTag(c)
  61. }
  62. func testPushMultipleTags(c *check.C) {
  63. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  64. repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
  65. repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
  66. // tag the image and upload it to the private registry
  67. dockerCmd(c, "tag", "busybox", repoTag1)
  68. dockerCmd(c, "tag", "busybox", repoTag2)
  69. dockerCmd(c, "push", repoName)
  70. // Ensure layer list is equivalent for repoTag1 and repoTag2
  71. out1, _ := dockerCmd(c, "pull", repoTag1)
  72. imageAlreadyExists := ": Image already exists"
  73. var out1Lines []string
  74. for _, outputLine := range strings.Split(out1, "\n") {
  75. if strings.Contains(outputLine, imageAlreadyExists) {
  76. out1Lines = append(out1Lines, outputLine)
  77. }
  78. }
  79. out2, _ := dockerCmd(c, "pull", repoTag2)
  80. var out2Lines []string
  81. for _, outputLine := range strings.Split(out2, "\n") {
  82. if strings.Contains(outputLine, imageAlreadyExists) {
  83. out1Lines = append(out1Lines, outputLine)
  84. }
  85. }
  86. c.Assert(out2Lines, checker.HasLen, len(out1Lines))
  87. for i := range out1Lines {
  88. c.Assert(out1Lines[i], checker.Equals, out2Lines[i])
  89. }
  90. }
  91. func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
  92. testPushMultipleTags(c)
  93. }
  94. func (s *DockerSchema1RegistrySuite) TestPushMultipleTags(c *check.C) {
  95. testPushMultipleTags(c)
  96. }
  97. func testPushEmptyLayer(c *check.C) {
  98. repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
  99. emptyTarball, err := ioutil.TempFile("", "empty_tarball")
  100. c.Assert(err, check.IsNil, check.Commentf("Unable to create test file"))
  101. tw := tar.NewWriter(emptyTarball)
  102. err = tw.Close()
  103. c.Assert(err, check.IsNil, check.Commentf("Error creating empty tarball"))
  104. freader, err := os.Open(emptyTarball.Name())
  105. c.Assert(err, check.IsNil, check.Commentf("Could not open test tarball"))
  106. defer freader.Close()
  107. icmd.RunCmd(icmd.Cmd{
  108. Command: []string{dockerBinary, "import", "-", repoName},
  109. Stdin: freader,
  110. }).Assert(c, icmd.Success)
  111. // Now verify we can push it
  112. out, _, err := dockerCmdWithError("push", repoName)
  113. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
  114. }
  115. func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
  116. testPushEmptyLayer(c)
  117. }
  118. func (s *DockerSchema1RegistrySuite) TestPushEmptyLayer(c *check.C) {
  119. testPushEmptyLayer(c)
  120. }
  121. // testConcurrentPush pushes multiple tags to the same repo
  122. // concurrently.
  123. func testConcurrentPush(c *check.C) {
  124. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  125. var repos []string
  126. for _, tag := range []string{"push1", "push2", "push3"} {
  127. repo := fmt.Sprintf("%v:%v", repoName, tag)
  128. buildImageSuccessfully(c, repo, build.WithDockerfile(fmt.Sprintf(`
  129. FROM busybox
  130. ENTRYPOINT ["/bin/echo"]
  131. ENV FOO foo
  132. ENV BAR bar
  133. CMD echo %s
  134. `, repo)))
  135. repos = append(repos, repo)
  136. }
  137. // Push tags, in parallel
  138. results := make(chan error)
  139. for _, repo := range repos {
  140. go func(repo string) {
  141. result := icmd.RunCommand(dockerBinary, "push", repo)
  142. results <- result.Error
  143. }(repo)
  144. }
  145. for range repos {
  146. err := <-results
  147. c.Assert(err, checker.IsNil, check.Commentf("concurrent push failed with error: %v", err))
  148. }
  149. // Clear local images store.
  150. args := append([]string{"rmi"}, repos...)
  151. dockerCmd(c, args...)
  152. // Re-pull and run individual tags, to make sure pushes succeeded
  153. for _, repo := range repos {
  154. dockerCmd(c, "pull", repo)
  155. dockerCmd(c, "inspect", repo)
  156. out, _ := dockerCmd(c, "run", "--rm", repo)
  157. c.Assert(strings.TrimSpace(out), checker.Equals, "/bin/sh -c echo "+repo)
  158. }
  159. }
  160. func (s *DockerRegistrySuite) TestConcurrentPush(c *check.C) {
  161. testConcurrentPush(c)
  162. }
  163. func (s *DockerSchema1RegistrySuite) TestConcurrentPush(c *check.C) {
  164. testConcurrentPush(c)
  165. }
  166. func (s *DockerRegistrySuite) TestCrossRepositoryLayerPush(c *check.C) {
  167. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  168. // tag the image to upload it to the private registry
  169. dockerCmd(c, "tag", "busybox", sourceRepoName)
  170. // push the image to the registry
  171. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  172. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  173. // ensure that none of the layers were mounted from another repository during push
  174. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  175. digest1 := reference.DigestRegexp.FindString(out1)
  176. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  177. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  178. // retag the image to upload the same layers to another repo in the same registry
  179. dockerCmd(c, "tag", "busybox", destRepoName)
  180. // push the image to the registry
  181. out2, _, err := dockerCmdWithError("push", destRepoName)
  182. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  183. // ensure that layers were mounted from the first repo during push
  184. c.Assert(strings.Contains(out2, "Mounted from dockercli/busybox"), check.Equals, true)
  185. digest2 := reference.DigestRegexp.FindString(out2)
  186. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  187. c.Assert(digest1, check.Equals, digest2)
  188. // ensure that pushing again produces the same digest
  189. out3, _, err := dockerCmdWithError("push", destRepoName)
  190. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  191. digest3 := reference.DigestRegexp.FindString(out3)
  192. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  193. c.Assert(digest3, check.Equals, digest2)
  194. // ensure that we can pull and run the cross-repo-pushed repository
  195. dockerCmd(c, "rmi", destRepoName)
  196. dockerCmd(c, "pull", destRepoName)
  197. out4, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  198. c.Assert(out4, check.Equals, "hello world")
  199. }
  200. func (s *DockerSchema1RegistrySuite) TestCrossRepositoryLayerPushNotSupported(c *check.C) {
  201. sourceRepoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  202. // tag the image to upload it to the private registry
  203. dockerCmd(c, "tag", "busybox", sourceRepoName)
  204. // push the image to the registry
  205. out1, _, err := dockerCmdWithError("push", sourceRepoName)
  206. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out1))
  207. // ensure that none of the layers were mounted from another repository during push
  208. c.Assert(strings.Contains(out1, "Mounted from"), check.Equals, false)
  209. digest1 := reference.DigestRegexp.FindString(out1)
  210. c.Assert(len(digest1), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  211. destRepoName := fmt.Sprintf("%v/dockercli/crossrepopush", privateRegistryURL)
  212. // retag the image to upload the same layers to another repo in the same registry
  213. dockerCmd(c, "tag", "busybox", destRepoName)
  214. // push the image to the registry
  215. out2, _, err := dockerCmdWithError("push", destRepoName)
  216. c.Assert(err, check.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out2))
  217. // schema1 registry should not support cross-repo layer mounts, so ensure that this does not happen
  218. c.Assert(strings.Contains(out2, "Mounted from"), check.Equals, false)
  219. digest2 := reference.DigestRegexp.FindString(out2)
  220. c.Assert(len(digest2), checker.GreaterThan, 0, check.Commentf("no digest found for pushed manifest"))
  221. c.Assert(digest1, check.Not(check.Equals), digest2)
  222. // ensure that we can pull and run the second pushed repository
  223. dockerCmd(c, "rmi", destRepoName)
  224. dockerCmd(c, "pull", destRepoName)
  225. out3, _ := dockerCmd(c, "run", destRepoName, "echo", "-n", "hello world")
  226. c.Assert(out3, check.Equals, "hello world")
  227. }
  228. func (s *DockerRegistryAuthHtpasswdSuite) TestPushNoCredentialsNoRetry(c *check.C) {
  229. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  230. dockerCmd(c, "tag", "busybox", repoName)
  231. out, _, err := dockerCmdWithError("push", repoName)
  232. c.Assert(err, check.NotNil, check.Commentf(out))
  233. c.Assert(out, check.Not(checker.Contains), "Retrying")
  234. c.Assert(out, checker.Contains, "no basic auth credentials")
  235. }
  236. // This may be flaky but it's needed not to regress on unauthorized push, see #21054
  237. func (s *DockerSuite) TestPushToCentralRegistryUnauthorized(c *check.C) {
  238. testRequires(c, Network)
  239. repoName := "test/busybox"
  240. dockerCmd(c, "tag", "busybox", repoName)
  241. out, _, err := dockerCmdWithError("push", repoName)
  242. c.Assert(err, check.NotNil, check.Commentf(out))
  243. c.Assert(out, check.Not(checker.Contains), "Retrying")
  244. }
  245. func getTestTokenService(status int, body string, retries int) *httptest.Server {
  246. var mu sync.Mutex
  247. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  248. mu.Lock()
  249. if retries > 0 {
  250. w.WriteHeader(http.StatusServiceUnavailable)
  251. w.Header().Set("Content-Type", "application/json")
  252. w.Write([]byte(`{"errors":[{"code":"UNAVAILABLE","message":"cannot create token at this time"}]}`))
  253. retries--
  254. } else {
  255. w.WriteHeader(status)
  256. w.Header().Set("Content-Type", "application/json")
  257. w.Write([]byte(body))
  258. }
  259. mu.Unlock()
  260. }))
  261. }
  262. func (s *DockerRegistryAuthTokenSuite) TestPushTokenServiceUnauthResponse(c *check.C) {
  263. ts := getTestTokenService(http.StatusUnauthorized, `{"errors": [{"Code":"UNAUTHORIZED", "message": "a message", "detail": null}]}`, 0)
  264. defer ts.Close()
  265. s.setupRegistryWithTokenService(c, ts.URL)
  266. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  267. dockerCmd(c, "tag", "busybox", repoName)
  268. out, _, err := dockerCmdWithError("push", repoName)
  269. c.Assert(err, check.NotNil, check.Commentf(out))
  270. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  271. c.Assert(out, checker.Contains, "unauthorized: a message")
  272. }
  273. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnauthorized(c *check.C) {
  274. ts := getTestTokenService(http.StatusUnauthorized, `{"error": "unauthorized"}`, 0)
  275. defer ts.Close()
  276. s.setupRegistryWithTokenService(c, ts.URL)
  277. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  278. dockerCmd(c, "tag", "busybox", repoName)
  279. out, _, err := dockerCmdWithError("push", repoName)
  280. c.Assert(err, check.NotNil, check.Commentf(out))
  281. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  282. split := strings.Split(out, "\n")
  283. c.Assert(split[len(split)-2], check.Equals, "unauthorized: authentication required")
  284. }
  285. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseError(c *check.C) {
  286. ts := getTestTokenService(http.StatusTooManyRequests, `{"errors": [{"code":"TOOMANYREQUESTS","message":"out of tokens"}]}`, 3)
  287. defer ts.Close()
  288. s.setupRegistryWithTokenService(c, ts.URL)
  289. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  290. dockerCmd(c, "tag", "busybox", repoName)
  291. out, _, err := dockerCmdWithError("push", repoName)
  292. c.Assert(err, check.NotNil, check.Commentf(out))
  293. // TODO: isolate test so that it can be guaranteed that the 503 will trigger xfer retries
  294. //c.Assert(out, checker.Contains, "Retrying")
  295. //c.Assert(out, checker.Not(checker.Contains), "Retrying in 15")
  296. split := strings.Split(out, "\n")
  297. c.Assert(split[len(split)-2], check.Equals, "toomanyrequests: out of tokens")
  298. }
  299. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseUnparsable(c *check.C) {
  300. ts := getTestTokenService(http.StatusForbidden, `no way`, 0)
  301. defer ts.Close()
  302. s.setupRegistryWithTokenService(c, ts.URL)
  303. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  304. dockerCmd(c, "tag", "busybox", repoName)
  305. out, _, err := dockerCmdWithError("push", repoName)
  306. c.Assert(err, check.NotNil, check.Commentf(out))
  307. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  308. split := strings.Split(out, "\n")
  309. c.Assert(split[len(split)-2], checker.Contains, "error parsing HTTP 403 response body: ")
  310. }
  311. func (s *DockerRegistryAuthTokenSuite) TestPushMisconfiguredTokenServiceResponseNoToken(c *check.C) {
  312. ts := getTestTokenService(http.StatusOK, `{"something": "wrong"}`, 0)
  313. defer ts.Close()
  314. s.setupRegistryWithTokenService(c, ts.URL)
  315. repoName := fmt.Sprintf("%s/busybox", privateRegistryURL)
  316. dockerCmd(c, "tag", "busybox", repoName)
  317. out, _, err := dockerCmdWithError("push", repoName)
  318. c.Assert(err, check.NotNil, check.Commentf(out))
  319. c.Assert(out, checker.Not(checker.Contains), "Retrying")
  320. split := strings.Split(out, "\n")
  321. c.Assert(split[len(split)-2], check.Equals, "authorization server did not include a token in the response")
  322. }