forked from jenkinsci/workflow-basic-steps-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeoutStepTest.java
410 lines (387 loc) · 17.8 KB
/
TimeoutStepTest.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
/*
* The MIT License
*
* Copyright 2014-2016 CloudBees, Inc.
*
* 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;
import hudson.Functions;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import jenkins.model.CauseOfInterruption;
import jenkins.model.InterruptedBuildAction;
import jenkins.plugins.git.GitSampleRepoRule;
import org.jenkinsci.plugins.workflow.actions.ErrorAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.support.visualization.table.FlowGraphTable;
import org.jenkinsci.plugins.workflow.support.visualization.table.FlowGraphTable.Row;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.*;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.recipes.LocalData;
import org.kohsuke.stapler.DataBoundConstructor;
public class TimeoutStepTest extends Assert {
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
@Rule public GitSampleRepoRule git = new GitSampleRepoRule();
@Test public void configRoundTrip() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
TimeoutStep s1 = new TimeoutStep(3);
s1.setUnit(TimeUnit.HOURS);
TimeoutStep s2 = new StepConfigTester(story.j).configRoundTrip(s1);
story.j.assertEqualDataBoundBeans(s1, s2);
}
});
}
/**
* The simplest possible timeout step ever.
*/
@Test
public void basic() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node { timeout(time:5, unit:'SECONDS') { sleep 10; echo 'NotHere' } }", true));
WorkflowRun b = story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
story.j.assertLogNotContains("NotHere", b);
}
});
}
@Issue("JENKINS-34637")
@Test
public void basicWithBlock() {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node { timeout(time:5, unit:'SECONDS') { withEnv([]) { sleep 7; echo 'NotHere' } } }", true));
WorkflowRun b = story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
story.j.assertLogNotContains("NotHere", b);
}
});
}
@Test
public void killingParallel() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " timeout(time:5, unit:'SECONDS') {\n"
+ " parallel(\n"
+ " a: { echo 'ShouldBeHere1'; sleep 10; echo 'NotHere' },\n"
+ " b: { echo 'ShouldBeHere2'; sleep 10; echo 'NotHere' },\n"
+ " );\n"
+ " echo 'NotHere'\n"
+ " }\n"
+ " echo 'NotHere'\n"
+ "}\n", true));
WorkflowRun b = story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
// make sure things that are supposed to run do, and things that are NOT supposed to run do not.
story.j.assertLogNotContains("NotHere", b);
story.j.assertLogContains("ShouldBeHere1", b);
story.j.assertLogContains("ShouldBeHere2", b);
// we expect every sleep step to have failed
FlowGraphTable t = new FlowGraphTable(b.getExecution());
t.build();
for (Row r : t.getRows()) {
if (r.getNode() instanceof StepAtomNode) {
StepAtomNode a = (StepAtomNode) r.getNode();
if (a.getDescriptor().getClass() == SleepStep.DescriptorImpl.class) {
assertTrue(a.getAction(ErrorAction.class) != null);
}
}
}
}
});
}
@Issue("JENKINS-26521")
@Test
public void activity() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " timeout(time:5, unit:'SECONDS', activity: true) {\n"
+ " echo 'NotHere';\n"
+ " sleep 3;\n"
+ " echo 'NotHereYet';\n"
+ " sleep 3;\n"
+ " echo 'JustHere!';\n"
+ " sleep 10;\n"
+ " echo 'ShouldNot!';\n"
+ " }\n"
+ "}\n", true));
WorkflowRun b = story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
story.j.assertLogContains("JustHere!", b);
story.j.assertLogNotContains("ShouldNot!", b);
}
});
}
@Issue("JENKINS-26521")
@Test
public void activityInParallel() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " parallel(\n"
+ " a: {\n"
+ " timeout(time:5, unit:'SECONDS', activity: true) {\n"
+ " echo 'NotHere';\n"
+ " sleep 3;\n"
+ " echo 'NotHereYet';\n"
+ " sleep 3;\n"
+ " echo 'JustHere!';\n"
+ " sleep 10;\n"
+ " echo 'ShouldNot!';\n"
+ " }\n"
+ " },\n"
+ " b: {\n"
+ " for (int i = 0; i < 5; i++) {\n"
+ " echo 'Other Thread'\n"
+ " sleep 3\n"
+ " }\n"
+ " })\n"
+ "}\n", true));
WorkflowRun b = story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
story.j.assertLogContains("JustHere!", b);
story.j.assertLogNotContains("ShouldNot!", b);
}
});
}
@Issue("JENKINS-26521")
@Test
public void activityRestart() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "restarted");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " timeout(time:15, unit:'SECONDS', activity: true) {\n"
+ " echo 'NotHere';\n"
+ " semaphore 'restarted'\n"
+ " echo 'NotHereYet';\n"
+ " sleep 10;\n"
+ " echo 'NotHereYet';\n"
+ " sleep 10;\n"
+ " echo 'JustHere!';\n"
+ " sleep 30;\n"
+ " echo 'ShouldNot!';\n"
+ " }\n"
+ "}\n", true));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("restarted/1", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("restarted", WorkflowJob.class);
WorkflowRun b = p.getBuildByNumber(1);
assertTrue("took more than 15s to restart?", b.isBuilding());
SemaphoreStep.success("restarted/1", null);
story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b));
story.j.assertLogContains("JustHere!", b);
story.j.assertLogNotContains("ShouldNot!", b);
}
});
}
@Test
public void activityRemote() {
assumeFalse(Functions.isWindows()); // TODO create analogue using bat
story.then(r -> {
r.createSlave();
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node('!master') {\n" +
" timeout(time:5, unit:'SECONDS', activity: true) {\n" +
" sh 'set +x; echo NotHere; sleep 3; echo NotHereYet; sleep 3; echo JustHere; sleep 10; echo ShouldNot'\n" +
" }\n" +
"}\n", true));
WorkflowRun b = r.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0));
story.j.assertLogContains("JustHere", b);
story.j.assertLogNotContains("ShouldNot", b);
});
}
@Issue("JENKINS-54078")
@Test public void activityGit() {
story.then(r -> {
r.createSlave();
git.init();
git.write("file", "content");
git.git("commit", "--all", "--message=init");
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("" +
"node('!master') {\n" +
" timeout(time: 5, unit: 'MINUTES', activity: true) {\n" +
" git($/" + git + "/$)\n" +
" }\n" +
"}\n", true));
r.buildAndAssertSuccess(p);
});
}
@Issue("JENKINS-26163")
@Test
public void restarted() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "restarted");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " timeout(time: 15, unit: 'SECONDS') {\n"
+ " semaphore 'restarted'\n"
+ " sleep 999\n"
+ " }\n"
+ "}\n", true));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("restarted/1", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("restarted", WorkflowJob.class);
WorkflowRun b = p.getBuildByNumber(1);
assertTrue("took more than 15s to restart?", b.isBuilding());
SemaphoreStep.success("restarted/1", null);
story.j.assertBuildStatus(Result.ABORTED, story.j.waitForCompletion(b));
}
});
}
@Test
public void timeIsConsumed() throws Exception {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "timeIsConsumed");
p.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " timeout(time: 20, unit: 'SECONDS') {\n"
+ " sleep 10\n"
+ " semaphore 'timeIsConsumed'\n"
+ " sleep 10\n"
+ " }\n"
+ "}\n", true));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("timeIsConsumed/1", b);
}
});
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("timeIsConsumed", WorkflowJob.class);
WorkflowRun b = p.getBuildByNumber(1);
SemaphoreStep.success("timeIsConsumed/1", null);
WorkflowRun run = story.j.waitForCompletion(b);
InterruptedBuildAction action = b.getAction(InterruptedBuildAction.class);
assertNotNull(action); // TODO flaked on windows-8-2.32.3
List<CauseOfInterruption> causes = action.getCauses();
assertEquals(1, causes.size());
assertEquals(TimeoutStepExecution.ExceededTimeout.class, causes.get(0).getClass());
story.j.assertBuildStatus(Result.ABORTED, run);
}
});
}
@Issue("JENKINS-39072")
@Test public void unresponsiveBody() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("timeout(time: 2, unit: 'SECONDS') {unkillable()}", true));
story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
}
});
}
public static class UnkillableStep extends Step {
@DataBoundConstructor public UnkillableStep() {}
@Override public StepExecution start(StepContext context) throws Exception {
return new Execution(context);
}
private static class Execution extends StepExecution {
private Execution(StepContext context) {
super(context);
}
@Override public boolean start() throws Exception {
return false;
}
@Override public void stop(Throwable cause) throws Exception {
getContext().get(TaskListener.class).getLogger().println("ignoring " + cause);
}
}
@TestExtension("unresponsiveBody") public static class DescriptorImpl extends StepDescriptor {
@Override public String getFunctionName() {
return "unkillable";
}
@Override public Set<? extends Class<?>> getRequiredContext() {
return Collections.singleton(TaskListener.class);
}
}
}
@Ignore("TODO cannot find any way to solve this case")
@Issue("JENKINS-39072")
@Test public void veryUnresponsiveBody() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("timeout(time: 2, unit: 'SECONDS') {while (true) {try {sleep 10} catch (e) {echo(/ignoring ${e}/)}}}", true));
story.j.assertBuildStatus(Result.ABORTED, p.scheduleBuild2(0).get());
}
});
}
// TODO: timeout inside parallel
@Issue("JENKINS-39134")
@LocalData
@Test public void serialForm() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("timeout", WorkflowJob.class);
WorkflowRun b = p.getBuildByNumber(1);
RunListener.fireStarted(b, TaskListener.NULL);
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
}
});
}
}