-
Notifications
You must be signed in to change notification settings - Fork 107
/
images_test.go
188 lines (164 loc) · 7.99 KB
/
images_test.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"regexp"
"testing"
)
type DotTest struct {
json string
regexps []string
}
type ShortTest struct {
json string
regexps []string
}
type TreeTest struct {
json string
startImage string
noTrunc bool
incr bool
regexps []string
}
func Test_BadJSON(t *testing.T) {
_, err := parseImagesJSON([]byte(` "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 }]`))
if err == nil {
t.Error("invalid json did not cause an error")
}
}
func Test_Dot(t *testing.T) {
allMatch := []string{
"(?s)digraph docker {.*}",
`(?m) base \[style=invisible\]`,
}
allRegex := compileRegexps(t, allMatch)
dotTests := []DotTest{
DotTest{
json: `[{ "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 }]`,
regexps: []string{
`base -> "4c1208b690c6"`,
},
},
DotTest{
json: `[{ "VirtualSize": 662553464, "Size": 0, "RepoTags": [ "foo:latest" ], "ParentId": "735f5db5626147582d2ae3f2c87be8e5e697c088574c5faaf8d4d1bccab99470", "Id": "c87be8e5e697c735f5db5626147582d2ae3f2088574c5faaf8d4d1bccab99470", "Created": 1386142123 },{ "VirtualSize": 662553464, "Size": 0, "RepoTags": [ "<none>:<none>" ], "ParentId": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Id": "735f5db5626147582d2ae3f2c87be8e5e697c088574c5faaf8d4d1bccab99470", "Created": 1386142123 },{ "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 }]`,
regexps: []string{
`base -> "4c1208b690c6"`,
`"4c1208b690c6" -> "735f5db56261"`,
`"c87be8e5e697" \[label="c87be8e5e697\\nfoo:latest"`,
},
},
}
for _, dotTest := range dotTests {
im, _ := parseImagesJSON([]byte(dotTest.json))
byParent := collectChildren(im)
roots := collectRoots(im)
// TODO: test start image limiting
result := jsonToDot(roots, byParent)
for _, regexp := range allRegex {
if !regexp.MatchString(result) {
t.Fatalf("images dot content '%s' did not match regexp '%s'", result, regexp)
}
}
for _, regexp := range compileRegexps(t, dotTest.regexps) {
if !regexp.MatchString(result) {
t.Fatalf("images dot content '%s' did not match regexp '%s'", result, regexp)
}
}
}
}
func Test_Tree(t *testing.T) {
treeJSON := `[{"VirtualSize":674553464,"Size":2000000,"RepoTags":["foo:latest"],"ParentId":"735f5db5626147582d2ae3f2c87be8e5e697c088574c5faaf8d4d1bccab99470","Id":"c87be8e5e697c735f5db5626147582d2ae3f2088574c5faaf8d4d1bccab99470","Created":1386142123},{"VirtualSize":682553464,"Size":20000000,"RepoTags":["<none>:<none>"],"ParentId":"4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358","Id":"626147582d2ae3735f5db5f2c87be8e5e697c088574c5faaf8d4d1bccab99470","Created":1386142123},{"VirtualSize":712553464,"Size":30000000,"RepoTags":["base:latest"],"ParentId":"626147582d2ae3735f5db5f2c87be8e5e697c088574c5faaf8d4d1bccab99470","Id":"574c5faaf8d4d1bccab994626147582d2ae3735f5db5f2c87be8e5e697c08870","Created":1386142123},{"VirtualSize":752553464,"Size":40000000,"RepoTags":["<none>:<none>"],"ParentId":"574c5faaf8d4d1bccab994626147582d2ae3735f5db5f2c87be8e5e697c08870","Id":"aaf8d4d1bccab994574c5f626147582d2ae3735f5db5f2c87be8e5e697c08870","Created":1386142123},{"VirtualSize":672553464,"Size":10000000,"RepoTags":["<none>:<none>"],"ParentId":"4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358","Id":"735f5db5626147582d2ae3f2c87be8e5e697c088574c5faaf8d4d1bccab99470","Created":1386142123},{"VirtualSize":662553464,"Size":662553464,"RepoTags":["<none>:<none>"],"ParentId":"","Id":"4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358","Created":1386114144}]`
treeTests := []TreeTest{
TreeTest{
json: treeJSON,
startImage: "",
noTrunc: false,
incr: false,
regexps: []string{
`(?m)└─4c1208b690c6 Virtual Size: 662.6 MB`,
`(?m) └─735f5db56261 Virtual Size: 672.6 MB`,
`(?m) └─c87be8e5e697 Virtual Size: 674.6 MB Tags: foo:latest`,
},
},
TreeTest{
json: treeJSON,
startImage: "",
noTrunc: false,
incr: true,
regexps: []string{
`(?m)└─4c1208b690c6 Virtual Size: 662.6 MB`,
`(?m) └─735f5db56261 Virtual Size: 10.0 MB`,
`(?m) └─c87be8e5e697 Virtual Size: 2.0 MB Tags: foo:latest`,
},
},
TreeTest{
json: treeJSON,
startImage: "626147582d2a",
noTrunc: false,
incr: false,
regexps: []string{
`(?m)└─626147582d2a`,
`(?m) └─574c5faaf8d4`,
`(?m) └─aaf8d4d1bcca`,
},
},
TreeTest{
json: treeJSON,
startImage: "base:latest",
noTrunc: true,
incr: false,
regexps: []string{
`(?m)└─574c5faaf8d4d1bccab994626147582d2ae3735f5db5f2c87be8e5e697c08870`,
`(?m) └─aaf8d4d1bccab994574c5f626147582d2ae3735f5db5f2c87be8e5e697c08870`,
},
},
}
for _, treeTest := range treeTests {
im, _ := parseImagesJSON([]byte(treeTest.json))
byParent := collectChildren(im)
var roots []Image
if len(treeTest.startImage) > 0 {
startImage, _ := findStartImage(treeTest.startImage, im)
startImage.ParentId = ""
roots = []Image{*startImage}
} else {
roots = collectRoots(im)
}
result := jsonToTree(roots, byParent, treeTest.noTrunc, treeTest.incr)
for _, regexp := range compileRegexps(t, treeTest.regexps) {
if !regexp.MatchString(result) {
t.Fatalf("images tree content '%s' did not match regexp '%s'", result, regexp)
}
}
}
}
func Test_Short(t *testing.T) {
shortJSON := `[ { "VirtualSize": 662553464, "Size": 0, "RepoTags": [ "foo:latest" ], "ParentId": "735f5db5626147582d2ae3f2c87be8e5e697c088574c5faaf8d4d1bccab99470", "Id": "c87be8e5e697c735f5db5626147582d2ae3f2088574c5faaf8d4d1bccab99470", "Created": 1386142123 }, { "VirtualSize": 682553464, "Size": 0, "RepoTags": [ "foo:1.0" ], "ParentId": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Id": "626147582d2ae3735f5db5f2c87be8e5e697c088574c5faaf8d4d1bccab99470", "Created": 1386142123 }, { "VirtualSize": 712553464, "Size": 0, "RepoTags": [ "foo:2.0" ], "ParentId": "626147582d2ae3735f5db5f2c87be8e5e697c088574c5faaf8d4d1bccab99470", "Id": "574c5faaf8d4d1bccab994626147582d2ae3735f5db5f2c87be8e5e697c08870", "Created": 1386142123 }, { "VirtualSize": 752553464, "Size": 0, "RepoTags": [ "private.repo.com:5000:latest" ], "ParentId": "574c5faaf8d4d1bccab994626147582d2ae3735f5db5f2c87be8e5e697c08870", "Id": "aaf8d4d1bccab994574c5f626147582d2ae3735f5db5f2c87be8e5e697c08870", "Created": 1386142123 }, { "VirtualSize": 662553464, "Size": 0, "RepoTags": [ "<none>:<none>" ], "ParentId": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Id": "735f5db5626147582d2ae3f2c87be8e5e697c088574c5faaf8d4d1bccab99470", "Created": 1386142123 }, { "VirtualSize": 662553464, "Size": 662553464, "RepoTags": [ "<none>:<none>" ], "ParentId": "", "Id": "4c1208b690c68af3476b437e7bc2bcc460f062bda2094d2d8f21a7e70368d358", "Created": 1386114144 } ]`
shortTests := []ShortTest{
ShortTest{
json: shortJSON,
regexps: []string{
`(?m)foo: latest, 1.0, 2.0`,
`(?m)private.repo.com:5000: latest`,
},
},
}
for _, shortTest := range shortTests {
im, _ := parseImagesJSON([]byte(shortTest.json))
result := jsonToShort(im)
for _, regexp := range compileRegexps(t, shortTest.regexps) {
if !regexp.MatchString(result) {
t.Fatalf("images short content '%s' did not match regexp '%s'", result, regexp)
}
}
}
}
func compileRegexps(t *testing.T, regexpStrings []string) []*regexp.Regexp {
compiledRegexps := []*regexp.Regexp{}
for _, regexpString := range regexpStrings {
regexp, err := regexp.Compile(regexpString)
if err != nil {
t.Errorf("Error in regex string '%s': %s", regexpString, err)
}
compiledRegexps = append(compiledRegexps, regexp)
}
return compiledRegexps
}