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

     1  package manifestlist
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/docker/distribution"
    10  )
    11  
    12  var expectedManifestListSerialization = []byte(`{
    13     "schemaVersion": 2,
    14     "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
    15     "manifests": [
    16        {
    17           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    18           "size": 985,
    19           "digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
    20           "platform": {
    21              "architecture": "amd64",
    22              "os": "linux",
    23              "features": [
    24                 "sse4"
    25              ]
    26           }
    27        },
    28        {
    29           "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
    30           "size": 2392,
    31           "digest": "sha256:6346340964309634683409684360934680934608934608934608934068934608",
    32           "platform": {
    33              "architecture": "sun4m",
    34              "os": "sunos"
    35           }
    36        }
    37     ]
    38  }`)
    39  
    40  func TestManifestList(t *testing.T) {
    41  	manifestDescriptors := []ManifestDescriptor{
    42  		{
    43  			Descriptor: distribution.Descriptor{
    44  				Digest:    "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
    45  				Size:      985,
    46  				MediaType: "application/vnd.docker.distribution.manifest.v2+json",
    47  			},
    48  			Platform: PlatformSpec{
    49  				Architecture: "amd64",
    50  				OS:           "linux",
    51  				Features:     []string{"sse4"},
    52  			},
    53  		},
    54  		{
    55  			Descriptor: distribution.Descriptor{
    56  				Digest:    "sha256:6346340964309634683409684360934680934608934608934608934068934608",
    57  				Size:      2392,
    58  				MediaType: "application/vnd.docker.distribution.manifest.v2+json",
    59  			},
    60  			Platform: PlatformSpec{
    61  				Architecture: "sun4m",
    62  				OS:           "sunos",
    63  			},
    64  		},
    65  	}
    66  
    67  	deserialized, err := FromDescriptors(manifestDescriptors)
    68  	if err != nil {
    69  		t.Fatalf("error creating DeserializedManifestList: %v", err)
    70  	}
    71  
    72  	mediaType, canonical, err := deserialized.Payload()
    73  
    74  	if mediaType != MediaTypeManifestList {
    75  		t.Fatalf("unexpected media type: %s", mediaType)
    76  	}
    77  
    78  	// Check that the canonical field is the same as json.MarshalIndent
    79  	// with these parameters.
    80  	p, err := json.MarshalIndent(&deserialized.ManifestList, "", "   ")
    81  	if err != nil {
    82  		t.Fatalf("error marshaling manifest list: %v", err)
    83  	}
    84  	if !bytes.Equal(p, canonical) {
    85  		t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p))
    86  	}
    87  
    88  	// Check that the canonical field has the expected value.
    89  	if !bytes.Equal(expectedManifestListSerialization, canonical) {
    90  		t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestListSerialization))
    91  	}
    92  
    93  	var unmarshalled DeserializedManifestList
    94  	if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
    95  		t.Fatalf("error unmarshaling manifest: %v", err)
    96  	}
    97  
    98  	if !reflect.DeepEqual(&unmarshalled, deserialized) {
    99  		t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
   100  	}
   101  
   102  	references := deserialized.References()
   103  	if len(references) != 2 {
   104  		t.Fatalf("unexpected number of references: %d", len(references))
   105  	}
   106  	for i := range references {
   107  		if !reflect.DeepEqual(references[i], manifestDescriptors[i].Descriptor) {
   108  			t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
   109  		}
   110  	}
   111  }