forked from d2iq-archive/cake-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_tree_test.go
367 lines (307 loc) · 9.21 KB
/
image_tree_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"fmt"
"reflect"
"testing"
)
const org = "organisation"
const repo = "image-name"
const fullName = "organisation/image-name"
const testChecksum = "fde9532"
const releaseTag = "1.0"
const tagSuffix = "suffix"
func TestGetFullName(t *testing.T) {
image := Image{
ImageConfig: ImageConfig{
Repository: org,
Name: repo,
},
}
if fullName != image.getFullName() {
t.Errorf("Expected '%s' but received '%s'", fullName, image.getFullName())
}
}
func TestGetDockerTags(t *testing.T) {
expected := []string{
fmt.Sprintf("%s:%s", fullName, "latest"),
fmt.Sprintf("%s:%s", fullName, testChecksum),
fmt.Sprintf("%s:%s", fullName, releaseTag),
}
image := Image{
ImageConfig: ImageConfig{
Repository: org,
Name: repo,
},
Checksum: testChecksum,
}
tags := image.getDockerTags(BuildConfig{
ReleaseTag: releaseTag,
})
if !reflect.DeepEqual(expected, tags) {
t.Errorf("Tags differ from the expected.\nExpected:\n%s\nFound:\n%s", expected, tags)
}
//Test no release tag
expected = []string{
fmt.Sprintf("%s:%s", fullName, "latest"),
fmt.Sprintf("%s:%s", fullName, testChecksum),
}
tags = image.getDockerTags(BuildConfig{})
if !reflect.DeepEqual(expected, tags) {
t.Errorf("Tags differ from the expected.\nExpected:\n%s\nFound:\n%s", expected, tags)
}
}
func TestGetStableTag(t *testing.T) {
image := Image{
ImageConfig: ImageConfig{
TagSuffix: tagSuffix,
},
}
//test release tag
tag := image.getStableTag(BuildConfig{ReleaseTag: releaseTag})
expected := fmt.Sprintf("%s-%s", releaseTag, tagSuffix)
if expected != tag {
t.Errorf("Expected: %s. Found: %s", expected, tag)
}
//test no release tag, no checksum
tag = image.getStableTag(BuildConfig{})
expected = fmt.Sprintf("latest-%s", tagSuffix)
if expected != tag {
t.Errorf("Expected: %s. Found: %s", expected, tag)
}
//test no release tag, checksum present
image.Checksum = testChecksum
tag = image.getStableTag(BuildConfig{})
expected = fmt.Sprintf("%s-%s", testChecksum, tagSuffix)
if expected != tag {
t.Errorf("Expected: %s. Found: %s", expected, tag)
}
}
func TestGetTagSuffix(t *testing.T) {
image := Image{
ImageConfig: ImageConfig{
TagSuffix: "",
},
}
//test no tag suffix
tSuffix := getTagSuffixStr(image)
if tSuffix != "" {
t.Errorf("Expected no tag suffix but found: %s", tSuffix)
}
//test with tag suffix present
image.ImageConfig.TagSuffix = tagSuffix
tSuffix = getTagSuffixStr(image)
expected := fmt.Sprintf("-%s", tagSuffix)
if tSuffix != expected {
t.Errorf("Expected tag suffix: '%s' but found: %s", expected, tSuffix)
}
}
func TestTransformConfigToImages(t *testing.T) {
/*Testing the following hierarchy:
parent
|
child-0
/\
/ \
/ \
child-01 child-02
*/
buildConfig := BuildConfig{
Images: []ImageConfig{
{
Id: "parent",
Name: "base-image",
},
{
Id: "child-0",
Name: "child-image",
Parent: "parent",
},
{
Id: "child-01",
Name: "child-image-1",
Parent: "child-0",
},
{
Id: "child-02",
Name: "child-image-2",
Parent: "child-0",
},
},
}
images, err := transformConfigToImages(buildConfig)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
expectedImages := make(map[string]*Image)
for _, imageConfig := range buildConfig.Images {
expectedImages[imageConfig.Id] = &Image{ImageConfig: imageConfig}
}
if !reflect.DeepEqual(expectedImages, images) {
t.Errorf("Images created from config differ from the expected.\nExpected:\n%s\nFound:\n%s", expectedImages, images)
}
}
func TestMultipleBaseImagesDetection(t *testing.T) {
/*Testing the following hierarchy:
parent-0 parent-1
|
child-0
*/
buildConfig := BuildConfig{
Images: []ImageConfig{
{
Id: "parent-0",
Name: "base-image",
},
{
Id: "child-0",
Name: "child-image",
Parent: "parent-0",
},
{
Id: "parent-1",
Name: "parent-image-1",
},
},
}
images, err := transformConfigToImages(buildConfig)
if err == nil {
t.Errorf("Expected error but received images: %s", images)
}
expectedError := "Multiple base images without declared parents: parent-0 and parent-1"
if err.Error() != expectedError {
t.Errorf("Expected error '%v', but received '%v'.", expectedError, err)
}
}
func TestMissingImageIdDetection(t *testing.T) {
/*Testing the following hierarchy:
image-1
|
image-1
*/
buildConfig := BuildConfig{
Images: []ImageConfig{
{
Id: "image-1",
Name: "base-image",
},
{
Id: "image-1",
Name: "child-image",
Parent: "image-1",
},
},
}
images, err := transformConfigToImages(buildConfig)
if err == nil {
t.Errorf("Expected error but received images: %s", images)
}
expectedError := "Duplicate Image ID in config: image-1"
if err.Error() != expectedError {
t.Errorf("Expected error '%v', but received = '%v'.", expectedError, err)
}
}
func TestCreateBuildGraph(t *testing.T) {
/*Expecting the following hierarchy:
parent
|
child-0
/\
/ \
/ \
child-01 child-02
*/
sourceImages := map[string]*Image{
"parent": {ImageConfig: ImageConfig{Id: "parent"}},
"child-0": {ImageConfig: ImageConfig{Id: "child-0", Parent: "parent"}},
"child-01": {ImageConfig: ImageConfig{Id: "child-01", Parent: "child-0"}},
"child-02": {ImageConfig: ImageConfig{Id: "child-02", Parent: "child-0"}},
}
parent, err := createImageBuildGraph(sourceImages)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if parent.ImageConfig.Id != "parent" {
t.Errorf("Expected parent with id 'parent' but received: %s", parent.ImageConfig.Id)
}
if len(parent.Children) != 1 {
t.Errorf("Expected single child on level one but got %d", len(parent.Children))
}
childLevelOne := parent.Children[0]
if childLevelOne.ImageConfig.Id != "child-0" {
t.Errorf("Expected level one child with id 'child-0' but received: %s", childLevelOne.ImageConfig.Id)
}
if childLevelOne.Parent.ImageConfig.Id != "parent" {
t.Errorf("Parent ID is incorrect for level one child expected 'parent' but got %s", childLevelOne.Parent.ImageConfig.Id)
}
if len(childLevelOne.Children) != 2 {
t.Errorf("Expected 2 child nodes for level one child but got %s", childLevelOne.Children)
}
if childLevelOne.Children[0].Parent.ImageConfig.Id != "child-0" || childLevelOne.Children[1].Parent.ImageConfig.Id != "child-0" {
t.Errorf("Level two nodes parent is incorrect. Expected 'child-0' but got [for child 1]: '%s', [for child 2]: %s",
childLevelOne.Children[0].Parent.ImageConfig.Id, childLevelOne.Children[1].Parent.ImageConfig.Id)
}
}
func TestImageIdNotFound(t *testing.T) {
sourceImages := map[string]*Image{
"parent": {ImageConfig: ImageConfig{Id: "parent"}},
"child-0": {ImageConfig: ImageConfig{Id: "child-0", Parent: "unknown"}},
}
root, err := createImageBuildGraph(sourceImages)
if err == nil {
t.Errorf("Expected error but received %s", root)
}
expectedError := "Unable to find parent with ID: unknown"
if err.Error() != expectedError {
t.Errorf("Expected error '%v', but received = '%v'.", expectedError, err)
}
}
func TestOrphanedImageDetection(t *testing.T) {
sourceImages := map[string]*Image{
"root": {ImageConfig: ImageConfig{Id: "root"}},
"child-0": {ImageConfig: ImageConfig{Id: "child-0", Parent: "child-1"}},
"child-1": {ImageConfig: ImageConfig{Id: "child-1", Parent: "child-0"}},
}
root, err := createImageBuildGraph(sourceImages)
if err == nil {
t.Errorf("Expected error but received %s", root)
}
expectedError := "Detected orphaned image defined in the config but not found in the build graph. Image ID: child-0"
if err.Error() != expectedError {
t.Errorf("Expected error '%v', but received = '%v'.", expectedError, err)
}
}
func TestNoBaseImage(t *testing.T) {
sourceImages := map[string]*Image{
"child-0": {ImageConfig: ImageConfig{Id: "child-0", Parent: "child-1"}},
"child-1": {ImageConfig: ImageConfig{Id: "child-1", Parent: "child-0"}},
}
root, err := createImageBuildGraph(sourceImages)
if err == nil {
t.Errorf("Expected error but received %s", root)
}
expectedError := "unable to find base image, check config for cycles"
if err.Error() != expectedError {
t.Errorf("Expected error '%v', but received = '%v'.", expectedError, err)
}
}
func TestWalkBuildGraph(t *testing.T) {
sourceImages := map[string]*Image{
"root": {ImageConfig: ImageConfig{Id: "root"}},
"child-0": {ImageConfig: ImageConfig{Id: "child-0", Parent: "root"}},
"child-1": {ImageConfig: ImageConfig{Id: "child-1", Parent: "root"}},
"child-10": {ImageConfig: ImageConfig{Id: "child-10", Parent: "child-1"}},
"child-11": {ImageConfig: ImageConfig{Id: "child-11", Parent: "child-1"}},
}
expected := []string{"root", "child-1", "child-0", "child-11", "child-10"}
root, err := createImageBuildGraph(sourceImages)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var visited []string
walkBuildGraph(root, func(image *Image) {
visited = append(visited, image.ImageConfig.Id)
})
if !reflect.DeepEqual(expected, visited) {
t.Errorf("Node order in a tree traversal differs from the expected.\nExpected:\n%s\nFound:\n%s", expected, visited)
}
}