docker_cli_push_test.go 12 KB

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