github.com/vincentwoo/docker@v0.7.3-0.20160116130405-82401a4b13c0/image/image_test.go (about)

     1  package image
     2  
     3  import (
     4  	"encoding/json"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  const sampleImageJSON = `{
    11  	"architecture": "amd64",
    12  	"os": "linux",
    13  	"config": {},
    14  	"rootfs": {
    15  		"type": "layers",
    16  		"diff_ids": []
    17  	}
    18  }`
    19  
    20  func TestJSON(t *testing.T) {
    21  	img, err := NewFromJSON([]byte(sampleImageJSON))
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	rawJSON := img.RawJSON()
    26  	if string(rawJSON) != sampleImageJSON {
    27  		t.Fatalf("Raw JSON of config didn't match: expected %+v, got %v", sampleImageJSON, rawJSON)
    28  	}
    29  }
    30  
    31  func TestInvalidJSON(t *testing.T) {
    32  	_, err := NewFromJSON([]byte("{}"))
    33  	if err == nil {
    34  		t.Fatal("Expected JSON parse error")
    35  	}
    36  }
    37  
    38  func TestMarshalKeyOrder(t *testing.T) {
    39  	b, err := json.Marshal(&Image{
    40  		V1Image: V1Image{
    41  			Comment:      "a",
    42  			Author:       "b",
    43  			Architecture: "c",
    44  		},
    45  	})
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	expectedOrder := []string{"architecture", "author", "comment"}
    51  	var indexes []int
    52  	for _, k := range expectedOrder {
    53  		indexes = append(indexes, strings.Index(string(b), k))
    54  	}
    55  
    56  	if !sort.IntsAreSorted(indexes) {
    57  		t.Fatal("invalid key order in JSON: ", string(b))
    58  	}
    59  }