docker_cli_pull_local_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/go-check/check"
  8. )
  9. // testPullImageWithAliases pulls a specific image tag and verifies that any aliases (i.e., other
  10. // tags for the same image) are not also pulled down.
  11. //
  12. // Ref: docker/docker#8141
  13. func testPullImageWithAliases(c *check.C) {
  14. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  15. repos := []string{}
  16. for _, tag := range []string{"recent", "fresh"} {
  17. repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
  18. }
  19. // Tag and push the same image multiple times.
  20. for _, repo := range repos {
  21. dockerCmd(c, "tag", "busybox", repo)
  22. dockerCmd(c, "push", repo)
  23. }
  24. // Clear local images store.
  25. args := append([]string{"rmi"}, repos...)
  26. dockerCmd(c, args...)
  27. // Pull a single tag and verify it doesn't bring down all aliases.
  28. dockerCmd(c, "pull", repos[0])
  29. dockerCmd(c, "inspect", repos[0])
  30. for _, repo := range repos[1:] {
  31. _, _, err := dockerCmdWithError("inspect", repo)
  32. c.Assert(err, checker.NotNil, check.Commentf("Image %v shouldn't have been pulled down", repo))
  33. }
  34. }
  35. func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
  36. testPullImageWithAliases(c)
  37. }
  38. func (s *DockerSchema1RegistrySuite) TestPullImageWithAliases(c *check.C) {
  39. testPullImageWithAliases(c)
  40. }
  41. // testConcurrentPullWholeRepo pulls the same repo concurrently.
  42. func testConcurrentPullWholeRepo(c *check.C) {
  43. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  44. repos := []string{}
  45. for _, tag := range []string{"recent", "fresh", "todays"} {
  46. repo := fmt.Sprintf("%v:%v", repoName, tag)
  47. _, err := buildImage(repo, fmt.Sprintf(`
  48. FROM busybox
  49. ENTRYPOINT ["/bin/echo"]
  50. ENV FOO foo
  51. ENV BAR bar
  52. CMD echo %s
  53. `, repo), true)
  54. c.Assert(err, checker.IsNil)
  55. dockerCmd(c, "push", repo)
  56. repos = append(repos, repo)
  57. }
  58. // Clear local images store.
  59. args := append([]string{"rmi"}, repos...)
  60. dockerCmd(c, args...)
  61. // Run multiple re-pulls concurrently
  62. results := make(chan error)
  63. numPulls := 3
  64. for i := 0; i != numPulls; i++ {
  65. go func() {
  66. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "pull", "-a", repoName))
  67. results <- err
  68. }()
  69. }
  70. // These checks are separate from the loop above because the check
  71. // package is not goroutine-safe.
  72. for i := 0; i != numPulls; i++ {
  73. err := <-results
  74. c.Assert(err, checker.IsNil, check.Commentf("concurrent pull failed with error: %v", err))
  75. }
  76. // Ensure all tags were pulled successfully
  77. for _, repo := range repos {
  78. dockerCmd(c, "inspect", repo)
  79. out, _ := dockerCmd(c, "run", "--rm", repo)
  80. c.Assert(strings.TrimSpace(out), checker.Equals, "/bin/sh -c echo "+repo)
  81. }
  82. }
  83. func (s *DockerRegistrySuite) testConcurrentPullWholeRepo(c *check.C) {
  84. testConcurrentPullWholeRepo(c)
  85. }
  86. func (s *DockerSchema1RegistrySuite) testConcurrentPullWholeRepo(c *check.C) {
  87. testConcurrentPullWholeRepo(c)
  88. }
  89. // testConcurrentFailingPull tries a concurrent pull that doesn't succeed.
  90. func testConcurrentFailingPull(c *check.C) {
  91. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  92. // Run multiple pulls concurrently
  93. results := make(chan error)
  94. numPulls := 3
  95. for i := 0; i != numPulls; i++ {
  96. go func() {
  97. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "pull", repoName+":asdfasdf"))
  98. results <- err
  99. }()
  100. }
  101. // These checks are separate from the loop above because the check
  102. // package is not goroutine-safe.
  103. for i := 0; i != numPulls; i++ {
  104. err := <-results
  105. c.Assert(err, checker.NotNil, check.Commentf("expected pull to fail"))
  106. }
  107. }
  108. func (s *DockerRegistrySuite) testConcurrentFailingPull(c *check.C) {
  109. testConcurrentFailingPull(c)
  110. }
  111. func (s *DockerSchema1RegistrySuite) testConcurrentFailingPull(c *check.C) {
  112. testConcurrentFailingPull(c)
  113. }
  114. // testConcurrentPullMultipleTags pulls multiple tags from the same repo
  115. // concurrently.
  116. func testConcurrentPullMultipleTags(c *check.C) {
  117. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  118. repos := []string{}
  119. for _, tag := range []string{"recent", "fresh", "todays"} {
  120. repo := fmt.Sprintf("%v:%v", repoName, tag)
  121. _, err := buildImage(repo, fmt.Sprintf(`
  122. FROM busybox
  123. ENTRYPOINT ["/bin/echo"]
  124. ENV FOO foo
  125. ENV BAR bar
  126. CMD echo %s
  127. `, repo), true)
  128. c.Assert(err, checker.IsNil)
  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. _, _, err := runCommandWithOutput(exec.Command(dockerBinary, "pull", repo))
  140. results <- err
  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. func (s *DockerSchema1RegistrySuite) TestConcurrentPullMultipleTags(c *check.C) {
  160. testConcurrentPullMultipleTags(c)
  161. }
  162. // testPullIDStability verifies that pushing an image and pulling it back
  163. // preserves the image ID.
  164. func testPullIDStability(c *check.C) {
  165. derivedImage := privateRegistryURL + "/dockercli/id-stability"
  166. baseImage := "busybox"
  167. _, err := buildImage(derivedImage, fmt.Sprintf(`
  168. FROM %s
  169. ENV derived true
  170. ENV asdf true
  171. RUN dd if=/dev/zero of=/file bs=1024 count=1024
  172. CMD echo %s
  173. `, baseImage, derivedImage), true)
  174. if err != nil {
  175. c.Fatal(err)
  176. }
  177. originalID, err := getIDByName(derivedImage)
  178. if err != nil {
  179. c.Fatalf("error inspecting: %v", err)
  180. }
  181. dockerCmd(c, "push", derivedImage)
  182. // Pull
  183. out, _ := dockerCmd(c, "pull", derivedImage)
  184. if strings.Contains(out, "Pull complete") {
  185. c.Fatalf("repull redownloaded a layer: %s", out)
  186. }
  187. derivedIDAfterPull, err := getIDByName(derivedImage)
  188. if err != nil {
  189. c.Fatalf("error inspecting: %v", err)
  190. }
  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, err = getIDByName(derivedImage)
  204. if err != nil {
  205. c.Fatalf("error inspecting: %v", err)
  206. }
  207. if derivedIDAfterPull != originalID {
  208. c.Fatal("image's ID unexpectedly changed after a repush/repull")
  209. }
  210. if err != nil {
  211. c.Fatalf("error inspecting: %v", err)
  212. }
  213. // Make sure the image still runs
  214. out, _ = dockerCmd(c, "run", "--rm", derivedImage)
  215. if strings.TrimSpace(out) != derivedImage {
  216. c.Fatalf("expected %s; got %s", derivedImage, out)
  217. }
  218. }
  219. func (s *DockerRegistrySuite) TestPullIDStability(c *check.C) {
  220. testPullIDStability(c)
  221. }
  222. func (s *DockerSchema1RegistrySuite) TestPullIDStability(c *check.C) {
  223. testPullIDStability(c)
  224. }
  225. // TestPullFallbackOn404 tries to pull a nonexistent manifest and confirms that
  226. // the pull falls back to the v1 protocol.
  227. //
  228. // Ref: docker/docker#18832
  229. func (s *DockerRegistrySuite) TestPullFallbackOn404(c *check.C) {
  230. repoName := fmt.Sprintf("%v/does/not/exist", privateRegistryURL)
  231. out, _, _ := dockerCmdWithError("pull", repoName)
  232. c.Assert(out, checker.Contains, "v1 ping attempt")
  233. }