docker_cli_by_digest_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. "github.com/docker/distribution/manifest/schema2"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/integration-cli/checker"
  10. "github.com/docker/docker/integration-cli/cli"
  11. "github.com/docker/docker/integration-cli/cli/build"
  12. "github.com/go-check/check"
  13. "github.com/opencontainers/go-digest"
  14. "gotest.tools/assert"
  15. is "gotest.tools/assert/cmp"
  16. )
  17. var (
  18. remoteRepoName = "dockercli/busybox-by-dgst"
  19. repoName = fmt.Sprintf("%s/%s", privateRegistryURL, remoteRepoName)
  20. pushDigestRegex = regexp.MustCompile("[\\S]+: digest: ([\\S]+) size: [0-9]+")
  21. digestRegex = regexp.MustCompile("Digest: ([\\S]+)")
  22. )
  23. func setupImage(c *check.C) (digest.Digest, error) {
  24. return setupImageWithTag(c, "latest")
  25. }
  26. func setupImageWithTag(c *check.C, tag string) (digest.Digest, error) {
  27. containerName := "busyboxbydigest"
  28. // new file is committed because this layer is used for detecting malicious
  29. // changes. if this was committed as empty layer it would be skipped on pull
  30. // and malicious changes would never be detected.
  31. cli.DockerCmd(c, "run", "-e", "digest=1", "--name", containerName, "busybox", "touch", "anewfile")
  32. // tag the image to upload it to the private registry
  33. repoAndTag := repoName + ":" + tag
  34. cli.DockerCmd(c, "commit", containerName, repoAndTag)
  35. // delete the container as we don't need it any more
  36. cli.DockerCmd(c, "rm", "-fv", containerName)
  37. // push the image
  38. out := cli.DockerCmd(c, "push", repoAndTag).Combined()
  39. // delete our local repo that we previously tagged
  40. cli.DockerCmd(c, "rmi", repoAndTag)
  41. matches := pushDigestRegex.FindStringSubmatch(out)
  42. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from push output: %s", out))
  43. pushDigest := matches[1]
  44. return digest.Digest(pushDigest), nil
  45. }
  46. func testPullByTagDisplaysDigest(c *check.C) {
  47. testRequires(c, DaemonIsLinux)
  48. pushDigest, err := setupImage(c)
  49. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  50. // pull from the registry using the tag
  51. out, _ := dockerCmd(c, "pull", repoName)
  52. // the pull output includes "Digest: <digest>", so find that
  53. matches := digestRegex.FindStringSubmatch(out)
  54. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  55. pullDigest := matches[1]
  56. // make sure the pushed and pull digests match
  57. c.Assert(pushDigest.String(), checker.Equals, pullDigest)
  58. }
  59. func (s *DockerRegistrySuite) TestPullByTagDisplaysDigest(c *check.C) {
  60. testPullByTagDisplaysDigest(c)
  61. }
  62. func testPullByDigest(c *check.C) {
  63. testRequires(c, DaemonIsLinux)
  64. pushDigest, err := setupImage(c)
  65. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  66. // pull from the registry using the <name>@<digest> reference
  67. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  68. out, _ := dockerCmd(c, "pull", imageReference)
  69. // the pull output includes "Digest: <digest>", so find that
  70. matches := digestRegex.FindStringSubmatch(out)
  71. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  72. pullDigest := matches[1]
  73. // make sure the pushed and pull digests match
  74. c.Assert(pushDigest.String(), checker.Equals, pullDigest)
  75. }
  76. func (s *DockerRegistrySuite) TestPullByDigest(c *check.C) {
  77. testPullByDigest(c)
  78. }
  79. func testPullByDigestNoFallback(c *check.C) {
  80. testRequires(c, DaemonIsLinux)
  81. // pull from the registry using the <name>@<digest> reference
  82. imageReference := fmt.Sprintf("%s@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", repoName)
  83. out, _, err := dockerCmdWithError("pull", imageReference)
  84. c.Assert(err, checker.NotNil, check.Commentf("expected non-zero exit status and correct error message when pulling non-existing image"))
  85. c.Assert(out, checker.Contains, fmt.Sprintf("manifest for %s not found", imageReference), check.Commentf("expected non-zero exit status and correct error message when pulling non-existing image"))
  86. }
  87. func (s *DockerRegistrySuite) TestPullByDigestNoFallback(c *check.C) {
  88. testPullByDigestNoFallback(c)
  89. }
  90. func (s *DockerRegistrySuite) TestCreateByDigest(c *check.C) {
  91. pushDigest, err := setupImage(c)
  92. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  93. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  94. containerName := "createByDigest"
  95. dockerCmd(c, "create", "--name", containerName, imageReference)
  96. res := inspectField(c, containerName, "Config.Image")
  97. c.Assert(res, checker.Equals, imageReference)
  98. }
  99. func (s *DockerRegistrySuite) TestRunByDigest(c *check.C) {
  100. pushDigest, err := setupImage(c)
  101. c.Assert(err, checker.IsNil)
  102. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  103. containerName := "runByDigest"
  104. out, _ := dockerCmd(c, "run", "--name", containerName, imageReference, "sh", "-c", "echo found=$digest")
  105. foundRegex := regexp.MustCompile("found=([^\n]+)")
  106. matches := foundRegex.FindStringSubmatch(out)
  107. c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", out))
  108. c.Assert(matches[1], checker.Equals, "1", check.Commentf("Expected %q, got %q", "1", matches[1]))
  109. res := inspectField(c, containerName, "Config.Image")
  110. c.Assert(res, checker.Equals, imageReference)
  111. }
  112. func (s *DockerRegistrySuite) TestRemoveImageByDigest(c *check.C) {
  113. digest, err := setupImage(c)
  114. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  115. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  116. // pull from the registry using the <name>@<digest> reference
  117. dockerCmd(c, "pull", imageReference)
  118. // make sure inspect runs ok
  119. inspectField(c, imageReference, "Id")
  120. // do the delete
  121. err = deleteImages(imageReference)
  122. c.Assert(err, checker.IsNil, check.Commentf("unexpected error deleting image"))
  123. // try to inspect again - it should error this time
  124. _, err = inspectFieldWithError(imageReference, "Id")
  125. //unexpected nil err trying to inspect what should be a non-existent image
  126. c.Assert(err, checker.NotNil)
  127. c.Assert(err.Error(), checker.Contains, "No such object")
  128. }
  129. func (s *DockerRegistrySuite) TestBuildByDigest(c *check.C) {
  130. digest, err := setupImage(c)
  131. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  132. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  133. // pull from the registry using the <name>@<digest> reference
  134. dockerCmd(c, "pull", imageReference)
  135. // get the image id
  136. imageID := inspectField(c, imageReference, "Id")
  137. // do the build
  138. name := "buildbydigest"
  139. buildImageSuccessfully(c, name, build.WithDockerfile(fmt.Sprintf(
  140. `FROM %s
  141. CMD ["/bin/echo", "Hello World"]`, imageReference)))
  142. c.Assert(err, checker.IsNil)
  143. // get the build's image id
  144. res := inspectField(c, name, "Config.Image")
  145. // make sure they match
  146. c.Assert(res, checker.Equals, imageID)
  147. }
  148. func (s *DockerRegistrySuite) TestTagByDigest(c *check.C) {
  149. digest, err := setupImage(c)
  150. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  151. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  152. // pull from the registry using the <name>@<digest> reference
  153. dockerCmd(c, "pull", imageReference)
  154. // tag it
  155. tag := "tagbydigest"
  156. dockerCmd(c, "tag", imageReference, tag)
  157. expectedID := inspectField(c, imageReference, "Id")
  158. tagID := inspectField(c, tag, "Id")
  159. c.Assert(tagID, checker.Equals, expectedID)
  160. }
  161. func (s *DockerRegistrySuite) TestListImagesWithoutDigests(c *check.C) {
  162. digest, err := setupImage(c)
  163. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  164. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  165. // pull from the registry using the <name>@<digest> reference
  166. dockerCmd(c, "pull", imageReference)
  167. out, _ := dockerCmd(c, "images")
  168. c.Assert(out, checker.Not(checker.Contains), "DIGEST", check.Commentf("list output should not have contained DIGEST header"))
  169. }
  170. func (s *DockerRegistrySuite) TestListImagesWithDigests(c *check.C) {
  171. // setup image1
  172. digest1, err := setupImageWithTag(c, "tag1")
  173. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  174. imageReference1 := fmt.Sprintf("%s@%s", repoName, digest1)
  175. c.Logf("imageReference1 = %s", imageReference1)
  176. // pull image1 by digest
  177. dockerCmd(c, "pull", imageReference1)
  178. // list images
  179. out, _ := dockerCmd(c, "images", "--digests")
  180. // make sure repo shown, tag=<none>, digest = $digest1
  181. re1 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest1.String() + `\s`)
  182. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  183. // setup image2
  184. digest2, err := setupImageWithTag(c, "tag2")
  185. //error setting up image
  186. c.Assert(err, checker.IsNil)
  187. imageReference2 := fmt.Sprintf("%s@%s", repoName, digest2)
  188. c.Logf("imageReference2 = %s", imageReference2)
  189. // pull image1 by digest
  190. dockerCmd(c, "pull", imageReference1)
  191. // pull image2 by digest
  192. dockerCmd(c, "pull", imageReference2)
  193. // list images
  194. out, _ = dockerCmd(c, "images", "--digests")
  195. // make sure repo shown, tag=<none>, digest = $digest1
  196. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  197. // make sure repo shown, tag=<none>, digest = $digest2
  198. re2 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest2.String() + `\s`)
  199. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  200. // pull tag1
  201. dockerCmd(c, "pull", repoName+":tag1")
  202. // list images
  203. out, _ = dockerCmd(c, "images", "--digests")
  204. // make sure image 1 has repo, tag, <none> AND repo, <none>, digest
  205. reWithDigest1 := regexp.MustCompile(`\s*` + repoName + `\s*tag1\s*` + digest1.String() + `\s`)
  206. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  207. // make sure image 2 has repo, <none>, digest
  208. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  209. // pull tag 2
  210. dockerCmd(c, "pull", repoName+":tag2")
  211. // list images
  212. out, _ = dockerCmd(c, "images", "--digests")
  213. // make sure image 1 has repo, tag, digest
  214. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  215. // make sure image 2 has repo, tag, digest
  216. reWithDigest2 := regexp.MustCompile(`\s*` + repoName + `\s*tag2\s*` + digest2.String() + `\s`)
  217. c.Assert(reWithDigest2.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest2.String(), out))
  218. // list images
  219. out, _ = dockerCmd(c, "images", "--digests")
  220. // make sure image 1 has repo, tag, digest
  221. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  222. // make sure image 2 has repo, tag, digest
  223. c.Assert(reWithDigest2.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest2.String(), out))
  224. // make sure busybox has tag, but not digest
  225. busyboxRe := regexp.MustCompile(`\s*busybox\s*latest\s*<none>\s`)
  226. c.Assert(busyboxRe.MatchString(out), checker.True, check.Commentf("expected %q: %s", busyboxRe.String(), out))
  227. }
  228. func (s *DockerRegistrySuite) TestListDanglingImagesWithDigests(c *check.C) {
  229. // setup image1
  230. digest1, err := setupImageWithTag(c, "dangle1")
  231. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  232. imageReference1 := fmt.Sprintf("%s@%s", repoName, digest1)
  233. c.Logf("imageReference1 = %s", imageReference1)
  234. // pull image1 by digest
  235. dockerCmd(c, "pull", imageReference1)
  236. // list images
  237. out, _ := dockerCmd(c, "images", "--digests")
  238. // make sure repo shown, tag=<none>, digest = $digest1
  239. re1 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest1.String() + `\s`)
  240. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  241. // setup image2
  242. digest2, err := setupImageWithTag(c, "dangle2")
  243. //error setting up image
  244. c.Assert(err, checker.IsNil)
  245. imageReference2 := fmt.Sprintf("%s@%s", repoName, digest2)
  246. c.Logf("imageReference2 = %s", imageReference2)
  247. // pull image1 by digest
  248. dockerCmd(c, "pull", imageReference1)
  249. // pull image2 by digest
  250. dockerCmd(c, "pull", imageReference2)
  251. // list images
  252. out, _ = dockerCmd(c, "images", "--digests", "--filter=dangling=true")
  253. // make sure repo shown, tag=<none>, digest = $digest1
  254. c.Assert(re1.MatchString(out), checker.True, check.Commentf("expected %q: %s", re1.String(), out))
  255. // make sure repo shown, tag=<none>, digest = $digest2
  256. re2 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest2.String() + `\s`)
  257. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  258. // pull dangle1 tag
  259. dockerCmd(c, "pull", repoName+":dangle1")
  260. // list images
  261. out, _ = dockerCmd(c, "images", "--digests", "--filter=dangling=true")
  262. // make sure image 1 has repo, tag, <none> AND repo, <none>, digest
  263. reWithDigest1 := regexp.MustCompile(`\s*` + repoName + `\s*dangle1\s*` + digest1.String() + `\s`)
  264. c.Assert(reWithDigest1.MatchString(out), checker.False, check.Commentf("unexpected %q: %s", reWithDigest1.String(), out))
  265. // make sure image 2 has repo, <none>, digest
  266. c.Assert(re2.MatchString(out), checker.True, check.Commentf("expected %q: %s", re2.String(), out))
  267. // pull dangle2 tag
  268. dockerCmd(c, "pull", repoName+":dangle2")
  269. // list images, show tagged images
  270. out, _ = dockerCmd(c, "images", "--digests")
  271. // make sure image 1 has repo, tag, digest
  272. c.Assert(reWithDigest1.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest1.String(), out))
  273. // make sure image 2 has repo, tag, digest
  274. reWithDigest2 := regexp.MustCompile(`\s*` + repoName + `\s*dangle2\s*` + digest2.String() + `\s`)
  275. c.Assert(reWithDigest2.MatchString(out), checker.True, check.Commentf("expected %q: %s", reWithDigest2.String(), out))
  276. // list images, no longer dangling, should not match
  277. out, _ = dockerCmd(c, "images", "--digests", "--filter=dangling=true")
  278. // make sure image 1 has repo, tag, digest
  279. c.Assert(reWithDigest1.MatchString(out), checker.False, check.Commentf("unexpected %q: %s", reWithDigest1.String(), out))
  280. // make sure image 2 has repo, tag, digest
  281. c.Assert(reWithDigest2.MatchString(out), checker.False, check.Commentf("unexpected %q: %s", reWithDigest2.String(), out))
  282. }
  283. func (s *DockerRegistrySuite) TestInspectImageWithDigests(c *check.C) {
  284. digest, err := setupImage(c)
  285. c.Assert(err, check.IsNil, check.Commentf("error setting up image"))
  286. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  287. // pull from the registry using the <name>@<digest> reference
  288. dockerCmd(c, "pull", imageReference)
  289. out, _ := dockerCmd(c, "inspect", imageReference)
  290. var imageJSON []types.ImageInspect
  291. err = json.Unmarshal([]byte(out), &imageJSON)
  292. c.Assert(err, checker.IsNil)
  293. c.Assert(imageJSON, checker.HasLen, 1)
  294. c.Assert(imageJSON[0].RepoDigests, checker.HasLen, 1)
  295. assert.Check(c, is.Contains(imageJSON[0].RepoDigests, imageReference))
  296. }
  297. func (s *DockerRegistrySuite) TestPsListContainersFilterAncestorImageByDigest(c *check.C) {
  298. existingContainers := ExistingContainerIDs(c)
  299. digest, err := setupImage(c)
  300. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  301. imageReference := fmt.Sprintf("%s@%s", repoName, digest)
  302. // pull from the registry using the <name>@<digest> reference
  303. dockerCmd(c, "pull", imageReference)
  304. // build an image from it
  305. imageName1 := "images_ps_filter_test"
  306. buildImageSuccessfully(c, imageName1, build.WithDockerfile(fmt.Sprintf(
  307. `FROM %s
  308. LABEL match me 1`, imageReference)))
  309. // run a container based on that
  310. dockerCmd(c, "run", "--name=test1", imageReference, "echo", "hello")
  311. expectedID := getIDByName(c, "test1")
  312. // run a container based on the a descendant of that too
  313. dockerCmd(c, "run", "--name=test2", imageName1, "echo", "hello")
  314. expectedID1 := getIDByName(c, "test2")
  315. expectedIDs := []string{expectedID, expectedID1}
  316. // Invalid imageReference
  317. out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", fmt.Sprintf("--filter=ancestor=busybox@%s", digest))
  318. // Filter container for ancestor filter should be empty
  319. c.Assert(strings.TrimSpace(out), checker.Equals, "")
  320. // Valid imageReference
  321. out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageReference)
  322. checkPsAncestorFilterOutput(c, RemoveOutputForExistingElements(out, existingContainers), imageReference, expectedIDs)
  323. }
  324. func (s *DockerRegistrySuite) TestDeleteImageByIDOnlyPulledByDigest(c *check.C) {
  325. pushDigest, err := setupImage(c)
  326. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  327. // pull from the registry using the <name>@<digest> reference
  328. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  329. dockerCmd(c, "pull", imageReference)
  330. // just in case...
  331. dockerCmd(c, "tag", imageReference, repoName+":sometag")
  332. imageID := inspectField(c, imageReference, "Id")
  333. dockerCmd(c, "rmi", imageID)
  334. _, err = inspectFieldWithError(imageID, "Id")
  335. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  336. }
  337. func (s *DockerRegistrySuite) TestDeleteImageWithDigestAndTag(c *check.C) {
  338. pushDigest, err := setupImage(c)
  339. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  340. // pull from the registry using the <name>@<digest> reference
  341. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  342. dockerCmd(c, "pull", imageReference)
  343. imageID := inspectField(c, imageReference, "Id")
  344. repoTag := repoName + ":sometag"
  345. repoTag2 := repoName + ":othertag"
  346. dockerCmd(c, "tag", imageReference, repoTag)
  347. dockerCmd(c, "tag", imageReference, repoTag2)
  348. dockerCmd(c, "rmi", repoTag2)
  349. // rmi should have deleted only repoTag2, because there's another tag
  350. inspectField(c, repoTag, "Id")
  351. dockerCmd(c, "rmi", repoTag)
  352. // rmi should have deleted the tag, the digest reference, and the image itself
  353. _, err = inspectFieldWithError(imageID, "Id")
  354. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  355. }
  356. func (s *DockerRegistrySuite) TestDeleteImageWithDigestAndMultiRepoTag(c *check.C) {
  357. pushDigest, err := setupImage(c)
  358. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  359. repo2 := fmt.Sprintf("%s/%s", repoName, "repo2")
  360. // pull from the registry using the <name>@<digest> reference
  361. imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
  362. dockerCmd(c, "pull", imageReference)
  363. imageID := inspectField(c, imageReference, "Id")
  364. repoTag := repoName + ":sometag"
  365. repoTag2 := repo2 + ":othertag"
  366. dockerCmd(c, "tag", imageReference, repoTag)
  367. dockerCmd(c, "tag", imageReference, repoTag2)
  368. dockerCmd(c, "rmi", repoTag)
  369. // rmi should have deleted repoTag and image reference, but left repoTag2
  370. inspectField(c, repoTag2, "Id")
  371. _, err = inspectFieldWithError(imageReference, "Id")
  372. c.Assert(err, checker.NotNil, check.Commentf("image digest reference should have been removed"))
  373. _, err = inspectFieldWithError(repoTag, "Id")
  374. c.Assert(err, checker.NotNil, check.Commentf("image tag reference should have been removed"))
  375. dockerCmd(c, "rmi", repoTag2)
  376. // rmi should have deleted the tag, the digest reference, and the image itself
  377. _, err = inspectFieldWithError(imageID, "Id")
  378. c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
  379. }
  380. // TestPullFailsWithAlteredManifest tests that a `docker pull` fails when
  381. // we have modified a manifest blob and its digest cannot be verified.
  382. // This is the schema2 version of the test.
  383. func (s *DockerRegistrySuite) TestPullFailsWithAlteredManifest(c *check.C) {
  384. testRequires(c, DaemonIsLinux)
  385. manifestDigest, err := setupImage(c)
  386. c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
  387. // Load the target manifest blob.
  388. manifestBlob := s.reg.ReadBlobContents(c, manifestDigest)
  389. var imgManifest schema2.Manifest
  390. err = json.Unmarshal(manifestBlob, &imgManifest)
  391. c.Assert(err, checker.IsNil, check.Commentf("unable to decode image manifest from blob"))
  392. // Change a layer in the manifest.
  393. imgManifest.Layers[0].Digest = digest.Digest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
  394. // Move the existing data file aside, so that we can replace it with a
  395. // malicious blob of data. NOTE: we defer the returned undo func.
  396. undo := s.reg.TempMoveBlobData(c, manifestDigest)
  397. defer undo()
  398. alteredManifestBlob, err := json.MarshalIndent(imgManifest, "", " ")
  399. c.Assert(err, checker.IsNil, check.Commentf("unable to encode altered image manifest to JSON"))
  400. s.reg.WriteBlobContents(c, manifestDigest, alteredManifestBlob)
  401. // Now try pulling that image by digest. We should get an error about
  402. // digest verification for the manifest digest.
  403. // Pull from the registry using the <name>@<digest> reference.
  404. imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
  405. out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
  406. c.Assert(exitStatus, checker.Not(check.Equals), 0)
  407. expectedErrorMsg := fmt.Sprintf("manifest verification failed for digest %s", manifestDigest)
  408. c.Assert(out, checker.Contains, expectedErrorMsg)
  409. }