2015-12-17 01:26:13 +00:00
|
|
|
package manifestlist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2021-10-21 22:09:39 +00:00
|
|
|
"github.com/docker/distribution/manifest/ocischema"
|
|
|
|
|
2019-10-09 12:02:21 +00:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2015-12-17 01:26:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var expectedManifestListSerialization = []byte(`{
|
|
|
|
"schemaVersion": 2,
|
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
|
|
|
|
"manifests": [
|
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
|
|
|
|
"size": 985,
|
|
|
|
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
"platform": {
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"features": [
|
|
|
|
"sse4"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
|
|
|
|
"size": 2392,
|
|
|
|
"digest": "sha256:6346340964309634683409684360934680934608934608934608934068934608",
|
|
|
|
"platform": {
|
|
|
|
"architecture": "sun4m",
|
|
|
|
"os": "sunos"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
func makeTestManifestList(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
|
2015-12-17 01:26:13 +00:00
|
|
|
manifestDescriptors := []ManifestDescriptor{
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
|
|
|
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
Size: 985,
|
|
|
|
MediaType: "application/vnd.docker.distribution.manifest.v2+json",
|
|
|
|
},
|
|
|
|
Platform: PlatformSpec{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
Features: []string{"sse4"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
|
|
|
Digest: "sha256:6346340964309634683409684360934680934608934608934608934068934608",
|
|
|
|
Size: 2392,
|
|
|
|
MediaType: "application/vnd.docker.distribution.manifest.v2+json",
|
|
|
|
},
|
|
|
|
Platform: PlatformSpec{
|
|
|
|
Architecture: "sun4m",
|
|
|
|
OS: "sunos",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
deserialized, err := FromDescriptorsWithMediaType(manifestDescriptors, mediaType)
|
2015-12-17 01:26:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating DeserializedManifestList: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
return manifestDescriptors, deserialized
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestList(t *testing.T) {
|
|
|
|
manifestDescriptors, deserialized := makeTestManifestList(t, MediaTypeManifestList)
|
2016-11-17 18:28:05 +00:00
|
|
|
mediaType, canonical, _ := deserialized.Payload()
|
2015-12-17 01:26:13 +00:00
|
|
|
|
|
|
|
if mediaType != MediaTypeManifestList {
|
|
|
|
t.Fatalf("unexpected media type: %s", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the canonical field is the same as json.MarshalIndent
|
|
|
|
// with these parameters.
|
|
|
|
p, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marshaling manifest list: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(p, canonical) {
|
|
|
|
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the canonical field has the expected value.
|
|
|
|
if !bytes.Equal(expectedManifestListSerialization, canonical) {
|
|
|
|
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestListSerialization))
|
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalled DeserializedManifestList
|
|
|
|
if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
|
|
|
|
t.Fatalf("error unmarshaling manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&unmarshalled, deserialized) {
|
|
|
|
t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
|
|
|
|
}
|
|
|
|
|
|
|
|
references := deserialized.References()
|
|
|
|
if len(references) != 2 {
|
|
|
|
t.Fatalf("unexpected number of references: %d", len(references))
|
|
|
|
}
|
|
|
|
for i := range references {
|
|
|
|
if !reflect.DeepEqual(references[i], manifestDescriptors[i].Descriptor) {
|
|
|
|
t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
|
2017-08-01 22:27:52 +00:00
|
|
|
// TODO (mikebrow): add annotations on the manifest list (index) and support for
|
|
|
|
// empty platform structs (move to Platform *Platform `json:"platform,omitempty"`
|
|
|
|
// from current Platform PlatformSpec `json:"platform"`) in the manifest descriptor.
|
|
|
|
// Requires changes to docker/distribution/manifest/manifestlist.ManifestList and .ManifestDescriptor
|
|
|
|
// and associated serialization APIs in manifestlist.go. Or split the OCI index and
|
|
|
|
// docker manifest list implementations, which would require a lot of refactoring.
|
2017-07-11 19:19:47 +00:00
|
|
|
var expectedOCIImageIndexSerialization = []byte(`{
|
2016-11-17 18:28:05 +00:00
|
|
|
"schemaVersion": 2,
|
2017-07-11 19:19:47 +00:00
|
|
|
"mediaType": "application/vnd.oci.image.index.v1+json",
|
2016-11-17 18:28:05 +00:00
|
|
|
"manifests": [
|
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
"size": 985,
|
|
|
|
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
"platform": {
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"features": [
|
|
|
|
"sse4"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2017-08-01 22:27:52 +00:00
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
"size": 985,
|
|
|
|
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
"annotations": {
|
|
|
|
"platform": "none"
|
|
|
|
},
|
|
|
|
"platform": {
|
|
|
|
"architecture": "",
|
|
|
|
"os": ""
|
|
|
|
}
|
|
|
|
},
|
2016-11-17 18:28:05 +00:00
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
"size": 2392,
|
|
|
|
"digest": "sha256:6346340964309634683409684360934680934608934608934608934068934608",
|
2017-08-01 22:27:52 +00:00
|
|
|
"annotations": {
|
|
|
|
"what": "for"
|
|
|
|
},
|
2016-11-17 18:28:05 +00:00
|
|
|
"platform": {
|
|
|
|
"architecture": "sun4m",
|
|
|
|
"os": "sunos"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
func makeTestOCIImageIndex(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
|
2016-11-17 18:28:05 +00:00
|
|
|
manifestDescriptors := []ManifestDescriptor{
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
|
|
|
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
Size: 985,
|
|
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
},
|
|
|
|
Platform: PlatformSpec{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
Features: []string{"sse4"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
2017-08-01 22:27:52 +00:00
|
|
|
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
Size: 985,
|
|
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
Annotations: map[string]string{"platform": "none"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
|
|
|
Digest: "sha256:6346340964309634683409684360934680934608934608934608934068934608",
|
|
|
|
Size: 2392,
|
|
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
Annotations: map[string]string{"what": "for"},
|
2016-11-17 18:28:05 +00:00
|
|
|
},
|
|
|
|
Platform: PlatformSpec{
|
|
|
|
Architecture: "sun4m",
|
|
|
|
OS: "sunos",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
deserialized, err := FromDescriptorsWithMediaType(manifestDescriptors, mediaType)
|
2016-11-17 18:28:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating DeserializedManifestList: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
return manifestDescriptors, deserialized
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOCIImageIndex(t *testing.T) {
|
|
|
|
manifestDescriptors, deserialized := makeTestOCIImageIndex(t, v1.MediaTypeImageIndex)
|
|
|
|
|
2016-11-17 18:28:05 +00:00
|
|
|
mediaType, canonical, _ := deserialized.Payload()
|
|
|
|
|
2017-07-11 19:19:47 +00:00
|
|
|
if mediaType != v1.MediaTypeImageIndex {
|
2016-11-17 18:28:05 +00:00
|
|
|
t.Fatalf("unexpected media type: %s", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the canonical field is the same as json.MarshalIndent
|
|
|
|
// with these parameters.
|
|
|
|
p, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marshaling manifest list: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(p, canonical) {
|
|
|
|
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the canonical field has the expected value.
|
2017-07-11 19:19:47 +00:00
|
|
|
if !bytes.Equal(expectedOCIImageIndexSerialization, canonical) {
|
2017-08-01 22:27:52 +00:00
|
|
|
t.Fatalf("manifest bytes not equal to expected: %q != %q", string(canonical), string(expectedOCIImageIndexSerialization))
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalled DeserializedManifestList
|
|
|
|
if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
|
|
|
|
t.Fatalf("error unmarshaling manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&unmarshalled, deserialized) {
|
|
|
|
t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
|
|
|
|
}
|
|
|
|
|
|
|
|
references := deserialized.References()
|
2017-08-01 22:27:52 +00:00
|
|
|
if len(references) != 3 {
|
2016-11-17 18:28:05 +00:00
|
|
|
t.Fatalf("unexpected number of references: %d", len(references))
|
|
|
|
}
|
|
|
|
for i := range references {
|
|
|
|
if !reflect.DeepEqual(references[i], manifestDescriptors[i].Descriptor) {
|
|
|
|
t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 20:55:45 +00:00
|
|
|
|
|
|
|
func mediaTypeTest(t *testing.T, contentType string, mediaType string, shouldError bool) {
|
|
|
|
var m *DeserializedManifestList
|
|
|
|
if contentType == MediaTypeManifestList {
|
|
|
|
_, m = makeTestManifestList(t, mediaType)
|
|
|
|
} else {
|
|
|
|
_, m = makeTestOCIImageIndex(t, mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, canonical, err := m.Payload()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting payload, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshalled, descriptor, err := distribution.UnmarshalManifest(
|
|
|
|
contentType,
|
|
|
|
canonical)
|
|
|
|
|
|
|
|
if shouldError {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad content type should have produced error")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error unmarshaling manifest, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
asManifest := unmarshalled.(*DeserializedManifestList)
|
|
|
|
if asManifest.MediaType != mediaType {
|
|
|
|
t.Fatalf("Bad media type '%v' as unmarshalled", asManifest.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if descriptor.MediaType != contentType {
|
|
|
|
t.Fatalf("Bad media type '%v' for descriptor", descriptor.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshalledMediaType, _, _ := unmarshalled.Payload()
|
|
|
|
if unmarshalledMediaType != contentType {
|
|
|
|
t.Fatalf("Bad media type '%v' for payload", unmarshalledMediaType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMediaTypes(t *testing.T) {
|
|
|
|
mediaTypeTest(t, MediaTypeManifestList, "", true)
|
|
|
|
mediaTypeTest(t, MediaTypeManifestList, MediaTypeManifestList, false)
|
|
|
|
mediaTypeTest(t, MediaTypeManifestList, MediaTypeManifestList+"XXX", true)
|
2018-03-14 21:22:36 +00:00
|
|
|
mediaTypeTest(t, v1.MediaTypeImageIndex, "", false)
|
2018-03-14 20:55:45 +00:00
|
|
|
mediaTypeTest(t, v1.MediaTypeImageIndex, v1.MediaTypeImageIndex, false)
|
|
|
|
mediaTypeTest(t, v1.MediaTypeImageIndex, v1.MediaTypeImageIndex+"XXX", true)
|
|
|
|
}
|
2021-10-21 22:09:39 +00:00
|
|
|
|
|
|
|
func TestValidateManifest(t *testing.T) {
|
|
|
|
manifest := ocischema.Manifest{
|
|
|
|
Config: distribution.Descriptor{Size: 1},
|
|
|
|
Layers: []distribution.Descriptor{{Size: 2}},
|
|
|
|
}
|
|
|
|
index := ManifestList{
|
|
|
|
Manifests: []ManifestDescriptor{
|
|
|
|
{Descriptor: distribution.Descriptor{Size: 3}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
b, err := json.Marshal(index)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error marshaling index", err)
|
|
|
|
}
|
|
|
|
if err := validateIndex(b); err != nil {
|
|
|
|
t.Error("index should be valid", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
b, err := json.Marshal(manifest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error marshaling manifest", err)
|
|
|
|
}
|
|
|
|
if err := validateIndex(b); err == nil {
|
|
|
|
t.Error("manifest should not be valid")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|