use docker/distribution/digest

Signed-off-by: Crazykev <crazykev@zju.edu.cn>
This commit is contained in:
Crazykev 2016-11-15 11:15:21 +08:00
parent 1c76bc950d
commit 8b73542d89
5 changed files with 22 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/containers/image/docker" "github.com/containers/image/docker"
"github.com/containers/image/manifest" "github.com/containers/image/manifest"
"github.com/docker/distribution/digest"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -14,7 +15,7 @@ import (
type inspectOutput struct { type inspectOutput struct {
Name string `json:",omitempty"` Name string `json:",omitempty"`
Tag string `json:",omitempty"` Tag string `json:",omitempty"`
Digest string Digest digest.Digest
RepoTags []string RepoTags []string
Created time.Time Created time.Time
DockerVersion string DockerVersion string

View File

@ -11,6 +11,7 @@ import (
"github.com/containers/image/image" "github.com/containers/image/image"
"github.com/containers/image/manifest" "github.com/containers/image/manifest"
"github.com/containers/image/types" "github.com/containers/image/types"
"github.com/docker/distribution/digest"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -39,10 +40,21 @@ var layersCmd = cli.Command{
} }
defer src.Close() defer src.Close()
blobDigests := c.Args().Tail() var blobDigests []digest.Digest
for _, dString := range c.Args().Tail() {
if !strings.HasPrefix(dString, "sha256:") {
dString = "sha256:" + dString
}
d, err := digest.ParseDigest(dString)
if err != nil {
return err
}
blobDigests = append(blobDigests, d)
}
if len(blobDigests) == 0 { if len(blobDigests) == 0 {
layers := src.LayerInfos() layers := src.LayerInfos()
seenLayers := map[string]struct{}{} seenLayers := map[digest.Digest]struct{}{}
for _, info := range layers { for _, info := range layers {
if _, ok := seenLayers[info.Digest]; !ok { if _, ok := seenLayers[info.Digest]; !ok {
blobDigests = append(blobDigests, info.Digest) blobDigests = append(blobDigests, info.Digest)
@ -70,9 +82,6 @@ var layersCmd = cli.Command{
defer dest.Close() defer dest.Close()
for _, digest := range blobDigests { for _, digest := range blobDigests {
if !strings.HasPrefix(digest, "sha256:") {
digest = "sha256:" + digest
}
r, blobSize, err := rawSource.GetBlob(digest) r, blobSize, err := rawSource.GetBlob(digest)
if err != nil { if err != nil {
return err return err

View File

@ -27,5 +27,5 @@ func TestManifestDigest(t *testing.T) {
// Success // Success
out, err = runSkopeo("manifest-digest", "fixtures/image.manifest.json") out, err = runSkopeo("manifest-digest", "fixtures/image.manifest.json")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fixturesTestImageManifestDigest+"\n", out) assert.Equal(t, fixturesTestImageManifestDigest.String()+"\n", out)
} }

View File

@ -6,13 +6,14 @@ import (
"testing" "testing"
"github.com/containers/image/signature" "github.com/containers/image/signature"
"github.com/docker/distribution/digest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
const ( const (
// fixturesTestImageManifestDigest is the Docker manifest digest of "image.manifest.json" // fixturesTestImageManifestDigest is the Docker manifest digest of "image.manifest.json"
fixturesTestImageManifestDigest = "sha256:20bf21ed457b390829cdbeec8795a7bea1626991fda603e0d01b4e7f60427e55" fixturesTestImageManifestDigest = digest.Digest("sha256:20bf21ed457b390829cdbeec8795a7bea1626991fda603e0d01b4e7f60427e55")
// fixturesTestKeyFingerprint is the fingerprint of the private key. // fixturesTestKeyFingerprint is the fingerprint of the private key.
fixturesTestKeyFingerprint = "1D8230F6CDB6A06716E414C1DB72F2188BB46CC8" fixturesTestKeyFingerprint = "1D8230F6CDB6A06716E414C1DB72F2188BB46CC8"
) )
@ -122,5 +123,5 @@ func TestStandaloneVerify(t *testing.T) {
out, err = runSkopeo("standalone-verify", manifestPath, out, err = runSkopeo("standalone-verify", manifestPath,
dockerReference, fixturesTestKeyFingerprint, signaturePath) dockerReference, fixturesTestKeyFingerprint, signaturePath)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "Signature verified, digest "+fixturesTestImageManifestDigest+"\n", out) assert.Equal(t, "Signature verified, digest "+fixturesTestImageManifestDigest.String()+"\n", out)
} }

View File

@ -11,6 +11,7 @@ import (
"strings" "strings"
"github.com/containers/image/manifest" "github.com/containers/image/manifest"
"github.com/docker/distribution/digest"
"github.com/go-check/check" "github.com/go-check/check"
) )
@ -167,7 +168,7 @@ func (s *CopySuite) TestCopyStreaming(c *check.C) {
assertSkopeoSucceeds(c, "", "--tls-verify=false", "copy", "atomic:localhost:5000/myns/unsigned:streaming", "dir:"+dir2) assertSkopeoSucceeds(c, "", "--tls-verify=false", "copy", "atomic:localhost:5000/myns/unsigned:streaming", "dir:"+dir2)
// The manifests will have different JWS signatures; so, compare the manifests by digests, which // The manifests will have different JWS signatures; so, compare the manifests by digests, which
// strips the signatures, and remove them, comparing the rest file by file. // strips the signatures, and remove them, comparing the rest file by file.
digests := []string{} digests := []digest.Digest{}
for _, dir := range []string{dir1, dir2} { for _, dir := range []string{dir1, dir2} {
manifestPath := filepath.Join(dir, "manifest.json") manifestPath := filepath.Join(dir, "manifest.json")
m, err := ioutil.ReadFile(manifestPath) m, err := ioutil.ReadFile(manifestPath)