docker_cli_pull_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "github.com/docker/distribution/digest"
  12. "github.com/docker/docker/pkg/integration/checker"
  13. "github.com/go-check/check"
  14. )
  15. // TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
  16. // prints all expected output.
  17. func (s *DockerHubPullSuite) TestPullFromCentralRegistry(c *check.C) {
  18. testRequires(c, DaemonIsLinux)
  19. out := s.Cmd(c, "pull", "hello-world")
  20. defer deleteImages("hello-world")
  21. c.Assert(out, checker.Contains, "Using default tag: latest", check.Commentf("expected the 'latest' tag to be automatically assumed"))
  22. c.Assert(out, checker.Contains, "Pulling from library/hello-world", check.Commentf("expected the 'library/' prefix to be automatically assumed"))
  23. c.Assert(out, checker.Contains, "Downloaded newer image for hello-world:latest")
  24. matches := regexp.MustCompile(`Digest: (.+)\n`).FindAllStringSubmatch(out, -1)
  25. c.Assert(len(matches), checker.Equals, 1, check.Commentf("expected exactly one image digest in the output"))
  26. c.Assert(len(matches[0]), checker.Equals, 2, check.Commentf("unexpected number of submatches for the digest"))
  27. _, err := digest.ParseDigest(matches[0][1])
  28. c.Check(err, checker.IsNil, check.Commentf("invalid digest %q in output", matches[0][1]))
  29. // We should have a single entry in images.
  30. img := strings.TrimSpace(s.Cmd(c, "images"))
  31. if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
  32. c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
  33. } else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
  34. c.Fatal("invalid output for `docker images` (expected image and tag name")
  35. }
  36. }
  37. // TestPullNonExistingImage pulls non-existing images from the central registry, with different
  38. // combinations of implicit tag and library prefix.
  39. func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
  40. testRequires(c, DaemonIsLinux)
  41. for _, e := range []struct {
  42. Image string
  43. Alias string
  44. }{
  45. {"library/asdfasdf:foobar", "asdfasdf:foobar"},
  46. {"library/asdfasdf:foobar", "library/asdfasdf:foobar"},
  47. {"library/asdfasdf:latest", "asdfasdf"},
  48. {"library/asdfasdf:latest", "asdfasdf:latest"},
  49. {"library/asdfasdf:latest", "library/asdfasdf"},
  50. {"library/asdfasdf:latest", "library/asdfasdf:latest"},
  51. } {
  52. out, err := s.CmdWithError("pull", e.Alias)
  53. c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", out))
  54. c.Assert(out, checker.Contains, fmt.Sprintf("Error: image %s not found", e.Image), check.Commentf("expected image not found error messages"))
  55. }
  56. }
  57. // TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
  58. // that pulling the same image with different combinations of implicit elements of the the image
  59. // reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
  60. // multiple images.
  61. func (s *DockerHubPullSuite) TestPullFromCentralRegistryImplicitRefParts(c *check.C) {
  62. testRequires(c, DaemonIsLinux)
  63. s.Cmd(c, "pull", "hello-world")
  64. defer deleteImages("hello-world")
  65. for _, i := range []string{
  66. "hello-world",
  67. "hello-world:latest",
  68. "library/hello-world",
  69. "library/hello-world:latest",
  70. "docker.io/library/hello-world",
  71. "index.docker.io/library/hello-world",
  72. } {
  73. out := s.Cmd(c, "pull", i)
  74. c.Assert(out, checker.Contains, "Image is up to date for hello-world:latest")
  75. }
  76. // We should have a single entry in images.
  77. img := strings.TrimSpace(s.Cmd(c, "images"))
  78. if splitImg := strings.Split(img, "\n"); len(splitImg) != 2 {
  79. c.Fatalf("expected only two lines in the output of `docker images`, got %d", len(splitImg))
  80. } else if re := regexp.MustCompile(`^hello-world\s+latest`); !re.Match([]byte(splitImg[1])) {
  81. c.Fatal("invalid output for `docker images` (expected image and tag name")
  82. }
  83. }
  84. // TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
  85. func (s *DockerHubPullSuite) TestPullScratchNotAllowed(c *check.C) {
  86. testRequires(c, DaemonIsLinux)
  87. out, err := s.CmdWithError("pull", "scratch")
  88. c.Assert(err, checker.NotNil, check.Commentf("expected pull of scratch to fail"))
  89. c.Assert(out, checker.Contains, "'scratch' is a reserved name")
  90. c.Assert(out, checker.Not(checker.Contains), "Pulling repository scratch")
  91. }
  92. // TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
  93. // results in more images than a naked pull.
  94. func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *check.C) {
  95. testRequires(c, DaemonIsLinux)
  96. s.Cmd(c, "pull", "busybox")
  97. outImageCmd := s.Cmd(c, "images", "busybox")
  98. splitOutImageCmd := strings.Split(strings.TrimSpace(outImageCmd), "\n")
  99. c.Assert(splitOutImageCmd, checker.HasLen, 2, check.Commentf("expected a single entry in images\n%v", outImageCmd))
  100. s.Cmd(c, "pull", "--all-tags=true", "busybox")
  101. outImageAllTagCmd := s.Cmd(c, "images", "busybox")
  102. if linesCount := strings.Count(outImageAllTagCmd, "\n"); linesCount <= 2 {
  103. c.Fatalf("pulling all tags should provide more images, got %d", linesCount-1)
  104. }
  105. // Verify that the line for 'busybox:latest' is left unchanged.
  106. var latestLine string
  107. for _, line := range strings.Split(outImageAllTagCmd, "\n") {
  108. if strings.HasPrefix(line, "busybox") && strings.Contains(line, "latest") {
  109. latestLine = line
  110. break
  111. }
  112. }
  113. c.Assert(latestLine, checker.Not(checker.Equals), "", check.Commentf("no entry for busybox:latest found after pulling all tags"))
  114. splitLatest := strings.Fields(latestLine)
  115. splitCurrent := strings.Fields(splitOutImageCmd[1])
  116. c.Assert(splitLatest, checker.DeepEquals, splitCurrent, check.Commentf("busybox:latest was changed after pulling all tags"))
  117. }
  118. // TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
  119. // still succesfully completes on the daemon side.
  120. //
  121. // Ref: docker/docker#15589
  122. func (s *DockerHubPullSuite) TestPullClientDisconnect(c *check.C) {
  123. testRequires(c, DaemonIsLinux)
  124. repoName := "hello-world:latest"
  125. pullCmd := s.MakeCmd("pull", repoName)
  126. stdout, err := pullCmd.StdoutPipe()
  127. c.Assert(err, checker.IsNil)
  128. err = pullCmd.Start()
  129. c.Assert(err, checker.IsNil)
  130. // Cancel as soon as we get some output.
  131. buf := make([]byte, 10)
  132. _, err = stdout.Read(buf)
  133. c.Assert(err, checker.IsNil)
  134. err = pullCmd.Process.Kill()
  135. c.Assert(err, checker.IsNil)
  136. maxAttempts := 20
  137. for i := 0; ; i++ {
  138. if _, err := s.CmdWithError("inspect", repoName); err == nil {
  139. break
  140. }
  141. if i >= maxAttempts {
  142. c.Fatal("timeout reached: image was not pulled after client disconnected")
  143. }
  144. time.Sleep(500 * time.Millisecond)
  145. }
  146. }
  147. type idAndParent struct {
  148. ID string
  149. Parent string
  150. }
  151. func inspectImage(c *check.C, imageRef string) idAndParent {
  152. out, _ := dockerCmd(c, "inspect", imageRef)
  153. var inspectOutput []idAndParent
  154. err := json.Unmarshal([]byte(out), &inspectOutput)
  155. if err != nil {
  156. c.Fatal(err)
  157. }
  158. return inspectOutput[0]
  159. }
  160. func imageID(c *check.C, imageRef string) string {
  161. return inspectImage(c, imageRef).ID
  162. }
  163. func imageParent(c *check.C, imageRef string) string {
  164. return inspectImage(c, imageRef).Parent
  165. }
  166. // TestPullMigration verifies that pulling an image based on layers
  167. // that already exists locally will reuse those existing layers.
  168. func (s *DockerRegistrySuite) TestPullMigration(c *check.C) {
  169. repoName := privateRegistryURL + "/dockercli/migration"
  170. baseImage := repoName + ":base"
  171. _, err := buildImage(baseImage, fmt.Sprintf(`
  172. FROM scratch
  173. ENV IMAGE base
  174. CMD echo %s
  175. `, baseImage), true)
  176. if err != nil {
  177. c.Fatal(err)
  178. }
  179. baseIDBeforePush := imageID(c, baseImage)
  180. baseParentBeforePush := imageParent(c, baseImage)
  181. derivedImage := repoName + ":derived"
  182. _, err = buildImage(derivedImage, fmt.Sprintf(`
  183. FROM %s
  184. CMD echo %s
  185. `, baseImage, derivedImage), true)
  186. if err != nil {
  187. c.Fatal(err)
  188. }
  189. derivedIDBeforePush := imageID(c, derivedImage)
  190. dockerCmd(c, "push", derivedImage)
  191. // Remove derived image from the local store
  192. dockerCmd(c, "rmi", derivedImage)
  193. // Repull
  194. dockerCmd(c, "pull", derivedImage)
  195. // Check that the parent of this pulled image is the original base
  196. // image
  197. derivedIDAfterPull1 := imageID(c, derivedImage)
  198. derivedParentAfterPull1 := imageParent(c, derivedImage)
  199. if derivedIDAfterPull1 == derivedIDBeforePush {
  200. c.Fatal("image's ID should have changed on after deleting and pulling")
  201. }
  202. if derivedParentAfterPull1 != baseIDBeforePush {
  203. c.Fatalf("pulled image's parent ID (%s) does not match base image's ID (%s)", derivedParentAfterPull1, baseIDBeforePush)
  204. }
  205. // Confirm that repushing and repulling does not change the computed ID
  206. dockerCmd(c, "push", derivedImage)
  207. dockerCmd(c, "rmi", derivedImage)
  208. dockerCmd(c, "pull", derivedImage)
  209. derivedIDAfterPull2 := imageID(c, derivedImage)
  210. derivedParentAfterPull2 := imageParent(c, derivedImage)
  211. if derivedIDAfterPull2 != derivedIDAfterPull1 {
  212. c.Fatal("image's ID unexpectedly changed after a repush/repull")
  213. }
  214. if derivedParentAfterPull2 != baseIDBeforePush {
  215. c.Fatalf("pulled image's parent ID (%s) does not match base image's ID (%s)", derivedParentAfterPull2, baseIDBeforePush)
  216. }
  217. // Remove everything, repull, and make sure everything uses computed IDs
  218. dockerCmd(c, "rmi", baseImage, derivedImage)
  219. dockerCmd(c, "pull", derivedImage)
  220. derivedIDAfterPull3 := imageID(c, derivedImage)
  221. derivedParentAfterPull3 := imageParent(c, derivedImage)
  222. derivedGrandparentAfterPull3 := imageParent(c, derivedParentAfterPull3)
  223. if derivedIDAfterPull3 != derivedIDAfterPull1 {
  224. c.Fatal("image's ID unexpectedly changed after a second repull")
  225. }
  226. if derivedParentAfterPull3 == baseIDBeforePush {
  227. c.Fatalf("pulled image's parent ID (%s) should not match base image's original ID (%s)", derivedParentAfterPull3, derivedIDBeforePush)
  228. }
  229. if derivedGrandparentAfterPull3 == baseParentBeforePush {
  230. c.Fatal("base image's parent ID should have been rewritten on pull")
  231. }
  232. }
  233. // TestPullMigrationRun verifies that pulling an image based on layers
  234. // that already exists locally will result in an image that runs properly.
  235. func (s *DockerRegistrySuite) TestPullMigrationRun(c *check.C) {
  236. type idAndParent struct {
  237. ID string
  238. Parent string
  239. }
  240. derivedImage := privateRegistryURL + "/dockercli/migration-run"
  241. baseImage := "busybox"
  242. _, err := buildImage(derivedImage, fmt.Sprintf(`
  243. FROM %s
  244. RUN dd if=/dev/zero of=/file bs=1024 count=1024
  245. CMD echo %s
  246. `, baseImage, derivedImage), true)
  247. if err != nil {
  248. c.Fatal(err)
  249. }
  250. baseIDBeforePush := imageID(c, baseImage)
  251. derivedIDBeforePush := imageID(c, derivedImage)
  252. dockerCmd(c, "push", derivedImage)
  253. // Remove derived image from the local store
  254. dockerCmd(c, "rmi", derivedImage)
  255. // Repull
  256. dockerCmd(c, "pull", derivedImage)
  257. // Check that this pulled image is based on the original base image
  258. derivedIDAfterPull1 := imageID(c, derivedImage)
  259. derivedParentAfterPull1 := imageParent(c, imageParent(c, derivedImage))
  260. if derivedIDAfterPull1 == derivedIDBeforePush {
  261. c.Fatal("image's ID should have changed on after deleting and pulling")
  262. }
  263. if derivedParentAfterPull1 != baseIDBeforePush {
  264. c.Fatalf("pulled image's parent ID (%s) does not match base image's ID (%s)", derivedParentAfterPull1, baseIDBeforePush)
  265. }
  266. // Make sure the image runs correctly
  267. out, _ := dockerCmd(c, "run", "--rm", derivedImage)
  268. if strings.TrimSpace(out) != derivedImage {
  269. c.Fatalf("expected %s; got %s", derivedImage, out)
  270. }
  271. // Confirm that repushing and repulling does not change the computed ID
  272. dockerCmd(c, "push", derivedImage)
  273. dockerCmd(c, "rmi", derivedImage)
  274. dockerCmd(c, "pull", derivedImage)
  275. derivedIDAfterPull2 := imageID(c, derivedImage)
  276. derivedParentAfterPull2 := imageParent(c, imageParent(c, derivedImage))
  277. if derivedIDAfterPull2 != derivedIDAfterPull1 {
  278. c.Fatal("image's ID unexpectedly changed after a repush/repull")
  279. }
  280. if derivedParentAfterPull2 != baseIDBeforePush {
  281. c.Fatalf("pulled image's parent ID (%s) does not match base image's ID (%s)", derivedParentAfterPull2, baseIDBeforePush)
  282. }
  283. // Make sure the image still runs
  284. out, _ = dockerCmd(c, "run", "--rm", derivedImage)
  285. if strings.TrimSpace(out) != derivedImage {
  286. c.Fatalf("expected %s; got %s", derivedImage, out)
  287. }
  288. }
  289. // TestPullConflict provides coverage of the situation where a computed
  290. // strongID conflicts with some unverifiable data in the graph.
  291. func (s *DockerRegistrySuite) TestPullConflict(c *check.C) {
  292. repoName := privateRegistryURL + "/dockercli/conflict"
  293. _, err := buildImage(repoName, `
  294. FROM scratch
  295. ENV IMAGE conflict
  296. CMD echo conflict
  297. `, true)
  298. if err != nil {
  299. c.Fatal(err)
  300. }
  301. dockerCmd(c, "push", repoName)
  302. // Pull to make it content-addressable
  303. dockerCmd(c, "rmi", repoName)
  304. dockerCmd(c, "pull", repoName)
  305. IDBeforeLoad := imageID(c, repoName)
  306. // Load/save to turn this into an unverified image with the same ID
  307. tmpDir, err := ioutil.TempDir("", "conflict-save-output")
  308. if err != nil {
  309. c.Errorf("failed to create temporary directory: %s", err)
  310. }
  311. defer os.RemoveAll(tmpDir)
  312. tarFile := filepath.Join(tmpDir, "repo.tar")
  313. dockerCmd(c, "save", "-o", tarFile, repoName)
  314. dockerCmd(c, "rmi", repoName)
  315. dockerCmd(c, "load", "-i", tarFile)
  316. // Check that the the ID is the same after save/load.
  317. IDAfterLoad := imageID(c, repoName)
  318. if IDAfterLoad != IDBeforeLoad {
  319. c.Fatal("image's ID should be the same after save/load")
  320. }
  321. // Repull
  322. dockerCmd(c, "pull", repoName)
  323. // Check that the ID is now different because of the conflict.
  324. IDAfterPull1 := imageID(c, repoName)
  325. // Expect the new ID to be SHA256(oldID)
  326. expectedIDDigest, err := digest.FromBytes([]byte(IDBeforeLoad))
  327. if err != nil {
  328. c.Fatalf("digest error: %v", err)
  329. }
  330. expectedID := expectedIDDigest.Hex()
  331. if IDAfterPull1 != expectedID {
  332. c.Fatalf("image's ID should have changed on pull to %s (got %s)", expectedID, IDAfterPull1)
  333. }
  334. // A second pull should use the new ID again.
  335. dockerCmd(c, "pull", repoName)
  336. IDAfterPull2 := imageID(c, repoName)
  337. if IDAfterPull2 != IDAfterPull1 {
  338. c.Fatal("image's ID unexpectedly changed after a repull")
  339. }
  340. }