-
Notifications
You must be signed in to change notification settings - Fork 117
/
CopyArtifactTest.java
2491 lines (2231 loc) · 119 KB
/
CopyArtifactTest.java
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
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Alan Harder
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.copyartifact;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher;
import hudson.cli.CLICommandInvoker;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.Combination;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import hudson.matrix.MatrixRun;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BooleanParameterDefinition;
import hudson.model.BooleanParameterValue;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.ChoiceParameterDefinition;
import hudson.model.Computer;
import hudson.model.Fingerprint;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Queue;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.model.User;
import hudson.plugins.copyartifact.testutils.CopyArtifactJenkinsRule;
import hudson.plugins.copyartifact.testutils.CopyArtifactUtil;
import hudson.plugins.copyartifact.testutils.FileWriteBuilder;
import hudson.plugins.copyartifact.testutils.WrapperBuilder;
import hudson.remoting.VirtualChannel;
import hudson.security.ACL;
import hudson.slaves.DumbSlave;
import hudson.slaves.SlaveComputer;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildTrigger;
import hudson.tasks.Builder;
import hudson.tasks.Fingerprinter;
import hudson.util.FormValidation;
import hudson.util.VersionNumber;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import jenkins.model.ArtifactManagerConfiguration;
import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.DirectArtifactManagerFactory;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.MockFolder;
import org.jvnet.hudson.test.MockQueueItemAuthenticator;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.ToolInstallations;
import org.jvnet.hudson.test.UnstableBuilder;
import org.jvnet.hudson.test.recipes.LocalData;
import org.jvnet.hudson.test.recipes.WithPlugin;
import com.cloudbees.hudson.plugins.folder.Folder;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebRequest;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeThat;
/**
* Test interaction of copyartifact plugin with Jenkins core.
* @author Alan Harder
*/
public class CopyArtifactTest {
@ClassRule
public static BuildWatcher watcher = new BuildWatcher();
@Rule
public final CopyArtifactJenkinsRule rule = new CopyArtifactJenkinsRule();
@Rule
public TestName name = new TestName();
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
// Tests using agents fails with Jenkins < 1.520 on Windows.
// See https://wiki.jenkins-ci.org/display/JENKINS/Unit+Test+on+Windows
private void purgeAgents() {
List<Computer> disconnectingComputers = new ArrayList<>();
List<VirtualChannel> closingChannels = new ArrayList<>();
for (Computer computer: rule.jenkins.getComputers()) {
if (!(computer instanceof SlaveComputer)) {
continue;
}
// disconnect agents.
// retrieve the channel before disconnecting.
// even a computer gets offline, channel delays to close.
if (!computer.isOffline()) {
VirtualChannel ch = computer.getChannel();
computer.disconnect(null);
disconnectingComputers.add(computer);
closingChannels.add(ch);
}
}
try {
// Wait for all computers disconnected and all channels closed.
for (Computer computer: disconnectingComputers) {
computer.waitUntilOffline();
}
for (VirtualChannel ch: closingChannels) {
ch.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@After
public void tearDown() throws Exception {
if(Functions.isWindows()) {
purgeAgents();
}
System.clearProperty("hudson.security.ArtifactsPermission");
}
private FreeStyleProject createProject(String otherProject, String parameters, String filter,
String target, boolean stable, boolean flatten, boolean optional, boolean fingerprintArtifacts)
throws IOException {
FreeStyleProject p = rule.createFreeStyleProject();
CopyArtifact copyArtifact = CopyArtifactUtil.createCopyArtifact(otherProject, parameters, new StatusBuildSelector(stable), filter, target, flatten, optional, fingerprintArtifacts);
p.getBuildersList().add(copyArtifact);
return p;
}
@Deprecated
private FreeStyleProject createProject(String otherProject, String parameters, String filter,
String target, boolean stable, boolean flatten, boolean optional)
throws IOException {
return createProject(otherProject, parameters, filter, target, stable, flatten, optional, true);
}
private static class ArtifactBuilder extends Builder {
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
// Make some files to archive as artifacts
FilePath ws = build.getWorkspace();
ws.child("foo.txt").touch(System.currentTimeMillis());
ws.child("subdir").mkdirs();
ws.child("subdir/subfoo.txt").touch(System.currentTimeMillis());
ws.child("deepfoo/a/b").mkdirs();
ws.child("deepfoo/a/b/c.log").touch(System.currentTimeMillis());
ws.child(".hg").mkdirs();
ws.child(".hg/defaultexclude.txt").touch(System.currentTimeMillis());
// For matrix tests write one more file:
String foo = build.getBuildVariables().get("FOO");
if (foo != null) {
ws.child(foo + ".txt").touch(System.currentTimeMillis());
}
return true;
}
}
private FreeStyleProject createArtifactProject(String name) throws IOException {
FreeStyleProject p = name != null ? rule.createFreeStyleProject(name) : rule.createFreeStyleProject();
p.getBuildersList().add(new ArtifactBuilder());
p.getPublishersList().add(new ArtifactArchiver("**", "", false, false));
return p;
}
private FreeStyleProject createArtifactProject() throws IOException {
return createArtifactProject(null);
}
private String createUniqueProjectName() {
return "test" + rule.jenkins.getItems().size();
}
/**
* Creates an empty Maven project with an unique name.
*
* @return an empty Maven project with an unique name.
*/
private MavenModuleSet createMavenProject() throws IOException {
MavenModuleSet mavenModuleSet = rule.jenkins.createProject(MavenModuleSet.class, createUniqueProjectName());
mavenModuleSet.setRunHeadless(true);
return mavenModuleSet;
}
/**
* Creates an empty Matrix project with an unique name.
*
* @return an empty Matrix project with an unique name.
*/
private MatrixProject createMatrixProject() throws IOException {
return rule.jenkins.createProject(MatrixProject.class, createUniqueProjectName());
}
private MatrixProject createMatrixArtifactProject() throws IOException {
MatrixProject p = createMatrixProject();
p.setAxes(new AxisList(new Axis("FOO", "one", "two")));
p.getBuildersList().add(new ArtifactBuilder());
p.getPublishersList().add(new ArtifactArchiver("**", "", false, false));
return p;
}
private void assertFile(boolean exists, String path, AbstractBuild<?,?> b)
throws IOException, InterruptedException {
if (b.getWorkspace().child(path).exists() != exists) {
assertEquals(path + ": " + JenkinsRule.getLog(b), exists, !exists);
}
}
@Test
public void testMissingProject() throws Exception {
FreeStyleProject p = createProject("invalid", null, "", "", false, false, false, true);
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testMissingBuild() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(),
p = createProject(other.getName(), null, "", "", false, false, false, true);
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testMissingStableBuild() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(),
p = createProject(other.getName(), null, "", "", true, false, false, true);
// Make an unstable build in "other"
other.getBuildersList().add(new UnstableBuilder());
rule.assertBuildStatus(Result.UNSTABLE, other.scheduleBuild2(0, new Cause.UserIdCause()).get());
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testMissingArtifact() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(),
p = createProject(other.getName(), null, "*.txt", "", false, false, false, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testCopyAllWithFingerprints() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = createProject(other.getName(), null, "", "", false, false, false, true);
FreeStyleBuild s = rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "subdir/subfoo.txt", b);
assertFile(true, "deepfoo/a/b/c.log", b);
// testing fingerprints
String d = b.getWorkspace().child("foo.txt").digest();
Fingerprint f = rule.jenkins.getFingerprintMap().get(d);
assertSame(f.getOriginal().getRun(),s);
assertTrue(f.getRangeSet(p).includes(b.getNumber()));
}
@Test
public void testCopyAllWithoutFingerprints() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = createProject(other.getName(), null, "", "", false, false, false, false);
FreeStyleBuild s = rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "subdir/subfoo.txt", b);
assertFile(true, "deepfoo/a/b/c.log", b);
// testing no fingerprints
String d = b.getWorkspace().child("foo.txt").digest();
assertNull(rule.jenkins.getFingerprintMap().get(d));
assertNull(s.getAction(Fingerprinter.FingerprintAction.class));
assertNull(b.getAction(Fingerprinter.FingerprintAction.class));
}
@Test
public void testCopyWithFilter() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = createProject(other.getName(), null, "**/bogus*, **/sub*, bogus/**", "",
false, false, false, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(false, "foo.txt", b);
assertFile(true, "subdir/subfoo.txt", b);
assertFile(false, "deepfoo/a/b/c.log", b);
}
@Test
public void testCopyToTarget() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = createProject(other.getName(), null, "deep*/**", "new/deep/dir",
true, false, false, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(false, "foo.txt", b);
assertFile(false, "new/deep/dir/foo.txt", b);
assertFile(true, "new/deep/dir/deepfoo/a/b/c.log", b);
}
@Test
public void testCopyToSlave() throws Exception {
DumbSlave node = rule.createSlave();
SlaveComputer c = node.getComputer();
c.connect(false).get(); // wait until it's connected
if (c.isOffline()) {
fail("Agent failed to go online: " + c.getLog());
}
FreeStyleProject other = createArtifactProject(),
p = createProject(other.getName(), null, "", "", false, false, false, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
p.setAssignedLabel(node.getSelfLabel());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertSame(node, b.getBuiltOn());
assertFile(true, "foo.txt", b);
assertFile(true, "subdir/subfoo.txt", b);
assertFile(true, "deepfoo/a/b/c.log", b);
}
@Test
public void testParameters() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = createProject("$PROJSRC", null, "$BASE/*.txt", "$TARGET/bar",
false, false, false, true);
ParameterDefinition paramDef = new StringParameterDefinition("PROJSRC", other.getName(), "");
ParameterDefinition paramDef2 = new StringParameterDefinition("BASE", "", "");
ParameterDefinition paramDef3 = new StringParameterDefinition("TARGET", "foo", "");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef, paramDef2, paramDef3);
p.addProperty(paramsDef);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("PROJSRC", other.getName()),
new StringParameterValue("BASE", "*r"),
new StringParameterValue("TARGET", "foo"))).get();
rule.assertBuildStatusSuccess(b);
assertFile(false, "foo/bar/foo.txt", b);
assertFile(true, "foo/bar/subdir/subfoo.txt", b);
}
@Issue("JENKINS-36554")
@Test
public void testEmptyParameter() throws Exception {
FreeStyleProject copiee = createArtifactProject();
FreeStyleProject copier = rule.createFreeStyleProject();
copier.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("EMPTY", "")
));
CopyArtifact ca = new CopyArtifact(String.format("%s${EMPTY}", copiee.getFullDisplayName()));
copier.getBuildersList().add(ca);
rule.buildAndAssertSuccess(copiee);
rule.buildAndAssertSuccess(copier);
}
/** Test copying artifacts from a particular configuration of a matrix job */
@Test
public void testMatrixJob() throws Exception {
MatrixProject other = createMatrixArtifactProject();
FreeStyleProject p = createProject(other.getName() + "/FOO=two", null, "", "",
true, false, false, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "two.txt", b);
assertFile(true, "subdir/subfoo.txt", b);
assertFile(true, "deepfoo/a/b/c.log", b);
}
/** Test artifact copy between matrix jobs, for artifact from matching axis */
@Test
public void testMatrixToMatrix() throws Exception {
MatrixProject other = createMatrixArtifactProject(),
p = createMatrixProject();
p.setAxes(new AxisList(new Axis("FOO", "one", "two"))); // should match other job
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName() + "/FOO=$FOO", null,
new StatusBuildSelector(true), "", "", false, false, true));
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
MatrixBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
MatrixRun r = b.getRun(new Combination(Collections.singletonMap("FOO", "one")));
assertFile(true, "one.txt", r);
assertFile(false, "two.txt", r);
r = b.getRun(new Combination(Collections.singletonMap("FOO", "two")));
assertFile(false, "one.txt", r);
assertFile(true, "two.txt", r);
}
private static class ArchMatrixBuilder extends Builder {
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
// Get matrix axis value
String arch = build.getBuildVariables().get("ARCH");
FilePath ws = build.getWorkspace();
ws.child("target").mkdirs();
// One fixed filename:
ws.child("target/readme.txt").touch(System.currentTimeMillis());
// Use axis value in one filename:
ws.child("target/" + arch + ".out").touch(System.currentTimeMillis());
return true;
}
}
/** Test copying artifacts from all configurations of a matrix job */
@Test
public void testMatrixAll() throws Exception {
MatrixProject mp = createMatrixProject();
mp.setAxes(new AxisList(new Axis("ARCH", "sparc", "x86")));
mp.getBuildersList().add(new ArchMatrixBuilder());
mp.getPublishersList().add(new ArtifactArchiver("target/*", "", false, false));
rule.assertBuildStatusSuccess(mp.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleProject p = createProject(mp.getName(), null, "", "", true, false, false, true);
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "ARCH=sparc/target/readme.txt", b);
assertFile(true, "ARCH=sparc/target/sparc.out", b);
assertFile(true, "ARCH=x86/target/readme.txt", b);
assertFile(true, "ARCH=x86/target/x86.out", b);
}
private MavenModuleSet setupMavenJob() throws Exception {
ToolInstallations.configureMaven3();
MavenModuleSet mp = createMavenProject();
mp.setGoals("clean package -Dmaven.compiler.source=1.8 -Dmaven.compiler.target=1.8");
mp.setScm(rule.getExtractResourceScm(tempFolder, getClass().getResource("maven-job")));
return mp;
}
private static final VersionNumber MAVEN_POM_CUTOFF = new VersionNumber("1.405");
private static String pomName(String module, String version) {
return module + '/' + version + '/' +
(MAVEN_POM_CUTOFF.isNewerThan(Jenkins.getVersion()) ? "pom.xml"
: (module + '-' + version + ".pom"));
}
/** Test copying from a particular module of a maven job */
@Test
public void testMavenJob() throws Exception {
MavenModuleSet mp = setupMavenJob();
rule.assertBuildStatusSuccess(mp.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleProject p = createProject(mp.getName() + "/org.jvnet.hudson.main.test.multimod$moduleB",
null, "", "", true, false, false, true);
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
String dir = "org.jvnet.hudson.main.test.multimod/";
assertFile(true, dir + "moduleB/1.0-SNAPSHOT/moduleB-1.0-SNAPSHOT.jar", b);
assertFile(true, dir + pomName("moduleB", "1.0-SNAPSHOT"), b);
}
/** Test copying all artifacts from a maven job */
@Test
public void testMavenAll() throws Exception {
MavenModuleSet mp = setupMavenJob();
rule.assertBuildStatusSuccess(mp.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleProject p = createProject(mp.getName(), null, "", "", true, false, false, true);
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
String dir = "org.jvnet.hudson.main.test.multimod/";
assertFile(true, dir + "moduleA/1.0-SNAPSHOT/moduleA-1.0-SNAPSHOT.jar", b);
assertFile(true, dir + pomName("moduleA", "1.0-SNAPSHOT"), b);
assertFile(true, dir + "moduleB/1.0-SNAPSHOT/moduleB-1.0-SNAPSHOT.jar", b);
assertFile(true, dir + pomName("moduleB", "1.0-SNAPSHOT"), b);
assertFile(true, dir + "moduleC/1.0-SNAPSHOT/moduleC-1.0-SNAPSHOT.jar", b);
assertFile(true, dir + pomName("moduleC", "1.0-SNAPSHOT"), b);
// Test with filter
p = createProject(mp.getName(), null, "**/*.jar", "", true, false, false, true);
b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
assertFile(true, dir + "moduleA/1.0-SNAPSHOT/moduleA-1.0-SNAPSHOT.jar", b);
assertFile(false, dir + pomName("moduleA", "1.0-SNAPSHOT"), b);
assertFile(true, dir + "moduleB/1.0-SNAPSHOT/moduleB-1.0-SNAPSHOT.jar", b);
assertFile(false, dir + pomName("moduleB", "1.0-SNAPSHOT"), b);
assertFile(true, dir + "moduleC/1.0-SNAPSHOT/moduleC-1.0-SNAPSHOT.jar", b);
assertFile(false, dir + pomName("moduleC", "1.0-SNAPSHOT"), b);
}
/** Test copying from maven job where artifacts manually archived instead of automatic */
@Test
public void testMavenJobWithArchivePostBuildStep() throws Exception {
MavenModuleSet mp = setupMavenJob();
// Turn off automatic archiving and use a post-build step instead.
// Artifacts will be stored with the parent build instead of the child module builds.
mp.setIsArchivingDisabled(true);
mp.getPublishersList().add(new ArtifactArchiver("moduleB/*.xml", "", false, false));
rule.assertBuildStatusSuccess(mp.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleProject p = createProject(mp.getName(), null, "", "", true, false, false, true);
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
// Archived artifact should be copied:
assertFile(true, "moduleB/pom.xml", b);
// None of the maven artifacts should be archived or copied:
String dir = "org.jvnet.hudson.main.test.multimod/";
assertFile(false, dir + "moduleA/1.0-SNAPSHOT/moduleA-1.0-SNAPSHOT.jar", b);
assertFile(false, dir + pomName("moduleA", "1.0-SNAPSHOT"), b);
assertFile(false, dir + "moduleB/1.0-SNAPSHOT/moduleB-1.0-SNAPSHOT.jar", b);
assertFile(false, dir + pomName("moduleB", "1.0-SNAPSHOT"), b);
assertFile(false, dir + "moduleC/1.0-SNAPSHOT/moduleC-1.0-SNAPSHOT.jar", b);
assertFile(false, dir + pomName("moduleC", "1.0-SNAPSHOT"), b);
}
/** Test copy from workspace instead of artifacts area */
@Test
public void testCopyFromWorkspace() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(), p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), null, new WorkspaceSelector(),
"**/*.txt", "", true, false, true));
// Run a build that places a file in the workspace, but does not archive anything
other.getBuildersList().add(new ArtifactBuilder());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "subfoo.txt", b);
assertFile(false, "c.log", b);
}
@Issue("JENKINS-14900")
@Test
public void testCopyFromWorkspaceWithDefaultExcludes() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(), p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), "", new WorkspaceSelector(),
"", "", false, false));
// Run a build that places a file in the workspace, but does not archive anything
other.getBuildersList().add(new ArtifactBuilder());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, ".hg/defaultexclude.txt", b);
}
@Issue("JENKINS-18662")
@Test
public void testExcludes() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(), p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), "", new WorkspaceSelector(),
"**", "**/b/,foo*", "", false, false, true));
// Run a build that places a file in the workspace, but does not archive anything
other.getBuildersList().add(new ArtifactBuilder());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "subdir/subfoo.txt", b);
assertFile(false, "deepfoo/a/b/c.log", b);
assertFile(false, "foo.txt", b);
}
@Issue("JENKINS-14900")
@Test
public void testCopyFromWorkspaceWithDefaultExcludesWithFlatten() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(), p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), "", new WorkspaceSelector(),
"", "", true, false));
// Run a build that places a file in the workspace, but does not archive anything
other.getBuildersList().add(new ArtifactBuilder());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "defaultexclude.txt", b);
}
@Issue("JENKINS-18662")
@Test
public void testExcludesWithFlatten() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(), p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), "", new WorkspaceSelector(),
"**", "**/*.log", "", true, false, true));
// Run a build that places a file in the workspace, but does not archive anything
other.getBuildersList().add(new ArtifactBuilder());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "subfoo.txt", b);
assertFile(false, "c.log", b);
}
/** projectName in CopyArtifact build steps should be updated if a job is renamed */
@Test
public void testJobRename() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(),
p = createProject(other.getName(), null, "", "", true, false, false, true);
assertEquals("before", other.getName(),
((CopyArtifact)p.getBuilders().get(0)).getProjectName());
String newName = other.getName() + "-new";
other.renameTo(newName);
assertEquals("after", newName,
((CopyArtifact)p.getBuilders().get(0)).getProjectName());
// Test reference to a matrix configuration
MatrixProject otherm = createMatrixProject(),
mp = createMatrixProject();
mp.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(otherm.getName(), "FOO=$FOO",
new SavedBuildSelector(), "", "", false, false, true));
assertEquals("before", otherm.getName(),
((CopyArtifact)mp.getBuilders().get(0)).getProjectName());
otherm.renameTo(newName = otherm.getName() + "-new");
assertEquals("after", newName,
((CopyArtifact)mp.getBuilders().get(0)).getProjectName());
}
@Test
public void testSavedBuildSelector() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = rule.createFreeStyleProject();
ParameterDefinition paramDef = new StringParameterDefinition("FOO", "foo");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
other.addProperty(paramsDef);
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new SavedBuildSelector(), "*.txt", "", false, false, true));
FreeStyleBuild b = other.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("FOO", "buildone"))).get();
rule.assertBuildStatusSuccess(b);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()));
b.keepLog(true);
b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "buildone.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
}
@Test
public void testSpecificBuildSelector() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = rule.createFreeStyleProject();
ParameterDefinition paramDef = new StringParameterDefinition("FOO", "foo");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
other.addProperty(paramsDef);
SpecificBuildSelector sbs = new SpecificBuildSelector("1");
assertEquals("1", sbs.getBuildNumber());
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), null, sbs, "*.txt", "", false, false, true));
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("FOO", "buildone"))).get());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()));
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "buildone.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
}
@Test
public void testSpecificBuildSelectorParameter() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = rule.createFreeStyleProject();
ParameterDefinition paramDef = new StringParameterDefinition("FOO", "foo");
ParameterDefinition paramDef2 = new StringParameterDefinition("BAR", "1");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
other.addProperty(paramsDef);
ParametersDefinitionProperty paramsDef2 = new ParametersDefinitionProperty(paramDef2);
p.addProperty(paramsDef2);
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new SpecificBuildSelector("$BAR"), "*.txt", "", false, false, true));
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("FOO", "buildone"))).get());
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()));
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("BAR", "1"))).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "buildone.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
}
@Test
public void testParameterizedBuildSelector() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = rule.createFreeStyleProject();
ParameterDefinition pParamDef = new StringParameterDefinition("PBS", "foo");
ParametersDefinitionProperty pParamsDef = new ParametersDefinitionProperty(pParamDef);
p.addProperty(pParamsDef);
ParameterDefinition paramDef = new StringParameterDefinition("FOO", "foo");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
other.addProperty(paramsDef);
ParameterizedBuildSelector pbs = new ParameterizedBuildSelector("PBS");
assertEquals("PBS", pbs.getParameterName());
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(), null, pbs, "*.txt", "", false, false, true));
FreeStyleBuild b = other.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("FOO", "buildone"))).get();
rule.assertBuildStatusSuccess(b);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()));
b.keepLog(true);
b = p.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("PBS", "<SavedBuildSelector/>"))).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "buildone.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
}
@Test
public void testPermalinkBuildSelector() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = rule.createFreeStyleProject();
ParameterDefinition paramDef = new StringParameterDefinition("FOO", "foo");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
other.addProperty(paramsDef);
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new PermalinkBuildSelector("lastStableBuild"), "*.txt", "", false, false, true));
FreeStyleBuild b = other.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("FOO", "buildone"))).get();
rule.assertBuildStatusSuccess(b);
other.getBuildersList().add(new UnstableBuilder());
rule.assertBuildStatus(Result.UNSTABLE, other.scheduleBuild2(0, new Cause.UserIdCause()).get());
b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "buildone.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
// Invalid permalink
p.getBuildersList().replace(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new PermalinkBuildSelector("fooBuild"), "*.txt", "", false, false, true));
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testTriggeredBuildSelector() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new TriggeredBuildSelector(false), "*.txt", "", false, false, true));
other.getPublishersList().add(new BuildTrigger(p.getFullName(), false));
rule.jenkins.rebuildDependencyGraph();
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()));
// p#1 was triggered, now building.
FreeStyleBuild b = p.getBuildByNumber(1);
for (int i = 0; b == null && i < 1000; i++) { Thread.sleep(10); b = p.getBuildByNumber(1); }
assertNotNull(b);
while (b.isBuilding()) {
Thread.sleep(10);
}
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
// Verify error if build not triggered by upstream job:
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
// test fallback
//run a failing build to make sure the fallback selects the last successful build
other.getPublishersList().clear();
other.getBuildersList().add(new FailureBuilder());
rule.assertBuildStatus(Result.FAILURE, other.scheduleBuild2(0, new Cause.UserIdCause()).get());
p.getBuildersList().remove(CopyArtifact.class);
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new TriggeredBuildSelector(true), "*.txt", "", false, false, true));
rule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testTriggeredBuildSelectorWithParentOfParent() throws Exception {
FreeStyleProject grandparent = createArtifactProject(),
parent = rule.createFreeStyleProject(),
p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(grandparent.getName(), null,
new TriggeredBuildSelector(false), "*.txt", "", false, false, true));
parent.getPublishersList().add(new BuildTrigger(p.getFullName(), false));
grandparent.getPublishersList().add(new BuildTrigger(parent.getFullName(), false));
rule.jenkins.rebuildDependencyGraph();
rule.assertBuildStatusSuccess(grandparent.scheduleBuild2(0, new Cause.UserIdCause()));
// parent#1 was triggered
FreeStyleBuild b = parent.getBuildByNumber(1);
for (int i = 0; b == null && i < 2000; i++) { Thread.sleep(10); b = p.getBuildByNumber(1); }
assertNotNull(b);
while (b.isBuilding()) {
Thread.sleep(10);
}
rule.assertBuildStatusSuccess(b);
// p#1 was triggered, now building.
b = p.getBuildByNumber(1);
for (int i = 0; b == null && i < 2000; i++) { Thread.sleep(10); b = p.getBuildByNumber(1); }
assertNotNull(b);
while (b.isBuilding()) {
Thread.sleep(10);
}
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(false, "subdir/subfoo.txt", b);
// Verify error if build not triggered by upstream job:
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
// test fallback
//run a failing build to make sure the fallback selects the last successful build
grandparent.getPublishersList().clear();
grandparent.getBuildersList().add(new FailureBuilder());
rule.assertBuildStatus(Result.FAILURE, grandparent.scheduleBuild2(0, new Cause.UserIdCause()).get());
p.getBuildersList().remove(CopyArtifact.class);
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(grandparent.getName(), null,
new TriggeredBuildSelector(true), "*.txt", "", false, false, true));
rule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
/**
* When copying from a particular matrix configuration, the upstream project
* is the matrix parent.
*/
@Test
public void testTriggeredBuildSelectorFromMatrix() throws Exception {
MatrixProject other = createMatrixArtifactProject();
FreeStyleProject p = rule.createFreeStyleProject();
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName() + "/FOO=two",
null, new TriggeredBuildSelector(false), "*.txt", "", false, false, true));
other.getPublishersList().add(new BuildTrigger(p.getFullName(), false));
rule.jenkins.rebuildDependencyGraph();
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
// p#1 was triggered, now building.
FreeStyleBuild b = p.getBuildByNumber(1);
for (int i = 0; b == null && i < 1000; i++) { Thread.sleep(10); b = p.getBuildByNumber(1); }
assertNotNull(b);
while (b.isBuilding()) {
Thread.sleep(10);
}
rule.assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
assertFile(true, "two.txt", b);
}
/**
* When copying to a matrix job, need to check the upstream cause of the
* matrix parent.
*/
@Test
public void testTriggeredBuildSelectorToMatrix() throws Exception {
FreeStyleProject other = createArtifactProject();
MatrixProject p = createMatrixProject();
p.setAxes(new AxisList(new Axis("FOO", "one", "two")));
p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(other.getName(),
null, new TriggeredBuildSelector(false), "*.txt", "", false, false, true));
other.getPublishersList().add(new BuildTrigger(p.getFullName(), false));
rule.jenkins.rebuildDependencyGraph();
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
// p#1 was triggered, now building.
MatrixBuild b = p.getBuildByNumber(1);
for (int i = 0; b == null && i < 1000; i++) { Thread.sleep(10); b = p.getBuildByNumber(1); }
assertNotNull(b);
while (b.isBuilding()) {
Thread.sleep(10);
}
rule.assertBuildStatusSuccess(b);
MatrixRun r = b.getRuns().get(0);
assertFile(true, "foo.txt", r);
}
@Test
public void testFlatten() throws Exception {
FreeStyleProject other = createArtifactProject(),
p = createProject(other.getName(), null, "", "newdir", false, true, false, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserIdCause()).get();
rule.assertBuildStatusSuccess(b);
assertFile(true, "newdir/foo.txt", b);
assertFile(true, "newdir/subfoo.txt", b);
assertFile(true, "newdir/c.log", b);
}
@Test
public void testOptional_MissingProject() throws Exception {
// Missing project still fails even when copy is optional
FreeStyleProject p = createProject("invalid", null, "", "", false, false, true, true);
rule.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testOptional_MissingBuild() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(),
p = createProject(other.getName(), null, "", "", false, false, true, true);
rule.assertBuildStatusSuccess(p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
@Test
public void testOptional_MissingArtifact() throws Exception {
FreeStyleProject other = rule.createFreeStyleProject(),
p = createProject(other.getName(), null, "*.txt", "", false, false, true, true);
rule.assertBuildStatusSuccess(other.scheduleBuild2(0, new Cause.UserIdCause()).get());
rule.assertBuildStatusSuccess(p.scheduleBuild2(0, new Cause.UserIdCause()).get());
}
/**
* Test that a user is prevented from bypassing permissions on other jobs
*/
@Test
public void testPermission() throws Exception {
// any users can be authenticated with the password same to the user id.
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
MockAuthorizationStrategy auth = new MockAuthorizationStrategy()
.grant(Jenkins.READ, Item.BUILD, Computer.BUILD).everywhere().toEveryone();
rule.jenkins.setAuthorizationStrategy(auth);
// only joe can access project "src"
FreeStyleProject src = rule.createFreeStyleProject();
src.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
src.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
auth.grant(Item.READ).onItems(src).to("joe");
rule.assertBuildStatusSuccess(src.scheduleBuild2(0));
// test access from anonymous
{
FreeStyleProject dest = rule.createFreeStyleProject();
dest.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(
src.getName(),
"",
new StatusBuildSelector(true),
"",
"",
false,
false,
true
));
auth.grant(Item.READ, Item.CONFIGURE).onItems(dest).toEveryone();
WebClient wc = rule.createWebClient();
try {
wc.getPage(src);
fail("Job should not be accessible to anonymous");
} catch(FailingHttpStatusCodeException e) {
assertEquals("Job should not be accessible to anonymous", 404, e.getStatusCode());
}
rule.submit(wc.getPage(dest, "configure").getFormByName("config"));
dest = rule.jenkins.getItemByFullName(dest.getFullName(), FreeStyleProject.class);
CopyArtifact ca = dest.getBuildersList().getAll(CopyArtifact.class).get(0);
// Preserves the configuration as-is in Production mode.
assertEquals(src.getName(), ca.getProjectName());
// Instead, the build will fail.
rule.assertBuildStatus(Result.FAILURE, dest.scheduleBuild2(0));