-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathTestDeclarativePipeline.groovy
More file actions
536 lines (473 loc) · 19.7 KB
/
TestDeclarativePipeline.groovy
File metadata and controls
536 lines (473 loc) · 19.7 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package com.lesfurets.jenkins.unit.declarative
import org.junit.Before
import org.junit.Test
class TestDeclarativePipeline extends DeclarativePipelineTest {
@Before
@Override
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins/jenkinsfiles'
scriptExtension = ''
super.setUp()
}
@Test void jenkinsfile_success() throws Exception {
def script = runScript('Declarative_Jenkinsfile')
printCallStack()
assertCallStackContains('pipeline unit tests PASSED')
assertCallStackContains('pipeline unit tests completed')
assertCallStackContains('pipeline unit tests post CLEANUP')
assertCallStack().doesNotContain('pipeline unit tests UNSUCCESSFUL')
assertCallStackContains('Skipping stage Checkout')
assertJobStatusSuccess()
}
@Test void jenkinsfile_failure() throws Exception {
helper.registerAllowedMethod('sh', [String.class], { String cmd ->
updateBuildStatus('FAILURE')
})
runScript('Declarative_Jenkinsfile')
printCallStack()
assertJobStatusFailure()
assertCallStack()
assertCallStack().contains('pipeline unit tests FAILED')
assertCallStackContains('pipeline unit tests post CLEANUP')
assertCallStack().contains('pipeline unit tests UNSUCCESSFUL')
assertCallStackContains('pipeline unit tests completed')
}
@Test void jenkinsfile_aborted() throws Exception {
helper.registerAllowedMethod('sh', [String.class], { String cmd ->
updateBuildStatus('ABORTED')
})
runScript('Declarative_Jenkinsfile')
printCallStack()
assertJobStatusAborted()
assertCallStack()
assertCallStack().contains('pipeline unit tests ABORTED')
assertCallStackContains('pipeline unit tests post CLEANUP')
assertCallStack().contains('pipeline unit tests UNSUCCESSFUL')
assertCallStackContains('pipeline unit tests completed')
}
@Test void jenkinsfile_fixed() throws Exception {
helper.registerAllowedMethod('sh', [String.class], { String cmd ->
addPreviousBuild('FAILURE')
updateBuildStatus('SUCCESS')
})
runScript('Declarative_Jenkinsfile')
printCallStack()
assertJobStatusSuccess()
assertCallStack()
assertCallStackContains('pipeline unit tests PASSED')
assertCallStackContains('pipeline unit tests completed')
assertCallStackContains('pipeline unit tests post CLEANUP')
assertCallStackContains('pipeline unit tests results have CHANGED')
assertCallStackContains('pipeline unit tests have been FIXED')
}
@Test void jenkinsfile_regression() throws Exception {
helper.registerAllowedMethod('sh', [String.class], { String cmd ->
addPreviousBuild('SUCCESS')
updateBuildStatus('UNSTABLE')
})
runScript('Declarative_Jenkinsfile')
printCallStack()
assertJobStatusUnstable()
assertCallStack()
assertCallStackContains('pipeline unit tests completed')
assertCallStackContains('pipeline unit tests have gone UNSTABLE')
assertCallStackContains('pipeline unit tests results have CHANGED')
assertCallStackContains('pipeline unit tests UNSUCCESSFUL')
assertCallStackContains('pipeline unit tests post REGRESSION')
assertCallStackContains('pipeline unit tests post CLEANUP')
}
@Test void should_params() throws Exception {
runScript('Params_Jenkinsfile')
printCallStack()
assertCallStack().contains('Hello Mr Jenkins')
assertJobStatusSuccess()
}
@Test void when_not_branch_master() throws Exception {
addEnvVar('BRANCH_NAME', 'dev')
runScript('not_Jenkinsfile')
printCallStack()
assertCallStack().contains('Running')
assertJobStatusSuccess()
}
@Test void when_anyOf_branch_not() throws Exception {
addEnvVar('BRANCH_NAME', 'master')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_branch_release() throws Exception {
addEnvVar('BRANCH_NAME', 'release')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_branch_production() throws Exception {
addEnvVar('BRANCH_NAME', 'production')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_branch_pattern_main() throws Exception {
addEnvVar('BRANCH_NAME', 'main-2')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_branch_pattern_main_not() throws Exception {
addEnvVar('BRANCH_NAME', 'main2')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_tag_version_pattern() throws Exception {
addEnvVar('TAG_NAME', 'version-X.Y.Z')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with tag')
assertJobStatusSuccess()
}
@Test void when_anyOf_tag_release_pattern() throws Exception {
addEnvVar('TAG_NAME', 'release-X.Y.Z')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with tag')
assertJobStatusSuccess()
}
@Test void when_anyOf_tag_release_pattern_main_not() throws Exception {
addEnvVar('TAG_NAME', 'releaseX.Y.Z')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf tag')
assertJobStatusSuccess()
}
@Test void when_anyOf_expression_not() throws Exception {
addEnvVar('SHOULD_EXECUTE', 'false')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf expression')
assertJobStatusSuccess()
}
@Test void when_anyOf_expression() throws Exception {
addEnvVar('SHOULD_EXECUTE', 'true')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with expression')
assertJobStatusSuccess()
}
@Test void when_allOf_expression() throws Exception {
addEnvVar('SHOULD_EXECUTE', 'true')
addEnvVar('SHOULD_ALSO_EXECUTE', 'true')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing allOf with expression')
assertJobStatusSuccess()
}
@Test void when_allOf_expression_first_false() throws Exception {
addEnvVar('SHOULD_EXECUTE', 'false')
addEnvVar('SHOULD_ALSO_EXECUTE', 'true')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example allOf expression')
assertJobStatusSuccess()
}
@Test void when_allOf_expression_second_false() throws Exception {
addEnvVar('SHOULD_EXECUTE', 'true')
addEnvVar('SHOULD_ALSO_EXECUTE', 'false')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example allOf expression')
assertJobStatusSuccess()
}
@Test void when_allOf_expression_both_false() throws Exception {
addEnvVar('SHOULD_EXECUTE', 'false')
addEnvVar('SHOULD_ALSO_EXECUTE', 'false')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example allOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_1() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'false')
addEnvVar('OPTIONAL_4', 'false')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_2() throws Exception {
addEnvVar('OPTIONAL_1', 'true')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'false')
addEnvVar('OPTIONAL_4', 'false')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_3() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'true')
addEnvVar('OPTIONAL_3', 'false')
addEnvVar('OPTIONAL_4', 'false')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_4() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'true')
addEnvVar('OPTIONAL_4', 'false')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_5() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'false')
addEnvVar('OPTIONAL_4', 'true')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_6() throws Exception {
addEnvVar('OPTIONAL_1', 'true')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'true')
addEnvVar('OPTIONAL_4', 'false')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_allOf_anyOf_expression_7() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'true')
addEnvVar('OPTIONAL_3', 'false')
addEnvVar('OPTIONAL_4', 'true')
runScript('Nested_AllOf_And_AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when allOf anyOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_anyOf_allOf_expression() throws Exception {
addEnvVar('OPTIONAL_1', 'true')
addEnvVar('OPTIONAL_2', 'true')
addEnvVar('OPTIONAL_3', 'true')
runScript('Nested_AnyOf_And_AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when anyOf allOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_anyOf_allOf_expression_2() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'true')
addEnvVar('OPTIONAL_3', 'true')
runScript('Nested_AnyOf_And_AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when anyOf allOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_anyOf_allOf_expression_3() throws Exception {
addEnvVar('OPTIONAL_1', 'true')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'true')
runScript('Nested_AnyOf_And_AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when anyOf allOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_anyOf_allOf_expression_4() throws Exception {
addEnvVar('OPTIONAL_1', 'true')
addEnvVar('OPTIONAL_2', 'true')
addEnvVar('OPTIONAL_3', 'false')
runScript('Nested_AnyOf_And_AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when anyOf allOf expression')
assertJobStatusSuccess()
}
@Test void when_nested_when_anyOf_allOf_expression_5() throws Exception {
addEnvVar('OPTIONAL_1', 'false')
addEnvVar('OPTIONAL_2', 'false')
addEnvVar('OPTIONAL_3', 'false')
runScript('Nested_AnyOf_And_AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when anyOf allOf expression')
assertJobStatusSuccess()
}
@Test void when_branch() throws Exception {
addEnvVar('BRANCH_NAME', 'production')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('Deploying')
assertJobStatusSuccess()
}
@Test void when_branch_not() throws Exception {
addEnvVar('BRANCH_NAME', 'master')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example Deploy')
assertJobStatusSuccess()
}
@Test void when_buildingTag() throws Exception {
addEnvVar('TAG_NAME', 'release-1.0.0')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Generating Release Notes')
assertJobStatusSuccess()
}
@Test void when_buildingTag_not() throws Exception {
// no TAG_NAME variable defined
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example Release Notes') // No stage bound to a "buildingTag()" condition
assertCallStack().contains('Skipping stage Example Deploy') // No stage bound to a "tag <arg>" condition
assertJobStatusSuccess()
}
@Test void when_tag() throws Exception {
addEnvVar('TAG_NAME', 'v1.1.1')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Deploying')
assertJobStatusSuccess()
}
@Test void when_tag_not() throws Exception {
addEnvVar('TAG_NAME', 'someothertag')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example Deploy')
assertJobStatusSuccess()
}
@Test void should_agent() throws Exception {
runScript('Agent_Jenkinsfile')
printCallStack()
assertCallStack().contains('openjdk:8-jre')
assertCallStack().contains('maven:3-alpine')
assertJobStatusSuccess()
}
@Test void should_kubernetes_agent() throws Exception {
runScript('Kubernetes_Agent_Jenkinsfile')
printCallStack()
assertCallStack().contains('namespace: "jenkins"')
assertCallStack().contains('image: jenkins/slave')
assertJobStatusSuccess()
}
@Test void should_kubernetes_map_agent() throws Exception {
runScript('Kubernetes_Map_Agent_Jenkinsfile')
printCallStack()
assertCallStack().contains('namespace: "jenkins"')
assertCallStack().contains('image: jenkins/slave')
assertJobStatusSuccess()
}
@Test void should_credentials() throws Exception {
addCredential('my-prefined-secret-text', 'something_secret')
runScript('Credentials_Jenkinsfile')
printCallStack()
assertCallStack().contains('AN_ACCESS_KEY:something_secret')
assertJobStatusSuccess()
}
@Test void should_parallel() throws Exception {
runScript('Parallel_Jenkinsfile')
printCallStack()
assertCallStack().contains('sh(run-tests.exe)')
assertCallStack().contains('sh(run-tests.sh)')
assertJobStatusSuccess()
}
@Test void should_parallel_nested_stages() throws Exception {
runScript('Parallel_NestedStages_Jenkinsfile')
printCallStack()
assertJobStatusSuccess()
}
@Test void should_sub_stages() throws Exception {
runScript('ComplexStages_Jenkinsfile')
printCallStack()
assertCallStack().contains('mvn build')
assertCallStack().contains('mvn --version')
assertCallStack().contains('java -version')
assertJobStatusSuccess()
}
@Test void should_environment() throws Exception {
runScript('Environment_Jenkinsfile')
printCallStack()
assertCallStack().contains('echo(LEVAR1 LE NEW VALUE)')
assertCallStack().contains('echo(LEVAR2 A COPY OF LE NEW VALUE in build#1)')
assertJobStatusSuccess()
}
@Test void not_running_stage_after_failure() throws Exception {
runScript('StageFailed_Jenkinsfile')
printCallStack()
assertCallStack().contains('Stage "Not executed stage" skipped due to earlier failure(s)')
assertJobStatusFailure()
}
@Test(expected = MissingPropertyException)
void should_non_valid_fail() throws Exception {
try {
runScript('Non_Valid_Jenkinsfile')
} catch (e) {
e.printStackTrace()
throw e
} finally {
printCallStack()
}
}
@Test void should_not_leave_environment_dirty() throws Exception {
runScript('WithEnv_Jenkinsfile')
printCallStack()
assertCallStack().contains('echo(SOMEVAR inside closure = SOMEVAL)')
assertCallStack().contains('echo(SOMEVAR overlapping inside closure = SOMEVAL)')
assertCallStack().contains('echo(SOMEVAR restored inside closure = SOMEVAL)')
assertCallStack().contains('echo(SOMEVAR outside closure = null)')
}
@Test void agent_with_param_label() throws Exception {
runScript('AgentParam_Jenkinsfile')
printCallStack()
assertCallStack().contains('aSlave')
assertJobStatusSuccess()
}
@Test void agent_with_mock_param_label() throws Exception {
def params = binding.getVariable('params')
params['AGENT'] = 'mockSlave'
runScript('AgentParam_Jenkinsfile')
printCallStack()
assertCallStack().contains('mockSlave')
assertJobStatusSuccess()
}
@Test void should_agent_with_environment() throws Exception {
def env = binding.getVariable('env')
env['ENV_VAR'] = 'ENV VAR_VALUE'
env['some_env_var'] = 'some env var_value'
runScript('Agent_env_Jenkinsfile')
printCallStack()
assertJobStatusSuccess()
}
@Test void should_agent_with_bindings() throws Exception {
final def some_var = 'someVar'
binding.setVariable('var', some_var)
binding.setVariable('binding_var', some_var)
runScript('Agent_bindings_Jenkinsfile')
printCallStack()
assertCallStack().contains('[label:someVar]')
assertCallStack().contains('docker:[', ', image:someVar, ', ']')
assertCallStack().contains('[label:someLabel]')
assertCallStack().contains('echo(Deploy to someLabel)')
assertJobStatusSuccess()
}
@Test
void should_log_downstream_var() throws Exception {
runScript('DownstreamBuild_Jenkinsfile')
printCallStack()
assertCallStack().contains('build({job=stop, parameters=[{name=stringVarName, value=stringVarValue}, {name=secretVarName, value=***********}, {name=booleanVarName, value=true}], wait=true}')
assertJobStatusSuccess()
}
}