| 1 |
package schema2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/docker/distribution" |
| 10 |
"github.com/docker/distribution/manifest" |
| 11 |
) |
| 12 |
|
| 13 |
var expectedManifestSerialization = []byte(`{ |
| 14 |
"schemaVersion": 2, |
| 15 |
"mediaType": "application/vnd.docker.distribution.manifest.v2+json", |
| 16 |
"config": { |
| 17 |
"mediaType": "application/vnd.docker.container.image.v1+json", |
| 18 |
"size": 985, |
| 19 |
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b" |
| 20 |
}, |
| 21 |
"layers": [ |
| 22 |
{ |
| 23 |
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 24 |
"size": 153263, |
| 25 |
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b" |
| 26 |
} |
| 27 |
] |
| 28 |
}`) |
| 29 |
|
| 30 |
func makeTestManifest(mediaType string) Manifest { |
| 31 |
return Manifest{ |
| 32 |
Versioned: manifest.Versioned{ |
| 33 |
SchemaVersion: 2, |
| 34 |
MediaType: mediaType, |
| 35 |
}, |
| 36 |
Config: distribution.Descriptor{ |
| 37 |
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b", |
| 38 |
Size: 985, |
| 39 |
MediaType: MediaTypeImageConfig, |
| 40 |
}, |
| 41 |
Layers: []distribution.Descriptor{ |
| 42 |
{ |
| 43 |
Digest: "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b", |
| 44 |
Size: 153263, |
| 45 |
MediaType: MediaTypeLayer, |
| 46 |
}, |
| 47 |
}, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestManifest(t *testing.T) { |
| 52 |
manifest := makeTestManifest(MediaTypeManifest) |
| 53 |
|
| 54 |
deserialized, err := FromStruct(manifest) |
| 55 |
if err != nil { |
| 56 |
t.Fatalf("error creating DeserializedManifest: %v", err) |
| 57 |
} |
| 58 |
|
| 59 |
mediaType, canonical, _ := deserialized.Payload() |
| 60 |
|
| 61 |
if mediaType != MediaTypeManifest { |
| 62 |
t.Fatalf("unexpected media type: %s", mediaType) |
| 63 |
} |
| 64 |
|
| 65 |
// Check that the canonical field is the same as json.MarshalIndent |
| 66 |
// with these parameters. |
| 67 |
p, err := json.MarshalIndent(&manifest, "", " ") |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("error marshaling manifest: %v", err) |
| 70 |
} |
| 71 |
if !bytes.Equal(p, canonical) { |
| 72 |
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p)) |
| 73 |
} |
| 74 |
|
| 75 |
// Check that canonical field matches expected value. |
| 76 |
if !bytes.Equal(expectedManifestSerialization, canonical) { |
| 77 |
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestSerialization)) |
| 78 |
} |
| 79 |
|
| 80 |
var unmarshalled DeserializedManifest |
| 81 |
if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil { |
| 82 |
t.Fatalf("error unmarshaling manifest: %v", err) |
| 83 |
} |
| 84 |
|
| 85 |
if !reflect.DeepEqual(&unmarshalled, deserialized) { |
| 86 |
t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized) |
| 87 |
} |
| 88 |
|
| 89 |
target := deserialized.Target() |
| 90 |
if target.Digest != "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b" { |
| 91 |
t.Fatalf("unexpected digest in target: %s", target.Digest.String()) |
| 92 |
} |
| 93 |
if target.MediaType != MediaTypeImageConfig { |
| 94 |
t.Fatalf("unexpected media type in target: %s", target.MediaType) |
| 95 |
} |
| 96 |
if target.Size != 985 { |
| 97 |
t.Fatalf("unexpected size in target: %d", target.Size) |
| 98 |
} |
| 99 |
|
| 100 |
references := deserialized.References() |
| 101 |
if len(references) != 2 { |
| 102 |
t.Fatalf("unexpected number of references: %d", len(references)) |
| 103 |
} |
| 104 |
|
| 105 |
if !reflect.DeepEqual(references[0], target) { |
| 106 |
t.Fatalf("first reference should be target: %v != %v", references[0], target) |
| 107 |
} |
| 108 |
|
| 109 |
// Test the second reference |
| 110 |
if references[1].Digest != "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b" { |
| 111 |
t.Fatalf("unexpected digest in reference: %s", references[0].Digest.String()) |
| 112 |
} |
| 113 |
if references[1].MediaType != MediaTypeLayer { |
| 114 |
t.Fatalf("unexpected media type in reference: %s", references[0].MediaType) |
| 115 |
} |
| 116 |
if references[1].Size != 153263 { |
| 117 |
t.Fatalf("unexpected size in reference: %d", references[0].Size) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func mediaTypeTest(t *testing.T, mediaType string, shouldError bool) { |
| 122 |
manifest := makeTestManifest(mediaType) |
| 123 |
|
| 124 |
deserialized, err := FromStruct(manifest) |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("error creating DeserializedManifest: %v", err) |
| 127 |
} |
| 128 |
|
| 129 |
unmarshalled, descriptor, err := distribution.UnmarshalManifest( |
| 130 |
MediaTypeManifest, |
| 131 |
deserialized.canonical) |
| 132 |
|
| 133 |
if shouldError { |
| 134 |
if err == nil { |
| 135 |
t.Fatalf("bad content type should have produced error") |
| 136 |
} |
| 137 |
} else { |
| 138 |
if err != nil { |
| 139 |
t.Fatalf("error unmarshaling manifest, %v", err) |
| 140 |
} |
| 141 |
|
| 142 |
asManifest := unmarshalled.(*DeserializedManifest) |
| 143 |
if asManifest.MediaType != mediaType { |
| 144 |
t.Fatalf("Bad media type '%v' as unmarshalled", asManifest.MediaType) |
| 145 |
} |
| 146 |
|
| 147 |
if descriptor.MediaType != MediaTypeManifest { |
| 148 |
t.Fatalf("Bad media type '%v' for descriptor", descriptor.MediaType) |
| 149 |
} |
| 150 |
|
| 151 |
unmarshalledMediaType, _, _ := unmarshalled.Payload() |
| 152 |
if unmarshalledMediaType != MediaTypeManifest { |
| 153 |
t.Fatalf("Bad media type '%v' for payload", unmarshalledMediaType) |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestMediaTypes(t *testing.T) { |
| 159 |
mediaTypeTest(t, "", true) |
| 160 |
mediaTypeTest(t, MediaTypeManifest, false) |
| 161 |
mediaTypeTest(t, MediaTypeManifest+"XXX", true) |
| 162 |
} |