docker_cli_pull_local_test.go 15 KB

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