1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-05-27 22:57:51 +02:00
sap-jenkins-library/test/groovy/PiperPipelineStagePostTest.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

70 lines
2.3 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.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.Rules
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertThat
class PiperPipelineStagePostTest extends BasePiperTest {
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private ExpectedException thrown = ExpectedException.none()
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
.around(jsr)
private List stepsCalled = []
@Before
void init() {
binding.variables.env.STAGE_NAME = 'Release'
helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
assertThat(m.stageName, is('Release'))
return body()
})
helper.registerAllowedMethod('influxWriteData', [Map.class], {m -> stepsCalled.add('influxWriteData')})
helper.registerAllowedMethod('slackSendNotification', [Map.class], {m -> stepsCalled.add('slackSendNotification')})
helper.registerAllowedMethod('mailSendNotification', [Map.class], {m -> stepsCalled.add('mailSendNotification')})
}
@Test
void testPostDefault() {
jsr.step.piperPipelineStagePost(script: nullScript, juStabUtils: utils)
assertThat(stepsCalled, hasItems('influxWriteData','mailSendNotification'))
assertThat(stepsCalled, not(hasItems('slackSendNotification')))
}
@Test
void testPostNotOnProductiveBranch() {
binding.variables.env.BRANCH_NAME = 'anyOtherBranch'
jsr.step.piperPipelineStagePost(script: nullScript, juStabUtils: utils)
assertThat(stepsCalled, hasItems('influxWriteData','mailSendNotification'))
assertThat(stepsCalled, not(hasItems('slackSendNotification')))
}
@Test
void testPostWithSlackNotification() {
nullScript.commonPipelineEnvironment.configuration = [runStep: ['Post Actions': [slackSendNotification: true]]]
jsr.step.piperPipelineStagePost(script: nullScript, juStabUtils: utils)
assertThat(stepsCalled, hasItems('influxWriteData','mailSendNotification','slackSendNotification'))
}
}