2015-08-27 00:58:56 +03:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2015-11-19 01:18:07 +03:00
|
|
|
"encoding/json"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-08-27 00:58:56 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-11-19 01:18:07 +03:00
|
|
|
const sampleImageJSON = `{
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"config": {},
|
|
|
|
"rootfs": {
|
|
|
|
"type": "layers",
|
|
|
|
"diff_ids": []
|
|
|
|
}
|
|
|
|
}`
|
2015-08-27 00:58:56 +03:00
|
|
|
|
2015-11-19 01:18:07 +03:00
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
img, err := NewFromJSON([]byte(sampleImageJSON))
|
2015-08-27 00:58:56 +03:00
|
|
|
if err != nil {
|
2015-11-19 01:18:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rawJSON := img.RawJSON()
|
|
|
|
if string(rawJSON) != sampleImageJSON {
|
2016-12-13 17:15:08 +03:00
|
|
|
t.Fatalf("raw JSON of config didn't match: expected %+v, got %v", sampleImageJSON, rawJSON)
|
2015-08-27 00:58:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:18:07 +03:00
|
|
|
func TestInvalidJSON(t *testing.T) {
|
|
|
|
_, err := NewFromJSON([]byte("{}"))
|
|
|
|
if err == nil {
|
2016-12-13 17:15:08 +03:00
|
|
|
t.Fatal("expected JSON parse error")
|
2015-08-27 00:58:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:18:07 +03:00
|
|
|
func TestMarshalKeyOrder(t *testing.T) {
|
|
|
|
b, err := json.Marshal(&Image{
|
|
|
|
V1Image: V1Image{
|
|
|
|
Comment: "a",
|
|
|
|
Author: "b",
|
|
|
|
Architecture: "c",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedOrder := []string{"architecture", "author", "comment"}
|
|
|
|
var indexes []int
|
|
|
|
for _, k := range expectedOrder {
|
|
|
|
indexes = append(indexes, strings.Index(string(b), k))
|
|
|
|
}
|
2015-08-27 00:58:56 +03:00
|
|
|
|
2015-11-19 01:18:07 +03:00
|
|
|
if !sort.IntsAreSorted(indexes) {
|
|
|
|
t.Fatal("invalid key order in JSON: ", string(b))
|
2015-08-27 00:58:56 +03:00
|
|
|
}
|
|
|
|
}
|