-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathTestDeclarativePipeline.groovy
More file actions
833 lines (732 loc) · 31 KB
/
TestDeclarativePipeline.groovy
File metadata and controls
833 lines (732 loc) · 31 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
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/A.B.C')
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_feature() throws Exception {
addEnvVar('BRANCH_NAME', 'feature/jenkins')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_branch_pattern_not() throws Exception {
addEnvVar('BRANCH_NAME', '?')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf branch')
assertJobStatusSuccess()
}
@Test void when_anyOf_tag_latest() throws Exception {
addEnvVar('TAG_NAME', 'latest')
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_v_pattern() throws Exception {
addEnvVar('TAG_NAME', 'v-X.Y.Z')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with tag')
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_not() throws Exception {
addEnvVar('TAG_NAME', '?')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf tag')
assertJobStatusSuccess()
}
@Test void when_anyOf_changeRequest() throws Exception {
addEnvVar('CHANGE_TARGET', 'release/1.2.3')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing anyOf with changeRequest')
assertJobStatusSuccess()
}
@Test void when_anyOf_changeRequest_not() throws Exception {
addEnvVar('CHANGE_TARGET', 'master')
runScript('AnyOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example anyOf changeRequests')
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_allOf_branches() throws Exception {
addEnvVar('BRANCH_NAME', 'production')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example allOf branches')
assertJobStatusSuccess()
}
@Test void when_allOf_tags() throws Exception {
addEnvVar('TAG_NAME', 'latest')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example allOf tags')
assertJobStatusSuccess()
}
@Test void when_allOf_changerequests() throws Exception {
addEnvVar('CHANGE_TARGET', 'develop')
addEnvVar('CHANGE_BRANCH', 'feature/ABC-123 new feature')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing allOf with changeRequest')
assertJobStatusSuccess()
}
@Test void when_allOf_changerequests_not() throws Exception {
addEnvVar('CHANGE_TARGET', 'develop')
addEnvVar('CHANGE_BRANCH', 'hotfix/ABC-123 problem solved')
runScript('AllOf_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example allOf changeRequests')
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_nested_when_beforeAgent_with_matching_branch_expression() throws Exception {
addEnvVar('BRANCH_NAME', 'main')
runScript('Nested_BeforeAgent_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when beforeAgent expression')
assertCallStack().contains('Executing on agent [label:beforeAgent-testLabel]')
assertJobStatusSuccess()
}
@Test void when_nested_when_beforeAgent_with_nonmatching_branch_expression() throws Exception {
addEnvVar('BRANCH_NAME', 'dev')
runScript('Nested_BeforeAgent_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when beforeAgent expression')
assertCallStack().doesNotContain('Executing on agent [label:beforeAgent-testLabel]')
assertJobStatusSuccess()
}
@Test void when_branch_using_explicit_equals_comparator() throws Exception {
addEnvVar('BRANCH_NAME', 'develop')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('EQUALS (explicit)')
assertJobStatusSuccess()
}
@Test void when_branch_using_implicit_glob_comparator() throws Exception {
addEnvVar('BRANCH_NAME', 'feature/xyz')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('GLOB (implicit)')
assertJobStatusSuccess()
}
@Test void when_branch_using_explicit_glob_comparator() throws Exception {
addEnvVar('BRANCH_NAME', 'bugfix/xyz')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('GLOB (explicit)')
assertJobStatusSuccess()
}
@Test void when_branch_using_explicit_regexp_comparator() throws Exception {
addEnvVar('BRANCH_NAME', 'hotfix/xyz')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('REGEXP (explicit)')
assertJobStatusSuccess()
}
@Test void when_branch_not() throws Exception {
addEnvVar('BRANCH_NAME', 'master')
runScript('Branch_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example - EQUALS comparator')
assertCallStack().contains('Skipping stage Example - GLOB comparator (implicit)')
assertCallStack().contains('Skipping stage Example - GLOB comparator (explicit)')
assertCallStack().contains('Skipping stage Example - REGEXP comparator')
assertJobStatusSuccess()
}
@Test void when_change_request() throws Exception {
addEnvVar('CHANGE_ID', '1')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test only change requests')
assertJobStatusSuccess()
}
@Test void when_change_request_not() throws Exception {
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example test')
assertJobStatusSuccess()
}
@Test void when_change_request_target() throws Exception {
addEnvVar('CHANGE_TARGET', 'develop')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test change requests with develop branch as target')
assertJobStatusSuccess()
}
@Test void when_change_request_branch_glob() throws Exception {
addEnvVar('CHANGE_BRANCH', 'feature/ABC-123 feature branch')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test change requests with any feature branch as source')
assertJobStatusSuccess()
}
@Test void when_change_request_branch_glob_not() throws Exception {
addEnvVar('CHANGE_BRANCH', 'hotfix/ABC-123 hotfix branch')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example test branch feature')
assertJobStatusSuccess()
}
@Test void when_change_request_author() throws Exception {
addEnvVar('CHANGE_AUTHOR', 'unreliableUser')
addEnvVar('CHANGE_AUTHOR_EMAIL', 'unreliableUser@com.com')
addEnvVar('CHANGE_AUTHOR_DISPLAY_NAME', 'Unreliable User')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test change requests with unreliableUser as author')
assertJobStatusSuccess()
}
@Test void when_change_request_id_equals() throws Exception {
addEnvVar('CHANGE_ID', '42')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test change request with id 42')
assertJobStatusSuccess()
}
@Test void when_change_request_id_equals_not() throws Exception {
addEnvVar('CHANGE_ID', '042')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example test id equals')
assertJobStatusSuccess()
}
@Test void when_change_request_title_regexp() throws Exception {
addEnvVar('CHANGE_TITLE', 'ABC-123 new feature')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test change requests with alphanumeric titles')
assertJobStatusSuccess()
}
@Test void when_change_request_title_regexp_not() throws Exception {
addEnvVar('CHANGE_TITLE', '[ABC-123] new feature')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example test title')
assertJobStatusSuccess()
}
@Test void when_change_request_url_fork_glob() throws Exception {
addEnvVar('CHANGE_URL', 'https://github.com/JenkinsPipelineUnit')
addEnvVar('CHANGE_FORK', 'https://github.com/user/JenkinsPipelineUnit')
runScript('ChangeRequest_Jenkinsfile')
printCallStack()
assertCallStack().contains('Test change requests with github in url and fork')
assertJobStatusSuccess()
}
@Test void when_buildingTag() throws Exception {
addEnvVar('TAG_NAME', 'release-1.0.0')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Generating Release Notes for any tag')
assertJobStatusSuccess()
}
@Test void when_buildingTag_not() throws Exception {
// no TAG_NAME variable defined
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example - EQUALS comparator')
assertCallStack().contains('Skipping stage Example - GLOB comparator (implicit)')
assertCallStack().contains('Skipping stage Example - GLOB comparator (explicit)')
assertCallStack().contains('Skipping stage Example - REGEXP comparator')
assertCallStack().contains('Skipping stage Example Release Notes')
assertJobStatusSuccess()
}
@Test void when_tag_using_explicit_equals_comparator() throws Exception {
addEnvVar('TAG_NAME', 'x.y.z')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('EQUALS (explicit)')
assertJobStatusSuccess()
}
@Test void when_tag_using_implicit_glob_comparator() throws Exception {
addEnvVar('TAG_NAME', 'v1.0.0')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('GLOB (implicit)')
assertJobStatusSuccess()
}
@Test void when_tag_using_explicit_glob_comparator() throws Exception {
addEnvVar('TAG_NAME', 'v-1.0.0')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('GLOB (explicit)')
assertJobStatusSuccess()
}
@Test void when_tag_using_explicit_regexp_comparator() throws Exception {
addEnvVar('TAG_NAME', '1.0.0')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('REGEXP (explicit)')
assertJobStatusSuccess()
}
@Test void when_tag_not() throws Exception {
addEnvVar('TAG_NAME', 'someothertag')
runScript('Tag_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example - EQUALS comparator')
assertCallStack().contains('Skipping stage Example - GLOB comparator (implicit)')
assertCallStack().contains('Skipping stage Example - GLOB comparator (explicit)')
assertCallStack().contains('Skipping stage Example - REGEXP comparator')
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_kubernetes_default_agent() throws Exception {
runScript('Kubernetes_Default_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing on agent [kubernetes')
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_with_when() throws Exception {
// given:
addEnvVar('SKIP_CI', 'true')
// when:
runScript('Parallel_When_Jenkinsfile')
// then:
printCallStack()
assertCallStack().contains('echo(Skipping stage Analysis)')
assertCallStack().doesNotContain('sh(run-lint.sh)')
assertCallStack().doesNotContain('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 withEnv_can_have_equals_in_value() throws Exception {
runScript('WithEnvEquals_Jenkinsfile')
printCallStack()
assertCallStack().contains('SOMEVAR inside closure = SOMETHING=SOME_OTHER_VAR')
assertJobStatusSuccess()
}
@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 {
addParam('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_agent_with_empty_label() throws Exception {
runScript('AgentEmptyLabel_Jenkinsfile')
printCallStack()
assertCallStack().contains('[label:]')
assertCallStack().contains('echo(Hello using custom workspace and empty label)')
assertJobStatusSuccess()
}
@Test void should_scope_this_in_closure() throws Exception {
runScript('ThisScope_Jenkinsfile')
printCallStack()
assertCallStack().contains('writeFile([file:messages/messages.msg, text:text])')
}
@Test void test_agent_in_stage_with_no_steps() {
runScript("AgentStageNoSteps_Jenkinsfile")
assertJobStatusSuccess()
assertCallStack().contains('post A')
assertCallStack().contains('post B')
assertCallStack().contains('post C')
assertCallStack().contains('echo(A)')
assertCallStack().contains('echo(C)')
assertCallStack().contains('stage(A, groovy.lang.Closure)')
assertCallStack().contains('stage(C, groovy.lang.Closure)')
assertCallStack().contains('labelA')
assertCallStack().contains('labelC')
// assertion bellow would fail
assertCallStack().contains('stage(B, groovy.lang.Closure)')
assertCallStack().contains('labelB')
}
@Test(expected = IllegalArgumentException)
void test_stage_and_steps() {
runScript("StageAndSteps_Jenkinsfile")
}
}