1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/test/groovy/PiperPipelineStageInitTest.groovy
Christopher Fenner 8a55e25f72
add Slack notification to Init and Post stage (#691)
* add Slack notification to post stage

* add Slack notification to init stage

* add trigger condition for Slack notification

* fix whitespaces

* use capital stage name

* add tests for init stage

* remove unused import

* add tests for post stage

* minor changes

* fix typo
2019-05-10 06:47:44 +02:00

123 lines
4.4 KiB
Groovy

#!groovy
package stages
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.Rules
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertThat
class PiperPipelineStageInitTest extends BasePiperTest {
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private ExpectedException thrown = ExpectedException.none()
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
.around(jlr)
.around(jsr)
private List stepsCalled = []
@Before
void init() {
binding.variables.env.STAGE_NAME = 'Init'
binding.setVariable('scm', {})
helper.registerAllowedMethod('deleteDir', [], null)
helper.registerAllowedMethod("findFiles", [Map.class], { map ->
switch (map.glob) {
case 'pom.xml':
return [new File('pom.xml')].toArray()
default:
return [].toArray()
}
})
helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
assertThat(m.stageName, is('Init'))
return body()
})
helper.registerAllowedMethod('checkout', [Closure.class], {c ->
stepsCalled.add('checkout')
return [
GIT_COMMIT: 'abcdef12345',
GIT_URL: 'some.url'
]
})
helper.registerAllowedMethod('setupCommonPipelineEnvironment', [Map.class], {m -> stepsCalled.add('setupCommonPipelineEnvironment')})
helper.registerAllowedMethod('piperInitRunStageConfiguration', [Map.class], {m -> stepsCalled.add('piperInitRunStageConfiguration')})
helper.registerAllowedMethod('slackSendNotification', [Map.class], {m -> stepsCalled.add('slackSendNotification')})
helper.registerAllowedMethod('artifactSetVersion', [Map.class], {m -> stepsCalled.add('artifactSetVersion')})
helper.registerAllowedMethod('pipelineStashFilesBeforeBuild', [Map.class], {m -> stepsCalled.add('pipelineStashFilesBeforeBuild')})
}
@Test
void testInitNoBuildTool() {
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool')
jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
}
@Test
void testInitBuildToolDoesNotMatchProject() {
thrown.expect(hudson.AbortException)
thrown.expectMessage(containsString("buildTool configuration 'npm' does not fit to your project"))
jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, buildTool: 'npm')
}
@Test
void testInitDefault() {
jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, buildTool: 'maven')
assertThat(stepsCalled, hasItems(
'checkout',
'setupCommonPipelineEnvironment',
'piperInitRunStageConfiguration',
'artifactSetVersion',
'pipelineStashFilesBeforeBuild'
))
assertThat(stepsCalled, not(hasItems('slackSendNotification')))
}
@Test
void testInitNotOnProductiveBranch() {
binding.variables.env.BRANCH_NAME = 'anyOtherBranch'
jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, buildTool: 'maven')
assertThat(stepsCalled, hasItems(
'checkout',
'setupCommonPipelineEnvironment',
'piperInitRunStageConfiguration',
'pipelineStashFilesBeforeBuild'
))
assertThat(stepsCalled, not(hasItems('artifactSetVersion')))
}
@Test
void testInitWithSlackNotification() {
nullScript.commonPipelineEnvironment.configuration = [runStep: [Init: [slackSendNotification: true]]]
jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, buildTool: 'maven')
assertThat(stepsCalled, hasItems(
'checkout',
'setupCommonPipelineEnvironment',
'piperInitRunStageConfiguration',
'artifactSetVersion',
'slackSendNotification',
'pipelineStashFilesBeforeBuild'
))
}
}