25
25
package org .jenkinsci .plugins .workflow .support .steps ;
26
26
27
27
import hudson .FilePath ;
28
+ import hudson .Functions ;
28
29
import hudson .init .InitMilestone ;
29
30
import hudson .init .Initializer ;
30
31
import hudson .model .Computer ;
57
58
import java .util .Collections ;
58
59
import java .util .HashSet ;
59
60
import java .util .List ;
60
- import java .util .concurrent .atomic .AtomicReference ;
61
61
import java .util .logging .ConsoleHandler ;
62
62
import java .util .logging .Handler ;
63
63
import java .util .logging .Level ;
64
64
import java .util .logging .Logger ;
65
+ import java .util .regex .Pattern ;
65
66
import jenkins .model .Jenkins ;
66
67
import org .apache .commons .io .FileUtils ;
67
68
import org .apache .tools .ant .util .JavaEnvUtils ;
80
81
import org .jenkinsci .plugins .workflow .test .steps .SemaphoreStep ;
81
82
import org .junit .AfterClass ;
82
83
import static org .junit .Assert .*;
84
+ import org .junit .Assume ;
83
85
import org .junit .ClassRule ;
84
86
import org .junit .Rule ;
85
87
import org .junit .Test ;
86
88
import org .junit .rules .TemporaryFolder ;
87
89
import org .junit .runners .model .Statement ;
88
90
import org .jvnet .hudson .test .BuildWatcher ;
89
91
import org .jvnet .hudson .test .Issue ;
92
+ import org .jvnet .hudson .test .JenkinsRule ;
90
93
import org .jvnet .hudson .test .MockAuthorizationStrategy ;
91
94
import org .jvnet .hudson .test .RestartableJenkinsRule ;
92
95
import org .jvnet .hudson .test .recipes .LocalData ;
@@ -114,11 +117,9 @@ public class ExecutorStepTest {
114
117
WorkflowJob p = story .j .jenkins .createProject (WorkflowJob .class , "demo" );
115
118
p .setDefinition (new CpsFlowDefinition (
116
119
"node('" + s .getNodeName () + "') {\n " +
117
- " sh('echo before=`basename $PWD`')\n " +
118
- " sh('echo ONSLAVE=$ONSLAVE')\n " +
120
+ " isUnix() ? sh('echo ONSLAVE=$ONSLAVE') : bat('echo ONSLAVE=%ONSLAVE%')\n " +
119
121
" semaphore 'wait'\n " +
120
- " sh('echo after=$PWD')\n " +
121
- "}" ));
122
+ "}" , true ));
122
123
123
124
WorkflowRun b = p .scheduleBuild2 (0 ).waitForStart ();
124
125
SemaphoreStep .waitForStart ("wait/1" , b );
@@ -131,7 +132,6 @@ public class ExecutorStepTest {
131
132
SemaphoreStep .success ("wait/1" , null );
132
133
story .j .assertBuildStatusSuccess (story .j .waitForCompletion (b ));
133
134
134
- story .j .assertLogContains ("before=demo" , b );
135
135
story .j .assertLogContains ("ONSLAVE=true" , b );
136
136
137
137
FlowGraphWalker walker = new FlowGraphWalker (b .getExecution ());
@@ -190,6 +190,7 @@ private void startJnlpProc() throws Exception {
190
190
}
191
191
192
192
@ Test public void buildShellScriptAcrossRestart () throws Exception {
193
+ Assume .assumeFalse ("TODO not sure how to write a corresponding batch script" , Functions .isWindows ());
193
194
story .addStep (new Statement () {
194
195
@ SuppressWarnings ("SleepWhileInLoop" )
195
196
@ Override public void evaluate () throws Throwable {
@@ -211,7 +212,7 @@ private void startJnlpProc() throws Exception {
211
212
"node('dumbo') {\n " +
212
213
" sh 'touch \" " + f2 + "\" ; while [ -f \" " + f1 + "\" ]; do sleep 1; done; echo finished waiting; rm \" " + f2 + "\" '\n " +
213
214
" echo 'OK, done'\n " +
214
- "}" ));
215
+ "}" , true ));
215
216
WorkflowRun b = p .scheduleBuild2 (0 ).waitForStart ();
216
217
while (!f2 .isFile ()) {
217
218
Thread .sleep (100 );
@@ -242,6 +243,7 @@ private void startJnlpProc() throws Exception {
242
243
}
243
244
244
245
@ Test public void buildShellScriptAcrossDisconnect () throws Exception {
246
+ Assume .assumeFalse ("TODO not sure how to write a corresponding batch script" , Functions .isWindows ());
245
247
story .addStep (new Statement () {
246
248
@ SuppressWarnings ("SleepWhileInLoop" )
247
249
@ Override public void evaluate () throws Throwable {
@@ -261,7 +263,7 @@ private void startJnlpProc() throws Exception {
261
263
"node('dumbo') {\n " +
262
264
" sh 'touch \" " + f2 + "\" ; while [ -f \" " + f1 + "\" ]; do sleep 1; done; echo finished waiting; rm \" " + f2 + "\" '\n " +
263
265
" echo 'OK, done'\n " +
264
- "}" ));
266
+ "}" , true ));
265
267
WorkflowRun b = p .scheduleBuild2 (0 ).waitForStart ();
266
268
while (!f2 .isFile ()) {
267
269
Thread .sleep (100 );
@@ -291,22 +293,18 @@ private void startJnlpProc() throws Exception {
291
293
}
292
294
293
295
@ Test public void buildShellScriptQuick () throws Exception {
294
- final AtomicReference <String > dir = new AtomicReference <String >();
295
296
story .addStep (new Statement () {
296
297
@ Override public void evaluate () throws Throwable {
297
298
DumbSlave s = story .j .createOnlineSlave ();
298
299
s .getNodeProperties ().add (new EnvironmentVariablesNodeProperty (new EnvironmentVariablesNodeProperty .Entry ("ONSLAVE" , "true" )));
299
300
300
301
WorkflowJob p = story .j .jenkins .createProject (WorkflowJob .class , "demo" );
301
- dir .set (s .getRemoteFS () + "/workspace/" + p .getFullName ());
302
302
p .setDefinition (new CpsFlowDefinition (
303
303
"node('" + s .getNodeName () + "') {\n " +
304
- " sh('pwd; echo ONSLAVE=$ONSLAVE')\n " +
305
- "}" ));
304
+ " isUnix() ? sh('echo ONSLAVE=$ONSLAVE') : bat('echo ONSLAVE=%ONSLAVE% ')\n " +
305
+ "}" , true ));
306
306
307
307
WorkflowRun b = story .j .assertBuildStatusSuccess (p .scheduleBuild2 (0 ));
308
-
309
- story .j .assertLogContains (dir .get (), b );
310
308
story .j .assertLogContains ("ONSLAVE=true" , b );
311
309
}
312
310
});
@@ -321,14 +319,14 @@ private void startJnlpProc() throws Exception {
321
319
WorkflowJob p = story .j .jenkins .createProject (WorkflowJob .class , "demo" );
322
320
p .setDefinition (new CpsFlowDefinition (
323
321
"node('slave') {\n " + // this locks the WS
324
- " sh(' echo default=`basename $PWD`' )\n " +
322
+ " echo(/ default=${pwd()}/ )\n " +
325
323
" ws {\n " + // and this locks a second one
326
- " sh(' echo before=`basename $PWD`' )\n " +
324
+ " echo(/ before=${pwd()}/ )\n " +
327
325
" semaphore 'wait'\n " +
328
- " sh(' echo after=`basename $PWD`' )\n " +
326
+ " echo(/ after=${pwd()}/ )\n " +
329
327
" }\n " +
330
328
"}"
331
- ));
329
+ , true ));
332
330
p .save ();
333
331
WorkflowRun b1 = p .scheduleBuild2 (0 ).waitForStart ();
334
332
SemaphoreStep .waitForStart ("wait/1" , b1 );
@@ -339,6 +337,12 @@ private void startJnlpProc() throws Exception {
339
337
}
340
338
});
341
339
story .addStep (new Statement () {
340
+ void assertLogMatches (WorkflowRun build , String regexp ) throws IOException { // TODO add to JenkinsRule
341
+ String log = JenkinsRule .getLog (build );
342
+ if (!Pattern .compile (regexp , Pattern .MULTILINE ).matcher (log ).find ()) { // assertMatches present in some utility extension to JUnit/Hamcrest but not in our test CP
343
+ fail (build + " log does not match /" + regexp + "/: " + log );
344
+ }
345
+ }
342
346
@ Override public void evaluate () throws Throwable {
343
347
WorkflowJob p = (WorkflowJob ) story .j .jenkins .getItem ("demo" );
344
348
WorkflowRun b = p .getLastBuild ();
@@ -349,18 +353,17 @@ private void startJnlpProc() throws Exception {
349
353
story .j .waitUntilNoActivity ();
350
354
story .j .assertBuildStatusSuccess (b1 );
351
355
story .j .assertBuildStatusSuccess (b2 );
352
- // TODO once got ‘InvalidClassException: cannot bind non-proxy descriptor to a proxy class’ inside BourneShellScript.doLaunch
353
- story .j .assertLogContains ("default=demo" , b1 );
354
- story .j .assertLogContains ("before=demo@2" , b1 );
355
- story .j .assertLogContains ("after=demo@2" , b1 );
356
- story .j .assertLogContains ("default=demo@3" , b2 );
357
- story .j .assertLogContains ("before=demo@4" , b2 );
358
- story .j .assertLogContains ("after=demo@4" , b2 );
356
+ assertLogMatches (b1 , "^default=.+demo$" );
357
+ assertLogMatches (b1 , "^before=.+demo@2$" );
358
+ assertLogMatches (b1 , "^after=.+demo@2$" );
359
+ assertLogMatches (b2 , "^default=.+demo@3$" );
360
+ assertLogMatches (b2 , "^before=.+demo@4$" );
361
+ assertLogMatches (b2 , "^after=.+demo@4$" );
359
362
SemaphoreStep .success ("wait/3" , null );
360
363
WorkflowRun b3 = story .j .assertBuildStatusSuccess (p .scheduleBuild2 (0 ));
361
- story . j . assertLogContains ( " default=demo" , b3 );
362
- story . j . assertLogContains ( " before=demo@2" , b3 );
363
- story . j . assertLogContains ( " after=demo@2" , b3 );
364
+ assertLogMatches ( b3 , "^ default=.+ demo$" );
365
+ assertLogMatches ( b3 , "^ before=.+ demo@2$" );
366
+ assertLogMatches ( b3 , "^ after=.+ demo@2$" );
364
367
}
365
368
});
366
369
}
0 commit comments