forked from d2iq-archive/cake-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
348 lines (302 loc) · 8.91 KB
/
utils_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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"sort"
"strings"
"testing"
)
func TestRenderDockerfileTemplate(t *testing.T) {
expectedEnvVar := "test"
expectedVersionVar := "x.y.z"
expectedParent := "foo/bar:baz"
template := `FROM {{parent}}
ENV PROPERTY {{tmpl_property}}
RUN echo "{{tmpl_version}}" > /version.txt
`
expectedDockerfile := fmt.Sprintf(`FROM %s
ENV PROPERTY %s
RUN echo "%s" > /version.txt
`, expectedParent, expectedEnvVar, expectedVersionVar)
tempDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
tmpFile, err := os.Create(path.Join(tempDir, "Dockerfile.template"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = tmpFile.Write([]byte(template))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
image := Image{
Parent: &Image{
ImageConfig: ImageConfig{
Repository: "foo",
Name: "bar",
},
Checksum: "baz",
},
ImageConfig: ImageConfig{
Template: tmpFile.Name(),
Properties: map[string]string{
"tmpl_property": expectedEnvVar,
"tmpl_version": expectedVersionVar,
},
},
}
err = image.renderDockerfileFromTemplate(BuildConfig{})
if err != nil {
t.Errorf("Unexpected error while rendering Dockerfile from template: %v", err)
}
dockerfile := path.Join(tempDir, GeneratedDockerFileNamePrefix+image.ImageConfig.TagSuffix)
_, err = os.Stat(dockerfile)
if os.IsNotExist(err) {
t.Error("Rendered Dockerfile not found")
}
bytes, err := ioutil.ReadFile(dockerfile)
if err != nil {
t.Errorf("Failed to read bytes from file %s: %v", dockerfile, err)
}
contents := string(bytes)
if strings.Compare(expectedDockerfile, contents) != 0 {
t.Errorf("Rendered Dockerfile contents differ from the expected.\nExpected:\n%s\nRendered:\n%s", expectedDockerfile, contents)
}
}
func TestErrorOnRenderingMissingTemplateProperties(t *testing.T) {
template := `FROM {{parent}}
ENV PROPERTY {{property}}
`
tempDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
tmpFile, err := os.Create(path.Join(tempDir, "Dockerfile.template"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = tmpFile.Write([]byte(template))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
image := Image{
Parent: &Image{
ImageConfig: ImageConfig{
Repository: "foo",
Name: "bar",
},
Checksum: "baz",
},
ImageConfig: ImageConfig{
Template: tmpFile.Name(),
},
}
err = image.renderDockerfileFromTemplate(BuildConfig{})
if err == nil {
t.Errorf("Expected error while rendering Dockerfile from template with missing variables")
}
}
func TestListFiles(t *testing.T) {
tempDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
tmpFileRoot, err := os.Create(path.Join(tempDir, "file1"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
levelOneDir := path.Join(tempDir, "nested")
err = os.Mkdir(levelOneDir, os.FileMode(0777))
if err != nil {
t.Errorf("Failed to create directory %s: %v", levelOneDir, err)
}
tmpFileLevelOne, err := os.Create(path.Join(levelOneDir, "file2"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
levelTwoDir := path.Join(tempDir, "nested2")
err = os.Mkdir(levelTwoDir, os.FileMode(0777))
if err != nil {
t.Errorf("Failed to create directory %s: %v", levelTwoDir, err)
}
tmpFileLevelTwo, err := os.Create(path.Join(levelTwoDir, "file3"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
expected := []string{
tmpFileRoot.Name(),
tmpFileLevelOne.Name(),
tmpFileLevelTwo.Name(),
}
sort.Strings(expected)
files, err := listFiles(tempDir)
if err != nil {
t.Errorf("Unexpected error while listing files: %v", err)
}
sort.Strings(files)
if !reflect.DeepEqual(expected, files) {
t.Errorf("Listed files differ from the expected.\nExpected:\n%s\nFound:\n%s", expected, files)
}
}
func TestContentChecksum(t *testing.T) {
testContents := `FROM ubuntu
COMMAND echo "Hello world"
`
expectedChecksum := checksum(testContents)
tmpFile, err := ioutil.TempFile("", "test")
if err != nil {
t.Errorf("Failed to create temporary file: %v", err)
}
_, err = tmpFile.Write([]byte(testContents))
if err != nil {
t.Errorf("Failed to write data to temporary file %s: %v", tmpFile.Name(), err)
}
calculatedChecksum, err := getContentChecksum(tmpFile.Name())
if err != nil {
t.Errorf("Unexpected error while calculating checksum: %v", err)
}
if expectedChecksum != calculatedChecksum {
t.Errorf("Calculated file checksum differs from the expected.\nExpected:\n%s\nCalculated:\n%s", expectedChecksum, calculatedChecksum)
}
}
func TestCalculateChecksum(t *testing.T) {
dockerfileContents := `FROM ubuntu
COMMAND echo "Hello world"
`
sharedFileContents := `#!/usr/bin/env bash
set -e -u
echo "Hello world"
`
nestedFileContents := `log4j.rootCategory=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
`
/*
Creating the following folder structure with non-empty files:
root/
__base/
- nested/
- nested.file
- Dockerfile.generated
shared/
- shared.file
*/
root, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
baseDir, err := ioutil.TempDir(root, "__base")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
rootDockerfile, err := os.Create(path.Join(baseDir, GeneratedDockerFileNamePrefix))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = rootDockerfile.Write([]byte(dockerfileContents))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
sharedDir, err := ioutil.TempDir(root, "shared")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
sharedFile, err := os.Create(path.Join(sharedDir, "shared.file"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = sharedFile.Write([]byte(sharedFileContents))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
nestedDir := path.Join(baseDir, "nested")
err = os.Mkdir(nestedDir, os.FileMode(0777))
if err != nil {
t.Errorf("Failed to create directory: %v", err)
}
nestedFile, err := os.Create(path.Join(nestedDir, "nested.file"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = nestedFile.Write([]byte(nestedFileContents))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
expectedChecksum := checksum(checksum(dockerfileContents) + checksum(nestedFileContents) + checksum(sharedFileContents))
image := Image{
Dockerfile: rootDockerfile.Name(),
ImageConfig: ImageConfig{
ExtraFiles: []string{sharedDir},
},
}
err = image.calculateChecksum()
if err != nil {
t.Errorf("Unexpected error while calculating checksum: %v", err)
}
if expectedChecksum != image.Checksum {
t.Errorf("Calculated image checksum differs from the expected.\nExpected:\n%s\nCalculated:\n%s", expectedChecksum, image.Checksum)
}
}
func TestCalculateChecksumWithMultipleDockerfiles(t *testing.T) {
primaryDockerfileContents := `FROM ubuntu:18.04
COMMAND echo "Hello world"
`
secondaryDockerfileContents := `FROM ubuntu:18.10
COMMAND echo "Hello world"
`
/*
Creating the following folder structure with non-empty files:
root/
image/
- Dockerfile.generated
- Dockerfile.generated.secondary
*/
root, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
baseDir, err := ioutil.TempDir(root, "image")
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
primaryDockerfile, err := os.Create(path.Join(baseDir, GeneratedDockerFileNamePrefix))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = primaryDockerfile.Write([]byte(primaryDockerfileContents))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
secondaryDockerfile, err := os.Create(path.Join(baseDir, GeneratedDockerFileNamePrefix+".secondary"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
_, err = secondaryDockerfile.Write([]byte(secondaryDockerfileContents))
if err != nil {
t.Errorf("Failed to write file: %v", err)
}
expectedChecksum := checksum(checksum(secondaryDockerfileContents))
image := Image{
Dockerfile: secondaryDockerfile.Name(),
ImageConfig: ImageConfig{
TagSuffix: "secondary",
},
}
err = image.calculateChecksum()
if err != nil {
t.Errorf("Unexpected error while calculating checksum: %v", err)
}
if expectedChecksum != image.Checksum {
t.Errorf("Calculated image checksum differs from the expected.\nExpected:\n%s\nCalculated:\n%s", expectedChecksum, image.Checksum)
}
}
func checksum(input string) string {
hash := sha256.New()
hash.Write([]byte(input))
return hex.EncodeToString(hash.Sum(nil))
}