/
test.go
237 lines (197 loc) · 7.03 KB
/
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
/*
test.go contains all of the logic specific to the test command
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
/*
tempPath is the location that all test files will be placed in when running
this application.
NOTE: Assumption that this is a safe path in the current working directory.
This application will delete this directory when run, which is a high risk for
deleting user data. If this assumption proves not to be safe, we will have to
rethink this constant.
*/
const tempPath = ".~tmp.test"
var errs []error
/*
getTestArray takes a single image from the inventory.yml file and converts
its test key (of type interface{}) to an array of strings. This allows us
to accept either a single string or an array of strings as a value for
test.
*/
func getTestArray(image map[string]interface{}) (tests []string) {
switch image["test"].(type) {
case string:
// If the value is a single string, append it to the array and be done
tests = append(tests, image["test"].(string))
break
case []interface{}:
// If the value is an array, iterate through and add all of the strings
// to the array one by one.
for _, str := range image["test"].([]interface{}) {
tests = append(tests, str.(string))
}
break
}
return
}
type TestOpts struct {
Threads int
Retries int
}
/*
runTests iterates through an Inventory object and builds every image, followed
by running each of the tests listed against the newly built image. We attempt
to build every image defined in inventory, and return an array of errors if any
are encountered.
*/
func runTests(inventory Inventory, opts TestOpts) (errs int) {
input := make(chan Job)
output := make(chan Job)
done := make(chan bool, len(inventory["images"]))
for i := 0; i < opts.Threads; i++ {
go testWorker(input, output)
}
go reporter(output, done)
for i, image := range inventory["images"] {
input <- Job{
Image: image,
Retries: opts.Retries,
Id: i,
}
}
errs = 0
for i := 0; i < len(inventory["images"]); i++ {
if <-done == false {
errs++
}
}
return
}
func testWorker(input chan Job, output chan Job) {
for {
tmp := <-input
var resultString string
// Initialize Output For Image
stdout := fmt.Sprintf("# Tested image `%v`\n\n## Build Log\n\n", tmp.Image["name"].(string))
resultString, tmp = testBuildImage(tmp)
stdout = stdout + resultString
// If we did not successfully build, there is nothing left to do
if !tmp.Success {
tmp.Output = stdout
output <- tmp
continue
}
resultString, tmp = testBuildTests(tmp)
stdout = stdout + resultString
tmp.Output = stdout
output <- tmp
}
}
func testBuildImage(tmp Job) (string, Job) {
var stdout string
// Attempt to build the image until we run out of retries
for retries := tmp.Retries; retries >= 0; retries-- {
// Try to build the image
result, err := buildImage(tmp.Image["name"].(string), tmp.Image["path"].(string), DockerOpts{})
stdout = stdout + fmt.Sprintf("```\n%v\n```\n\n", string(result))
// If we fail, determine proper notice to log, else break out of retry loop
if err != nil {
stdout = stdout + fmt.Sprintf("**Failed** with error: `%v`\nRetries Remaining: %v", err, retries)
if retries == 0 {
stdout = stdout + fmt.Sprintf("... Moving on")
tmp.Success = false
}
stdout = stdout + fmt.Sprintf("\n\n")
} else {
tmp.Success = true
break
}
}
return stdout, tmp
}
func testBuildTests(tmp Job) (string, Job) {
// Get an array of tests we want to run against our newly built image
tests := getTestArray(tmp.Image)
stdout := fmt.Sprintf("Array of tests: `%v`\n\n", tests)
for testNum, test := range tests {
output, err := testBuildTest(tmp.Image, tmp.Id, tmp.Retries, testNum, test)
stdout = stdout + output
if err != nil {
tmp.Success = false
}
}
return stdout, tmp
}
func testBuildTest(image ImageDefinition, id int, retries int, testNum int, test string) (output string, err error) {
var tempDir string
// Grab an absolute path to the directory we will store our tests in.
// We need to use a temporary directory since we will be modifying the
// contents of the directory to build the tests against the base image.
localTempPath := tempPath + strconv.Itoa(id)
tempDir, err = filepath.Abs(localTempPath)
var testpath string
var contents []byte
output = output + fmt.Sprintf("## Running test #%v\n\n", testNum)
// Generate a unique name for the test image that we will build
testname := image["name"].(string) + "-test" + strconv.Itoa(testNum+1)
// Get the absolute path to the test Dockerfile and context location
testpath, err = filepath.Abs(test)
if err != nil {
output = output + fmt.Sprintf("**Failed** Could not get path to file `%v`: `%v`\n\n", test, err)
// If we can't get the path, we can't build the image. Moving on.
return
}
// Delete the tempDir directory if it exists to ensure our test has a
// clean context and isn't polluted by a corrupted previous run of
// docker-test
os.RemoveAll(tempDir)
// We need to copy the test context to a temp directory. In order to use
// our new docker image as a base, we prepend a FROM statement to the
// test's Dockerfile. This tool should be repeatable, generating the same
// results if run in the same environment multiple times (assuming the
// Dockerfiles it builds are deterministic). This means we can not modify
// the original Dockerfile in place, so we create a temporary directory
// where we copy the test's context and are then able to safely mutate
// the context's state to suite our tool. We delete the temp directory
// when finished.
output = output + fmt.Sprintf("Copying `%v` to `%v`\n\n", testpath, tempDir)
copyDir(testpath, tempDir)
// tmpDir should already be an absolute path. So we are now getting
// an absolute path to the Dockerfile we just copied into our tempDir
dockerfile := filepath.Join(tempDir, "Dockerfile")
// We then prepend a FROM statement to our Dockerfile so that when the
// docker daemon builds it, it would build the layers on top of the
// the image we are attempting to test.
prependToFile(dockerfile, "FROM "+image["name"].(string)+"\n")
contents, err = ioutil.ReadFile(dockerfile)
if err != nil {
output = output + fmt.Sprintf("**Failed** Could not get contents of Dockerfile `%v`: `%v`\n\n", test, err)
// If we can't get the Dockerfile, we can't build the image. Moving on.
return
}
output = output + fmt.Sprintf("Contents of dockerfile `%v`:\n\n```\n%v\n```\n\n", dockerfile, string(contents))
output = output + fmt.Sprintf("Building `%v` from `%v`\n\n", testname, tempDir)
// Build our test image against our base image until we succeed or run out of retries
for ; retries >= 0; retries-- {
var resultStr string
resultStr, err = buildImage(testname, tempDir, DockerOpts{})
output = output + fmt.Sprintf("```\n%v\n```\n\n", string(resultStr))
if err != nil {
output = output + fmt.Sprintf("**Failed** with error: `%v`\nRetries Remaining: %v", err, retries)
if retries == 0 {
output = output + fmt.Sprintf("... Moving on")
}
output = output + fmt.Sprintf("\n\n")
} else {
break
}
}
return
}