2018-10-18 08:51:48 +02:00
|
|
|
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import util.*
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.containsString
|
|
|
|
import static org.hamcrest.CoreMatchers.is
|
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
|
|
|
|
class PipelineRestartStepsTest extends BasePiperTest {
|
|
|
|
|
2019-01-22 10:27:45 +02:00
|
|
|
private JenkinsErrorRule errorRule = new JenkinsErrorRule(this)
|
2019-01-22 10:22:15 +02:00
|
|
|
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2018-10-18 08:51:48 +02:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain chain = Rules.getCommonRules(this)
|
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2019-01-22 10:27:45 +02:00
|
|
|
.around(errorRule)
|
2019-01-22 10:22:15 +02:00
|
|
|
.around(loggingRule)
|
2019-01-22 10:25:42 +02:00
|
|
|
.around(stepRule)
|
2018-10-18 08:51:48 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testError() throws Exception {
|
|
|
|
|
|
|
|
def mailBuildResult = ''
|
|
|
|
helper.registerAllowedMethod('mailSendNotification', [Map.class], { m ->
|
|
|
|
mailBuildResult = m.buildResult
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('timeout', [Map.class, Closure.class], { m, closure ->
|
|
|
|
assertThat(m.time, is(1))
|
|
|
|
assertThat(m.unit, is('SECONDS'))
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
|
|
|
|
def iterations = 0
|
|
|
|
helper.registerAllowedMethod('input', [Map.class], { m ->
|
|
|
|
iterations ++
|
|
|
|
assertThat(m.message, is('Do you want to restart?'))
|
|
|
|
assertThat(m.ok, is('Restart'))
|
|
|
|
if (iterations > 1) {
|
|
|
|
throw new FlowInterruptedException()
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.pipelineRestartSteps ([
|
2018-10-18 08:51:48 +02:00
|
|
|
script: nullScript,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
sendMail: true,
|
|
|
|
timeoutInSeconds: 1
|
|
|
|
|
|
|
|
]) {
|
|
|
|
throw new hudson.AbortException('I just created an error')
|
|
|
|
}
|
|
|
|
} catch(err) {
|
2020-09-24 07:41:06 +02:00
|
|
|
assertThat(loggingRule.log, containsString('ERROR occurred: hudson.AbortException: I just created an error'))
|
2018-10-18 08:51:48 +02:00
|
|
|
assertThat(mailBuildResult, is('UNSTABLE'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testErrorNoMail() throws Exception {
|
|
|
|
|
|
|
|
def mailBuildResult = ''
|
|
|
|
helper.registerAllowedMethod('mailSendNotification', [Map.class], { m ->
|
|
|
|
mailBuildResult = m.buildResult
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('timeout', [Map.class, Closure.class], { m, closure ->
|
|
|
|
assertThat(m.time, is(1))
|
|
|
|
assertThat(m.unit, is('SECONDS'))
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
|
|
|
|
def iterations = 0
|
|
|
|
helper.registerAllowedMethod('input', [Map.class], { m ->
|
|
|
|
iterations ++
|
|
|
|
assertThat(m.message, is('Do you want to restart?'))
|
|
|
|
assertThat(m.ok, is('Restart'))
|
|
|
|
if (iterations > 1) {
|
|
|
|
throw new FlowInterruptedException()
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.pipelineRestartSteps ([
|
2018-10-18 08:51:48 +02:00
|
|
|
script: nullScript,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
sendMail: false,
|
|
|
|
timeoutInSeconds: 1
|
|
|
|
|
|
|
|
]) {
|
|
|
|
throw new hudson.AbortException('I just created an error')
|
|
|
|
}
|
|
|
|
} catch(err) {
|
2020-09-24 07:41:06 +02:00
|
|
|
assertThat(loggingRule.log, containsString('ERROR occurred: hudson.AbortException: I just created an error'))
|
2018-10-18 08:51:48 +02:00
|
|
|
assertThat(mailBuildResult, is(''))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testSuccess() throws Exception {
|
|
|
|
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.pipelineRestartSteps ([
|
2018-10-18 08:51:48 +02:00
|
|
|
script: nullScript,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
sendMail: false,
|
|
|
|
timeoutInSeconds: 1
|
|
|
|
|
|
|
|
]) {
|
|
|
|
nullScript.echo 'This is a test'
|
|
|
|
}
|
|
|
|
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString('This is a test'))
|
2018-10-18 08:51:48 +02:00
|
|
|
}
|
|
|
|
}
|