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

     1  package templates
     2  
     3  import org.junit.Before
     4  import org.junit.Rule
     5  import org.junit.Test
     6  import org.junit.rules.RuleChain
     7  import util.BasePiperTest
     8  import util.JenkinsReadYamlRule
     9  import util.JenkinsStepRule
    10  import util.PipelineWhenException
    11  import util.Rules
    12  
    13  import static org.hamcrest.Matchers.*
    14  import static org.junit.Assert.assertThat
    15  
    16  class AbapEnvironmentPipelineTest extends BasePiperTest {
    17      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    18  
    19      @Rule
    20      public RuleChain rules = Rules
    21          .getCommonRules(this)
    22          .around(new JenkinsReadYamlRule(this))
    23          .around(jsr)
    24  
    25      private skipDefaultCheckout = false
    26      private timestamps = false
    27      private stagesExecuted = []
    28      private stepsCalled = []
    29  
    30      @Before
    31      void init() {
    32  
    33          helper.registerAllowedMethod('library', [String.class], null)
    34  
    35          helper.registerAllowedMethod('pipeline', [Closure.class], null)
    36  
    37          helper.registerAllowedMethod('agent', [Closure.class], null)
    38          binding.setVariable('any', {})
    39          binding.setVariable('none', {})
    40  
    41          helper.registerAllowedMethod('options', [Closure.class], null)
    42          helper.registerAllowedMethod('skipDefaultCheckout', [], {skipDefaultCheckout = true})
    43  
    44          helper.registerAllowedMethod('stages', [Closure.class], null)
    45  
    46          helper.registerAllowedMethod('stage', [String.class, Closure.class], {stageName, body ->
    47  
    48              def stageResult
    49  
    50              binding.variables.env.STAGE_NAME = stageName
    51  
    52              helper.registerAllowedMethod('when', [Closure.class], {cWhen ->
    53  
    54                  helper.registerAllowedMethod('allOf', [Closure.class], {cAllOf ->
    55                      def branchResult = false
    56                      helper.registerAllowedMethod('branch', [String.class], {branchName  ->
    57                          if (!branchResult)
    58                              branchResult = (branchName == env.BRANCH_NAME)
    59                          if( !branchResult) {
    60                              throw new PipelineWhenException("Stage '${stageName}' skipped - expression: '${branchResult}'")
    61                          }
    62                      })
    63                      helper.registerAllowedMethod('expression', [Closure.class], { Closure cExp ->
    64                          def result = cExp()
    65                          if(!result) {
    66                              throw new PipelineWhenException("Stage '${stageName}' skipped - expression: '${result}'")
    67                          }
    68                          return result
    69                      })
    70                      return cAllOf()
    71                  })
    72  
    73                  helper.registerAllowedMethod('anyOf', [Closure.class], {cAnyOf ->
    74                      def result = false
    75                      helper.registerAllowedMethod('branch', [String.class], {branchName  ->
    76                          if (!result)
    77                              result = (branchName == env.BRANCH_NAME)
    78                          return result
    79                      })
    80                      helper.registerAllowedMethod('expression', [Closure.class], { Closure cExp ->
    81                          if (!result)
    82                              result = cExp()
    83                          return result
    84                      })
    85                      cAnyOf()
    86                      if(!result) {
    87                          throw new PipelineWhenException("Stage '${stageName}' skipped - anyOf: '${result}'")
    88                      }
    89                      return cAnyOf()
    90                  })
    91  
    92                  helper.registerAllowedMethod('branch', [String.class], {branchName  ->
    93                      def result =  (branchName == env.BRANCH_NAME)
    94                      if(result == false) {
    95                          throw new PipelineWhenException("Stage '${stageName}' skipped - expected branch: '${branchName}' while current branch: '${env.BRANCH_NAME}'")
    96                      }
    97                      return result
    98                  })
    99  
   100                  helper.registerAllowedMethod('expression', [Closure.class], { Closure cExp ->
   101                      def result = cExp()
   102                      if(!result) {
   103                          throw new PipelineWhenException("Stage '${stageName}' skipped - expression: '${result}'")
   104                      }
   105                      return result
   106                  })
   107                  return cWhen()
   108              })
   109  
   110              // Stage is not executed if build fails or aborts
   111              def status = currentBuild.result
   112              switch (status) {
   113                  case 'FAILURE':
   114                  case 'ABORTED':
   115                      break
   116                  default:
   117                      try {
   118                          stageResult = body()
   119                          stagesExecuted.add(stageName)
   120                      }
   121                      catch (PipelineWhenException pwe) {
   122                          //skip stage due to not met when expression
   123                      }
   124                      catch (Exception e) {
   125                          throw e
   126                      }
   127              }
   128              return stageResult
   129          })
   130  
   131          helper.registerAllowedMethod('steps', [Closure], null)
   132          helper.registerAllowedMethod('parallel', [Closure], null)
   133          helper.registerAllowedMethod('post', [Closure], {c -> c()})
   134          helper.registerAllowedMethod('success', [Closure], {c -> c()})
   135          helper.registerAllowedMethod('failure', [Closure], {c -> c()})
   136          helper.registerAllowedMethod('aborted', [Closure], {c -> c()})
   137          helper.registerAllowedMethod('unstable', [Closure], {c -> c()})
   138          helper.registerAllowedMethod('unsuccessful', [Closure], {c -> c()})
   139          helper.registerAllowedMethod('cleanup', [Closure], {c -> c()})
   140          helper.registerAllowedMethod('input', [Map], {m -> return null})
   141  
   142          helper.registerAllowedMethod('abapEnvironmentPipelineStageInit', [Map.class], {m ->
   143              stepsCalled.add('abapEnvironmentPipelineStageInit')
   144          })
   145  
   146          helper.registerAllowedMethod('abapEnvironmentPipelineStagePrepareSystem', [Map.class], {m ->
   147              stepsCalled.add('abapEnvironmentPipelineStagePrepareSystem')
   148          })
   149  
   150          helper.registerAllowedMethod('abapEnvironmentPipelineStageCloneRepositories', [Map.class], {m ->
   151              stepsCalled.add('abapEnvironmentPipelineStageCloneRepositories')
   152          })
   153  
   154          helper.registerAllowedMethod('abapEnvironmentPipelineStageAUnit', [Map.class], {m ->
   155              stepsCalled.add('abapEnvironmentPipelineStageAUnit')
   156          })
   157  
   158          helper.registerAllowedMethod('abapEnvironmentPipelineStageATC', [Map.class], {m ->
   159              stepsCalled.add('abapEnvironmentPipelineStageATC')
   160          })
   161  
   162          helper.registerAllowedMethod('abapEnvironmentPipelineStagePost', [Map.class], {m ->
   163              stepsCalled.add('abapEnvironmentPipelineStagePost')
   164          })
   165  
   166          helper.registerAllowedMethod('abapEnvironmentPipelineStageBuild', [Map.class], {m ->
   167              stepsCalled.add('abapEnvironmentPipelineStageBuild')
   168          })
   169  
   170          helper.registerAllowedMethod('abapEnvironmentPipelineStageInitialChecks', [Map.class], {m ->
   171              stepsCalled.add('abapEnvironmentPipelineStageInitialChecks')
   172          })
   173  
   174          helper.registerAllowedMethod('abapEnvironmentPipelineStagePublish', [Map.class], {m ->
   175              stepsCalled.add('abapEnvironmentPipelineStagePublish')
   176          })
   177  
   178          helper.registerAllowedMethod('abapEnvironmentPipelineStageConfirm', [Map.class], {m ->
   179              stepsCalled.add('abapEnvironmentPipelineStageConfirm')
   180          })
   181  
   182          helper.registerAllowedMethod('abapEnvironmentPipelineStageIntegrationTests', [Map.class], {m ->
   183              stepsCalled.add('abapEnvironmentPipelineStageIntegrationTests')
   184          })
   185  
   186          nullScript.prepareDefaultValues(script: nullScript)
   187  
   188      }
   189  
   190      @Test
   191      void testAbapEnvironmentPipelineNoPrepareNoCleanup() {
   192  
   193  
   194          nullScript.commonPipelineEnvironment.configuration.runStage = [
   195              'Clone Repositories': true,
   196              'Tests': true,
   197              'ATC': true,
   198              'AUnit': true,
   199          ]
   200  
   201          jsr.step.abapEnvironmentPipeline(script: nullScript)
   202  
   203          assertThat(stepsCalled, hasItems(
   204              'abapEnvironmentPipelineStageInit',
   205              'abapEnvironmentPipelineStageCloneRepositories',
   206              'abapEnvironmentPipelineStageATC',
   207              'abapEnvironmentPipelineStageAUnit',
   208              'abapEnvironmentPipelineStagePost'
   209          ))
   210          assertThat(stepsCalled, not(hasItem('abapEnvironmentPipelineStagePrepareSystem')))
   211      }
   212  
   213      @Test
   214      void testAbapEnvironmentPipelineNoCloneNoRunTests() {
   215  
   216          nullScript.commonPipelineEnvironment.configuration.runStage = [
   217              'Prepare System': true,
   218          ]
   219          jsr.step.abapEnvironmentPipeline(script: nullScript)
   220  
   221          assertThat(stepsCalled, hasItems(
   222              'abapEnvironmentPipelineStageInit',
   223              'abapEnvironmentPipelineStagePrepareSystem',
   224              'abapEnvironmentPipelineStagePost'
   225          ))
   226          assertThat(stepsCalled, not(hasItem('abapEnvironmentPipelineStageCloneRepositories')))
   227          assertThat(stepsCalled, not(hasItem('abapEnvironmentPipelineStageATC')))
   228          assertThat(stepsCalled, not(hasItem('abapEnvironmentPipelineStageAUnit')))
   229      }
   230  
   231      @Test
   232      void testAbapEnvironmentPipelineAllOn() {
   233  
   234          nullScript.commonPipelineEnvironment.configuration.runStage = [
   235              'Prepare System': true,
   236              'Clone Repositories': true,
   237              'Tests': true,
   238              'ATC': true,
   239              'AUnit': true,
   240              'Build': true,
   241              'Integration Tests': true,
   242              'Publish': true
   243          ]
   244          jsr.step.abapEnvironmentPipeline(script: nullScript)
   245  
   246          assertThat(stepsCalled, hasItems(
   247              'abapEnvironmentPipelineStageInit',
   248              'abapEnvironmentPipelineStagePrepareSystem',
   249              'abapEnvironmentPipelineStageCloneRepositories',
   250              'abapEnvironmentPipelineStageATC',
   251              'abapEnvironmentPipelineStageAUnit',
   252              'abapEnvironmentPipelineStagePost',
   253              'abapEnvironmentPipelineStageBuild',
   254              'abapEnvironmentPipelineStageInitialChecks',
   255              'abapEnvironmentPipelineStagePublish',
   256              'abapEnvironmentPipelineStageConfirm',
   257              'abapEnvironmentPipelineStageIntegrationTests'
   258          ))
   259      }
   260  
   261      @Test
   262      void testAbapEnvironmentPipelineATCOnly() {
   263  
   264          nullScript.commonPipelineEnvironment.configuration.runStage = [
   265              'ATC': true,
   266          ]
   267          jsr.step.abapEnvironmentPipeline(script: nullScript)
   268  
   269          assertThat(stepsCalled, hasItems(
   270              'abapEnvironmentPipelineStageInit',
   271              'abapEnvironmentPipelineStageATC',
   272              'abapEnvironmentPipelineStagePost'
   273          ))
   274          assertThat(stepsCalled, not(hasItems(
   275              'abapEnvironmentPipelineStageAUnit'
   276          )))
   277      }
   278      @Test
   279      void testAbapEnvironmentPipelineAunitOnly() {
   280  
   281          nullScript.commonPipelineEnvironment.configuration.runStage = [
   282              'AUnit': true,
   283          ]
   284          jsr.step.abapEnvironmentPipeline(script: nullScript)
   285  
   286          assertThat(stepsCalled, hasItems(
   287              'abapEnvironmentPipelineStageInit',
   288              'abapEnvironmentPipelineStageAUnit',
   289              'abapEnvironmentPipelineStagePost'
   290          ))
   291          assertThat(stepsCalled, not(hasItems(
   292              'abapEnvironmentPipelineStageATC'
   293          )))
   294      }
   295  }