docker_cli_push_test.go 12 KB

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