This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
/
ParallelStepTest.java
480 lines (420 loc) · 18.4 KB
/
ParallelStepTest.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
package org.jenkinsci.plugins.workflow.steps.parallel;
import hudson.AbortException;
import hudson.model.Result;
import hudson.FilePath;
import java.util.ArrayList;
import java.util.Arrays;
import static java.util.Arrays.*;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.workflow.SingleJobTestBase;
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.steps.ParallelStep;
import org.jenkinsci.plugins.workflow.cps.steps.ParallelStepException;
import org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.EchoStep;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.durable_task.BatchScriptStep;
import org.jenkinsci.plugins.workflow.steps.durable_task.ShellStep;
import org.jenkinsci.plugins.workflow.support.steps.input.InputAction;
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.Assert;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
/**
* Tests for {@link ParallelStep}.
*
* @author Kohsuke Kawaguchi
*/
public class ParallelStepTest extends SingleJobTestBase {
private FlowGraphTable t;
/**
* The first baby step.
*/
@Test
public void minimumViableParallelRun() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"node {",
" x = parallel( a: { echo('echo a'); return 1; }, b: { echo('echo b'); return 2; } )",
" assert x.a==1",
" assert x.b==2",
"}"
)));
startBuilding().get(); // 15, SECONDS);
assertBuildCompletedSuccessfully();
buildTable();
shouldHaveParallelStepsInTheOrder("a", "b");
}
});
}
/**
* Failure in a branch will cause the join to fail.
*/
@Test
public void failure_in_subflow_will_cause_join_to_fail() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"import "+AbortException.class.getName(),
"import "+ParallelStepException.class.getName(),
"node {",
" try {",
" parallel(",
" b: { error 'died' },",
// make sure this branch takes longer than a
" a: { sleep 3; writeFile text: '', file: 'a.done' }",
" )",
" assert false;",
" } catch (ParallelStepException e) {",
" assert e.name=='b'",
" assert e.cause instanceof AbortException",
" }",
"}"
)));
startBuilding().get();
assertBuildCompletedSuccessfully();
assert jenkins().getWorkspaceFor(p).child("a.done").exists();
buildTable();
shouldHaveParallelStepsInTheOrder("b","a");
}
});
}
/**
* Failure in a branch will cause the join to fail.
*/
@Test @Issue("JENKINS-26034")
public void failure_in_subflow_will_fail_fast() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"import "+AbortException.class.getName(),
"import "+ParallelStepException.class.getName(),
"node {",
" try {",
" parallel(",
" b: { error 'died' },",
// make sure this branch takes longer than a
" a: { sleep 25; writeFile text: '', file: 'a.done' },",
" failFast: true",
" )",
" assert false",
" } catch (ParallelStepException e) {",
" echo e.toString()",
" assert e.name=='b'",
" assert e.cause instanceof AbortException",
" }",
"}"
)));
startBuilding().get();
assertBuildCompletedSuccessfully();
Assert.assertFalse("a should have aborted", jenkins().getWorkspaceFor(p).child("a.done").exists());
}
});
}
/**
* FailFast should not kill branches if there is no failure.
*/
@Test @Issue("JENKINS-26034")
public void failFast_has_no_effect_on_success() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"import "+AbortException.class.getName(),
"import "+ParallelStepException.class.getName(),
"node {",
" parallel(",
" a: { echo 'hello from a';sleep 1;echo 'goodbye from a' },",
" b: { echo 'hello from b';sleep 1;echo 'goodbye from b' },",
" c: { echo 'hello from c';sleep 1;echo 'goodbye from c' },",
// make sure this branch is quicker than the others.
" d: { echo 'hello from d' },",
" failFast: true",
" )",
"}"
)));
startBuilding().get();
assertBuildCompletedSuccessfully();
}
});
}
@Test
public void localMethodCallWithinBranch() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
FilePath aa = jenkins().getRootPath().child("a");
FilePath bb = jenkins().getRootPath().child("b");
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"def touch(f) { writeFile text: '', file: f }",
"node {",
" parallel(aa: {touch($/" + aa + "/$)}, bb: {touch($/" + bb + "/$)})",
"}"
)));
startBuilding().get();
assertBuildCompletedSuccessfully();
assertTrue(aa.exists());
assertTrue(bb.exists());
}
});
}
@Test
public void localMethodCallWithinBranch2() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"def notify(msg) {",
" echo msg",
"}",
"node {",
" ws {",
" echo 'start'",
" parallel(one: {",
" notify('one')",
" }, two: {",
" notify('two')",
" })",
" echo 'end'",
" }",
"}"
)));
startBuilding().get();
assertBuildCompletedSuccessfully();
story.j.assertLogContains("one", b);
story.j.assertLogContains("two", b);
}
});
}
@Test
public void localMethodCallWithinLotsOfBranches() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(
IOUtils.toString(getClass().getResource("localMethodCallWithinLotsOfBranches.groovy"))));
startBuilding().get();
assertBuildCompletedSuccessfully();
// count number of shell steps
FlowGraphTable t = buildTable();
int shell=0;
for (Row r : t.getRows()) {
if (r.getNode() instanceof StepAtomNode) {
StepDescriptor descriptor = ((StepAtomNode)r.getNode()).getDescriptor();
if (descriptor instanceof ShellStep.DescriptorImpl || descriptor instanceof BatchScriptStep.DescriptorImpl) {
shell++;
}
}
}
assertEquals(128*3,shell);
}
});
}
/**
* Restarts in the middle of a parallel workflow.
*/
@Test
public void suspend() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"node {",
" parallel(",
" a: { semaphore 'suspendA'; echo 'A done' },",
" b: { semaphore 'suspendB'; echo 'B done' },",
" c: { semaphore 'suspendC'; echo 'C done' },",
" )",
"}"
)));
startBuilding();
// let the workflow run until all parallel branches settle
SemaphoreStep.waitForStart("suspendA/1", b);
SemaphoreStep.waitForStart("suspendB/1", b);
SemaphoreStep.waitForStart("suspendC/1", b);
assert !e.isComplete();
assert e.getCurrentHeads().size()==3;
assert b.isBuilding();
buildTable();
shouldHaveParallelStepsInTheOrder("a","b","c");
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
rebuildContext(story.j);
// make sure we are still running two heads
assert e.getCurrentHeads().size()==3;
assert b.isBuilding();
// we let one branch go at a time
for (String branch : asList("A", "B")) {
SemaphoreStep.success("suspend" + branch + "/1", null);
waitForWorkflowToSuspend();
// until all execution joins into one, we retain all heads
assert e.getCurrentHeads().size() == 3;
assert b.isBuilding();
}
// when we let the last one go, it will now run till the completion
SemaphoreStep.success("suspendC/1", null);
while (!e.isComplete())
waitForWorkflowToSuspend();
// make sure all the three branches have executed to the end.
for (String branch : asList("A", "B", "C")) {
story.j.assertLogContains(branch + " done", b);
}
// check the shape of the graph
buildTable();
shouldHaveSteps(SemaphoreStep.DescriptorImpl.class, 3);
shouldHaveSteps(EchoStep.DescriptorImpl.class, 3);
shouldHaveParallelStepsInTheOrder("a","b","c");
}
});
}
private void shouldHaveSteps(Class<? extends StepDescriptor> d, int n) {
int count=0;
for (Row row : t.getRows()) {
if (row.getNode() instanceof StepAtomNode) {
StepAtomNode a = (StepAtomNode)row.getNode();
if (a.getDescriptor().getClass()==d)
count++;
}
}
assertEquals(d.getName(), n, count);
}
/**
* Builds {@link FlowGraphTable}. Convenient for inspecting a shape of the flow nodes.
*/
private FlowGraphTable buildTable() {
t = new FlowGraphTable(e);
t.build();
return t;
}
private void shouldHaveParallelStepsInTheOrder(String... expected) {
List<String> actual = new ArrayList<String>();
for (Row row : t.getRows()) {
ThreadNameAction a = row.getNode().getAction(ThreadNameAction.class);
if (a!=null)
actual.add(a.getThreadName());
}
assertEquals(Arrays.asList(expected),actual);
}
/**
* Parallel branches become invisible once completed until the whole parallel step is completed.
*/
@Test @Issue("JENKINS-26074")
public void invisibleParallelBranch() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
" parallel(\n" +
" 'abc' : {\n" +
" noSuchFunctionExists(); \n"+
" }\n" +
" ,\n" +
" 'waitForever' : {\n" +
" input message: 'This is just to prove a point' \n" +
" }\n" +
" ,\n" +
" 'someSimpleError' : {\n" +
" noSuchFunctionExists(); \n"+
" }\n" +
" )\n"
)));
startBuilding();
// wait for workflow to progress far enough to the point that it has finished failing two branches
// and pause on one
for (int i=0; i<10; i++)
waitForWorkflowToSuspend();
InputAction a = b.getAction(InputAction.class);
assertNotNull("Expected to pause on input",a);
assertEquals("Expecting 3 heads for 3 branches", 3,e.getCurrentHeads().size());
a.getExecutions().get(0).proceed(null);
waitForWorkflowToComplete();
story.j.assertBuildStatus(Result.FAILURE, b);
// make sure the table builds OK
buildTable();
}
});
}
@Issue("JENKINS-29413")
@Test public void emptyMap() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition("parallel [:]", true));
story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
});
}
@Test
@Issue("JENKINS-26122")
public void parallelBranchLabels() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(join(
"node {\n" +
" parallel( \n" +
" a: { \n" +
" echo('echo a');\n" +
" echo('echo a');\n" +
" }, \n" +
" b: { \n" +
" echo('echo b'); \n" +
" echo('echo b'); \n" +
" }\n" +
" )\n" +
"}\n"
)));
startBuilding().get();
assertBuildCompletedSuccessfully();
// Check that the individual labeled lines are as expected
//System.out.println(b.getLog());
List<String> logLines = b.getLog(50);
assertGoodLabeledLogs(logLines);
// Check that the logs are printed in the right sequence e.g. that a
// "[a] Running: Print Message" is followed by a "[a] echo a"
assertGoodSequence("a", logLines);
assertGoodSequence("b", logLines);
}
private void assertGoodLabeledLogs(List<String> logLines) {
for (int i = 0; i < logLines.size(); i++) {
String logLine = logLines.get(i);
if (logLine.startsWith("[a] ")) {
assertGoodLabeledLog("a", logLine);
} else if (logLine.startsWith("[b] ")) {
assertGoodLabeledLog("b", logLine);
}
}
}
private void assertGoodLabeledLog(String label, String logLine) {
List<String> possibleLogLines = Arrays.asList(
String.format("[%s] Running: Parallel branch: %s", label, label),
String.format("[%s] Running: Print Message", label),
String.format("[%s] echo %s", label, label)
);
boolean contains = possibleLogLines.contains(logLine);
assertTrue(contains);
}
private void assertGoodSequence(String label, List<String> logLines) {
String running = String.format("[%s] Running: Print Message", label);
String echo = String.format("[%s] echo %s", label, label);
for (int i = 0; i < logLines.size() - 1; i++) { // skip the last log line in this loop
if (logLines.get(i).equals(running)) {
assertEquals(echo, logLines.get(i + 1));
}
}
}
});
}
}