2018-02-05 16:05:59 -05:00
|
|
|
package image // import "github.com/docker/docker/image"
|
2015-08-26 17:58:56 -04:00
|
|
|
|
|
|
|
import (
|
2015-11-18 17:18:07 -05:00
|
|
|
"encoding/json"
|
2017-09-17 21:58:08 -04:00
|
|
|
"runtime"
|
2015-11-18 17:18:07 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-08-26 17:58:56 -04:00
|
|
|
"testing"
|
2017-03-03 12:38:06 -05:00
|
|
|
|
2017-07-11 17:17:38 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/layer"
|
2017-12-22 16:30:49 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-08-26 17:58:56 -04:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
const sampleImageJSON = `{
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"config": {},
|
|
|
|
"rootfs": {
|
|
|
|
"type": "layers",
|
|
|
|
"diff_ids": []
|
|
|
|
}
|
|
|
|
}`
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
func TestNewFromJSON(t *testing.T) {
|
2015-11-18 17:18:07 -05:00
|
|
|
img, err := NewFromJSON([]byte(sampleImageJSON))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(sampleImageJSON, string(img.RawJSON())))
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
func TestNewFromJSONWithInvalidJSON(t *testing.T) {
|
2015-11-18 17:18:07 -05:00
|
|
|
_, err := NewFromJSON([]byte("{}"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Error(err, "invalid image JSON, no RootFS key"))
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
func TestMarshalKeyOrder(t *testing.T) {
|
|
|
|
b, err := json.Marshal(&Image{
|
|
|
|
V1Image: V1Image{
|
|
|
|
Comment: "a",
|
|
|
|
Author: "b",
|
|
|
|
Architecture: "c",
|
|
|
|
},
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, err)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
expectedOrder := []string{"architecture", "author", "comment"}
|
|
|
|
var indexes []int
|
|
|
|
for _, k := range expectedOrder {
|
|
|
|
indexes = append(indexes, strings.Index(string(b), k))
|
|
|
|
}
|
2015-08-26 17:58:56 -04:00
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
if !sort.IntsAreSorted(indexes) {
|
|
|
|
t.Fatal("invalid key order in JSON: ", string(b))
|
2015-08-26 17:58:56 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 17:17:38 -04:00
|
|
|
|
2017-09-17 21:58:08 -04:00
|
|
|
func TestImage(t *testing.T) {
|
|
|
|
cid := "50a16564e727"
|
|
|
|
config := &container.Config{
|
|
|
|
Hostname: "hostname",
|
|
|
|
Domainname: "domain",
|
|
|
|
User: "root",
|
|
|
|
}
|
2017-09-13 15:49:04 -04:00
|
|
|
os := runtime.GOOS
|
2017-09-17 21:58:08 -04:00
|
|
|
|
|
|
|
img := &Image{
|
|
|
|
V1Image: V1Image{
|
|
|
|
Config: config,
|
|
|
|
},
|
|
|
|
computedID: ID(cid),
|
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(cid, img.ImageID()))
|
|
|
|
assert.Check(t, is.Equal(cid, img.ID().String()))
|
|
|
|
assert.Check(t, is.Equal(os, img.OperatingSystem()))
|
|
|
|
assert.Check(t, is.DeepEqual(config, img.RunConfig()))
|
2017-09-17 21:58:08 -04:00
|
|
|
}
|
|
|
|
|
2017-09-13 15:49:04 -04:00
|
|
|
func TestImageOSNotEmpty(t *testing.T) {
|
|
|
|
os := "os"
|
2017-09-17 21:58:08 -04:00
|
|
|
img := &Image{
|
|
|
|
V1Image: V1Image{
|
2017-09-13 15:49:04 -04:00
|
|
|
OS: os,
|
2017-09-17 21:58:08 -04:00
|
|
|
},
|
|
|
|
OSVersion: "osversion",
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(os, img.OperatingSystem()))
|
2017-09-17 21:58:08 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:17:38 -04:00
|
|
|
func TestNewChildImageFromImageWithRootFS(t *testing.T) {
|
|
|
|
rootFS := NewRootFS()
|
|
|
|
rootFS.Append(layer.DiffID("ba5e"))
|
|
|
|
parent := &Image{
|
|
|
|
RootFS: rootFS,
|
|
|
|
History: []History{
|
|
|
|
NewHistory("a", "c", "r", false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
childConfig := ChildConfig{
|
|
|
|
DiffID: layer.DiffID("abcdef"),
|
|
|
|
Author: "author",
|
|
|
|
Comment: "comment",
|
|
|
|
ContainerConfig: &container.Config{
|
|
|
|
Cmd: []string{"echo", "foo"},
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
|
|
|
}
|
|
|
|
|
|
|
|
newImage := NewChildImage(parent, childConfig, "platform")
|
|
|
|
expectedDiffIDs := []layer.DiffID{layer.DiffID("ba5e"), layer.DiffID("abcdef")}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(expectedDiffIDs, newImage.RootFS.DiffIDs))
|
|
|
|
assert.Check(t, is.Equal(childConfig.Author, newImage.Author))
|
|
|
|
assert.Check(t, is.DeepEqual(childConfig.Config, newImage.Config))
|
|
|
|
assert.Check(t, is.DeepEqual(*childConfig.ContainerConfig, newImage.ContainerConfig))
|
|
|
|
assert.Check(t, is.Equal("platform", newImage.OS))
|
|
|
|
assert.Check(t, is.DeepEqual(childConfig.Config, newImage.Config))
|
2017-07-11 17:17:38 -04:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Len(newImage.History, 2))
|
|
|
|
assert.Check(t, is.Equal(childConfig.Comment, newImage.History[1].Comment))
|
2017-07-11 17:17:38 -04:00
|
|
|
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, !cmp.Equal(parent.RootFS.DiffIDs, newImage.RootFS.DiffIDs),
|
|
|
|
"RootFS should be copied not mutated")
|
2017-07-11 17:17:38 -04:00
|
|
|
}
|