forked from adamlaska/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_test.go
More file actions
148 lines (128 loc) · 3.69 KB
/
image_test.go
File metadata and controls
148 lines (128 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package image // import "github.com/docker/docker/image"
import (
"encoding/json"
"runtime"
"sort"
"strings"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/layer"
"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
const sampleImageJSON = `{
"architecture": "amd64",
"os": "linux",
"config": {},
"rootfs": {
"type": "layers",
"diff_ids": []
}
}`
func TestNewFromJSON(t *testing.T) {
img, err := NewFromJSON([]byte(sampleImageJSON))
assert.NilError(t, err)
assert.Check(t, is.Equal(sampleImageJSON, string(img.RawJSON())))
}
func TestNewFromJSONWithInvalidJSON(t *testing.T) {
_, err := NewFromJSON([]byte("{}"))
assert.Check(t, is.Error(err, "invalid image JSON, no RootFS key"))
}
func TestMarshalKeyOrder(t *testing.T) {
b, err := json.Marshal(&Image{
V1Image: V1Image{
Comment: "a",
Author: "b",
Architecture: "c",
},
})
assert.Check(t, err)
expectedOrder := []string{"architecture", "author", "comment"}
var indexes []int
for _, k := range expectedOrder {
indexes = append(indexes, strings.Index(string(b), k))
}
if !sort.IntsAreSorted(indexes) {
t.Fatal("invalid key order in JSON: ", string(b))
}
}
const sampleHistoryJSON = `{
"created": "2021-01-13T09:35:56Z",
"created_by": "image_test.go"
}`
func TestHistoryEqual(t *testing.T) {
h := historyFromJSON(t, sampleHistoryJSON)
hCopy := h
assert.Check(t, h.Equal(hCopy))
hUTC := historyFromJSON(t, `{"created": "2021-01-13T14:00:00Z"}`)
hOffset0 := historyFromJSON(t, `{"created": "2021-01-13T14:00:00+00:00"}`)
assert.Check(t, hUTC.Created != hOffset0.Created)
assert.Check(t, hUTC.Equal(hOffset0))
}
func historyFromJSON(t *testing.T, historyJSON string) History {
var h History
err := json.Unmarshal([]byte(historyJSON), &h)
assert.Check(t, err)
return h
}
func TestImage(t *testing.T) {
cid := "50a16564e727"
config := &container.Config{
Hostname: "hostname",
Domainname: "domain",
User: "root",
}
os := runtime.GOOS
img := &Image{
V1Image: V1Image{
Config: config,
},
computedID: ID(cid),
}
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()))
}
func TestImageOSNotEmpty(t *testing.T) {
os := "os"
img := &Image{
V1Image: V1Image{
OS: os,
},
OSVersion: "osversion",
}
assert.Check(t, is.Equal(os, img.OperatingSystem()))
}
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")}
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))
assert.Check(t, is.Len(newImage.History, 2))
assert.Check(t, is.Equal(childConfig.Comment, newImage.History[1].Comment))
assert.Check(t, !cmp.Equal(parent.RootFS.DiffIDs, newImage.RootFS.DiffIDs),
"RootFS should be copied not mutated")
}