From dabbc724ad577d40bc056f17ca2d4eb67aaa555e Mon Sep 17 00:00:00 2001 From: Christopher Fenner Date: Wed, 24 Oct 2018 13:36:30 +0200 Subject: [PATCH] handlePipelineStepErrors: extract error message to template (#350) * add template for handleStepErrors * add tests * use template * fix indent * fix typo * Update HandlePipelineStepErrorTest.groovy * Update HandlePipelineStepErrorTest.groovy --- resources/com.sap.piper/templates/error.log | 21 +++++ .../groovy/HandlePipelineStepErrorTest.groovy | 82 +++++++++++++++++++ vars/handlePipelineStepErrors.groovy | 48 ++++------- 3 files changed, 119 insertions(+), 32 deletions(-) create mode 100644 resources/com.sap.piper/templates/error.log create mode 100644 test/groovy/HandlePipelineStepErrorTest.groovy diff --git a/resources/com.sap.piper/templates/error.log b/resources/com.sap.piper/templates/error.log new file mode 100644 index 000000000..3058ed649 --- /dev/null +++ b/resources/com.sap.piper/templates/error.log @@ -0,0 +1,21 @@ +---------------------------------------------------------- +--- ERROR OCCURRED IN LIBRARY STEP: ${stepName} +---------------------------------------------------------- + +FOLLOWING PARAMETERS WERE AVAILABLE TO THIS STEP: +*** +${stepParameters} +*** + +ERROR WAS: +*** +${error} +*** + +FURTHER INFORMATION: +* Documentation of library step ${stepName}: https://sap.github.io/jenkins-library/steps/${stepName}/ +* Source code of library step ${stepName}: https://github.com/SAP/jenkins-library/blob/master/vars/${stepName}.groovy +* Library documentation: https://sap.github.io/jenkins-library/ +* Library repository: https://github.com/SAP/jenkins-library + +---------------------------------------------------------- diff --git a/test/groovy/HandlePipelineStepErrorTest.groovy b/test/groovy/HandlePipelineStepErrorTest.groovy new file mode 100644 index 000000000..830c2855e --- /dev/null +++ b/test/groovy/HandlePipelineStepErrorTest.groovy @@ -0,0 +1,82 @@ +#!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) + private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this) + private ExpectedException thrown = ExpectedException.none() + + @Rule + public RuleChain rules = Rules + .getCommonRules(this) + .around(jlr) + .around(jsr) + .around(thrown) + + @Test + void testBeginAndEndMessage() { + def isExecuted + jsr.step.handlePipelineStepErrors([ + stepName: 'testStep', + stepParameters: ['something': 'anything'] + ]) { + isExecuted = true + } + // asserts + assertThat(isExecuted, is(true)) + assertThat(jlr.log, containsString('--- BEGIN LIBRARY STEP: testStep')) + assertThat(jlr.log, containsString('--- END LIBRARY STEP: testStep')) + } + + @Test + void testNonVerbose() { + try { + jsr.step.handlePipelineStepErrors([ + stepName: 'testStep', + stepParameters: ['something': 'anything'], + echoDetails: false + ]) { + throw new Exception('TestError') + } + } catch (ignore) { + } finally { + // asserts + assertThat(jlr.log, not(containsString('--- BEGIN LIBRARY STEP: testStep'))) + assertThat(jlr.log, not(containsString('--- END LIBRARY STEP: testStep'))) + assertThat(jlr.log, not(containsString('--- ERROR OCCURRED IN LIBRARY STEP: testStep'))) + } + } + + @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)) + assertThat(jlr.log, containsString('--- ERROR OCCURRED IN LIBRARY STEP: testStep')) + assertThat(jlr.log, containsString('[something:anything]')) + } + } +} diff --git a/vars/handlePipelineStepErrors.groovy b/vars/handlePipelineStepErrors.groovy index 42a456f33..00bb0ab5d 100644 --- a/vars/handlePipelineStepErrors.groovy +++ b/vars/handlePipelineStepErrors.groovy @@ -1,50 +1,34 @@ +import groovy.text.SimpleTemplateEngine import groovy.transform.Field @Field STEP_NAME = 'handlePipelineStepErrors' - void call(Map parameters = [:], body) { - def stepParameters = parameters.stepParameters //mandatory def stepName = parameters.stepName //mandatory - def echoDetails = parameters.get('echoDetails', true) - + def verbose = parameters.get('echoDetails', true) + def message = '' try { - if (stepParameters == null && stepName == null) error "step handlePipelineStepErrors requires following mandatory parameters: stepParameters, stepName" - if (echoDetails) - echo "--- BEGIN LIBRARY STEP: ${stepName}.groovy ---" + if (verbose) + echo "--- BEGIN LIBRARY STEP: ${stepName} ---" body() - } catch (Throwable err) { - if (echoDetails) - echo """---------------------------------------------------------- ---- ERROR OCCURRED IN LIBRARY STEP: ${stepName} ----------------------------------------------------------- - -FOLLOWING PARAMETERS WERE AVAILABLE TO THIS STEP: -*** -${stepParameters?.toString()} -*** - -ERROR WAS: -*** -${err} -*** - -FURTHER INFORMATION: -* Documentation of library step ${stepName}: https://sap.github.io/jenkins-library/steps/${stepName}/ -* Source code of library step ${stepName}: https://github.com/SAP/jenkins-library/blob/master/vars/${stepName}.groovy -* Library documentation: https://sap.github.io/jenkins-library/ -* Library repository: https://github.com/SAP/jenkins-library - -----------------------------------------------------------""" + if (verbose) + message += SimpleTemplateEngine.newInstance() + .createTemplate(libraryResource('com.sap.piper/templates/error.log')) + .make([ + stepName: stepName, + stepParameters: stepParameters?.toString(), + error: err + ]).toString() throw err } finally { - if (echoDetails) - echo "--- END LIBRARY STEP: ${stepName}.groovy ---" + if (verbose) + message += "--- END LIBRARY STEP: ${stepName} ---" + echo message } }