docker_cli_pull_local_test.go 15 KB

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