github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/HandlePipelineStepErrorsTest.groovy (about)

     1  import com.sap.piper.DebugReport
     2  import hudson.AbortException
     3  
     4  import static org.hamcrest.Matchers.is
     5  import static org.hamcrest.Matchers.not
     6  import static org.hamcrest.Matchers.containsString
     7  
     8  import org.junit.Rule
     9  import org.junit.Test
    10  import org.junit.rules.ExpectedException
    11  import org.junit.rules.RuleChain
    12  import static org.junit.Assert.assertThat
    13  
    14  import util.BasePiperTest
    15  import util.JenkinsLoggingRule
    16  import util.JenkinsReadYamlRule
    17  import util.JenkinsStepRule
    18  import util.Rules
    19  
    20  class HandlePipelineStepErrorsTest extends BasePiperTest {
    21      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    22      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    23      private ExpectedException thrown = ExpectedException.none()
    24  
    25      @Rule
    26      public RuleChain rules = Rules
    27          .getCommonRules(this)
    28          .around(new JenkinsReadYamlRule(this))
    29          .around(loggingRule)
    30          .around(stepRule)
    31          .around(thrown)
    32  
    33      @Test
    34      void testBeginAndEndMessage() {
    35          def isExecuted
    36          stepRule.step.handlePipelineStepErrors([
    37              stepName: 'testStep',
    38              stepParameters: ['something': 'anything']
    39          ]) {
    40              isExecuted = true
    41          }
    42          // asserts
    43          assertThat(isExecuted, is(true))
    44          assertThat(loggingRule.log, containsString('--- Begin library step of: testStep'))
    45          assertThat(loggingRule.log, containsString('--- End library step of: testStep'))
    46      }
    47  
    48      @Test
    49      void testNonVerbose() {
    50          try {
    51              stepRule.step.handlePipelineStepErrors([
    52                  stepName: 'testStep',
    53                  stepParameters: ['something': 'anything'],
    54                  echoDetails: false
    55              ]) {
    56                  throw new Exception('TestError')
    57              }
    58          } catch (ignore) {
    59          } finally {
    60              // asserts
    61              assertThat(loggingRule.log, not(containsString('--- Begin library step of: testStep')))
    62              assertThat(loggingRule.log, not(containsString('--- End library step: testStep')))
    63              assertThat(loggingRule.log, not(containsString('--- An error occurred in the library step: testStep')))
    64          }
    65      }
    66  
    67      @Test
    68      void testErrorsMessage() {
    69          def isReported
    70          try {
    71              stepRule.step.handlePipelineStepErrors([
    72                  stepName: 'testStep',
    73                  stepParameters: ['something': 'anything']
    74              ]) {
    75                  throw new Exception('TestError')
    76              }
    77          } catch (ignore) {
    78              isReported = true
    79          } finally {
    80              // asserts
    81              assertThat(isReported, is(true))
    82              assertThat(loggingRule.log, containsString('--- An error occurred in the library step: testStep'))
    83              assertThat(loggingRule.log, containsString('to show step parameters, set verbose:true'))
    84          }
    85      }
    86  
    87      @Test
    88      void testHandleErrorsIgnoreFailure() {
    89          def errorOccured = false
    90          helper.registerAllowedMethod('unstable', [String.class], {s ->
    91              nullScript.currentBuild.result = 'UNSTABLE'
    92          })
    93          try {
    94              stepRule.step.handlePipelineStepErrors([
    95                  stepName: 'test',
    96                  stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
    97                  failOnError: false
    98              ]) {
    99                  throw new AbortException('TestError')
   100              }
   101          } catch (err) {
   102              errorOccured = true
   103          }
   104          assertThat(errorOccured, is(false))
   105          assertThat(nullScript.currentBuild.result, is('UNSTABLE'))
   106      }
   107  
   108      @Test
   109      void testHandleErrorsIgnoreFailureBlacklist() {
   110          def errorOccured = false
   111  
   112          //define blacklist in defaults
   113          helper.registerAllowedMethod("readYaml", [Map], { Map m ->
   114              return [steps: [handlePipelineStepErrors: [mandatorySteps: ['step1', 'test']]]]
   115          })
   116  
   117          try {
   118              stepRule.step.handlePipelineStepErrors([
   119                  stepName: 'test',
   120                  stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
   121                  failOnError: false
   122              ]) {
   123                  throw new AbortException('TestError')
   124              }
   125          } catch (err) {
   126              errorOccured = true
   127          }
   128          assertThat(errorOccured, is(true))
   129      }
   130  
   131      @Test
   132      void testHandleErrorsIgnoreFailureNoScript() {
   133          def errorOccured = false
   134          helper.registerAllowedMethod('unstable', [String.class], {s ->
   135              //test behavior in case plugina are not yet up to date
   136              throw new java.lang.NoSuchMethodError('No such DSL method \'unstable\' found')
   137          })
   138          try {
   139              stepRule.step.handlePipelineStepErrors([
   140                  stepName: 'test',
   141                  stepParameters: [jenkinsUtilsStub: jenkinsUtils],
   142                  failOnError: false
   143              ]) {
   144                  throw new AbortException('TestError')
   145              }
   146          } catch (err) {
   147              errorOccured = true
   148          }
   149          assertThat(errorOccured, is(false))
   150      }
   151  
   152      @Test
   153      void testHandleErrorsTimeout() {
   154          def timeout = 0
   155          helper.registerAllowedMethod('timeout', [Map.class, Closure.class], {m, body ->
   156              timeout = m.time
   157              throw new org.jenkinsci.plugins.workflow.steps.FlowInterruptedException(hudson.model.Result.ABORTED, new jenkins.model.CauseOfInterruption.UserInterruption('Test'))
   158          })
   159          String errorMsg
   160          helper.registerAllowedMethod('unstable', [String.class], {s ->
   161              nullScript.currentBuild.result = 'UNSTABLE'
   162              errorMsg = s
   163          })
   164  
   165          stepRule.step.handlePipelineStepErrors([
   166              stepName: 'test',
   167              stepParameters: [jenkinsUtilsStub: jenkinsUtils, script: nullScript],
   168              failOnError: false,
   169              stepTimeouts: [test: 10]
   170          ]) {
   171              //do something
   172          }
   173          assertThat(timeout, is(10))
   174          assertThat(nullScript.currentBuild.result, is('UNSTABLE'))
   175          assertThat(errorMsg, is('[handlePipelineStepErrors] Error in step test - Build result set to \'UNSTABLE\''))
   176      }
   177  
   178      @Test
   179      void testFeedDebugReport() {
   180          Exception err = new Exception('TestError')
   181          try {
   182              stepRule.step.handlePipelineStepErrors([
   183                  stepName: 'testStep',
   184                  stepParameters: ['something': 'anything'],
   185              ]) {
   186                  throw err
   187              }
   188          } catch (ignore) {
   189          } finally {
   190              // asserts
   191              assertThat(DebugReport.instance.failedBuild.step, is('testStep'))
   192              assertThat(DebugReport.instance.failedBuild.fatal, is('true'))
   193              assertThat(DebugReport.instance.failedBuild.reason, is(err))
   194          }
   195      }
   196  
   197  }