2015-02-26 21:23:50 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-08-01 02:27:19 -04:00
|
|
|
"encoding/json"
|
2015-02-26 21:23:50 -05:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2015-08-01 02:27:19 -04:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-10-30 20:46:25 -04:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2015-10-22 06:34:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-02-26 21:23:50 -05:00
|
|
|
"github.com/docker/docker/utils"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2015-02-26 21:23:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-08-01 02:27:19 -04:00
|
|
|
remoteRepoName = "dockercli/busybox-by-dgst"
|
|
|
|
repoName = fmt.Sprintf("%v/%s", privateRegistryURL, remoteRepoName)
|
2015-07-15 16:42:45 -04:00
|
|
|
pushDigestRegex = regexp.MustCompile("[\\S]+: digest: ([\\S]+) size: [0-9]+")
|
|
|
|
digestRegex = regexp.MustCompile("Digest: ([\\S]+)")
|
2015-02-26 21:23:50 -05:00
|
|
|
)
|
|
|
|
|
2015-08-01 02:27:19 -04:00
|
|
|
func setupImage(c *check.C) (digest.Digest, error) {
|
2015-07-14 02:35:36 -04:00
|
|
|
return setupImageWithTag(c, "latest")
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
2015-08-01 02:27:19 -04:00
|
|
|
func setupImageWithTag(c *check.C, tag string) (digest.Digest, error) {
|
2015-02-26 21:23:50 -05:00
|
|
|
containerName := "busyboxbydigest"
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "run", "-d", "-e", "digest=1", "--name", containerName, "busybox")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// tag the image to upload it to the private registry
|
|
|
|
repoAndTag := utils.ImageReference(repoName, tag)
|
2015-07-27 14:13:25 -04:00
|
|
|
if out, _, err := dockerCmdWithError("commit", containerName, repoAndTag); err != nil {
|
2015-02-26 21:23:50 -05:00
|
|
|
return "", fmt.Errorf("image tagging failed: %s, %v", out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the container as we don't need it any more
|
|
|
|
if err := deleteContainer(containerName); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// push the image
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("push", repoAndTag)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("pushing the image to the private registry has failed: %s, %v", out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete our local repo that we previously tagged
|
2015-07-27 14:13:25 -04:00
|
|
|
if rmiout, _, err := dockerCmdWithError("rmi", repoAndTag); err != nil {
|
2015-07-14 02:35:36 -04:00
|
|
|
return "", fmt.Errorf("error deleting images prior to real test: %s, %v", rmiout, err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
2015-07-15 16:42:45 -04:00
|
|
|
matches := pushDigestRegex.FindStringSubmatch(out)
|
2015-02-26 21:23:50 -05:00
|
|
|
if len(matches) != 2 {
|
|
|
|
return "", fmt.Errorf("unable to parse digest from push output: %s", out)
|
|
|
|
}
|
|
|
|
pushDigest := matches[1]
|
|
|
|
|
2015-08-01 02:27:19 -04:00
|
|
|
return digest.Digest(pushDigest), nil
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestPullByTagDisplaysDigest(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 02:35:36 -04:00
|
|
|
pushDigest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// pull from the registry using the tag
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "pull", repoName)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// the pull output includes "Digest: <digest>", so find that
|
|
|
|
matches := digestRegex.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unable to parse digest from pull output: %s", out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
pullDigest := matches[1]
|
|
|
|
|
|
|
|
// make sure the pushed and pull digests match
|
2015-08-01 02:27:19 -04:00
|
|
|
if pushDigest.String() != pullDigest {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("push digest %q didn't match pull digest %q", pushDigest, pullDigest)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestPullByDigest(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 02:35:36 -04:00
|
|
|
pushDigest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "pull", imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// the pull output includes "Digest: <digest>", so find that
|
|
|
|
matches := digestRegex.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unable to parse digest from pull output: %s", out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
pullDigest := matches[1]
|
|
|
|
|
|
|
|
// make sure the pushed and pull digests match
|
2015-08-01 02:27:19 -04:00
|
|
|
if pushDigest.String() != pullDigest {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("push digest %q didn't match pull digest %q", pushDigest, pullDigest)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:29:50 -04:00
|
|
|
func (s *DockerRegistrySuite) TestPullByDigestNoFallback(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-29 15:29:50 -04:00
|
|
|
// pull from the registry using the <name>@<digest> reference
|
|
|
|
imageReference := fmt.Sprintf("%s@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", repoName)
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("pull", imageReference)
|
2015-02-12 13:23:22 -05:00
|
|
|
if err == nil || !strings.Contains(out, "manifest unknown") {
|
2015-04-29 15:29:50 -04:00
|
|
|
c.Fatalf("expected non-zero exit status and correct error message when pulling non-existing image: %s", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestCreateByDigest(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
pushDigest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
|
|
|
|
|
|
|
|
containerName := "createByDigest"
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "create", "--name", containerName, imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
res, err := inspectField(containerName, "Config.Image")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to get Config.Image: %s, %v", out, err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
if res != imageReference {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected Config.Image: %s (expected %s)", res, imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestRunByDigest(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
pushDigest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
|
|
|
|
|
|
|
|
containerName := "runByDigest"
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", containerName, imageReference, "sh", "-c", "echo found=$digest")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
foundRegex := regexp.MustCompile("found=([^\n]+)")
|
|
|
|
matches := foundRegex.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error locating expected 'found=1' output: %s", out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
if matches[1] != "1" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected %q, got %q", "1", matches[1])
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := inspectField(containerName, "Config.Image")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to get Config.Image: %s, %v", out, err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
if res != imageReference {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected Config.Image: %s (expected %s)", res, imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestRemoveImageByDigest(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
digest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, digest)
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// make sure inspect runs ok
|
|
|
|
if _, err := inspectField(imageReference, "Id"); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to inspect image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// do the delete
|
|
|
|
if err := deleteImages(imageReference); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected error deleting image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to inspect again - it should error this time
|
|
|
|
if _, err := inspectField(imageReference, "Id"); err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("unexpected nil err trying to inspect what should be a non-existent image")
|
2015-02-26 21:23:50 -05:00
|
|
|
} else if !strings.Contains(err.Error(), "No such image") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected 'No such image' output, got %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestBuildByDigest(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
digest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, digest)
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// get the image id
|
|
|
|
imageID, err := inspectField(imageReference, "Id")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error getting image id: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// do the build
|
|
|
|
name := "buildbydigest"
|
|
|
|
_, err = buildImage(name, fmt.Sprintf(
|
|
|
|
`FROM %s
|
|
|
|
CMD ["/bin/echo", "Hello World"]`, imageReference),
|
|
|
|
true)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the build's image id
|
|
|
|
res, err := inspectField(name, "Config.Image")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
// make sure they match
|
|
|
|
if res != imageID {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Image %s, expected %s", res, imageID)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestTagByDigest(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
digest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, digest)
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// tag it
|
|
|
|
tag := "tagbydigest"
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "tag", imageReference, tag)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
expectedID, err := inspectField(imageReference, "Id")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error getting original image id: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tagID, err := inspectField(tag, "Id")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error getting tagged image id: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if tagID != expectedID {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected image id %q, got %q", expectedID, tagID)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestListImagesWithoutDigests(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
digest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, digest)
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "images")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
if strings.Contains(out, "DIGEST") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("list output should not have contained DIGEST header: %s", out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestListImagesWithDigests(c *check.C) {
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// setup image1
|
2015-07-14 02:35:36 -04:00
|
|
|
digest1, err := setupImageWithTag(c, "tag1")
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
imageReference1 := fmt.Sprintf("%s@%s", repoName, digest1)
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Logf("imageReference1 = %s", imageReference1)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// pull image1 by digest
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference1)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// list images
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ := dockerCmd(c, "images", "--digests")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// make sure repo shown, tag=<none>, digest = $digest1
|
2015-08-01 02:27:19 -04:00
|
|
|
re1 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest1.String() + `\s`)
|
2015-02-26 21:23:50 -05:00
|
|
|
if !re1.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re1.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup image2
|
2015-07-14 02:35:36 -04:00
|
|
|
digest2, err := setupImageWithTag(c, "tag2")
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
imageReference2 := fmt.Sprintf("%s@%s", repoName, digest2)
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Logf("imageReference2 = %s", imageReference2)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// pull image1 by digest
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference1)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// pull image2 by digest
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference2)
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// list images
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "images", "--digests")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// make sure repo shown, tag=<none>, digest = $digest1
|
|
|
|
if !re1.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re1.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure repo shown, tag=<none>, digest = $digest2
|
2015-08-01 02:27:19 -04:00
|
|
|
re2 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest2.String() + `\s`)
|
2015-02-26 21:23:50 -05:00
|
|
|
if !re2.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re2.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// pull tag1
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", repoName+":tag1")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// list images
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "images", "--digests")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// make sure image 1 has repo, tag, <none> AND repo, <none>, digest
|
|
|
|
reWithTag1 := regexp.MustCompile(`\s*` + repoName + `\s*tag1\s*<none>\s`)
|
2015-08-01 02:27:19 -04:00
|
|
|
reWithDigest1 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest1.String() + `\s`)
|
2015-02-26 21:23:50 -05:00
|
|
|
if !reWithTag1.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", reWithTag1.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
if !reWithDigest1.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", reWithDigest1.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
// make sure image 2 has repo, <none>, digest
|
|
|
|
if !re2.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re2.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// pull tag 2
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", repoName+":tag2")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// list images
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "images", "--digests")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// make sure image 1 has repo, tag, digest
|
|
|
|
if !reWithTag1.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re1.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure image 2 has repo, tag, digest
|
|
|
|
reWithTag2 := regexp.MustCompile(`\s*` + repoName + `\s*tag2\s*<none>\s`)
|
2015-08-01 02:27:19 -04:00
|
|
|
reWithDigest2 := regexp.MustCompile(`\s*` + repoName + `\s*<none>\s*` + digest2.String() + `\s`)
|
2015-02-26 21:23:50 -05:00
|
|
|
if !reWithTag2.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", reWithTag2.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
if !reWithDigest2.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", reWithDigest2.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// list images
|
2015-07-14 02:35:36 -04:00
|
|
|
out, _ = dockerCmd(c, "images", "--digests")
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
// make sure image 1 has repo, tag, digest
|
|
|
|
if !reWithTag1.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re1.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
// make sure image 2 has repo, tag, digest
|
|
|
|
if !reWithTag2.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", re2.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
// make sure busybox has tag, but not digest
|
|
|
|
busyboxRe := regexp.MustCompile(`\s*busybox\s*latest\s*<none>\s`)
|
|
|
|
if !busyboxRe.MatchString(out) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("expected %q: %s", busyboxRe.String(), out)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 06:34:12 -04:00
|
|
|
func (s *DockerRegistrySuite) TestInspectImageWithDigests(c *check.C) {
|
|
|
|
digest, err := setupImage(c)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("error setting up image: %v", err))
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, digest)
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
|
|
|
dockerCmd(c, "pull", imageReference)
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "inspect", imageReference)
|
|
|
|
|
|
|
|
var imageJSON []types.ImageInspect
|
|
|
|
if err = json.Unmarshal([]byte(out), &imageJSON); err != nil {
|
|
|
|
c.Fatalf("unable to unmarshal body for latest version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Assert(len(imageJSON), check.Equals, 1)
|
|
|
|
c.Assert(len(imageJSON[0].RepoDigests), check.Equals, 1)
|
|
|
|
c.Assert(stringutils.InSlice(imageJSON[0].RepoDigests, imageReference), check.Equals, true)
|
|
|
|
}
|
|
|
|
|
2015-08-20 03:57:15 -04:00
|
|
|
func (s *DockerRegistrySuite) TestPsListContainersFilterAncestorImageByDigest(c *check.C) {
|
|
|
|
digest, err := setupImage(c)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("error setting up image: %v", err))
|
|
|
|
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, digest)
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
|
|
|
dockerCmd(c, "pull", imageReference)
|
|
|
|
|
|
|
|
// build a image from it
|
|
|
|
imageName1 := "images_ps_filter_test"
|
|
|
|
_, err = buildImage(imageName1, fmt.Sprintf(
|
|
|
|
`FROM %s
|
|
|
|
LABEL match me 1`, imageReference), true)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// run a container based on that
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", imageReference, "echo", "hello")
|
|
|
|
expectedID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
// run a container based on the a descendant of that too
|
|
|
|
out, _ = dockerCmd(c, "run", "-d", imageName1, "echo", "hello")
|
|
|
|
expectedID1 := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
expectedIDs := []string{expectedID, expectedID1}
|
|
|
|
|
|
|
|
// Invalid imageReference
|
|
|
|
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", fmt.Sprintf("--filter=ancestor=busybox@%s", digest))
|
|
|
|
if strings.TrimSpace(out) != "" {
|
|
|
|
c.Fatalf("Expected filter container for %s ancestor filter to be empty, got %v", fmt.Sprintf("busybox@%s", digest), strings.TrimSpace(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid imageReference
|
|
|
|
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageReference)
|
|
|
|
checkPsAncestorFilterOutput(c, out, imageReference, expectedIDs)
|
|
|
|
}
|
|
|
|
|
2015-04-24 17:16:56 -04:00
|
|
|
func (s *DockerRegistrySuite) TestDeleteImageByIDOnlyPulledByDigest(c *check.C) {
|
2015-07-14 02:35:36 -04:00
|
|
|
pushDigest, err := setupImage(c)
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error setting up image: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// pull from the registry using the <name>@<digest> reference
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, pushDigest)
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "pull", imageReference)
|
2015-02-26 21:23:50 -05:00
|
|
|
// just in case...
|
|
|
|
|
2015-07-08 15:41:01 -04:00
|
|
|
imageID, err := inspectField(imageReference, "Id")
|
2015-02-26 21:23:50 -05:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("error inspecting image id: %v", err)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "rmi", imageID)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
2015-08-01 02:27:19 -04:00
|
|
|
|
|
|
|
// TestPullFailsWithAlteredManifest tests that a `docker pull` fails when
|
|
|
|
// we have modified a manifest blob and its digest cannot be verified.
|
|
|
|
func (s *DockerRegistrySuite) TestPullFailsWithAlteredManifest(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-01 02:27:19 -04:00
|
|
|
manifestDigest, err := setupImage(c)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("error setting up image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the target manifest blob.
|
|
|
|
manifestBlob := s.reg.readBlobContents(c, manifestDigest)
|
|
|
|
|
2015-10-30 20:46:25 -04:00
|
|
|
var imgManifest schema1.Manifest
|
2015-08-01 02:27:19 -04:00
|
|
|
if err := json.Unmarshal(manifestBlob, &imgManifest); err != nil {
|
|
|
|
c.Fatalf("unable to decode image manifest from blob: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-30 20:46:25 -04:00
|
|
|
// Change a layer in the manifest.
|
|
|
|
imgManifest.FSLayers[0] = schema1.FSLayer{
|
2015-08-01 02:27:19 -04:00
|
|
|
BlobSum: digest.Digest("sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"),
|
2015-10-30 20:46:25 -04:00
|
|
|
}
|
2015-08-01 02:27:19 -04:00
|
|
|
|
|
|
|
// Move the existing data file aside, so that we can replace it with a
|
|
|
|
// malicious blob of data. NOTE: we defer the returned undo func.
|
|
|
|
undo := s.reg.tempMoveBlobData(c, manifestDigest)
|
|
|
|
defer undo()
|
|
|
|
|
2015-10-30 20:46:25 -04:00
|
|
|
alteredManifestBlob, err := json.MarshalIndent(imgManifest, "", " ")
|
2015-08-01 02:27:19 -04:00
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("unable to encode altered image manifest to JSON: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.reg.writeBlobContents(c, manifestDigest, alteredManifestBlob)
|
|
|
|
|
|
|
|
// Now try pulling that image by digest. We should get an error about
|
|
|
|
// digest verification for the manifest digest.
|
|
|
|
|
|
|
|
// Pull from the registry using the <name>@<digest> reference.
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
|
|
|
|
out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
|
|
|
|
if exitStatus == 0 {
|
2015-08-05 20:47:37 -04:00
|
|
|
c.Fatalf("expected a non-zero exit status but got %d: %s", exitStatus, out)
|
2015-08-01 02:27:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedErrorMsg := fmt.Sprintf("image verification failed for digest %s", manifestDigest)
|
|
|
|
if !strings.Contains(out, expectedErrorMsg) {
|
|
|
|
c.Fatalf("expected error message %q in output: %s", expectedErrorMsg, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPullFailsWithAlteredLayer tests that a `docker pull` fails when
|
|
|
|
// we have modified a layer blob and its digest cannot be verified.
|
|
|
|
func (s *DockerRegistrySuite) TestPullFailsWithAlteredLayer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-01 02:27:19 -04:00
|
|
|
manifestDigest, err := setupImage(c)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("error setting up image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the target manifest blob.
|
|
|
|
manifestBlob := s.reg.readBlobContents(c, manifestDigest)
|
|
|
|
|
2015-10-30 20:46:25 -04:00
|
|
|
var imgManifest schema1.Manifest
|
2015-08-01 02:27:19 -04:00
|
|
|
if err := json.Unmarshal(manifestBlob, &imgManifest); err != nil {
|
|
|
|
c.Fatalf("unable to decode image manifest from blob: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, get the digest of one of the layers from the manifest.
|
|
|
|
targetLayerDigest := imgManifest.FSLayers[0].BlobSum
|
|
|
|
|
|
|
|
// Move the existing data file aside, so that we can replace it with a
|
|
|
|
// malicious blob of data. NOTE: we defer the returned undo func.
|
|
|
|
undo := s.reg.tempMoveBlobData(c, targetLayerDigest)
|
|
|
|
defer undo()
|
|
|
|
|
|
|
|
// Now make a fake data blob in this directory.
|
|
|
|
s.reg.writeBlobContents(c, targetLayerDigest, []byte("This is not the data you are looking for."))
|
|
|
|
|
|
|
|
// Now try pulling that image by digest. We should get an error about
|
|
|
|
// digest verification for the target layer digest.
|
|
|
|
|
|
|
|
// Pull from the registry using the <name>@<digest> reference.
|
|
|
|
imageReference := fmt.Sprintf("%s@%s", repoName, manifestDigest)
|
|
|
|
out, exitStatus, _ := dockerCmdWithError("pull", imageReference)
|
|
|
|
if exitStatus == 0 {
|
|
|
|
c.Fatalf("expected a zero exit status but got: %d", exitStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedErrorMsg := fmt.Sprintf("filesystem layer verification failed for digest %s", targetLayerDigest)
|
|
|
|
if !strings.Contains(out, expectedErrorMsg) {
|
|
|
|
c.Fatalf("expected error message %q in output: %s", expectedErrorMsg, out)
|
|
|
|
}
|
|
|
|
}
|