docker_cli_pull_local_test.go 14 KB

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