Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,25 @@ pipeline {
unstable {
echo 'pipeline unit tests have gone UNSTABLE'
}

fixed {
echo 'pipeline unit tests are FIXED'
}

regression {
echo 'pipeline unit tests were SUCCESSFUL, but are now FAILED, UNSTABLE or ABORTED'
}

aborted {
echo 'pipeline unit tests are ABORTED'
}

unsuccessful {
echo 'pipeline unit tests have not PASSED'
}

cleanup {
echo 'This is where cleanup is done'
}
}
}
35 changes: 32 additions & 3 deletions pipelineTests/groovy/testSupport/PipelineTestHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,38 @@ class PipelineTestHelper extends BasePipelineTest {
def postResultEmulator = { String section, Closure c ->

def currentBuild = binding.getVariable('currentBuild')
def previousBuild = binding.getVariable('currentBuild').previousBuild

switch (section) {
case 'always':
case 'changed': // How to handle changed? It may happen so just run it..
case 'cleanup':
return c.call()
break
case 'changed':
if(currentBuild.result != previousBuild.result) { return c.call() }
else { println "post ${section} skipped as not CHANGED"; return null}
break
case 'fixed':
if(currentBuild.result == 'SUCCESS' &&
(previousBuild.result == 'FAILED' ||
previousBuild.result == 'UNSTABLE')) { return c.call() }
else { println "post ${section} skipped as not FIXED"; return null}
break
case 'success':
if(currentBuild.result == 'SUCCESS') { return c.call() }
else { println "post ${section} skipped as not SUCCESS"; return null}
break
case 'regression':
if(previousBuild.result == 'SUCCESS' &&
(currentBuild.result == 'UNSTABLE' ||
currentBuild.result == 'FAILURE' ||
currentBuild.result == 'ABORTED')) { return c.call() }
else { println "post ${section} skipped as not UNSTABLE, FAILURE or ABORTED after SUCCESS"; return null}
break
case 'unsuccessful':
if(currentBuild.result != 'SUCCESS') { return c.call() }
else { println "post ${section} skipped as SUCCESS"; return null}
break
case 'unstable':
if(currentBuild.result == 'UNSTABLE') { return c.call() }
else { println "post ${section} skipped as SUCCESS"; return null}
Expand All @@ -212,9 +234,14 @@ class PipelineTestHelper extends BasePipelineTest {
}
helper.registerAllowedMethod('always', [Closure.class], postResultEmulator.curry('always'))
helper.registerAllowedMethod('changed', [Closure.class], postResultEmulator.curry('changed'))
helper.registerAllowedMethod('fixed', [Closure.class], postResultEmulator.curry('fixed'))
helper.registerAllowedMethod('regression', [Closure.class], postResultEmulator.curry('regression'))
helper.registerAllowedMethod('aborted', [Closure.class], postResultEmulator.curry('aborted'))
helper.registerAllowedMethod('failure', [Closure.class], postResultEmulator.curry('failure'))
helper.registerAllowedMethod('success', [Closure.class], postResultEmulator.curry('success'))
helper.registerAllowedMethod('unstable', [Closure.class], postResultEmulator.curry('unstable'))
helper.registerAllowedMethod('failure', [Closure.class], postResultEmulator.curry('failure'))
helper.registerAllowedMethod('unsuccessful', [Closure.class], postResultEmulator.curry('unsuccessful'))
helper.registerAllowedMethod('cleanup', [Closure.class], postResultEmulator.curry('cleanup'))
}

/**
Expand Down Expand Up @@ -272,8 +299,10 @@ class PipelineTestHelper extends BasePipelineTest {

/**
* The currentBuild in the job
* Includes a previous build which finished successfully
*/
binding.setVariable('currentBuild', new Expando(result: 'SUCCESS', displayName: 'Build #1234'))
def previous = new Expando(result: 'SUCCESS', displayName: 'Build #1233')
binding.setVariable('currentBuild', new Expando(result: 'SUCCESS', displayName: 'Build #1234', previousBuild: previous))

/**
* agent any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
Jenkinsfile.echo(pipeline unit tests PASSED)
Jenkinsfile.failure(groovy.lang.Closure)
Jenkinsfile.changed(groovy.lang.Closure)
Jenkinsfile.echo(pipeline unit tests results have CHANGED)
Jenkinsfile.unstable(groovy.lang.Closure)
Jenkinsfile.fixed(groovy.lang.Closure)
Jenkinsfile.regression(groovy.lang.Closure)
Jenkinsfile.aborted(groovy.lang.Closure)
Jenkinsfile.unsuccessful(groovy.lang.Closure)
Jenkinsfile.cleanup(groovy.lang.Closure)
Jenkinsfile.echo(This is where cleanup is done)
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
Jenkinsfile.echo(pipeline unit tests PASSED)
Jenkinsfile.failure(groovy.lang.Closure)
Jenkinsfile.changed(groovy.lang.Closure)
Jenkinsfile.echo(pipeline unit tests results have CHANGED)
Jenkinsfile.unstable(groovy.lang.Closure)
Jenkinsfile.fixed(groovy.lang.Closure)
Jenkinsfile.regression(groovy.lang.Closure)
Jenkinsfile.aborted(groovy.lang.Closure)
Jenkinsfile.unsuccessful(groovy.lang.Closure)
Jenkinsfile.cleanup(groovy.lang.Closure)
Jenkinsfile.echo(This is where cleanup is done)
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
Jenkinsfile.echo(pipeline unit tests PASSED)
Jenkinsfile.failure(groovy.lang.Closure)
Jenkinsfile.changed(groovy.lang.Closure)
Jenkinsfile.echo(pipeline unit tests results have CHANGED)
Jenkinsfile.unstable(groovy.lang.Closure)
Jenkinsfile.fixed(groovy.lang.Closure)
Jenkinsfile.regression(groovy.lang.Closure)
Jenkinsfile.aborted(groovy.lang.Closure)
Jenkinsfile.unsuccessful(groovy.lang.Closure)
Jenkinsfile.cleanup(groovy.lang.Closure)
Jenkinsfile.echo(This is where cleanup is done)
20 changes: 14 additions & 6 deletions pipelineTests/groovy/tests/job/JenkinsfileTestSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class JenkinsfileTestSpec extends PipelineSpockTestBase {
}

@Unroll
def "Jenkinsfile cover all build results for post sections - #RESULT"() {
def "Jenkinsfile cover all build results for post sections - #SCENARIO"() {

given:
helper.registerAllowedMethod('validateDeclarativePipeline', [String.class], { true } )
Expand All @@ -97,6 +97,7 @@ class JenkinsfileTestSpec extends PipelineSpockTestBase {

and:
binding.getVariable('currentBuild').result = RESULT
binding.getVariable('currentBuild').previousBuild.result = PREVIOUS

when:
runScript('Jenkinsfile')
Expand All @@ -105,10 +106,17 @@ class JenkinsfileTestSpec extends PipelineSpockTestBase {
printCallStack()

where:
RESULT | NONE
'SUCCESS' | _
'FAILURE' | _
'ABORTED' | _
'UNSTABLE' | _
RESULT | PREVIOUS | SCENARIO
'SUCCESS' | 'SUCCESS' | 'SUCCESS'
'FAILURE' | 'SUCCESS' | 'REGRESSION FAILURE'
'UNSTABLE' | 'SUCCESS' | 'REGRESSION UNSTABLE'
'ABORTED' | 'SUCCESS' | 'REGRESSION ABORTED'
'FAILURE' | 'FAILURE' | 'FAILURE'
'FAILURE' | 'UNSTABLE' | 'CHANGED'
'ABORTED' | 'ABORTED' | 'ABORTED'
'UNSTABLE' | 'UNSTABLE' | 'UNSTABLE'
'SUCCESS' | 'UNSTABLE' | 'FIXED UNSTABLE'
'SUCCESS' | 'FAILED' | 'FIXED FAILED'
'FAILURE' | 'ABORTED' | 'UNSUCCESSFUL'
}
}