/
SCMStepTest.java
166 lines (156 loc) · 7.96 KB
/
SCMStepTest.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
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* 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 org.jenkinsci.plugins.workflow.steps.scm;
import hudson.model.Label;
import hudson.scm.ChangeLogSet;
import hudson.scm.PollingResult;
import hudson.triggers.SCMTrigger;
import hudson.util.StreamTaskListener;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.scm.impl.subversion.SubversionSampleRepoRule;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;
public class SCMStepTest {
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public RestartableJenkinsRule r = new RestartableJenkinsRule();
@Rule public GitSampleRepoRule sampleGitRepo = new GitSampleRepoRule();
@Rule public SubversionSampleRepoRule sampleSvnRepo = new SubversionSampleRepoRule();
@Issue("JENKINS-26100")
@Test
public void scmVars() throws Exception {
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
sampleSvnRepo.init();
sampleSvnRepo.write("Jenkinsfile", "node('remote') {\n" +
" def svnRev = checkout(scm).SVN_REVISION\n" +
" echo \"SVN_REVISION is ${svnRev}\"\n" +
"}\n");
sampleSvnRepo.svnkit("add", sampleSvnRepo.wc() + "/Jenkinsfile");
sampleSvnRepo.svnkit("commit", "--message=+Jenkinsfile", sampleSvnRepo.wc());
long revision = sampleSvnRepo.revision();
WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsScmFlowDefinition(new SubversionStep(sampleSvnRepo.trunkUrl()).createSCM(), "Jenkinsfile"));
r.j.createOnlineSlave(Label.get("remote"));
WorkflowRun b = r.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.j.assertLogContains("SVN_REVISION is " + revision, b);
}
});
}
@Issue("JENKINS-26761")
@Test public void checkoutsRestored() throws Exception {
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
sampleGitRepo.init();
WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p");
p.addTrigger(new SCMTrigger(""));
r.j.createOnlineSlave(Label.get("remote"));
p.setDefinition(new CpsFlowDefinition(
"node('remote') {\n" +
" ws {\n" +
" git($/" + sampleGitRepo + "/$)\n" +
" }\n" +
"}", true));
p.save();
WorkflowRun b = r.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
r.j.assertLogContains("Cloning the remote Git repository", b);
FileUtils.copyFile(new File(b.getRootDir(), "build.xml"), System.out);
}
});
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = r.j.jenkins.getItemByFullName("p", WorkflowJob.class);
r.j.createOnlineSlave(Label.get("remote"));
sampleGitRepo.write("nextfile", "");
sampleGitRepo.git("add", "nextfile");
sampleGitRepo.git("commit", "--message=next");
sampleGitRepo.notifyCommit(r.j);
WorkflowRun b = p.getLastBuild();
assertEquals(2, b.number);
r.j.assertLogContains("Cloning the remote Git repository", b); // new slave, new workspace
List<ChangeLogSet<? extends ChangeLogSet.Entry>> changeSets = b.getChangeSets();
assertEquals(1, changeSets.size());
ChangeLogSet<? extends ChangeLogSet.Entry> changeSet = changeSets.get(0);
assertEquals(b, changeSet.getRun());
assertEquals("git", changeSet.getKind());
Iterator<? extends ChangeLogSet.Entry> iterator = changeSet.iterator();
assertTrue(iterator.hasNext());
ChangeLogSet.Entry entry = iterator.next();
assertEquals("[nextfile]", entry.getAffectedPaths().toString());
assertFalse(iterator.hasNext());
}
});
}
@Issue("JENKINS-32214")
@Test public void pollDuringBuild() throws Exception {
r.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
sampleSvnRepo.init();
WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"semaphore 'before'\n" +
"node {svn '" + sampleSvnRepo.trunkUrl() + "'}\n" +
"semaphore 'after'", true));
assertPolling(p, PollingResult.Change.INCOMPARABLE);
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.success("before/1", null);
SemaphoreStep.waitForStart("after/1", b1);
assertPolling(p, PollingResult.Change.NONE);
SemaphoreStep.success("after/1", null);
r.j.assertBuildStatusSuccess(r.j.waitForCompletion(b1));
sampleSvnRepo.write("file2", "");
sampleSvnRepo.svnkit("add", sampleSvnRepo.wc() + "/file2");
sampleSvnRepo.svnkit("commit", "--message=+file2", sampleSvnRepo.wc());
WorkflowRun b2 = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.success("before/2", null);
SemaphoreStep.waitForStart("after/2", b2);
assertPolling(p, PollingResult.Change.NONE);
WorkflowRun b3 = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("before/3", b3);
assertPolling(p, PollingResult.Change.NONE);
sampleSvnRepo.write("file3", "");
sampleSvnRepo.svnkit("add", sampleSvnRepo.wc() + "/file3");
sampleSvnRepo.svnkit("commit", "--message=+file3", sampleSvnRepo.wc());
assertPolling(p, PollingResult.Change.SIGNIFICANT);
}
});
}
private static void assertPolling(WorkflowJob p, PollingResult.Change expectedChange) {
assertEquals(expectedChange, p.poll(StreamTaskListener.fromStdout()).change);
}
}