docker_cli_push_test.go 14 KB

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