github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/image/image_test.go (about)

     1  package image
     2  
     3  import (
     4  	"encoding/json"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  const sampleImageJSON = `{
    14  	"architecture": "amd64",
    15  	"os": "linux",
    16  	"config": {},
    17  	"rootfs": {
    18  		"type": "layers",
    19  		"diff_ids": []
    20  	}
    21  }`
    22  
    23  func TestNewFromJSON(t *testing.T) {
    24  	img, err := NewFromJSON([]byte(sampleImageJSON))
    25  	require.NoError(t, err)
    26  	assert.Equal(t, sampleImageJSON, string(img.RawJSON()))
    27  }
    28  
    29  func TestNewFromJSONWithInvalidJSON(t *testing.T) {
    30  	_, err := NewFromJSON([]byte("{}"))
    31  	assert.EqualError(t, err, "invalid image JSON, no RootFS key")
    32  }
    33  
    34  func TestMarshalKeyOrder(t *testing.T) {
    35  	b, err := json.Marshal(&Image{
    36  		V1Image: V1Image{
    37  			Comment:      "a",
    38  			Author:       "b",
    39  			Architecture: "c",
    40  		},
    41  	})
    42  	assert.NoError(t, err)
    43  
    44  	expectedOrder := []string{"architecture", "author", "comment"}
    45  	var indexes []int
    46  	for _, k := range expectedOrder {
    47  		indexes = append(indexes, strings.Index(string(b), k))
    48  	}
    49  
    50  	if !sort.IntsAreSorted(indexes) {
    51  		t.Fatal("invalid key order in JSON: ", string(b))
    52  	}
    53  }