2018-10-24 13:36:30 +02:00
|
|
|
#!groovy
|
|
|
|
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
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
class HandlePipelineStepErrorsTest extends BasePiperTest {
|
|
|
|
private JenkinsStepRule jsr = 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-01-22 10:22:15 +02:00
|
|
|
.around(loggingRule)
|
2018-10-24 13:36:30 +02:00
|
|
|
.around(jsr)
|
|
|
|
.around(thrown)
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testBeginAndEndMessage() {
|
|
|
|
def isExecuted
|
|
|
|
jsr.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'testStep',
|
|
|
|
stepParameters: ['something': 'anything']
|
|
|
|
]) {
|
|
|
|
isExecuted = true
|
|
|
|
}
|
|
|
|
// asserts
|
|
|
|
assertThat(isExecuted, is(true))
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString('--- BEGIN LIBRARY STEP: testStep'))
|
|
|
|
assertThat(loggingRule.log, containsString('--- END LIBRARY STEP: testStep'))
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testNonVerbose() {
|
|
|
|
try {
|
|
|
|
jsr.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'testStep',
|
|
|
|
stepParameters: ['something': 'anything'],
|
|
|
|
echoDetails: false
|
|
|
|
]) {
|
|
|
|
throw new Exception('TestError')
|
|
|
|
}
|
|
|
|
} catch (ignore) {
|
|
|
|
} finally {
|
|
|
|
// asserts
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, not(containsString('--- BEGIN LIBRARY STEP: testStep')))
|
|
|
|
assertThat(loggingRule.log, not(containsString('--- END LIBRARY STEP: testStep')))
|
|
|
|
assertThat(loggingRule.log, not(containsString('--- ERROR OCCURRED IN LIBRARY STEP: testStep')))
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testErrorsMessage() {
|
|
|
|
def isReported
|
|
|
|
try {
|
|
|
|
jsr.step.handlePipelineStepErrors([
|
|
|
|
stepName: 'testStep',
|
|
|
|
stepParameters: ['something': 'anything']
|
|
|
|
]) {
|
|
|
|
throw new Exception('TestError')
|
|
|
|
}
|
|
|
|
} catch (ignore) {
|
|
|
|
isReported = true
|
|
|
|
} finally {
|
|
|
|
// asserts
|
|
|
|
assertThat(isReported, is(true))
|
2019-01-22 10:22:15 +02:00
|
|
|
assertThat(loggingRule.log, containsString('--- ERROR OCCURRED IN LIBRARY STEP: testStep'))
|
|
|
|
assertThat(loggingRule.log, containsString('[something:anything]'))
|
2018-10-24 13:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|