docker_cli_pull_local_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "github.com/docker/distribution"
  11. "github.com/docker/distribution/manifest"
  12. "github.com/docker/distribution/manifest/manifestlist"
  13. "github.com/docker/distribution/manifest/schema2"
  14. "github.com/docker/docker/integration-cli/checker"
  15. "github.com/docker/docker/integration-cli/cli/build"
  16. "github.com/go-check/check"
  17. "github.com/opencontainers/go-digest"
  18. "gotest.tools/assert"
  19. "gotest.tools/icmd"
  20. )
  21. // testPullImageWithAliases pulls a specific image tag and verifies that any aliases (i.e., other
  22. // tags for the same image) are not also pulled down.
  23. //
  24. // Ref: docker/docker#8141
  25. func testPullImageWithAliases(c *check.C) {
  26. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  27. var repos []string
  28. for _, tag := range []string{"recent", "fresh"} {
  29. repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
  30. }
  31. // Tag and push the same image multiple times.
  32. for _, repo := range repos {
  33. dockerCmd(c, "tag", "busybox", repo)
  34. dockerCmd(c, "push", repo)
  35. }
  36. // Clear local images store.
  37. args := append([]string{"rmi"}, repos...)
  38. dockerCmd(c, args...)
  39. // Pull a single tag and verify it doesn't bring down all aliases.
  40. dockerCmd(c, "pull", repos[0])
  41. dockerCmd(c, "inspect", repos[0])
  42. for _, repo := range repos[1:] {
  43. _, _, err := dockerCmdWithError("inspect", repo)
  44. assert.ErrorContains(c, err, "", "Image %v shouldn't have been pulled down", repo)
  45. }
  46. }
  47. func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
  48. testPullImageWithAliases(c)
  49. }
  50. func (s *DockerSchema1RegistrySuite) TestPullImageWithAliases(c *check.C) {
  51. testPullImageWithAliases(c)
  52. }
  53. // testConcurrentPullWholeRepo pulls the same repo concurrently.
  54. func testConcurrentPullWholeRepo(c *check.C) {
  55. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  56. var repos []string
  57. for _, tag := range []string{"recent", "fresh", "todays"} {
  58. repo := fmt.Sprintf("%v:%v", repoName, tag)
  59. buildImageSuccessfully(c, repo, build.WithDockerfile(fmt.Sprintf(`
  60. FROM busybox
  61. ENTRYPOINT ["/bin/echo"]
  62. ENV FOO foo
  63. ENV BAR bar
  64. CMD echo %s
  65. `, repo)))
  66. dockerCmd(c, "push", repo)
  67. repos = append(repos, repo)
  68. }
  69. // Clear local images store.
  70. args := append([]string{"rmi"}, repos...)
  71. dockerCmd(c, args...)
  72. // Run multiple re-pulls concurrently
  73. results := make(chan error)
  74. numPulls := 3
  75. for i := 0; i != numPulls; i++ {
  76. go func() {
  77. result := icmd.RunCommand(dockerBinary, "pull", "-a", repoName)
  78. results <- result.Error
  79. }()
  80. }
  81. // These checks are separate from the loop above because the check
  82. // package is not goroutine-safe.
  83. for i := 0; i != numPulls; i++ {
  84. err := <-results
  85. assert.NilError(c, err, "concurrent pull failed with error: %v", err)
  86. }
  87. // Ensure all tags were pulled successfully
  88. for _, repo := range repos {
  89. dockerCmd(c, "inspect", repo)
  90. out, _ := dockerCmd(c, "run", "--rm", repo)
  91. assert.Equal(c, strings.TrimSpace(out), "/bin/sh -c echo "+repo)
  92. }
  93. }
  94. func (s *DockerRegistrySuite) testConcurrentPullWholeRepo(c *check.C) {
  95. testConcurrentPullWholeRepo(c)
  96. }
  97. func (s *DockerSchema1RegistrySuite) testConcurrentPullWholeRepo(c *check.C) {
  98. testConcurrentPullWholeRepo(c)
  99. }
  100. // testConcurrentFailingPull tries a concurrent pull that doesn't succeed.
  101. func testConcurrentFailingPull(c *check.C) {
  102. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  103. // Run multiple pulls concurrently
  104. results := make(chan error)
  105. numPulls := 3
  106. for i := 0; i != numPulls; i++ {
  107. go func() {
  108. result := icmd.RunCommand(dockerBinary, "pull", repoName+":asdfasdf")
  109. results <- result.Error
  110. }()
  111. }
  112. // These checks are separate from the loop above because the check
  113. // package is not goroutine-safe.
  114. for i := 0; i != numPulls; i++ {
  115. err := <-results
  116. assert.ErrorContains(c, err, "", "expected pull to fail")
  117. }
  118. }
  119. func (s *DockerRegistrySuite) testConcurrentFailingPull(c *check.C) {
  120. testConcurrentFailingPull(c)
  121. }
  122. func (s *DockerSchema1RegistrySuite) testConcurrentFailingPull(c *check.C) {
  123. testConcurrentFailingPull(c)
  124. }
  125. // testConcurrentPullMultipleTags pulls multiple tags from the same repo
  126. // concurrently.
  127. func testConcurrentPullMultipleTags(c *check.C) {
  128. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  129. var repos []string
  130. for _, tag := range []string{"recent", "fresh", "todays"} {
  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. dockerCmd(c, "push", repo)
  140. repos = append(repos, repo)
  141. }
  142. // Clear local images store.
  143. args := append([]string{"rmi"}, repos...)
  144. dockerCmd(c, args...)
  145. // Re-pull individual tags, in parallel
  146. results := make(chan error)
  147. for _, repo := range repos {
  148. go func(repo string) {
  149. result := icmd.RunCommand(dockerBinary, "pull", repo)
  150. results <- result.Error
  151. }(repo)
  152. }
  153. // These checks are separate from the loop above because the check
  154. // package is not goroutine-safe.
  155. for range repos {
  156. err := <-results
  157. assert.NilError(c, err, "concurrent pull failed with error: %v", err)
  158. }
  159. // Ensure all tags were pulled successfully
  160. for _, repo := range repos {
  161. dockerCmd(c, "inspect", repo)
  162. out, _ := dockerCmd(c, "run", "--rm", repo)
  163. assert.Equal(c, strings.TrimSpace(out), "/bin/sh -c echo "+repo)
  164. }
  165. }
  166. func (s *DockerRegistrySuite) TestConcurrentPullMultipleTags(c *check.C) {
  167. testConcurrentPullMultipleTags(c)
  168. }
  169. func (s *DockerSchema1RegistrySuite) TestConcurrentPullMultipleTags(c *check.C) {
  170. testConcurrentPullMultipleTags(c)
  171. }
  172. // testPullIDStability verifies that pushing an image and pulling it back
  173. // preserves the image ID.
  174. func testPullIDStability(c *check.C) {
  175. derivedImage := privateRegistryURL + "/dockercli/id-stability"
  176. baseImage := "busybox"
  177. buildImageSuccessfully(c, derivedImage, build.WithDockerfile(fmt.Sprintf(`
  178. FROM %s
  179. ENV derived true
  180. ENV asdf true
  181. RUN dd if=/dev/zero of=/file bs=1024 count=1024
  182. CMD echo %s
  183. `, baseImage, derivedImage)))
  184. originalID := getIDByName(c, derivedImage)
  185. dockerCmd(c, "push", derivedImage)
  186. // Pull
  187. out, _ := dockerCmd(c, "pull", derivedImage)
  188. if strings.Contains(out, "Pull complete") {
  189. c.Fatalf("repull redownloaded a layer: %s", out)
  190. }
  191. derivedIDAfterPull := getIDByName(c, derivedImage)
  192. if derivedIDAfterPull != originalID {
  193. c.Fatal("image's ID unexpectedly changed after a repush/repull")
  194. }
  195. // Make sure the image runs correctly
  196. out, _ = dockerCmd(c, "run", "--rm", derivedImage)
  197. if strings.TrimSpace(out) != derivedImage {
  198. c.Fatalf("expected %s; got %s", derivedImage, out)
  199. }
  200. // Confirm that repushing and repulling does not change the computed ID
  201. dockerCmd(c, "push", derivedImage)
  202. dockerCmd(c, "rmi", derivedImage)
  203. dockerCmd(c, "pull", derivedImage)
  204. derivedIDAfterPull = getIDByName(c, derivedImage)
  205. if derivedIDAfterPull != originalID {
  206. c.Fatal("image's ID unexpectedly changed after a repush/repull")
  207. }
  208. // Make sure the image still runs
  209. out, _ = dockerCmd(c, "run", "--rm", derivedImage)
  210. if strings.TrimSpace(out) != derivedImage {
  211. c.Fatalf("expected %s; got %s", derivedImage, out)
  212. }
  213. }
  214. func (s *DockerRegistrySuite) TestPullIDStability(c *check.C) {
  215. testPullIDStability(c)
  216. }
  217. func (s *DockerSchema1RegistrySuite) TestPullIDStability(c *check.C) {
  218. testPullIDStability(c)
  219. }
  220. // #21213
  221. func testPullNoLayers(c *check.C) {
  222. repoName := fmt.Sprintf("%v/dockercli/scratch", privateRegistryURL)
  223. buildImageSuccessfully(c, repoName, build.WithDockerfile(`
  224. FROM scratch
  225. ENV foo bar`))
  226. dockerCmd(c, "push", repoName)
  227. dockerCmd(c, "rmi", repoName)
  228. dockerCmd(c, "pull", repoName)
  229. }
  230. func (s *DockerRegistrySuite) TestPullNoLayers(c *check.C) {
  231. testPullNoLayers(c)
  232. }
  233. func (s *DockerSchema1RegistrySuite) TestPullNoLayers(c *check.C) {
  234. testPullNoLayers(c)
  235. }
  236. func (s *DockerRegistrySuite) TestPullManifestList(c *check.C) {
  237. testRequires(c, NotArm)
  238. pushDigest, err := setupImage(c)
  239. assert.NilError(c, err, "error setting up image")
  240. // Inject a manifest list into the registry
  241. manifestList := &manifestlist.ManifestList{
  242. Versioned: manifest.Versioned{
  243. SchemaVersion: 2,
  244. MediaType: manifestlist.MediaTypeManifestList,
  245. },
  246. Manifests: []manifestlist.ManifestDescriptor{
  247. {
  248. Descriptor: distribution.Descriptor{
  249. Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
  250. Size: 3253,
  251. MediaType: schema2.MediaTypeManifest,
  252. },
  253. Platform: manifestlist.PlatformSpec{
  254. Architecture: "bogus_arch",
  255. OS: "bogus_os",
  256. },
  257. },
  258. {
  259. Descriptor: distribution.Descriptor{
  260. Digest: pushDigest,
  261. Size: 3253,
  262. MediaType: schema2.MediaTypeManifest,
  263. },
  264. Platform: manifestlist.PlatformSpec{
  265. Architecture: runtime.GOARCH,
  266. OS: runtime.GOOS,
  267. },
  268. },
  269. },
  270. }
  271. manifestListJSON, err := json.MarshalIndent(manifestList, "", " ")
  272. assert.NilError(c, err, "error marshalling manifest list")
  273. manifestListDigest := digest.FromBytes(manifestListJSON)
  274. hexDigest := manifestListDigest.Hex()
  275. registryV2Path := s.reg.Path()
  276. // Write manifest list to blob store
  277. blobDir := filepath.Join(registryV2Path, "blobs", "sha256", hexDigest[:2], hexDigest)
  278. err = os.MkdirAll(blobDir, 0755)
  279. assert.NilError(c, err, "error creating blob dir")
  280. blobPath := filepath.Join(blobDir, "data")
  281. err = ioutil.WriteFile(blobPath, []byte(manifestListJSON), 0644)
  282. assert.NilError(c, err, "error writing manifest list")
  283. // Add to revision store
  284. revisionDir := filepath.Join(registryV2Path, "repositories", remoteRepoName, "_manifests", "revisions", "sha256", hexDigest)
  285. err = os.Mkdir(revisionDir, 0755)
  286. c.Assert(err, checker.IsNil, check.Commentf("error creating revision dir"))
  287. revisionPath := filepath.Join(revisionDir, "link")
  288. err = ioutil.WriteFile(revisionPath, []byte(manifestListDigest.String()), 0644)
  289. c.Assert(err, checker.IsNil, check.Commentf("error writing revision link"))
  290. // Update tag
  291. tagPath := filepath.Join(registryV2Path, "repositories", remoteRepoName, "_manifests", "tags", "latest", "current", "link")
  292. err = ioutil.WriteFile(tagPath, []byte(manifestListDigest.String()), 0644)
  293. assert.NilError(c, err, "error writing tag link")
  294. // Verify that the image can be pulled through the manifest list.
  295. out, _ := dockerCmd(c, "pull", repoName)
  296. // The pull output includes "Digest: <digest>", so find that
  297. matches := digestRegex.FindStringSubmatch(out)
  298. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  299. pullDigest := matches[1]
  300. // Make sure the pushed and pull digests match
  301. assert.Equal(c, manifestListDigest.String(), pullDigest)
  302. // Was the image actually created?
  303. dockerCmd(c, "inspect", repoName)
  304. dockerCmd(c, "rmi", repoName)
  305. }
  306. // #23100
  307. func (s *DockerRegistryAuthHtpasswdSuite) TestPullWithExternalAuthLoginWithScheme(c *check.C) {
  308. osPath := os.Getenv("PATH")
  309. defer os.Setenv("PATH", osPath)
  310. workingDir, err := os.Getwd()
  311. assert.NilError(c, err)
  312. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  313. assert.NilError(c, err)
  314. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  315. os.Setenv("PATH", testPath)
  316. repoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
  317. tmp, err := ioutil.TempDir("", "integration-cli-")
  318. assert.NilError(c, err)
  319. externalAuthConfig := `{ "credsStore": "shell-test" }`
  320. configPath := filepath.Join(tmp, "config.json")
  321. err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
  322. assert.NilError(c, err)
  323. dockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
  324. b, err := ioutil.ReadFile(configPath)
  325. assert.NilError(c, err)
  326. c.Assert(string(b), checker.Not(checker.Contains), "\"auth\":")
  327. dockerCmd(c, "--config", tmp, "tag", "busybox", repoName)
  328. dockerCmd(c, "--config", tmp, "push", repoName)
  329. dockerCmd(c, "--config", tmp, "logout", privateRegistryURL)
  330. dockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), "https://"+privateRegistryURL)
  331. dockerCmd(c, "--config", tmp, "pull", repoName)
  332. // likewise push should work
  333. repoName2 := fmt.Sprintf("%v/dockercli/busybox:nocreds", privateRegistryURL)
  334. dockerCmd(c, "tag", repoName, repoName2)
  335. dockerCmd(c, "--config", tmp, "push", repoName2)
  336. // logout should work w scheme also because it will be stripped
  337. dockerCmd(c, "--config", tmp, "logout", "https://"+privateRegistryURL)
  338. }
  339. func (s *DockerRegistryAuthHtpasswdSuite) TestPullWithExternalAuth(c *check.C) {
  340. osPath := os.Getenv("PATH")
  341. defer os.Setenv("PATH", osPath)
  342. workingDir, err := os.Getwd()
  343. assert.NilError(c, err)
  344. absolute, err := filepath.Abs(filepath.Join(workingDir, "fixtures", "auth"))
  345. assert.NilError(c, err)
  346. testPath := fmt.Sprintf("%s%c%s", osPath, filepath.ListSeparator, absolute)
  347. os.Setenv("PATH", testPath)
  348. repoName := fmt.Sprintf("%v/dockercli/busybox:authtest", privateRegistryURL)
  349. tmp, err := ioutil.TempDir("", "integration-cli-")
  350. assert.NilError(c, err)
  351. externalAuthConfig := `{ "credsStore": "shell-test" }`
  352. configPath := filepath.Join(tmp, "config.json")
  353. err = ioutil.WriteFile(configPath, []byte(externalAuthConfig), 0644)
  354. assert.NilError(c, err)
  355. dockerCmd(c, "--config", tmp, "login", "-u", s.reg.Username(), "-p", s.reg.Password(), privateRegistryURL)
  356. b, err := ioutil.ReadFile(configPath)
  357. assert.NilError(c, err)
  358. c.Assert(string(b), checker.Not(checker.Contains), "\"auth\":")
  359. dockerCmd(c, "--config", tmp, "tag", "busybox", repoName)
  360. dockerCmd(c, "--config", tmp, "push", repoName)
  361. dockerCmd(c, "--config", tmp, "pull", repoName)
  362. }
  363. // TestRunImplicitPullWithNoTag should pull implicitly only the default tag (latest)
  364. func (s *DockerRegistrySuite) TestRunImplicitPullWithNoTag(c *check.C) {
  365. testRequires(c, DaemonIsLinux)
  366. repo := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  367. repoTag1 := fmt.Sprintf("%v:latest", repo)
  368. repoTag2 := fmt.Sprintf("%v:t1", repo)
  369. // tag the image and upload it to the private registry
  370. dockerCmd(c, "tag", "busybox", repoTag1)
  371. dockerCmd(c, "tag", "busybox", repoTag2)
  372. dockerCmd(c, "push", repo)
  373. dockerCmd(c, "rmi", repoTag1)
  374. dockerCmd(c, "rmi", repoTag2)
  375. out, _ := dockerCmd(c, "run", repo)
  376. c.Assert(out, checker.Contains, fmt.Sprintf("Unable to find image '%s:latest' locally", repo))
  377. // There should be only one line for repo, the one with repo:latest
  378. outImageCmd, _ := dockerCmd(c, "images", repo)
  379. splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
  380. c.Assert(splitOutImageCmd, checker.HasLen, 2)
  381. }