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

     1  package templates
     2  
     3  import com.sap.piper.StageNameProvider
     4  import org.junit.Before
     5  import org.junit.Rule
     6  import org.junit.Test
     7  import org.junit.rules.ExpectedException
     8  import org.junit.rules.RuleChain
     9  import util.*
    10  
    11  import static org.hamcrest.Matchers.hasItem
    12  import static org.hamcrest.Matchers.hasItems
    13  import static org.hamcrest.Matchers.hasKey
    14  import static org.hamcrest.Matchers.is
    15  import static org.hamcrest.Matchers.isEmptyOrNullString
    16  import static org.hamcrest.Matchers.not
    17  import static org.junit.Assert.assertEquals
    18  import static org.junit.Assert.assertThat
    19  import static org.junit.Assert.assertTrue
    20  
    21  class PiperPipelineStageInitTest extends BasePiperTest {
    22      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    23      private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
    24      private JenkinsReadYamlRule jryr = new JenkinsReadYamlRule(this)
    25      private JenkinsReadMavenPomRule jrmpr = new JenkinsReadMavenPomRule(this, null)
    26      private ExpectedException thrown = new ExpectedException()
    27  
    28      @Rule
    29      public RuleChain rules = Rules
    30          .getCommonRules(this)
    31          .around(jryr)
    32          .around(jrmpr)
    33          .around(thrown)
    34          .around(jlr)
    35          .around(jsr)
    36  
    37      private List stepsCalled = []
    38      private Map  stepParams = [:]
    39  
    40      @Before
    41      void init() {
    42          StageNameProvider.instance.useTechnicalStageNames = false
    43  
    44          nullScript.env.STAGE_NAME = 'Init'
    45  
    46          nullScript.commonPipelineEnvironment.configuration = [:]
    47  
    48          helper.registerAllowedMethod("findFiles", [Map.class], { map ->
    49              switch (map.glob) {
    50                  case 'mta.yaml':
    51                  case 'path/mta.yaml':
    52                  case 'pathFromStep/mta.yaml':
    53                  case 'pathFromStage/mta.yaml':
    54                  case 'pom.xml':
    55                  case 'path/pom.xml':
    56                  case 'pathFromStep/pom.xml':
    57                  case 'pathFromStage/pom.xml':
    58                      return [new File(map.glob)].toArray()
    59                  default:
    60                      return [].toArray()
    61              }
    62          })
    63  
    64          helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], { m, body ->
    65              assertThat(m.stageName, is('Init'))
    66              return body()
    67          })
    68  
    69          helper.registerAllowedMethod('checkout', [Closure.class], { c ->
    70              stepsCalled.add('checkout')
    71              return [GIT_BRANCH: 'master', GIT_COMMIT: 'testGitCommitId', GIT_URL: 'https://github.com/testOrg/testRepo']
    72          })
    73          binding.setVariable('scm', {})
    74  
    75          helper.registerAllowedMethod('setupCommonPipelineEnvironment', [Map.class], { m ->
    76              stepsCalled.add('setupCommonPipelineEnvironment')
    77              stepParams['setupCommonPipelineEnvironment'] = m
    78          })
    79  
    80          helper.registerAllowedMethod('piperInitRunStageConfiguration', [Map.class], { m ->
    81              assertThat(m.stageConfigResource, not(isEmptyOrNullString()))
    82              stepsCalled.add('piperInitRunStageConfiguration')
    83          })
    84  
    85          helper.registerAllowedMethod('artifactSetVersion', [Map.class], { m ->
    86              stepsCalled.add('artifactSetVersion')
    87          })
    88  
    89          helper.registerAllowedMethod('artifactPrepareVersion', [Map.class], { m ->
    90              stepsCalled.add('artifactPrepareVersion')
    91          })
    92  
    93          helper.registerAllowedMethod('pipelineStashFilesBeforeBuild', [Map.class], { m ->
    94              stepsCalled.add('pipelineStashFilesBeforeBuild')
    95          })
    96  
    97          helper.registerAllowedMethod('slackSendNotification', [Map.class], {m ->
    98              stepsCalled.add('slackSendNotification')
    99          })
   100  
   101          helper.registerAllowedMethod('transportRequestReqIDFromGit', [Map.class], {m ->
   102              stepsCalled.add('transportRequestReqIDFromGit')
   103          })
   104      }
   105  
   106      @Test
   107      void testInitNoBuildTool() {
   108  
   109          thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR buildTool')
   110          jsr.step.piperPipelineStageInit(
   111              script: nullScript,
   112              juStabUtils: utils,
   113              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml'
   114          )
   115  
   116      }
   117  
   118      @Test
   119      void testInitBuildToolDoesNotMatchProject() {
   120  
   121          thrown.expectMessage('[piperPipelineStageInit] buildTool configuration \'npm\' does not fit to your project (buildDescriptorPattern: \'package.json\'), please set buildTool as general setting in your .pipeline/config.yml correctly, see also https://sap.github.io/jenkins-library/configuration/')
   122          jsr.step.piperPipelineStageInit(
   123              script: nullScript,
   124              juStabUtils: utils,
   125              buildTool: 'npm',
   126              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml'
   127          )
   128  
   129      }
   130      
   131      @Test
   132      void testInitMtaBuildToolDoesNotThrowException() {
   133  
   134          jsr.step.piperPipelineStageInit(
   135              script: nullScript,
   136              juStabUtils: utils,
   137              buildTool: 'mta',
   138              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml'
   139          )
   140          assertThat(stepsCalled, hasItems('checkout', 'setupCommonPipelineEnvironment', 'piperInitRunStageConfiguration', 'artifactPrepareVersion', 'pipelineStashFilesBeforeBuild'))
   141      }
   142  
   143      @Test
   144      void testInitDefault() {
   145          jsr.step.piperPipelineStageInit(
   146              script: nullScript,
   147              juStabUtils: utils,
   148              buildTool: 'maven',
   149              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml'
   150          )
   151  
   152          assertThat(stepsCalled, hasItems('checkout', 'setupCommonPipelineEnvironment', 'piperInitRunStageConfiguration', 'artifactPrepareVersion', 'pipelineStashFilesBeforeBuild'))
   153          assertThat(stepsCalled, not(hasItems('slackSendNotification')))
   154          assertThat(nullScript.commonPipelineEnvironment.configuration.stageStashes.Init.unstash, is([]))
   155      }
   156  
   157      @Test
   158      void testTransportRequestReqIDFromGitIfFalse() {
   159          jsr.step.piperPipelineStageInit(
   160              script: nullScript,
   161              juStabUtils: utils,
   162              buildTool: 'maven',
   163              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   164              transportRequestReqIDFromGit: false
   165          )
   166          assertThat(stepsCalled, not(hasItem('transportRequestReqIDFromGit')))
   167      }
   168  
   169      @Test
   170      void testTransportRequestReqIDFromGitIfTrue() {
   171          jsr.step.piperPipelineStageInit(
   172              script: nullScript,
   173              juStabUtils: utils,
   174              buildTool: 'maven',
   175              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   176              transportRequestReqIDFromGit: true
   177          )
   178          assertThat(stepsCalled, hasItem( 'transportRequestReqIDFromGit'))
   179      }
   180  
   181      @Test
   182      void testCustomStashSettings() {
   183          jryr.registerYaml('customStashSettings.yml',"Init: \n  unstash: source")
   184  
   185          jsr.step.piperPipelineStageInit(
   186              script: nullScript,
   187              juStabUtils: utils,
   188              buildTool: 'maven',
   189              customStashSettings: 'customStashSettings.yml',
   190              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml'
   191          )
   192  
   193          assertThat(stepsCalled, hasItems('checkout', 'setupCommonPipelineEnvironment', 'piperInitRunStageConfiguration', 'artifactPrepareVersion', 'pipelineStashFilesBeforeBuild'))
   194          assertThat(stepsCalled, not(hasItems('slackSendNotification')))
   195          assertThat(nullScript.commonPipelineEnvironment.configuration.stageStashes.Init.unstash, is("source"))
   196      }
   197  
   198      @Test
   199      void testInitNotOnProductiveBranch() {
   200  
   201          binding.variables.env.BRANCH_NAME = 'testBranch'
   202  
   203          jsr.step.piperPipelineStageInit(
   204              script: nullScript,
   205              juStabUtils: utils,
   206              buildTool: 'maven',
   207              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml'
   208          )
   209  
   210          assertThat(stepsCalled, hasItems('checkout', 'setupCommonPipelineEnvironment', 'piperInitRunStageConfiguration', 'pipelineStashFilesBeforeBuild'))
   211          assertThat(stepsCalled, not(hasItems('artifactSetVersion')))
   212  
   213      }
   214  
   215      @Test
   216      void testPullRequestStageStepActivation() {
   217  
   218          nullScript.commonPipelineEnvironment.configuration = [
   219              runStep: [:]
   220          ]
   221          def config = [
   222              pullRequestStageName: 'Pull-Request Voting',
   223              stepMappings        : [
   224                  karma      : 'karmaExecuteTests',
   225                  whitesource: 'whitesourceExecuteScan'
   226              ],
   227              labelPrefix         : 'pr_'
   228          ]
   229  
   230          def actions = ['karma', 'pr_whitesource']
   231          jsr.step.piperPipelineStageInit.setPullRequestStageStepActivation(nullScript, config, actions)
   232  
   233          assertThat(nullScript.commonPipelineEnvironment.configuration.runStep."Pull-Request Voting".karmaExecuteTests, is(true))
   234          assertThat(nullScript.commonPipelineEnvironment.configuration.runStep."Pull-Request Voting".whitesourceExecuteScan, is(true))
   235      }
   236  
   237      @Test
   238      void testInitWithSlackNotification() {
   239          nullScript.commonPipelineEnvironment.configuration = [runStep: [Init: [slackSendNotification: true]]]
   240  
   241          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, buildTool: 'maven')
   242  
   243          assertThat(stepsCalled, hasItems(
   244              'checkout',
   245              'setupCommonPipelineEnvironment',
   246              'piperInitRunStageConfiguration',
   247              'artifactPrepareVersion',
   248              'slackSendNotification',
   249              'pipelineStashFilesBeforeBuild'
   250          ))
   251      }
   252  
   253      @Test
   254      void testInferBuildToolDescMta() {
   255          assertEquals('mta.yaml', jsr.step.piperPipelineStageInit.inferBuildToolDesc(nullScript, "mta"))
   256      }
   257  
   258      @Test
   259      void testInferBuildToolDescMaven() {
   260          assertEquals('pom.xml', jsr.step.piperPipelineStageInit.inferBuildToolDesc(nullScript, "maven"))
   261      }
   262  
   263      @Test
   264      void testInferBuildToolDescNpm() {
   265          assertEquals('package.json', jsr.step.piperPipelineStageInit.inferBuildToolDesc(nullScript, "npm"))
   266      }
   267  
   268      @Test
   269      void testInferBuildToolDescMtaSource() {
   270          nullScript.commonPipelineEnvironment.configuration = [general: [buildTool: 'mta'], steps : [mtaBuild: [source: 'pathFromStep']]]
   271  
   272          helper.registerAllowedMethod('artifactPrepareVersion', [Map.class, Closure.class], { m, body ->
   273              assertThat(m.filePath, is('pathFromStep/mta.yaml'))
   274              return body()
   275          })
   276  
   277          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
   278  
   279      }
   280  
   281      @Test
   282      void testInferBuildToolDescMtaSourceStage() {
   283          nullScript.commonPipelineEnvironment.configuration = [general: [buildTool: 'mta'], stages: [Build: [source: 'pathFromStage']], steps : [mtaBuild: [source: 'pathFromStep']]]
   284  
   285          helper.registerAllowedMethod('artifactPrepareVersion', [Map.class, Closure.class], { m, body ->
   286              assertThat(m.filePath, is('pathFromStage/mta.yaml'))
   287              return body()
   288          })
   289  
   290          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
   291      }
   292  
   293      @Test
   294      void testInferBuildToolDescMavenSource() {
   295          nullScript.commonPipelineEnvironment.configuration = [general: [buildTool: 'maven'], steps : [mavenBuild: [pomPath: 'pathFromStep/pom.xml']]]
   296  
   297          helper.registerAllowedMethod('artifactPrepareVersion', [Map.class, Closure.class], { m, body ->
   298              assertThat(m.filePath, is('pathFromStep/pom.xml'))
   299              return body()
   300          })
   301  
   302          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
   303      }
   304  
   305      @Test
   306      void testInferBuildToolDescMavenSourceStage() {
   307          nullScript.commonPipelineEnvironment.configuration = [general: [buildTool: 'maven'], stages: [Build: [pomPath: 'pathFromStage/pom.xml']], steps : [mavenBuild: [pomPath: 'pathFromStep/pom.xml']]]
   308  
   309          helper.registerAllowedMethod('artifactPrepareVersion', [Map.class, Closure.class], { m, body ->
   310              assertThat(m.filePath, is('pathFromStage/pom.xml'))
   311              return body()
   312          })
   313  
   314          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
   315      }
   316  
   317      @Test
   318      void testInferBuildToolDescUnknown() {
   319          assertEquals(null, jsr.step.piperPipelineStageInit.inferBuildToolDesc(nullScript, "unknown"))
   320      }
   321  
   322      @Test
   323      void testInferBuildToolDescNull() {
   324          assertEquals(null, jsr.step.piperPipelineStageInit.inferBuildToolDesc(nullScript, null))
   325      }
   326  
   327      @Test
   328      void testInitInferBuildTool() {
   329          nullScript.commonPipelineEnvironment.configuration = [general: [inferBuildTool: true]]
   330          nullScript.commonPipelineEnvironment.buildTool = 'maven'
   331  
   332          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
   333  
   334          assertThat(stepsCalled, hasItems(
   335              'checkout',
   336              'setupCommonPipelineEnvironment',
   337              'piperInitRunStageConfiguration',
   338              'artifactPrepareVersion',
   339              'pipelineStashFilesBeforeBuild'
   340          ))
   341      }
   342  
   343      @Test
   344      void testInferProjectNameFromMta() {
   345          jryr.registerYaml('mta.yaml','ID: "fromMtaYaml"')
   346          assertEquals('fromMtaYaml', jsr.step.piperPipelineStageInit.inferProjectName(nullScript, "mta", "mta.yaml"))
   347      }
   348  
   349      @Test
   350      void testInferProjectNameFromMtaSource() {
   351          nullScript.commonPipelineEnvironment.configuration = [general: [buildTool: 'mta', inferProjectName: true], steps : [mtaBuild: [source: 'path']]]
   352  
   353          jryr.registerYaml('path/mta.yaml','ID: "fromPathMtaYaml"')
   354          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils)
   355          assertEquals('fromPathMtaYaml', nullScript.commonPipelineEnvironment.projectName)
   356      }
   357  
   358      @Test
   359      void testInferProjectNameFromMavenPath() {
   360          jrmpr.registerPom('path/pom.xml',
   361              '<project>'
   362                  + '<groupId>gidFromPathPom</groupId>'
   363                  + '<artifactId>aidFromPathPom</artifactId>'
   364              + '</project>'
   365          )
   366  
   367          assertEquals('gidFromPathPom-aidFromPathPom', jsr.step.piperPipelineStageInit.inferProjectName(nullScript, "maven", "path/pom.xml"))
   368      }
   369  
   370      @Test
   371      void testInitWithTechnicalStageNames() {
   372          helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], { m, body ->
   373              assertThat(m.stageName, is('init'))
   374              return body()
   375          })
   376  
   377          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, useTechnicalStageNames: true, buildTool: 'maven')
   378  
   379          assertThat(stepsCalled, hasItems(
   380              'checkout',
   381              'setupCommonPipelineEnvironment',
   382              'piperInitRunStageConfiguration',
   383              'artifactPrepareVersion',
   384              'pipelineStashFilesBeforeBuild'
   385          ))
   386      }
   387  
   388      @Test
   389      void testInitForwardConfigParams() {
   390          jsr.step.piperPipelineStageInit(script: nullScript, juStabUtils: utils, configFile: 'my-config.yml',
   391              customDefaults: ['my-custom-defaults.yml'], customDefaultsFromFiles: ['my-custom-default-file.yml'],
   392              buildTool: 'maven')
   393  
   394          assertThat(stepsCalled, hasItems('setupCommonPipelineEnvironment'))
   395          assertThat(stepParams.setupCommonPipelineEnvironment?.configFile, is('my-config.yml'))
   396          assertThat(stepParams.setupCommonPipelineEnvironment?.customDefaults, is(['my-custom-defaults.yml']))
   397          assertThat(stepParams.setupCommonPipelineEnvironment?.customDefaultsFromFiles, is(['my-custom-default-file.yml']))
   398      }
   399  
   400      @Test
   401      void "Parameter skipCheckout skips the checkout call"() {
   402          jsr.step.piperPipelineStageInit(
   403              script: nullScript,
   404              juStabUtils: utils,
   405              buildTool: 'maven',
   406              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   407              skipCheckout: true,
   408              stashContent: ['mystash'],
   409              scmInfo: ["dummyScmKey":"dummyScmKey"]
   410          )
   411  
   412          assertThat(stepsCalled, hasItems('setupCommonPipelineEnvironment', 'piperInitRunStageConfiguration', 'artifactPrepareVersion', 'pipelineStashFilesBeforeBuild'))
   413          assertThat(stepsCalled, not(hasItem('checkout')))
   414      }
   415  
   416      @Test
   417      void "Try to skip checkout with parameter skipCheckout not boolean throws error"() {
   418          thrown.expectMessage('[piperPipelineStageInit] Parameter skipCheckout has to be of type boolean. Instead got \'java.lang.String\'')
   419  
   420          jsr.step.piperPipelineStageInit(
   421              script: nullScript,
   422              juStabUtils: utils,
   423              buildTool: 'maven',
   424              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   425              skipCheckout: "false"
   426          )
   427      }
   428  
   429      @Test
   430      void "Try to skip checkout without scmInfo parameter throws error"() {
   431          thrown.expectMessage('[piperPipelineStageInit] Need am scmInfo map retrieved from a checkout. ' +
   432              'If you want to skip the checkout the scm info needs to be provided by you with parameter scmInfo, ' +
   433              'for example as follows:\n' +
   434              '  def scmInfo = checkout scm\n' +
   435              '  piperPipelineStageInit script:this, skipCheckout: true, scmInfo: scmInfo')
   436  
   437          jsr.step.piperPipelineStageInit(
   438              script: nullScript,
   439              juStabUtils: utils,
   440              buildTool: 'maven',
   441              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   442              stashContent: ['mystash'],
   443              skipCheckout: true
   444          )
   445      }
   446  
   447      @Test
   448      void "Try to skip checkout without stashContent parameter throws error"() {
   449          thrown.expectMessage('[piperPipelineStageInit] needs stashes if you skip checkout')
   450  
   451          jsr.step.piperPipelineStageInit(
   452              script: nullScript,
   453              juStabUtils: utils,
   454              buildTool: 'maven',
   455              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   456              skipCheckout: true,
   457              scmInfo: ["dummyScmKey":"dummyScmKey"]
   458          )
   459      }
   460  
   461      @Test
   462      void "Try to skip checkout with empty stashContent parameter throws error"() {
   463          thrown.expectMessage('[piperPipelineStageInit] needs stashes if you skip checkout')
   464  
   465          jsr.step.piperPipelineStageInit(
   466              script: nullScript,
   467              juStabUtils: utils,
   468              buildTool: 'maven',
   469              stashSettings: 'com.sap.piper/pipeline/stashSettings.yml',
   470              skipCheckout: true,
   471              stashContent: [],
   472              scmInfo: ["dummyScmKey":"dummyScmKey"]
   473          )
   474      }
   475  }