docker_cli_push_test.go 14 KB

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