-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test and basedeclarativepipelinetest
- Loading branch information
Showing
5 changed files
with
228 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/groovy/com/lesfurets/jenkins/unit/BaseDeclarativePipelineTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.lesfurets.jenkins.unit | ||
|
||
abstract class BaseDeclarativePipelineTest extends BasePipelineTest { | ||
|
||
Script jenkinsfile = null | ||
|
||
DeclarativePipelineHarness harness | ||
|
||
|
||
@Override | ||
void setUp() { | ||
if(harness == null || jenkinsfile == null) { | ||
super.setUp() | ||
harness = new DeclarativePipelineHarness(this, allowedClosureSteps(), allowedParameterizedClosureSteps()) | ||
loadJenkinsfile() | ||
} else { | ||
harness.clearAllCalls() | ||
} | ||
} | ||
|
||
abstract String jenkinsfileName() | ||
|
||
def loadJenkinsfile() { | ||
this.jenkinsfile = super.loadScript(jenkinsfileName()) | ||
} | ||
|
||
List<String> defaultAllowedClosureSteps = ['pipeline', | ||
'options', | ||
'parameters', | ||
'environment', | ||
'triggers', | ||
'tools', | ||
'input', | ||
|
||
'agent', | ||
'docker', | ||
'dockerfile', | ||
'node', | ||
|
||
'stages', | ||
'parallel', | ||
'script', | ||
'steps', | ||
|
||
'when', | ||
'allOf', | ||
'anyOf', | ||
'expression', | ||
|
||
'post', | ||
'always', | ||
'cleanup', | ||
'success', | ||
'failure', | ||
'regression', | ||
'changed', | ||
'fixed', | ||
'aborted', | ||
'unstable', | ||
'unsuccessful'] | ||
|
||
Map<String, Class<Object>> defaultAllowedParameterizedClosureSteps = [ | ||
'stage': String.class as Class<Object>, | ||
'node' : String.class as Class<Object>, | ||
'withCredentials': Object.class, | ||
'withEnv': List.class as Class<Object>, | ||
'dir': String.class as Class<Object>, | ||
] | ||
|
||
List<String> allowedClosureSteps() { | ||
return defaultAllowedClosureSteps | ||
} | ||
|
||
Map<String, Class<Object>> allowedParameterizedClosureSteps() { | ||
return defaultAllowedParameterizedClosureSteps | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/java/com/lesfurets/jenkins/TestBaseDeclarativePipelineTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.lesfurets.jenkins | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.lesfurets.jenkins.unit.BaseDeclarativePipelineTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class TestBaseDeclarativePipelineTest extends BaseDeclarativePipelineTest { | ||
@Override | ||
String jenkinsfileName() { | ||
return "src/test/jenkins/job/declarative.jenkins" | ||
} | ||
|
||
@Override | ||
@Before | ||
void setUp() { | ||
super.setUp() | ||
} | ||
|
||
@Test | ||
void runAStage() { | ||
super.jenkinsfile.run() | ||
harness | ||
.runStage('init') | ||
.runClosureStep('steps') | ||
|
||
def call = helper.callStack.find { it.methodName == 'sh' } | ||
assertThat(call.args.toList()).isEqualTo(['echo \'hello world\'']) | ||
} | ||
|
||
@Test | ||
void runASubStage1() { | ||
super.jenkinsfile.run() | ||
harness | ||
.runParallelSubStage('parallel build', 'build 1') | ||
.runClosureStep('steps') | ||
|
||
def call = helper.callStack.find { it.methodName == 'sh' } | ||
assertThat(call.args.toList()).isEqualTo(['echo \'hello build 1\'']) | ||
} | ||
|
||
@Test | ||
void runASubStage2() { | ||
super.jenkinsfile.run() | ||
harness | ||
.runParallelSubStage('parallel build', 'build 2') | ||
.runClosureStep('steps') | ||
|
||
def call = helper.callStack.find { it.methodName == 'sh' } | ||
assertThat(call.args.toList()).isEqualTo(['echo \'hello build 2\'']) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pipeline { | ||
stages { | ||
stage("init") { | ||
steps { | ||
sh "echo 'hello world'" | ||
} | ||
} | ||
stage("parallel build") { | ||
parallel { | ||
stage("build 1") { | ||
steps { | ||
sh "echo 'hello build 1'" | ||
} | ||
} | ||
stage("build 2") { | ||
steps { | ||
sh "echo 'hello build 2'" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |