github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/manifest/schema2/manifest_test.go (about)

     1  package schema2
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/docker/distribution"
    10  )
    11  
    12  var expectedManifestSerialization = []byte(`{
    13     "schemaVersion": 2,
    14     "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    15     "config": {
    16        "mediaType": "application/vnd.docker.container.image.v1+json",
    17        "size": 985,
    18        "digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
    19     },
    20     "layers": [
    21        {
    22           "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
    23           "size": 153263,
    24           "digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
    25        }
    26     ]
    27  }`)
    28  
    29  func TestManifest(t *testing.T) {
    30  	manifest := Manifest{
    31  		Versioned: SchemaVersion,
    32  		Config: distribution.Descriptor{
    33  			Digest:    "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
    34  			Size:      985,
    35  			MediaType: MediaTypeConfig,
    36  		},
    37  		Layers: []distribution.Descriptor{
    38  			{
    39  				Digest:    "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b",
    40  				Size:      153263,
    41  				MediaType: MediaTypeLayer,
    42  			},
    43  		},
    44  	}
    45  
    46  	deserialized, err := FromStruct(manifest)
    47  	if err != nil {
    48  		t.Fatalf("error creating DeserializedManifest: %v", err)
    49  	}
    50  
    51  	mediaType, canonical, err := deserialized.Payload()
    52  
    53  	if mediaType != MediaTypeManifest {
    54  		t.Fatalf("unexpected media type: %s", mediaType)
    55  	}
    56  
    57  	// Check that the canonical field is the same as json.MarshalIndent
    58  	// with these parameters.
    59  	p, err := json.MarshalIndent(&manifest, "", "   ")
    60  	if err != nil {
    61  		t.Fatalf("error marshaling manifest: %v", err)
    62  	}
    63  	if !bytes.Equal(p, canonical) {
    64  		t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p))
    65  	}
    66  
    67  	// Check that canonical field matches expected value.
    68  	if !bytes.Equal(expectedManifestSerialization, canonical) {
    69  		t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestSerialization))
    70  	}
    71  
    72  	var unmarshalled DeserializedManifest
    73  	if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
    74  		t.Fatalf("error unmarshaling manifest: %v", err)
    75  	}
    76  
    77  	if !reflect.DeepEqual(&unmarshalled, deserialized) {
    78  		t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
    79  	}
    80  
    81  	target := deserialized.Target()
    82  	if target.Digest != "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b" {
    83  		t.Fatalf("unexpected digest in target: %s", target.Digest.String())
    84  	}
    85  	if target.MediaType != MediaTypeConfig {
    86  		t.Fatalf("unexpected media type in target: %s", target.MediaType)
    87  	}
    88  	if target.Size != 985 {
    89  		t.Fatalf("unexpected size in target: %d", target.Size)
    90  	}
    91  
    92  	references := deserialized.References()
    93  	if len(references) != 1 {
    94  		t.Fatalf("unexpected number of references: %d", len(references))
    95  	}
    96  	if references[0].Digest != "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b" {
    97  		t.Fatalf("unexpected digest in reference: %s", references[0].Digest.String())
    98  	}
    99  	if references[0].MediaType != MediaTypeLayer {
   100  		t.Fatalf("unexpected media type in reference: %s", references[0].MediaType)
   101  	}
   102  	if references[0].Size != 153263 {
   103  		t.Fatalf("unexpected size in reference: %d", references[0].Size)
   104  	}
   105  }