docker_cli_pull_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 := fmt.Sprintf("%v/dockerclipull/trusted:latest", privateRegistryURL)
  126. // tag the image and upload it to the private registry
  127. dockerCmd(c, "tag", "busybox", repoName)
  128. pushCmd := exec.Command(dockerBinary, "push", repoName)
  129. s.trustedCmd(pushCmd)
  130. out, _, err := runCommandWithOutput(pushCmd)
  131. if err != nil {
  132. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  133. }
  134. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  135. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  136. }
  137. dockerCmd(c, "rmi", repoName)
  138. // Try pull
  139. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  140. s.trustedCmd(pullCmd)
  141. out, _, err = runCommandWithOutput(pullCmd)
  142. if err != nil {
  143. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  144. }
  145. if !strings.Contains(string(out), "Tagging") {
  146. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  147. }
  148. dockerCmd(c, "rmi", repoName)
  149. // Try untrusted pull to ensure we pushed the tag to the registry
  150. pullCmd = exec.Command(dockerBinary, "pull", "--untrusted=true", repoName)
  151. s.trustedCmd(pullCmd)
  152. out, _, err = runCommandWithOutput(pullCmd)
  153. if err != nil {
  154. c.Fatalf("Error running trusted pull: %s\n%s", err, out)
  155. }
  156. if !strings.Contains(string(out), "Status: Downloaded") {
  157. c.Fatalf("Missing expected output on trusted pull with --untrusted:\n%s", out)
  158. }
  159. }
  160. func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
  161. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  162. // tag the image and upload it to the private registry
  163. dockerCmd(c, "tag", "busybox", repoName)
  164. dockerCmd(c, "push", repoName)
  165. dockerCmd(c, "rmi", repoName)
  166. // Try trusted pull on untrusted tag
  167. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  168. s.trustedCmd(pullCmd)
  169. out, _, err := runCommandWithOutput(pullCmd)
  170. if err == nil {
  171. c.Fatalf("Error expected when running trusted pull with:\n%s", out)
  172. }
  173. if !strings.Contains(string(out), "no trust data available") {
  174. c.Fatalf("Missing expected output on trusted pull:\n%s", out)
  175. }
  176. }
  177. func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
  178. repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
  179. // tag the image and upload it to the private registry
  180. dockerCmd(c, "tag", "busybox", repoName)
  181. pushCmd := exec.Command(dockerBinary, "push", repoName)
  182. s.trustedCmd(pushCmd)
  183. out, _, err := runCommandWithOutput(pushCmd)
  184. if err != nil {
  185. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  186. }
  187. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  188. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  189. }
  190. dockerCmd(c, "rmi", repoName)
  191. // Certificates have 10 years of expiration
  192. elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
  193. runAtDifferentDate(elevenYearsFromNow, func() {
  194. // Try pull
  195. pullCmd := exec.Command(dockerBinary, "pull", repoName)
  196. s.trustedCmd(pullCmd)
  197. out, _, err = runCommandWithOutput(pullCmd)
  198. if err == nil {
  199. c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
  200. }
  201. if !strings.Contains(string(out), "could not validate the path to a trusted root") {
  202. c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
  203. }
  204. })
  205. runAtDifferentDate(elevenYearsFromNow, func() {
  206. // Try pull
  207. pullCmd := exec.Command(dockerBinary, "pull", "--untrusted", repoName)
  208. s.trustedCmd(pullCmd)
  209. out, _, err = runCommandWithOutput(pullCmd)
  210. if err != nil {
  211. c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
  212. }
  213. if !strings.Contains(string(out), "Status: Downloaded") {
  214. c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
  215. }
  216. })
  217. }