docker_cli_pull_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/go-check/check"
  8. )
  9. // See issue docker/docker#8141
  10. func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
  11. repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
  12. repos := []string{}
  13. for _, tag := range []string{"recent", "fresh"} {
  14. repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
  15. }
  16. // Tag and push the same image multiple times.
  17. for _, repo := range repos {
  18. dockerCmd(c, "tag", "busybox", repo)
  19. dockerCmd(c, "push", repo)
  20. }
  21. // Clear local images store.
  22. args := append([]string{"rmi"}, repos...)
  23. dockerCmd(c, args...)
  24. // Pull a single tag and verify it doesn't bring down all aliases.
  25. dockerCmd(c, "pull", repos[0])
  26. dockerCmd(c, "inspect", repos[0])
  27. for _, repo := range repos[1:] {
  28. if _, _, err := dockerCmdWithError(c, "inspect", repo); err == nil {
  29. c.Fatalf("Image %v shouldn't have been pulled down", repo)
  30. }
  31. }
  32. }
  33. // pulling library/hello-world should show verified message
  34. func (s *DockerSuite) TestPullVerified(c *check.C) {
  35. c.Skip("Skipping hub dependent test")
  36. // Image must be pulled from central repository to get verified message
  37. // unless keychain is manually updated to contain the daemon's sign key.
  38. verifiedName := "hello-world"
  39. // pull it
  40. expected := "The image you are pulling has been verified"
  41. if out, exitCode, err := dockerCmdWithError(c, "pull", verifiedName); err != nil || !strings.Contains(out, expected) {
  42. if err != nil || exitCode != 0 {
  43. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  44. }
  45. c.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
  46. }
  47. // pull it again
  48. if out, exitCode, err := dockerCmdWithError(c, "pull", verifiedName); err != nil || strings.Contains(out, expected) {
  49. if err != nil || exitCode != 0 {
  50. c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
  51. }
  52. c.Fatalf("pulling a verified image failed. unexpected verify message\ngot: %s, %v", out, err)
  53. }
  54. }
  55. // pulling an image from the central registry should work
  56. func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
  57. testRequires(c, Network)
  58. dockerCmd(c, "pull", "hello-world")
  59. }
  60. // pulling a non-existing image from the central registry should return a non-zero exit code
  61. func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
  62. testRequires(c, Network)
  63. name := "sadfsadfasdf"
  64. out, _, err := dockerCmdWithError(c, "pull", name)
  65. if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
  66. c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
  67. }
  68. }
  69. // pulling an image from the central registry using official names should work
  70. // ensure all pulls result in the same image
  71. func (s *DockerSuite) TestPullImageOfficialNames(c *check.C) {
  72. testRequires(c, Network)
  73. names := []string{
  74. "library/hello-world",
  75. "docker.io/library/hello-world",
  76. "index.docker.io/library/hello-world",
  77. }
  78. for _, name := range names {
  79. out, exitCode, err := dockerCmdWithError(c, "pull", name)
  80. if err != nil || exitCode != 0 {
  81. c.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
  82. continue
  83. }
  84. // ensure we don't have multiple image names.
  85. out, _ = dockerCmd(c, "images")
  86. if strings.Contains(out, name) {
  87. c.Errorf("images should not have listed '%s'", name)
  88. }
  89. }
  90. }
  91. func (s *DockerSuite) TestPullScratchNotAllowed(c *check.C) {
  92. testRequires(c, Network)
  93. out, exitCode, err := dockerCmdWithError(c, "pull", "scratch")
  94. if err == nil {
  95. c.Fatal("expected pull of scratch to fail, but it didn't")
  96. }
  97. if exitCode != 1 {
  98. c.Fatalf("pulling scratch expected exit code 1, got %d", exitCode)
  99. }
  100. if strings.Contains(out, "Pulling repository scratch") {
  101. c.Fatalf("pulling scratch should not have begun: %s", out)
  102. }
  103. if !strings.Contains(out, "'scratch' is a reserved name") {
  104. c.Fatalf("unexpected output pulling scratch: %s", out)
  105. }
  106. }
  107. // pulling an image with --all-tags=true
  108. func (s *DockerSuite) TestPullImageWithAllTagFromCentralRegistry(c *check.C) {
  109. testRequires(c, Network)
  110. dockerCmd(c, "pull", "busybox")
  111. outImageCmd, _ := dockerCmd(c, "images", "busybox")
  112. dockerCmd(c, "pull", "--all-tags=true", "busybox")
  113. outImageAllTagCmd, _ := dockerCmd(c, "images", "busybox")
  114. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  115. c.Fatalf("Pulling with all tags should get more images")
  116. }
  117. // FIXME has probably no effect (tags already pushed)
  118. dockerCmd(c, "pull", "-a", "busybox")
  119. outImageAllTagCmd, _ = dockerCmd(c, "images", "busybox")
  120. if strings.Count(outImageCmd, "busybox") >= strings.Count(outImageAllTagCmd, "busybox") {
  121. c.Fatalf("Pulling with all tags should get more images")
  122. }
  123. }
  124. func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
  125. repoName := s.setupTrustedImage(c, "trusted-pull")
  126. // Try pull
  127. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  128. s.trustedCmd(pullCmd)
  129. out, _, err := runCommandWithOutput(pullCmd)
  130. if err != nil {
  131. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  132. }
  133. if !strings.Contains(string(out), "Tagging") {
  134. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  135. }
  136. dockerCmd(c, "rmi", repoName)
  137. // Try untrusted pull to ensure we pushed the tag to the registry
  138. pullCmd = exec.Command(dockerBinary, "pull", "--untrusted=true", repoName)
  139. s.trustedCmd(pullCmd)
  140. out, _, err = runCommandWithOutput(pullCmd)
  141. if err != nil {
  142. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  143. }
  144. if !strings.Contains(string(out), "Status: Downloaded") {
  145. c.Fatalf("Missing expected output on trusted pull with --untrusted:\n%s", out)
  146. }
  147. }
  148. func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
  149. repoName := s.setupTrustedImage(c, "trusted-isolatd-pull")
  150. // Try pull (run from isolated directory without trust information)
  151. pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
  152. s.trustedCmd(pullCmd)
  153. out, _, err := runCommandWithOutput(pullCmd)
  154. if err != nil {
  155. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  156. }
  157. if !strings.Contains(string(out), "Tagging") {
  158. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  159. }
  160. dockerCmd(c, "rmi", repoName)
  161. }
  162. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  163. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  164. // tag the image and upload it to the private registry
  165. dockerCmd(c, "tag", "busybox", repoName)
  166. dockerCmd(c, "push", repoName)
  167. dockerCmd(c, "rmi", repoName)
  168. // Try trusted pull on untrusted tag
  169. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  170. s.trustedCmd(pullCmd)
  171. out, _, err := runCommandWithOutput(pullCmd)
  172. if err == nil {
  173. c.Fatalf("Error expected when running trusted pull with:\n%s", out)
  174. }
  175. if !strings.Contains(string(out), "no trust data available") {
  176. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  177. }
  178. }
  179. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  180. repoName := s.setupTrustedImage(c, "trusted-cert-expired")
  181. // Certificates have 10 years of expiration
  182. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  183. runAtDifferentDate(elevenYearsFromNow, func() {
  184. // Try pull
  185. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  186. s.trustedCmd(pullCmd)
  187. out, _, err := runCommandWithOutput(pullCmd)
  188. if err == nil {
  189. c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
  190. }
  191. if !strings.Contains(string(out), "could not validate the path to a trusted root") {
  192. c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
  193. }
  194. })
  195. runAtDifferentDate(elevenYearsFromNow, func() {
  196. // Try pull
  197. pullCmd := exec.Command(dockerBinary, "pull", "--untrusted", repoName)
  198. s.trustedCmd(pullCmd)
  199. out, _, err := runCommandWithOutput(pullCmd)
  200. if err != nil {
  201. c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
  202. }
  203. if !strings.Contains(string(out), "Status: Downloaded") {
  204. c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
  205. }
  206. })
  207. }