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

     1  package templates
     2  
     3  import org.junit.Before
     4  import org.junit.After
     5  import org.junit.Rule
     6  import org.junit.Test
     7  import org.junit.rules.RuleChain
     8  import util.BasePiperTest
     9  import util.JenkinsReadYamlRule
    10  import util.JenkinsStepRule
    11  import util.PipelineWhenException
    12  import org.junit.rules.ExpectedException
    13  import util.Rules
    14  import util.JenkinsShellCallRule
    15  import com.sap.piper.PiperGoUtils
    16  import com.sap.piper.Utils
    17  
    18  import static org.hamcrest.Matchers.*
    19  import static org.junit.Assert.assertThat
    20  import static org.junit.Assert.assertTrue
    21  
    22  class abapEnvironmentPipelineStageInitTest extends BasePiperTest {
    23      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    24      private JenkinsReadYamlRule readYamlRule = new JenkinsReadYamlRule(this).registerYaml('mta.yaml', defaultsYaml() )
    25      private List stepsCalled = []
    26      private List activeStages = []
    27      private ExpectedException thrown = new ExpectedException()
    28      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    29      private PiperGoUtils piperGoUtils = new PiperGoUtils(nullScript, utils) { void unstashPiperBin() { }}
    30  
    31      @Rule
    32      public RuleChain rules = Rules
    33          .getCommonRules(this)
    34          .around(readYamlRule)
    35          .around(thrown)
    36          .around(jsr)
    37          .around(shellCallRule)
    38  
    39      @After
    40      public void tearDown() {
    41          Utils.metaClass = null
    42      }
    43  
    44      @Before
    45      void init()  {
    46          Utils.metaClass.unstash = { def n -> ["dummy"] }
    47          binding.variables.env.STAGE_NAME = 'Init'
    48  
    49          helper.registerAllowedMethod('deleteDir', [], null)
    50          helper.registerAllowedMethod("writeFile", [Map.class], null)
    51          helper.registerAllowedMethod("readJSON", [Map.class],null)
    52  
    53          helper.registerAllowedMethod('setupCommonPipelineEnvironment', [Map.class], { m ->
    54              stepsCalled.add('setupCommonPipelineEnvironment')
    55          })
    56  
    57          helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
    58              assertThat(m.stageName, is('Init'))
    59              return body()
    60          })
    61  
    62          helper.registerAllowedMethod('checkout', [Closure.class], { c ->
    63              stepsCalled.add('checkout')
    64              return [GIT_BRANCH: 'master', GIT_COMMIT: 'testGitCommitId', GIT_URL: 'https://github.com/testOrg/testRepo']
    65          })
    66          binding.setVariable('scm', {})
    67  
    68          helper.registerAllowedMethod('activateStage', [Map.class, String.class], {p, m ->
    69              stepsCalled('activateStage')
    70              activeStages.add(m)
    71          })
    72          shellCallRule.setReturnValue('[ -x ./piper ]', 1)
    73          shellCallRule.setReturnValue('./piper checkIfStepActive --stageConfig .pipeline/stage_conditions.yaml --useV1 --stageOutputFile .pipeline/stage_out.json --stepOutputFile .pipeline/step_out.json --stage _ --step _', 0)
    74          nullScript.prepareDefaultValues(script: nullScript)
    75      }
    76  
    77      @Test
    78      void testStageConfigurationToggleFalse() {
    79          jsr.step.abapEnvironmentPipelineStageInit(script: nullScript, skipCheckout: false, piperGoUtils: piperGoUtils)
    80          assertThat(stepsCalled, hasItems('setupCommonPipelineEnvironment', 'checkout'))
    81          assertThat(shellCallRule.shell, hasItem('./piper checkIfStepActive --stageConfig .pipeline/stage_conditions.yaml --useV1 --stageOutputFile .pipeline/stage_out.json --stepOutputFile .pipeline/step_out.json --stage _ --step _'))
    82  
    83      }
    84  
    85      @Test
    86      void testSkipCheckoutToggleTrue() {
    87          jsr.step.abapEnvironmentPipelineStageInit(
    88              script: nullScript,
    89              skipCheckout: true,
    90              juStabUtils: utils,
    91              piperGoUtils: piperGoUtils,
    92              stashContent: ['mystash']
    93          )
    94          assertThat(stepsCalled, not(hasItems('checkout')))
    95          assertThat(stepsCalled, hasItems('setupCommonPipelineEnvironment'))
    96          assertThat(shellCallRule.shell, hasItem('./piper checkIfStepActive --stageConfig .pipeline/stage_conditions.yaml --useV1 --stageOutputFile .pipeline/stage_out.json --stepOutputFile .pipeline/step_out.json --stage _ --step _'))
    97  
    98      }
    99  
   100      @Test
   101      void testSkipCheckoutToggleNull() {
   102          jsr.step.abapEnvironmentPipelineStageInit(script: nullScript,  skipCheckout: null, piperGoUtils: piperGoUtils)
   103          assertThat(stepsCalled, hasItems('setupCommonPipelineEnvironment', 'checkout'))
   104          assertThat(shellCallRule.shell, hasItem('./piper checkIfStepActive --stageConfig .pipeline/stage_conditions.yaml --useV1 --stageOutputFile .pipeline/stage_out.json --stepOutputFile .pipeline/step_out.json --stage _ --step _'))
   105      }
   106  
   107      @Test
   108      void testSkipCheckoutToggleString() {
   109          thrown.expectMessage('[abapEnvironmentPipelineStageInit] Parameter skipCheckout has to be of type boolean. Instead got \'java.lang.String\'')
   110          jsr.step.abapEnvironmentPipelineStageInit(script: nullScript,  skipCheckout: 'string')
   111      }
   112  
   113      @Test
   114      void "Try to skip checkout with parameter skipCheckout not boolean throws error"() {
   115          thrown.expectMessage('[abapEnvironmentPipelineStageInit] Parameter skipCheckout has to be of type boolean. Instead got \'java.lang.String\'')
   116  
   117          jsr.step.abapEnvironmentPipelineStageInit(
   118              script: nullScript,
   119              juStabUtils: utils,
   120              skipCheckout: "false",
   121              piperGoUtils: piperGoUtils
   122          )
   123      }
   124  
   125      @Test
   126      void "Try to skip checkout without stashContent parameter throws error"() {
   127          thrown.expectMessage('[abapEnvironmentPipelineStageInit] needs stashes if you skip checkout')
   128  
   129          jsr.step.abapEnvironmentPipelineStageInit(
   130              script: nullScript,
   131              juStabUtils: utils,
   132              skipCheckout: true,
   133              piperGoUtils: piperGoUtils
   134          )
   135      }
   136  
   137      @Test
   138      void "Try to skip checkout with empty stashContent parameter throws error"() {
   139          thrown.expectMessage('[abapEnvironmentPipelineStageInit] needs stashes if you skip checkout')
   140  
   141          jsr.step.abapEnvironmentPipelineStageInit(
   142              script: nullScript,
   143              juStabUtils: utils,
   144              skipCheckout: true,
   145              stashContent: [],
   146              piperGoUtils: piperGoUtils
   147          )
   148      }
   149  
   150      private defaultsYaml() {
   151          return  '''
   152                  stages:
   153                      Init: {}
   154                      Prepare System: {}
   155                      Clone Repositories: {}
   156                      ATC: {}
   157                  '''
   158      }
   159  }