/
JenkinsJobManagementSpec.groovy
1074 lines (857 loc) · 36 KB
/
JenkinsJobManagementSpec.groovy
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaposse.jobdsl.plugin
import jenkins.branch.OrganizationFolder
import com.cloudbees.hudson.plugins.folder.AbstractFolder
import com.cloudbees.hudson.plugins.folder.AbstractFolderProperty
import com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor
import com.cloudbees.hudson.plugins.folder.Folder
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider
import com.cloudbees.plugins.credentials.CredentialsScope
import com.cloudbees.plugins.credentials.domains.Domain
import com.cloudbees.plugins.credentials.domains.DomainCredentials
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl
import com.google.common.io.Resources
import hudson.Extension
import hudson.FilePath
import hudson.model.AbstractBuild
import hudson.model.AllView
import hudson.model.Cause
import hudson.model.Failure
import hudson.model.FreeStyleBuild
import hudson.model.FreeStyleProject
import hudson.model.Job
import hudson.model.ListView
import hudson.model.Run
import hudson.model.View
import hudson.model.listeners.SaveableListener
import hudson.tasks.ArtifactArchiver
import hudson.tasks.Fingerprinter
import javaposse.jobdsl.dsl.ConfigurationMissingException
import javaposse.jobdsl.dsl.DslException
import javaposse.jobdsl.dsl.DslScriptException
import javaposse.jobdsl.dsl.Item
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.dsl.NameNotProvidedException
import javaposse.jobdsl.dsl.UserContent
import javaposse.jobdsl.dsl.helpers.step.StepContext
import javaposse.jobdsl.dsl.helpers.triggers.TriggerContext
import javaposse.jobdsl.dsl.views.ColumnsContext
import javaposse.jobdsl.plugin.fixtures.TestContextExtensionPoint
import javaposse.jobdsl.plugin.fixtures.TestContextExtensionPoint2
import jenkins.model.Jenkins
import org.custommonkey.xmlunit.XMLUnit
import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.WithoutJenkins
import spock.lang.IgnoreIf
import spock.lang.Specification
import static hudson.model.Result.UNSTABLE
import static java.nio.charset.StandardCharsets.UTF_8
class JenkinsJobManagementSpec extends Specification {
private static final String FILE_NAME = 'test.txt'
private static final String JOB_NAME = 'test-job'
@Rule
public JenkinsRule jenkinsRule = new JenkinsRule()
private ByteArrayOutputStream buffer
private Job seedJob
private Run build
private JenkinsJobManagement jobManagement
private JenkinsJobManagement testJobManagement
def setup() {
seedJob = Mock(Job)
build = Mock(Run)
build.parent >> seedJob
buffer = new ByteArrayOutputStream()
jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, new FilePath(new File('.')), LookupStrategy.JENKINS_ROOT
)
testJobManagement = new JenkinsJobManagement(new PrintStream(buffer), [:], new File('.'))
}
@WithoutJenkins
def 'createOrUpdateView without name'() {
when:
jobManagement.createOrUpdateView(null, '<View/>', true)
then:
thrown(NameNotProvidedException)
when:
jobManagement.createOrUpdateView('', '<View/>', true)
then:
thrown(NameNotProvidedException)
}
@WithoutJenkins
def 'createOrUpdateView without config'() {
when:
jobManagement.createOrUpdateView('test', null, true)
then:
thrown(ConfigurationMissingException)
when:
jobManagement.createOrUpdateView('test', null, true)
then:
thrown(ConfigurationMissingException)
}
@WithoutJenkins
def 'createOrUpdateView with invalid name'() {
when:
jobManagement.createOrUpdateView('t<e*st', '<View/>', true)
then:
thrown(Failure)
}
def 'logPluginDeprecationWarning for unknown plugin'() {
when:
jobManagement.logPluginDeprecationWarning('foo', '1.2.3')
then:
buffer.size() == 0
}
def 'logPluginDeprecationWarning for plugin'() {
when:
jobManagement.logPluginDeprecationWarning('script-security', '20.0')
then:
buffer.toString() =~
/Warning: \(.+, line \d+\) support for Script Security Plugin versions older than 20.0 is deprecated/
}
def 'logPluginDeprecationWarning does not log anything if plugin version is newer'() {
when:
jobManagement.logPluginDeprecationWarning('script-security', '1.0')
then:
buffer.size() == 0
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'requirePlugin not installed'() {
when:
jobManagement.requirePlugin('foo')
then:
1 * build.setResult(UNSTABLE)
buffer.size() > 0
}
def 'requirePlugin not installed (without build)'() {
when:
testJobManagement.requirePlugin('foo')
then:
buffer.size() > 0
noExceptionThrown()
}
def 'fail requirePlugin not installed'() {
when:
jobManagement.requirePlugin('foo', true)
then:
thrown(DslScriptException)
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'requirePlugin success'() {
when:
jobManagement.requirePlugin('script-security', failIfMissing)
then:
0 * build.setResult(UNSTABLE)
buffer.size() == 0
noExceptionThrown()
where:
failIfMissing << [true, false]
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'checkMinimumPluginVersion not installed'() {
when:
jobManagement.requireMinimumPluginVersion('foo', '1.2.3')
then:
1 * build.setResult(UNSTABLE)
buffer.size() > 0
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'checkMinimumPluginVersion too old'() {
when:
jobManagement.requireMinimumPluginVersion('ldap', '20.0')
then:
1 * build.setResult(UNSTABLE)
buffer.size() > 0
}
def 'fail checkMinimumPluginVersion not installed'() {
when:
jobManagement.requireMinimumPluginVersion('foo', '1.2.3', true)
then:
thrown(DslScriptException)
}
def 'fail checkMinimumPluginVersion too old'() {
when:
jobManagement.requireMinimumPluginVersion('ldap', '20.0', true)
then:
thrown(DslScriptException)
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'checkMinimumPluginVersion success'() {
when:
jobManagement.requireMinimumPluginVersion('script-security', '1.1', failIfMissing)
then:
0 * build.setResult(UNSTABLE)
buffer.size() == 0
noExceptionThrown()
where:
failIfMissing << [true, false]
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'requireMinimumCoreVersion success'() {
when:
jobManagement.requireMinimumCoreVersion('1.480')
then:
0 * build.setResult(UNSTABLE)
buffer.size() == 0
}
@SuppressWarnings('UnnecessarySetter') // false positive for setResult
def 'requireMinimumCoreVersion failed'() {
when:
jobManagement.requireMinimumCoreVersion('3.600')
then:
1 * build.setResult(UNSTABLE)
buffer.size() > 0
}
def 'callExtension not found'() {
when:
Node result = jobManagement.callExtension('foo', Mock(Item), StepContext)
then:
result == null
}
def 'callExtension with no args'() {
when:
Node result = jobManagement.callExtension('test', Mock(Item), StepContext)
then:
isXmlIdentical('extension.xml', result)
}
def 'callExtension defined twice'() {
when:
jobManagement.callExtension('twice', Mock(Item), StepContext)
then:
Exception e = thrown(DslException)
e.message.contains(TestContextExtensionPoint.name)
e.message.contains(TestContextExtensionPoint2.name)
}
def 'callExtension with object result'() {
when:
Node result = jobManagement.callExtension('testComplexObject', Mock(Item), StepContext, 'foo', 42, true)
then:
isXmlIdentical('extension.xml', result)
}
def 'callExtension with closure'() {
setup:
Closure closure = {
value1('foo')
value2(42)
value3(true)
}
when:
Node result = jobManagement.callExtension('withNestedContext', Mock(Item), StepContext, closure)
then:
isXmlIdentical('extension.xml', result)
}
def 'callExtension with environment'() {
when:
Node result = jobManagement.callExtension('withEnvironment', Mock(Item), StepContext, 'foo', 42, true)
then:
isXmlIdentical('extension.xml', result)
}
def 'callExtension with no value'() {
when:
Node result = jobManagement.callExtension('withNoValue', Mock(Item), StepContext)
then:
result == JobManagement.NO_VALUE
}
def 'callExtension for view'() {
when:
Node result = jobManagement.callExtension('myColumn', null, ColumnsContext)
then:
isXmlIdentical('view-extension.xml', result)
}
def 'call deprecated extension'() {
when:
jobManagement.callExtension('old', null, TriggerContext)
then:
buffer.toString() =~ /Warning: \(.+, line \d+\) old is deprecated/
}
def 'extension is being notified'() {
when:
jobManagement.createOrUpdateConfig(createItem('test-123', '/config.xml'), false)
then:
ContextExtensionPoint.all().get(TestContextExtensionPoint).isItemCreated('test-123')
when:
jobManagement.createOrUpdateConfig(createItem('test-123', '/config2.xml'), false)
then:
ContextExtensionPoint.all().get(TestContextExtensionPoint).isItemUpdated('test-123')
}
def 'create job with nonexisting parent'() {
when:
jobManagement.createOrUpdateConfig(createItem('nonexistingfolder/project', '/minimal-job.xml'), true)
then:
DslException e = thrown()
e.message == 'Could not create item, unknown parent path in "nonexistingfolder/project"'
}
def 'create view with nonexisting parent'() {
when:
jobManagement.createOrUpdateView(
'nonexistingfolder/view', loadResource('minimal-view.xml'), true
)
then:
DslException e = thrown()
e.message == 'Could not create view, unknown parent path in "nonexistingfolder/view"'
}
def 'rename job'() {
setup:
jenkinsRule.createFreeStyleProject('oldName')
when:
jobManagement.renameJobMatching('oldName', 'newName')
then:
jenkinsRule.jenkins.getItemByFullName('newName') != null
jenkinsRule.jenkins.getItemByFullName('oldName') == null
}
def 'rename job relative to seed job'() {
setup:
Folder folder = jenkinsRule.jenkins.createProject(Folder, 'folder')
folder.createProject(FreeStyleProject, 'oldName')
FreeStyleProject seedJob = folder.createProject(FreeStyleProject, 'seed')
AbstractBuild build = seedJob.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.SEED_JOB
)
when:
jobManagement.renameJobMatching('oldName', 'newName')
then:
jenkinsRule.jenkins.getItemByFullName('folder/newName') != null
jenkinsRule.jenkins.getItemByFullName('folder/oldName') == null
}
def 'rename job relative to seed job with absolute destination'() {
setup:
Folder folder = jenkinsRule.jenkins.createProject(Folder, 'folder')
folder.createProject(FreeStyleProject, 'oldName')
FreeStyleProject seedJob = folder.createProject(FreeStyleProject, 'seed')
AbstractBuild build = seedJob.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.SEED_JOB
)
when:
jobManagement.renameJobMatching('oldName', '/newName')
then:
jenkinsRule.jenkins.getItemByFullName('newName') != null
jenkinsRule.jenkins.getItemByFullName('oldName') == null
}
def 'move Job to other Folder'() {
setup:
Folder oldFolder = jenkinsRule.jenkins.createProject(Folder, 'oldFolder')
jenkinsRule.jenkins.createProject(Folder, 'newFolder')
oldFolder.createProject(FreeStyleProject, 'oldName')
when:
jobManagement.renameJobMatching('oldFolder/oldName', 'newFolder/newName')
then:
jenkinsRule.jenkins.getItemByFullName('newFolder/newName') != null
jenkinsRule.jenkins.getItemByFullName('oldFolder/oldName') == null
}
def 'move job to non-existing folder'() {
setup:
jenkinsRule.createFreeStyleProject('bar')
when:
jobManagement.renameJobMatching('bar', 'foo/bar')
then:
Exception e = thrown(DslException)
e.message == 'Could not rename job bar to foo/bar, destination folder does not exist'
}
def 'createOrUpdateConfig relative to folder'() {
setup:
Folder folder = jenkinsRule.jenkins.createProject(Folder, 'folder')
FreeStyleProject project = folder.createProject(FreeStyleProject, 'seed')
AbstractBuild build = project.scheduleBuild2(0).get()
JenkinsJobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.SEED_JOB
)
when:
jobManagement.createOrUpdateConfig(createItem('project', ('/minimal-job.xml')), true)
then:
jenkinsRule.jenkins.getItemByFullName('/folder/project') != null
}
def 'createOrUpdateConfig without build'() {
when:
testJobManagement.createOrUpdateConfig(createItem('project', ('/minimal-job.xml')), true)
then:
jenkinsRule.jenkins.getItemByFullName('/project') != null
}
def 'createOrUpdateConfig with absolute path'() {
setup:
Folder folder = jenkinsRule.jenkins.createProject(Folder, 'folder')
FreeStyleProject project = folder.createProject(FreeStyleProject, 'seed')
AbstractBuild build = project.scheduleBuild2(0).get()
JenkinsJobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.SEED_JOB
)
when:
jobManagement.createOrUpdateConfig(createItem('/project', ('/minimal-job.xml')), true)
then:
jenkinsRule.jenkins.getItemByFullName('/project') != null
}
def 'createOrUpdateView relative to folder'() {
setup:
Folder folder = jenkinsRule.jenkins.createProject(Folder, 'folder')
FreeStyleProject project = folder.createProject(FreeStyleProject, 'seed')
AbstractBuild build = project.scheduleBuild2(0).get()
JenkinsJobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.SEED_JOB
)
when:
jobManagement.createOrUpdateView('view', loadResource('minimal-view.xml'), true)
then:
folder.getView('view') != null
}
def 'createOrUpdateConfig with changing element order'() {
when:
jobManagement.createOrUpdateConfig(createItem('project', '/order-a.xml'), false)
then:
FreeStyleProject job = jenkinsRule.jenkins.getItemByFullName('project') as FreeStyleProject
job.publishersList[0] instanceof ArtifactArchiver
job.publishersList[1] instanceof Fingerprinter
when:
jobManagement.createOrUpdateConfig(createItem('project', '/order-b.xml'), false)
then:
job.publishersList[0] instanceof Fingerprinter
job.publishersList[1] instanceof ArtifactArchiver
}
def 'createOrUpdateConfig skips update if identical'() {
setup:
SaveableListener saveableListener = Mock(SaveableListener)
when:
jobManagement.createOrUpdateConfig(createItem('project', '/config.xml'), false)
then:
FreeStyleProject job = jenkinsRule.jenkins.getItemByFullName('project') as FreeStyleProject
SaveableListener.all().add(0, saveableListener)
when:
jobManagement.createOrUpdateConfig(createItem('project', '/config.xml'), false)
then:
0 * saveableListener.onChange(job, _)
}
def 'createOrUpdateConfig should fail if item is managed by another seed and failOnSeedCollision is enabled'() {
setup:
FreeStyleProject seedJob = jenkinsRule.createFreeStyleProject('seed')
seedJob.buildersList.add(new ExecuteDslScripts('job("project")'))
jenkinsRule.buildAndAssertSuccess(seedJob)
FreeStyleProject seedJob2 = jenkinsRule.createFreeStyleProject('seed2')
AbstractBuild build = seedJob2.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.JENKINS_ROOT
)
jobManagement.failOnSeedCollision = true
when:
jobManagement.createOrUpdateConfig(createItem('project', '/config.xml'), false)
then:
Exception e = thrown(DslException)
e.message == 'Could not create item project, item is already managed by seed job seed'
}
def 'JENKINS-59995 seed in folder can update jobs in and out of folder'() {
Folder folder = jenkinsRule.jenkins.createProject(Folder, 'folder')
FreeStyleProject seedJob = folder.createProject(FreeStyleProject, 'seed')
seedJob.buildersList.add(new ExecuteDslScripts('job("/project")'))
seedJob.buildersList.add(new ExecuteDslScripts('job("/folder/project")'))
jenkinsRule.buildAndAssertSuccess(seedJob)
AbstractBuild build = seedJob.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.JENKINS_ROOT
)
jobManagement.failOnSeedCollision = true
when:
jobManagement.createOrUpdateConfig(createItem('/project', '/config.xml'), false)
jobManagement.createOrUpdateConfig(createItem('/folder/project', '/config.xml'), false)
then:
jenkinsRule.jenkins.getItemByFullName('/project') != null
jenkinsRule.jenkins.getItemByFullName('/folder/project') != null
}
def 'JENKINS-59995 two seeds should have collision on updating job in folder'() {
setup:
jenkinsRule.jenkins.createProject(Folder, 'folder')
FreeStyleProject seedJob = jenkinsRule.createFreeStyleProject('seed')
seedJob.buildersList.add(new ExecuteDslScripts('job("folder/project")'))
jenkinsRule.buildAndAssertSuccess(seedJob)
FreeStyleProject seedJob2 = jenkinsRule.createFreeStyleProject('seed2')
AbstractBuild build = seedJob2.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.JENKINS_ROOT
)
jobManagement.failOnSeedCollision = true
when:
jobManagement.createOrUpdateConfig(createItem('folder/project', '/config.xml'), false)
then:
Exception e = thrown(DslException)
e.message == 'Could not create item folder/project, item is already managed by seed job seed'
}
def 'createOrUpdateConfig should pass if item is managed by another seed and failOnSeedCollision is disabled'() {
setup:
FreeStyleProject seedJob = jenkinsRule.createFreeStyleProject('seed')
seedJob.buildersList.add(new ExecuteDslScripts('job("project")'))
jenkinsRule.buildAndAssertSuccess(seedJob)
FreeStyleProject seedJob2 = jenkinsRule.createFreeStyleProject('seed2')
AbstractBuild build = seedJob2.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.JENKINS_ROOT
)
when:
jobManagement.createOrUpdateConfig(createItem('project', '/config.xml'), false)
then:
ContextExtensionPoint.all().get(TestContextExtensionPoint).isItemUpdated('project')
}
def 'createOrUpdateConfig should work if generated item was managed by a deleted seed'() {
setup:
FreeStyleProject seedJob = jenkinsRule.createFreeStyleProject('seed')
seedJob.buildersList.add(new ExecuteDslScripts('job("project")'))
jenkinsRule.buildAndAssertSuccess(seedJob)
Jenkins.get().getItemByFullName('seed').delete()
FreeStyleProject seedJob2 = jenkinsRule.createFreeStyleProject('seed2')
AbstractBuild build = seedJob2.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.JENKINS_ROOT
)
when:
jobManagement.createOrUpdateConfig(createItem('project', '/config.xml'), false)
then:
ContextExtensionPoint.all().get(TestContextExtensionPoint).isItemUpdated('project')
}
def 'createOrUpdateConfig should fail if item type does not match'() {
setup:
jenkinsRule.createFolder('my-job')
when:
jobManagement.createOrUpdateConfig(createItem('my-job', '/minimal-job.xml'), false)
then:
Exception e = thrown(DslException)
e.message == 'Type of item "my-job" does not match existing type, item type can not be changed'
}
def 'createOrUpdateConfig should preserve credentials if they exist on a Folder'() {
setup:
Folder folder = jenkinsRule.createProject(Folder, 'folder')
folder.addProperty(createCredentialProperty())
when:
jobManagement.createOrUpdateConfig(createItem('folder', '/folder.xml'), false)
then:
def actual = jenkinsRule.jenkins.getItem('folder')
actual.properties.size() == 1
}
def 'createOrUpdateConfig should ignore other properties on the Folder'() {
setup:
Folder folder = jenkinsRule.createProject(Folder, 'folder')
folder.addProperty(new FakeProperty())
when:
jobManagement.createOrUpdateConfig(createItem('folder', '/folder.xml'), false)
then:
def actual = jenkinsRule.jenkins.getItem('folder')
actual.properties.size() == 0
}
def 'createOrUpdateConfig should preserve credentials if they exist on an OrganizationFolder'() {
setup:
OrganizationFolder folder = jenkinsRule.createProject(OrganizationFolder, 'org')
// `OrganizationFolder`s include a lot of existing metadata that is appended, regardless of how few we already
// set, so we should calculate expected as the default computed properties
int defaultProperties = folder.properties.size()
def property = createCredentialProperty()
folder.addProperty(property)
when:
jobManagement.createOrUpdateConfig(createItem('org', '/organizationfolder.xml'), false)
then:
def actualItem = jenkinsRule.jenkins.getItem('org')
def actual = (AbstractFolder<?>) actualItem
actual.properties.size() == defaultProperties + 1
}
def 'createOrUpdateConfig should ignore other properties on the OrganizationFolder'() {
setup:
OrganizationFolder folder = jenkinsRule.createProject(OrganizationFolder, 'org')
// `OrganizationFolder`s include a lot of existing metadata that is appended, regardless of how few we already
// set, so we should calculate expected as the default computed properties
int defaultProperties = folder.properties.size()
folder.addProperty(new FakeProperty())
when:
jobManagement.createOrUpdateConfig(createItem('org', '/organizationfolder.xml'), false)
folder.writeConfigDotXml(System.out)
then:
def actual = jenkinsRule.jenkins.getItem('org')
actual.properties.size() == defaultProperties
}
def 'createOrUpdateView should work if view type changes'() {
setup:
jenkinsRule.jenkins.addView(new AllView('foo'))
when:
jobManagement.createOrUpdateView(
'foo',
JenkinsJobManagementSpec.getResourceAsStream('/javaposse/jobdsl/dsl/views/ListView-template.xml').text,
false
)
then:
noExceptionThrown()
jenkinsRule.jenkins.getView('foo') != null
jenkinsRule.jenkins.getView('foo').class == ListView
}
def isMinimumPluginVersionInstalled() {
when:
boolean result = jobManagement.isMinimumPluginVersionInstalled('script-security', '0.1')
then:
result
when:
result = jobManagement.isMinimumPluginVersionInstalled('script-security', '10.0')
then:
!result
when:
result = jobManagement.isMinimumPluginVersionInstalled('foo', '0.1')
then:
!result
}
def 'get vSphere cloud hash without vSphere cloud plugin'() {
when:
Integer hash = jobManagement.getVSphereCloudHash('test')
then:
hash == null
}
def 'read file from any workspace, job does not exist'() {
when:
String result = jobManagement.readFileInWorkspace(JOB_NAME, FILE_NAME)
then:
result == null
buffer.toString().contains(FILE_NAME)
buffer.toString().contains(JOB_NAME)
}
def 'read file from any workspace, no build, no workspace'() {
setup:
jenkinsRule.createFreeStyleProject(JOB_NAME)
when:
String result = jobManagement.readFileInWorkspace(JOB_NAME, FILE_NAME)
then:
result == null
buffer.toString().contains(FILE_NAME)
buffer.toString().contains(JOB_NAME)
}
def 'read file from any workspace, file does not exist'() {
setup:
jenkinsRule.buildAndAssertSuccess(jenkinsRule.createFreeStyleProject(JOB_NAME))
when:
String result = jobManagement.readFileInWorkspace(JOB_NAME, FILE_NAME)
then:
result == null
buffer.toString().contains(FILE_NAME)
buffer.toString().contains(JOB_NAME)
}
def 'read file from any workspace, file exists'() {
setup:
FreeStyleBuild build = jenkinsRule.buildAndAssertSuccess(jenkinsRule.createFreeStyleProject(JOB_NAME))
build.workspace.child(FILE_NAME).write('hello', 'UTF-8')
when:
String result = jobManagement.readFileInWorkspace(JOB_NAME, FILE_NAME)
then:
result == 'hello'
}
def 'create view'() {
when:
jobManagement.createOrUpdateView('test-view', '<hudson.model.ListView/>', false)
then:
View view = jenkinsRule.instance.getView('test-view')
view instanceof ListView
}
def 'create view without build'() {
when:
testJobManagement.createOrUpdateView('test-view', '<hudson.model.ListView/>', false)
then:
View view = jenkinsRule.instance.getView('test-view')
view instanceof ListView
}
def 'update view'() {
setup:
jenkinsRule.instance.addView(new ListView('test-view'))
when:
jobManagement.createOrUpdateView(
'test-view',
'<hudson.model.ListView><description>lorem ipsum</description></hudson.model.ListView>',
false
)
then:
View view = jenkinsRule.instance.getView('test-view')
view instanceof ListView
view.description == 'lorem ipsum'
}
def 'update view ignoring changes'() {
setup:
jenkinsRule.instance.addView(new ListView('test-view'))
when:
jobManagement.createOrUpdateView(
'test-view',
'<hudson.model.ListView><description>lorem ipsum</description></hudson.model.ListView>',
true
)
then:
View view = jenkinsRule.instance.getView('test-view')
view instanceof ListView
view.description == null
}
def 'create view with invalid config'() {
when:
jobManagement.createOrUpdateView('test-view', '<hudson.model.ListView', false)
then:
thrown(DslException)
}
def 'readFileFromWorkspace with exception'() {
setup:
AbstractBuild build = jenkinsRule.buildAndAssertSuccess(jenkinsRule.createFreeStyleProject())
jobManagement = new JenkinsJobManagement(System.out, [:], build, build.workspace, LookupStrategy.JENKINS_ROOT)
String fileName = 'test.txt'
when:
jobManagement.readFileInWorkspace(fileName)
then:
Exception e = thrown(DslScriptException)
e.message.contains(fileName)
}
def 'get all job permissions'() {
setup:
String propertyName = 'hudson.security.AuthorizationMatrixProperty'
when:
Set<String> permissions = jobManagement.getPermissions(propertyName)
then:
'hudson.model.Item.Delete' in permissions
'hudson.model.Item.Configure' in permissions
'hudson.model.Item.Read' in permissions
'hudson.model.Item.Discover' in permissions
'hudson.model.Item.Build' in permissions
'hudson.model.Item.Workspace' in permissions
'hudson.model.Item.Cancel' in permissions
'hudson.model.Item.Move' in permissions
'hudson.model.Run.Delete' in permissions
'hudson.model.Run.Update' in permissions
'hudson.scm.SCM.Tag' in permissions
}
def 'get all folder permissions'() {
setup:
String propertyName = 'com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty'
when:
Set<String> permissions = jobManagement.getPermissions(propertyName)
then:
'hudson.model.Item.Create' in permissions
'hudson.model.Item.Delete' in permissions
'hudson.model.Item.Configure' in permissions
'hudson.model.Item.Read' in permissions
'hudson.model.Item.Discover' in permissions
'hudson.model.Item.Build' in permissions
'hudson.model.Item.Workspace' in permissions
'hudson.model.Item.Cancel' in permissions
'hudson.model.Item.Move' in permissions
'hudson.model.Run.Delete' in permissions
'hudson.model.Run.Update' in permissions
}
def 'upload user content'(boolean ignoreExisting) {
setup:
UserContent userContent = new UserContent('foo.txt', new ByteArrayInputStream('foo'.bytes))
when:
jobManagement.createOrUpdateUserContent(userContent, ignoreExisting)
then:
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').exists()
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').readToString() == 'foo'
where:
ignoreExisting << [true, false]
}
def 'update user content'() {
setup:
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').write('lorem ipsum', 'UTF-8')
UserContent userContent = new UserContent('foo.txt', new ByteArrayInputStream('foo'.bytes))
when:
jobManagement.createOrUpdateUserContent(userContent, false)
then:
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').exists()
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').readToString() == 'foo'
}
def 'do not update existing user content'() {
setup:
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').write('lorem ipsum', 'UTF-8')
UserContent userContent = new UserContent('foo.txt', new ByteArrayInputStream('foo'.bytes))
when:
jobManagement.createOrUpdateUserContent(userContent, true)
then:
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').exists()
jenkinsRule.instance.rootPath.child('userContent').child('foo.txt').readToString() == 'lorem ipsum'
}
@SuppressWarnings('BusyWait')
@IgnoreIf({ os.isWindows() })
def 'queue job'() {
setup:
FreeStyleProject project = jenkinsRule.createProject(FreeStyleProject, 'project')
FreeStyleProject seedJob = jenkinsRule.createProject(FreeStyleProject, 'seed')
AbstractBuild build = seedJob.scheduleBuild2(0).get()
JobManagement jobManagement = new JenkinsJobManagement(
new PrintStream(buffer), [:], build, build.workspace, LookupStrategy.JENKINS_ROOT
)
jenkinsRule.instance.quietPeriod = 0
when:
jobManagement.queueJob(project.name)
while (project.builds.lastBuild == null) {
Thread.sleep(100)
}
then:
project.builds.lastBuild.causes.size() == 1
project.builds.lastBuild.causes[0] instanceof Cause.UpstreamCause
Cause.UpstreamCause upstreamCause = project.builds.lastBuild.causes[0] as Cause.UpstreamCause
upstreamCause.upstreamBuild == 1
upstreamCause.upstreamProject == 'seed'
}
@SuppressWarnings('BusyWait')
@IgnoreIf({ os.isWindows() })
def 'queue job without build'() {
setup:
FreeStyleProject project = jenkinsRule.createProject(FreeStyleProject, 'project')
jenkinsRule.instance.quietPeriod = 0
when:
testJobManagement.queueJob(project.name)
while (project.builds.lastBuild == null) {
Thread.sleep(100)
}
then:
project.builds.lastBuild.causes.size() == 1
project.builds.lastBuild.causes[0].shortDescription == 'Started by a Job DSL script'
}