2019-04-04 08:38:54 +02:00
|
|
|
import hudson.AbortException
|
|
|
|
|
2018-10-24 13:36:30 +02:00
|
|
|
import static org.hamcrest.Matchers.is
|
|
|
|
import static org.hamcrest.Matchers.not
|
|
|
|
import static org.hamcrest.Matchers.containsString
|
|
|
|
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
|
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsLoggingRule
|
2019-03-20 12:21:06 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-10-24 13:36:30 +02:00
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
class HandlePipelineStepErrorsTest extends BasePiperTest {
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2019-01-22 10:22:15 +02:00
|
|
|
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
2018-10-24 13:36:30 +02:00
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
2019-03-20 12:21:06 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2019-01-22 10:22:15 +02:00
|
|
|
.around(loggingRule)
|
2019-01-22 10:25:42 +02:00
|
|
|
.around(stepRule)
|
2018-10-24 13:36:30 +02:00
|
|
|
.around(thrown)
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testBeginAndEndMessage() {
|
|
|
|
def isExecuted
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.handlePipelineStepErrors([
|
2018-10-24 13:36:30 +02:00
|
|
|
stepName: 'testStep',
|
|
|
|
stepParameters: ['something': 'anything']
|
|
|
|
]) {
|
|
|
|
isExecuted = true
|
|
|
|
}
|
|
|
|
// asserts
|
|
|
|
assertThat(isExecuted, is(true))
|
2019-02-21 16:46:17 +02:00
|
|
|
assertThat(loggingRule.log, containsString('--- Begin library step of: testStep'))
|
|
|
|
assertThat(loggingRule.log, containsString('--- End library step of: testStep'))
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testNonVerbose() {
|
|
|
|
try {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.handlePipelineStepErrors([
|
2018-10-24 13:36:30 +02:00
|
|
|
stepName: 'testStep',
|
|
|
|
stepParameters: ['something': 'anything'],
|
|
|
|
echoDetails: false
|
|
|
|
]) {
|
|
|
|
throw new Exception('TestError')
|
|
|
|
}
|
|
|
|
} catch (ignore) {
|
|
|
|
} finally {
|
|
|
|
// asserts
|
2019-02-21 16:46:17 +02:00
|
|
|
assertThat(loggingRule.log, not(containsString('--- Begin library step of: testStep')))
|
|
|
|
assertThat(loggingRule.log, not(containsString('--- End library step: testStep')))
|
|
|
|
assertThat(loggingRule.log, not(containsString('--- An error occurred in the library step: testStep')))
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testErrorsMessage() {
|
|
|
|
def isReported
|
|
|
|
try {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.handlePipelineStepErrors([
|
2018-10-24 13:36:30 +02:00
|
|
|
stepName: 'testStep',
|
|
|
|
stepParameters: ['something': 'anything']
|
|
|
|
]) {
|
|
|
|
throw new Exception('TestError')
|
|
|
|
}
|
|
|
|
} catch (ignore) {
|
|
|
|
isReported = true
|
|
|
|
} finally {
|
|
|
|
// asserts
|
|
|
|
assertThat(isReported, is(true))
|
2019-02-21 16:46:17 +02:00
|
|
|
assertThat(loggingRule.log, containsString('--- An error occurred in the library step: testStep'))
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString('[something:anything]'))
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 08:38:54 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHandleErrorsIgnoreFailure() {
|
|
|
|
def errorOccured = false
|
|
|
|
try {
|
|
|
|
stepRule.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'test',
|
|
|
|
stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
|
|
|
|
failOnError: false
|
|
|
|
]) {
|
|
|
|
throw new AbortException('TestError')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
errorOccured = true
|
|
|
|
}
|
|
|
|
assertThat(errorOccured, is(false))
|
|
|
|
assertThat(nullScript.currentBuild.result, is('UNSTABLE'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHandleErrorsIgnoreFailureBlacklist() {
|
|
|
|
def errorOccured = false
|
|
|
|
|
|
|
|
//define blacklist in defaults
|
|
|
|
helper.registerAllowedMethod("readYaml", [Map], { Map m ->
|
|
|
|
return [steps: [handlePipelineStepErrors: [mandatorySteps: ['step1', 'test']]]]
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
stepRule.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'test',
|
|
|
|
stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
|
|
|
|
failOnError: false
|
|
|
|
]) {
|
|
|
|
throw new AbortException('TestError')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
errorOccured = true
|
|
|
|
}
|
|
|
|
assertThat(errorOccured, is(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHandleErrorsIgnoreFailureNoScript() {
|
|
|
|
def errorOccured = false
|
|
|
|
try {
|
|
|
|
stepRule.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'test',
|
|
|
|
stepParameters: [jenkinsUtilsStub: jenkinsUtils],
|
|
|
|
failOnError: false
|
|
|
|
]) {
|
|
|
|
throw new AbortException('TestError')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
errorOccured = true
|
|
|
|
}
|
|
|
|
assertThat(errorOccured, is(false))
|
|
|
|
}
|
2019-04-05 11:16:34 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testHandleErrorsTimeout() {
|
|
|
|
def timeout = 0
|
|
|
|
helper.registerAllowedMethod('timeout', [Map.class, Closure.class], {m, body ->
|
|
|
|
timeout = m.time
|
|
|
|
throw new org.jenkinsci.plugins.workflow.steps.FlowInterruptedException(hudson.model.Result.ABORTED, new jenkins.model.CauseOfInterruption.UserInterruption('Test'))
|
|
|
|
})
|
|
|
|
|
|
|
|
stepRule.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'test',
|
|
|
|
stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
|
|
|
|
failOnError: false,
|
|
|
|
stepTimeouts: [test: 10]
|
|
|
|
]) {
|
|
|
|
//do something
|
|
|
|
}
|
|
|
|
assertThat(timeout, is(10))
|
|
|
|
assertThat(nullScript.currentBuild.result, is('UNSTABLE'))
|
|
|
|
}
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|