/
build.bats
executable file
·353 lines (320 loc) · 13.6 KB
/
build.bats
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
#!/usr/bin/env ./test/libs/bats/bin/bats
load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'helpers'
load 'docker_mock'
profile_script="./bin/build.sh"
setup() {
export CI_COMMIT_REF_SLUG="CI_COMMIT_REF_SLUG"
export CI_PROJECT_NAME="CI_PROJECT_NAME"
}
teardown() {
teardown_docker_mocks
}
setup_publish() {
# required to publish
export CI_REGISTRY="CI_REGISTRY"
export CI_REGISTRY_USER="CI_REGISTRY_USER"
export CI_REGISTRY_PASSWORD="CI_REGISTRY_PASSWORD"
export CI_REGISTRY_IMAGE="CI_REGISTRY_IMAGE"
}
@test ".check_required_environment requires CI_COMMIT_REF_SLUG environment variable" {
unset CI_COMMIT_REF_SLUG
assert_empty "${CI_COMMIT_REF_SLUG}"
source ${profile_script}
run check_required_environment
assert_failure
assert_output --partial "CI_COMMIT_REF_SLUG"
}
@test ".check_required_environment requires CI_PROJECT_NAME environment variable" {
unset CI_PROJECT_NAME
assert_empty "${CI_PROJECT_NAME}"
source ${profile_script}
run check_required_environment
assert_failure
assert_output --partial "CI_PROJECT_NAME"
}
@test ".check_required_environment is successful if required environment is present" {
source ${profile_script}
run check_required_environment
assert_success
}
@test ".check_required_publish_environment requires CI_REGISTRY environment variable when DO_NOT_PUBLISH is not present" {
setup_publish
unset CI_REGISTRY
assert_empty "${CI_REGISTRY}"
source ${profile_script}
run check_required_publish_environment
assert_failure
assert_output --partial "CI_REGISTRY"
}
@test ".check_required_publish_environment does not require CI_REGISTRY environment variable when DO_NOT_PUBLISH is present" {
setup_publish
export DO_NOT_PUBLISH=1
unset CI_REGISTRY
assert_empty "${CI_REGISTRY}"
source ${profile_script}
run check_required_publish_environment
assert_success
refute_output --partial "CI_REGISTRY"
}
@test ".check_required_publish_environment requires CI_REGISTRY_USER environment variable when DO_NOT_PUBLISH is not present" {
setup_publish
unset CI_REGISTRY_USER
assert_empty "${CI_REGISTRY_USER}"
source ${profile_script}
run check_required_publish_environment
assert_failure
assert_output --partial "CI_REGISTRY_USER"
}
@test ".check_required_publish_environment does not require CI_REGISTRY_USER environment variable when DO_NOT_PUBLISH is present" {
setup_publish
export DO_NOT_PUBLISH=1
unset CI_REGISTRY_USER
assert_empty "${CI_REGISTRY_USER}"
source ${profile_script}
run check_required_publish_environment
assert_success
refute_output --partial "CI_REGISTRY_USER"
}
@test ".check_required_publish_environment requires CI_REGISTRY_PASSWORD environment variable when DO_NOT_PUBLISH is not present" {
setup_publish
unset CI_REGISTRY_PASSWORD
assert_empty "${CI_REGISTRY_PASSWORD}"
source ${profile_script}
run check_required_publish_environment
assert_failure
assert_output --partial "CI_REGISTRY_PASSWORD"
}
@test ".check_required_publish_environment does not require CI_REGISTRY_PASSWORD environment variable when DO_NOT_PUBLISH is present" {
setup_publish
export DO_NOT_PUBLISH=1
unset CI_REGISTRY_PASSWORD
assert_empty "${CI_REGISTRY_PASSWORD}"
source ${profile_script}
run check_required_publish_environment
assert_success
refute_output --partial "CI_REGISTRY_PASSWORD"
}
@test ".check_required_publish_environment requires CI_REGISTRY_IMAGE environment variable when DO_NOT_PUBLISH is not present" {
setup_publish
unset CI_REGISTRY_IMAGE
assert_empty "${CI_REGISTRY_IMAGE}"
source ${profile_script}
run check_required_publish_environment
assert_failure
assert_output --partial "CI_REGISTRY_IMAGE"
}
@test ".check_required_publish_environment does not require CI_REGISTRY_IMAGE environment variable when DO_NOT_PUBLISH is present" {
setup_publish
export DO_NOT_PUBLISH=1
unset CI_REGISTRY_IMAGE
assert_empty "${CI_REGISTRY_IMAGE}"
source ${profile_script}
run check_required_publish_environment
assert_success
refute_output --partial "CI_REGISTRY_IMAGE"
}
@test ".check_required_publish_environment is successful when DO_NOT_PUBLISH is absent and required environment is present" {
setup_publish
source ${profile_script}
run check_required_publish_environment
assert_success
}
@test ".dry_run returns true if the DRY_RUN environment is present" {
export DRY_RUN=1
source ${profile_script}
run dry_run
assert_success
}
@test ".dry_run returns false if the DRY_RUN environment is not present" {
assert_empty "${DRY_RUN}"
source ${profile_script}
run dry_run
assert_failure
}
@test ".login does not call check_required_publish_environment, print information, or run docker login when DO_NOT_PUBLISH environment variable is present" {
source ${profile_script}
setup_publish
export DO_NOT_PUBLISH=1
function check_required_publish_environment() { echo "check_required_publish_environment called"; }
export -f check_required_publish_environment
mock_docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
run login
assert_success
refute_output -p "check_required_environment called"
refute_output -p "logging into ${CI_REGISTRY}"
refute_output -p "login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
unset DO_NOT_PUBLISH
}
@test ".login calls check_required_publish_environment and prints information without calling docker login when DO_NOT_PUBLISH environment variable is not present but it is a dry_run" {
source ${profile_script}
setup_publish
function dry_run() { echo "dry_run called"; return; };
function check_required_publish_environment() { echo "check_required_publish_environment called"; return; }
export -f dry_run check_required_publish_environment
mock_docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
run login
assert_output -p "check_required_publish_environment called"
assert_output -p "logging into ${CI_REGISTRY}"
refute_output -p "login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
}
@test ".login calls check_required_publish_environment, prints information, and calls docker login when DO_NOT_PUBLISH environment variable is not present and it is not a dry_run" {
source ${profile_script}
setup_publish
function check_required_publish_environment() { echo "check_required_publish_environment called"; }
export -f check_required_publish_environment
mock_docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
run login
assert_output -p "check_required_publish_environment called"
assert_output -p "logging into ${CI_REGISTRY}"
assert_output -p "login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}"
}
@test ".build_candidate takes an application and environment to use in naming the candidate image, prints information, runs docker build and publish_image on the candidate image when its not a dry_run" {
source ${profile_script}
local application='application'
local environment='environment'
local expected_candidate_image="${application}-candidate:${environment}"
mock_docker build --pull -t "${expected_candidate_image}" .
function publish_image() { echo "publishing ${1}"; }
export -f publish_image
run build_candidate "${application}" "${environment}"
assert_success
assert_output -p "Building candidate ${expected_candidate_image}"
assert_output -p "build --pull -t ${expected_candidate_image} ."
assert_output -p "publishing ${expected_candidate_image}"
}
@test ".build_candidate fails when docker build fails" {
source ${profile_script}
local application='application'
local environment='environment'
local expected_candidate_image="${application}-candidate:${environment}"
mock_docker build --pull -t "${expected_candidate_image}" . and_fail
function publish_image() { echo "publishing ${1}"; }
export -f publish_image
run build_candidate "${application}" "${environment}"
assert_failure
assert_output -p "Building candidate ${expected_candidate_image}"
assert_output -p "build --pull -t ${expected_candidate_image} ."
refute_output -p "publishing ${expected_candidate_image}"
assert_output -p "Problem in the Build"
}
@test ".build_candidate fails when publish_image fails" {
source ${profile_script}
local application='application'
local environment='environment'
local expected_candidate_image="${application}-candidate:${environment}"
mock_docker build --pull -t "${expected_candidate_image}" .
function publish_image() { echo "publishing ${1}"; return 1; }
export -f publish_image
run build_candidate "${application}" "${environment}"
assert_failure
assert_output -p "Building candidate ${expected_candidate_image}"
assert_output -p "build --pull -t ${expected_candidate_image} ."
assert_output -p "publishing ${expected_candidate_image}"
}
@test ".build_candidate only prints information and runs publish_image when its a dry_run" {
source ${profile_script}
local application='application'
local environment='environment'
local expected_candidate_image="${application}-candidate:${environment}"
mock_docker build --pull -t "${expected_candidate_image}" .
function publish_image() { echo "publishing ${1}"; }
export -f publish_image
function dry_run() { return; }
run build_candidate "${application}" "${environment}"
assert_success
assert_output -p "Building candidate ${expected_candidate_image}"
refute_output -p "build --pull -t ${expected_candidate_image} ."
assert_output -p "publishing ${expected_candidate_image}"
}
@test ".publish_image does not print information, docker tag, or docker push if DO_NOT_PUBLISH environment variable is present" {
source ${profile_script}
setup_publish
export DO_NOT_PUBLISH=1
local expected_image="image"
local expected_publishable_image="${CI_REGISTRY_IMAGE}/${expected_image}"
mock_docker tag "${expected_image}" "${expected_publishable_image}"
mock_docker push "${expected_publishable_image}"
run publish_image "${expected_image}"
assert_success
refute_output -p "tagging ${expected_image} as ${expected_publishable_image}"
refute_output -p "tag ${expected_image} ${expected_publishable_image}"
refute_output -p "pushing image to gitlab registry"
refute_output -p "push ${expected_publishable_image}"
}
@test ".publish_image prints information, runs docker tag, and docker push if DO_NOT_PUBLISH environment variable is not present" {
source ${profile_script}
setup_publish
local expected_image="image"
local expected_publishable_image="${CI_REGISTRY_IMAGE}/${expected_image}"
mock_docker tag "${expected_image}" "${expected_publishable_image}"
mock_docker push "${expected_publishable_image}"
run publish_image "${expected_image}"
assert_success
assert_output -p "tagging ${expected_image} as ${expected_publishable_image}"
assert_output -p "tag ${expected_image} ${expected_publishable_image}"
assert_output -p "pushing image to gitlab registry"
assert_output -p "push ${expected_publishable_image}"
}
@test ".publish_image fails if docker tag fails" {
source ${profile_script}
setup_publish
local expected_image="image"
local expected_publishable_image="${CI_REGISTRY_IMAGE}/${expected_image}"
mock_docker tag "${expected_image}" "${expected_publishable_image}" and_fail
mock_docker push "${expected_publishable_image}"
run publish_image "${expected_image}"
assert_failure
assert_output -p "tagging ${expected_image} as ${expected_publishable_image}"
assert_output -p "tag ${expected_image} ${expected_publishable_image}"
refute_output -p "pushing image to gitlab registry"
refute_output -p "push ${expected_publishable_image}"
}
@test ".publish_image fails if docker push fails" {
source ${profile_script}
setup_publish
local expected_image="image"
local expected_publishable_image="${CI_REGISTRY_IMAGE}/${expected_image}"
mock_docker tag "${expected_image}" "${expected_publishable_image}"
mock_docker push "${expected_publishable_image}" and_fail
run publish_image "${expected_image}"
assert_failure
assert_output -p "tagging ${expected_image} as ${expected_publishable_image}"
assert_output -p "tag ${expected_image} ${expected_publishable_image}"
assert_output -p "pushing image to gitlab registry"
assert_output -p "push ${expected_publishable_image}"
}
@test ".publish_image prints information, does not run docker tag or docker push if DO_NOT_PUBLISH environment variable is not present but its a dry_run" {
source ${profile_script}
setup_publish
local expected_image="image"
local expected_publishable_image="${CI_REGISTRY_IMAGE}/${expected_image}"
mock_docker tag "${expected_image}" "${expected_publishable_image}"
mock_docker push "${expected_publishable_image}"
function dry_run() { return; }
export -f dry_run
run publish_image "${expected_image}"
assert_success
assert_output -p "tagging ${expected_image} as ${expected_publishable_image}"
refute_output -p "tag ${expected_image} ${expected_publishable_image}"
assert_output -p "pushing image to gitlab registry"
refute_output -p "push ${expected_publishable_image}"
}
@test ".run_main calls check_required_environment, login, and build_candidate when the environment" {
source ${profile_script}
setup_publish
local expected_environment=${CI_COMMIT_REF_SLUG}
local expected_application=${CI_PROJECT_NAME}
function check_required_environment() { echo "check_required_environment called"; }
export -f check_required_environment
function login() { echo "login called"; }
export -f login
function build_candidate() { echo "build_candidate ${*} called"; }
export -f build_candidate;
run run_main
assert_success
assert_output -p "check_required_environment called"
assert_output -p "login called"
assert_output -p "build_candidate ${expected_application} ${expected_environment}"
}