-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtest_expansion.bats
executable file
·376 lines (280 loc) · 12.9 KB
/
test_expansion.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env bats
# load custom assertions and functions
load bats_helper
# setup is run beofre each test
function setup {
INPUT_PROJECT_CONFIG=${BATS_TMPDIR}/input_config-${BATS_TEST_NUMBER}
PROCESSED_PROJECT_CONFIG=${BATS_TMPDIR}/packed_config-${BATS_TEST_NUMBER}
JSON_PROJECT_CONFIG=${BATS_TMPDIR}/json_config-${BATS_TEST_NUMBER}
echo "#using temp file ${BATS_TMPDIR}/"
# the name used in example config files.
INLINE_ORB_NAME="queue"
if [ -z "$BATS_IMPORT_DEV_ORB" ]; then
echo "#Using \`inline\` orb assembly, to test against published orb, set BATS_IMPORT_DEV_ORB to fully qualified path" >&3
else
echo "#BATS_IMPORT_DEV_ORB env var is set, all config will be tested against imported orb $BATS_IMPORT_DEV_ORB" >&3
fi
}
@test "Job: full job expands properly" {
# given
process_config_with test/inputs/fulljob.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/onepreviousjob-differentname.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["Single File"].steps | length' 1 #only 1 steps
}
@test "Command: Input parameters are respected by command" {
# given
process_config_with test/inputs/command-non-default.yml
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
run jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG
assert_contains_text "max_time=1"
}
@test "Default job sets block workflow properly" {
# given
process_config_with test/inputs/fulljob.yml
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["Single File"].steps | length' 1 #only 1 steps
jq -r '.jobs["Single File"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Orb parameter block-workflow is true."
}
@test "Race condition on previous workflow does not fool us" {
# given
process_config_with test/inputs/command-defaults.yml
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME=""
# set API Payload to temp location
export TESTING_MOCK_RESPONSE=/tmp/dynamic_response.json
# set initial response to mimic in-btween race condition, no running jobs
cp test/api/jobs/nopreviousjobs.json /tmp/dynamic_response.json
# in 11 seconds (> 10) switch to return the running job BACKGROUND PROCESS
(sleep 11 && cp test/api/jobs/onepreviousjobsamename.json /tmp/dynamic_response.json) &
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Rerunning check 1/1"
assert_contains_text "This build (${CIRCLE_BUILD_NUM}) is queued, waiting for build number (3) to complete."
assert_contains_text "Max wait time exceeded"
assert_contains_text "Cancelleing build 2"
[[ "$status" == "1" ]]
}
@test "Command: script will proceed with no previous jobs" {
# given
process_config_with test/inputs/command-defaults.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/nopreviousjobs.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Front of the line, WooHoo!, Build continuing"
[[ "$status" == "0" ]]
}
@test "Command: script will proceed with previous job of different name" {
# given
process_config_with test/inputs/command-defaults.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/onepreviousjob-differentname.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Front of the line, WooHoo!, Build continuing"
[[ "$status" == "0" ]]
}
@test "Command: script will WAIT with previous job of same name" {
# given
process_config_with test/inputs/command-defaults.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/onepreviousjobsamename.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Max wait time exceeded"
assert_contains_text "Cancelleing build 2"
[[ "$status" == "1" ]]
}
@test "Command: script with dont-quit will not fail current job" {
# given
process_config_with test/inputs/command-non-default.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/onepreviousjobsamename.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Max wait time exceeded"
assert_contains_text "Orb parameter dont-quit is set to true, letting this job proceed!"
[[ "$status" == "0" ]]
}
@test "Command: script will consider branch" {
# given
process_config_with test/inputs/command-non-default.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/nopreviousjobs.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_BRANCH="somespecialbranch"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Orb parameter 'consider-branch' is false, will block previous builds on any branch"
assert_contains_text "Front of the line, WooHoo!, Build continuing"
[[ "$status" == "0" ]]
}
@test "Command: script will skip queueing on branches that don't match filter" {
# given
process_config_with test/inputs/command-filter-branch.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/nopreviousjobs.json #Response shouldn't matter as we're ending early
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_BRANCH="dev"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Queueing only happens on master branch, skipping queue"
assert_text_not_found "Max Queue Time: 1 minutes"
[[ "$status" == "0" ]]
}
@test "Command: script will consider branch default" {
# given
process_config_with test/inputs/command-defaults.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/nopreviousjobs.json #branch filtereing handles by API, so return no matching builds
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_BRANCH="somespecialbranch"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "${CIRCLE_BRANCH} queueable"
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Only blocking execution if running previous jobs on branch: ${CIRCLE_BRANCH}"
assert_contains_text "Front of the line, WooHoo!, Build continuing"
[[ "$status" == "0" ]]
}
@test "Command: script will queue on different job when block-workflow is true" {
# given
process_config_with test/inputs/command-non-default.yml
export TESTING_MOCK_RESPONSE=test/api/jobs/onepreviousjob-differentname.json
export TESTING_MOCK_WORKFLOW_RESPONSES=test/api/workflows
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME=""
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Max Queue Time: 1 minutes"
assert_contains_text "Max wait time exceeded"
}
@test "Command: script will skip queueing on forks" {
# given
process_config_with test/inputs/command-defaults.yml
# when
assert_jq_match '.jobs | length' 1 #only 1 job
assert_jq_match '.jobs["build"].steps | length' 1 #only 1 steps
jq -r '.jobs["build"].steps[0].run.command' $JSON_PROJECT_CONFIG > ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
export CIRCLECI_API_KEY="madethisup"
export CIRCLE_BUILD_NUM="2"
export CIRCLE_JOB="singlejob"
export CIRCLE_PROJECT_USERNAME="madethisup"
export CIRCLE_PROJECT_REPONAME="madethisup"
export CIRCLE_REPOSITORY_URL="madethisup"
export CIRCLE_BRANCH="madethisup"
export CIRCLE_PR_REPONAME="fork"
run bash ${BATS_TMPDIR}/script-${BATS_TEST_NUMBER}.bash
assert_contains_text "Queueing on forks is not supported. Skipping queue..."
}