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

     1  import static org.hamcrest.Matchers.*
     2  import static org.junit.Assert.assertThat
     3  
     4  import org.junit.After
     5  import org.junit.Before
     6  import org.junit.Rule
     7  import org.junit.Test
     8  import org.junit.rules.ExpectedException
     9  import org.junit.rules.RuleChain
    10  
    11  import com.sap.piper.Utils
    12  import com.sap.piper.cm.ChangeManagement
    13  import com.sap.piper.cm.ChangeManagementException
    14  
    15  import hudson.AbortException
    16  import util.BasePiperTest
    17  import util.JenkinsCredentialsRule
    18  import util.JenkinsLoggingRule
    19  import util.JenkinsReadYamlRule
    20  import util.JenkinsStepRule
    21  import util.Rules
    22  
    23  public class TransportRequestUploadFileTest extends BasePiperTest {
    24  
    25      private ExpectedException thrown = new ExpectedException()
    26      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    27      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    28  
    29      @Rule
    30      public RuleChain ruleChain = Rules.getCommonRules(this)
    31          .around(thrown)
    32          .around(new JenkinsReadYamlRule(this))
    33          .around(stepRule)
    34          .around(loggingRule)
    35          .around(new JenkinsCredentialsRule(this)
    36              .withCredentials('CM', 'anonymous', '********'))
    37  
    38      private Map cmUtilReceivedParams = [:]
    39  
    40      @Before
    41      public void setup() {
    42  
    43          cmUtilReceivedParams.clear()
    44  
    45          nullScript.commonPipelineEnvironment.configuration = [general:
    46                                       [changeManagement:
    47                                           [
    48                                            credentialsId: 'CM',
    49                                            type: 'SOLMAN',
    50                                            endpoint: 'https://example.org/cm'
    51                                           ]
    52                                       ]
    53                                   ]
    54          Utils.metaClass.echo = { def m -> }
    55      }
    56  
    57      @After
    58      public void tearDown() {
    59          Utils.metaClass = null
    60      }
    61  
    62      @Test
    63      public void changeDocumentIdNotProvidedSOLMANTest() {
    64  
    65          def calledWithParameters = null
    66  
    67          helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List],
    68              {
    69                  params, stepName, metaData, creds ->
    70                      if(stepName.equals("transportRequestDocIDFromGit")) {
    71                          calledWithParameters = params
    72                      }
    73              }
    74          )
    75  
    76          thrown.expect(IllegalArgumentException)
    77          thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' provided to the step call or via commit history).")
    78  
    79          stepRule.step.transportRequestUploadFile(script: nullScript, transportRequestId: '001', applicationId: 'app', filePath: '/path', 
    80              changeManagement: [
    81                  type: 'SOLMAN',
    82                  endpoint: 'https://example.org/cm',
    83                  clientOpts: '--client opts'
    84              ],
    85              credentialsId: 'CM'
    86          )
    87  
    88          assert calledWithParameters != null
    89      }
    90  
    91      @Test
    92      public void transportRequestIdNotProvidedTest() {
    93  
    94          def calledWithParameters = null
    95  
    96          helper.registerAllowedMethod( 'piperExecuteBin', [Map, String, String, List],
    97              {
    98                  params, stepName, metaData, creds ->
    99                      if(stepName.equals("transportRequestReqIDFromGit")) {
   100                          calledWithParameters = params
   101                      }
   102              }
   103          )
   104  
   105          thrown.expect(IllegalArgumentException)
   106          thrown.expectMessage("Transport request id not provided (parameter: 'transportRequestId' provided to the step call or via commit history).")
   107  
   108          stepRule.step.transportRequestUploadFile(script: nullScript, changeDocumentId: '001', applicationId: 'app', filePath: '/path', 
   109              changeManagement: [
   110                  type: 'SOLMAN',
   111                  endpoint: 'https://example.org/cm',
   112                  clientOpts: '--client opts'
   113              ],
   114              credentialsId: 'CM'
   115          )
   116  
   117          assert calledWithParameters != null
   118      }
   119  
   120      @Test
   121      public void applicationIdNotProvidedSOLMANTest() {
   122  
   123          // we expect the failure only for SOLMAN (which is the default).
   124          // Use case for CTS without applicationId is checked by the
   125          // straight forward test case for CTS
   126  
   127          thrown.expect(IllegalArgumentException)
   128          thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR applicationId")
   129  
   130          stepRule.step.transportRequestUploadFile(script: nullScript, changeDocumentId: '001', transportRequestId: '001', filePath: '/path')
   131      }
   132  
   133      @Test
   134      public void filePathNotProvidedTest() {
   135  
   136          thrown.expect(IllegalArgumentException)
   137          thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR filePath")
   138  
   139          stepRule.step.transportRequestUploadFile(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app')
   140      }
   141  
   142      @Test
   143      public void uploadFileToTransportRequestSOLMANFailureTest() {
   144  
   145          thrown.expect(AbortException)
   146          thrown.expectMessage("Exception message")
   147  
   148          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   149                  throw new AbortException('Exception message')
   150              }
   151          )
   152  
   153          stepRule.step.transportRequestUploadFile(script: nullScript,
   154                        changeDocumentId: '001',
   155                        transportRequestId: '001',
   156                        applicationId: 'app',
   157                        filePath: '/path',
   158                        changeManagement: [
   159                            type: 'SOLMAN',
   160                            endpoint: 'https://example.org/cm',
   161                            clientOpts: '--client opts'
   162                        ],
   163                        credentialsId: 'CM'
   164                    )
   165      }
   166  
   167      @Test
   168      public void uploadFileToTransportRequestCTSSuccessTest() {
   169  
   170          loggingRule.expect("[INFO] Uploading application 'myApp' to transport request '002'.")
   171          loggingRule.expect("[INFO] Application 'myApp' has been successfully uploaded to transport request '002'.")
   172  
   173          def calledWithParameters,
   174              calledWithStepName,
   175              calledWithMetadata,
   176              calledWithCredentials
   177  
   178          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   179              params, stepName, metaData, creds ->
   180                  calledWithParameters = params
   181                  calledWithStepName = stepName
   182                  calledWithMetadata = metaData
   183                  calledWithCredentials = creds
   184              })
   185  
   186          stepRule.step.transportRequestUploadFile(script: nullScript,
   187                        changeManagement: [
   188                            type: 'CTS',
   189                            client: '001',
   190                            cts: [
   191                                osDeployUser: 'node2',
   192                                deployToolDependencies: ['@ui5/cli', '@sap/ux-ui5-tooling', '@ui5/logger', '@ui5/fs', '@dummy/foo'],
   193                                npmInstallOpts: ['--verbose'],
   194                            ]
   195                        ],
   196                        applicationName: 'myApp',
   197                        applicationDescription: 'the description',
   198                        abapPackage: 'myPackage',
   199                        transportRequestId: '002',
   200                        credentialsId: 'CM')
   201  
   202          assertThat(calledWithStepName, is('transportRequestUploadCTS'))
   203          assertThat(calledWithParameters.transportRequestId, is('002'))
   204          assertThat(calledWithParameters.endpoint, is('https://example.org/cm'))
   205          assertThat(calledWithParameters.client, is('001'))
   206          assertThat(calledWithParameters.applicationName, is('myApp'))
   207          assertThat(calledWithParameters.description, is('the description'))
   208          assertThat(calledWithParameters.abapPackage, is('myPackage'))
   209          assertThat(calledWithParameters.osDeployUser, is('node2'))
   210          assertThat(calledWithParameters.deployToolDependencies, is(['@ui5/cli', '@sap/ux-ui5-tooling', '@ui5/logger', '@ui5/fs', '@dummy/foo']))
   211          assertThat(calledWithParameters.npmInstallOpts, is(['--verbose']))
   212          assertThat(calledWithParameters.deployConfigFile, is('ui5-deploy.yaml'))
   213          assertThat(calledWithParameters.uploadCredentialsId, is('CM'))
   214      }
   215  
   216      @Test
   217      public void uploadFileToTransportRequestCTSDockerParams() {
   218  
   219          loggingRule.expect("[INFO] Uploading application 'myApp' to transport request '002'.")
   220          loggingRule.expect("[INFO] Application 'myApp' has been successfully uploaded to transport request '002'.")
   221  
   222          def calledWithParameters
   223  
   224          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   225              params, stepName, metaData, creds ->
   226                  calledWithParameters = params
   227              })
   228  
   229          stepRule.step.transportRequestUploadFile(script: nullScript,
   230                        changeManagement: [
   231                            type: 'CTS',
   232                            client: '001',
   233                            cts: [
   234                                osDeployUser: 'node2',
   235                                deployToolDependencies: ['@ui5/cli', '@sap/ux-ui5-tooling', '@ui5/logger', '@ui5/fs', '@dummy/foo'],
   236                                npmInstallOpts: ['--verbose'],
   237                                nodeDocker: [
   238                                     image: 'ctsImage',
   239                                     options: ['-o1', 'opt1', '-o2', 'opt2'],
   240                                     envVars: [env1: 'env1', env2: 'env2'],
   241                                     pullImage: false,
   242                                ],
   243                            ]
   244                        ],
   245                        applicationName: 'myApp',
   246                        applicationDescription: 'the description',
   247                        abapPackage: 'myPackage',
   248                        transportRequestId: '002',
   249                        credentialsId: 'CM')
   250  
   251          assertThat(calledWithParameters.dockerImage, is('ctsImage'))
   252          assertThat(calledWithParameters.dockerOptions, is(['-o1', 'opt1', '-o2', 'opt2']))
   253          assertThat(calledWithParameters.dockerEnvVars, is([env1: 'env1', env2: 'env2']))
   254          assertThat(calledWithParameters.dockerPullImage, is(false))
   255      }
   256  
   257      @Test
   258      public void uploadFileToTransportRequestRFCSanityChecksTest() {
   259  
   260          thrown.expect(IllegalArgumentException)
   261          thrown.expectMessage(allOf(
   262              containsString('NO VALUE AVAILABLE FOR'),
   263              containsString('applicationUrl'),
   264              containsString('developmentInstance'),
   265              containsString('developmentClient'),
   266              containsString('applicationDescription'),
   267              containsString('abapPackage'),
   268              containsString('applicationName')))
   269  
   270          stepRule.step.transportRequestUploadFile(script: nullScript,
   271                   transportRequestId: '123456', //no sanity check, can be read from git history
   272                   changeManagement: [type: 'RFC'],
   273          )
   274      }
   275  
   276      @Test
   277      public void uploadFileToTransportRequestRFCSuccessTest() {
   278  
   279          def calledWithParameters,
   280              calledWithStepName,
   281              calledWithMetadata,
   282              calledWithCredentials
   283  
   284          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   285              params, stepName, metaData, creds ->
   286                  calledWithParameters = params
   287                  calledWithStepName = stepName
   288                  calledWithMetadata = metaData
   289                  calledWithCredentials = creds
   290              }
   291          )
   292  
   293          nullScript.commonPipelineEnvironment.configuration =
   294          [general:
   295              [changeManagement:
   296                  [
   297                   endpoint: 'https://example.org/rfc'
   298                  ]
   299              ]
   300          ]
   301  
   302          stepRule.step.transportRequestUploadFile(script: nullScript,
   303                   applicationUrl: 'http://example.org/blobstore/xyz.zip',
   304                   codePage: 'UTF-9',
   305                   acceptUnixStyleLineEndings: true,
   306                   transportRequestId: '123456',
   307                   changeManagement: [
   308                       type: 'RFC',
   309                       rfc: [
   310                           developmentClient: '002',
   311                           developmentInstance: '001'
   312                       ]
   313                   ],
   314                   applicationName: '42',
   315                   applicationDescription: 'Lorem ipsum',
   316                   abapPackage: 'XYZ',
   317                   credentialsId: 'CM'
   318          )
   319  
   320          assertThat(calledWithStepName, is('transportRequestUploadRFC'))
   321          assertThat(calledWithParameters.applicationName, is('42'))
   322          assertThat(calledWithParameters.applicationUrl, is('http://example.org/blobstore/xyz.zip'))
   323          assertThat(calledWithParameters.endpoint, is('https://example.org/rfc'))
   324          assertThat(calledWithParameters.uploadCredentialsId, is('CM'))
   325          assertThat(calledWithParameters.instance, is('001'))
   326          assertThat(calledWithParameters.client, is('002'))
   327          assertThat(calledWithParameters.applicationDescription, is('Lorem ipsum'))
   328          assertThat(calledWithParameters.abapPackage, is('XYZ'))
   329          assertThat(calledWithParameters.codePage, is('UTF-9'))
   330          assertThat(calledWithParameters.acceptUnixStyleLineEndings, is(true))
   331          assertThat(calledWithParameters.failUploadOnWarning, is(true))
   332          assertThat(calledWithParameters.transportRequestId, is('123456'))
   333      }
   334  
   335      @Test
   336      public void uploadFileToTransportRequestRFCUploadFailsTest() {
   337  
   338          thrown.expect(AbortException)
   339          thrown.expectMessage('upload failed')
   340  
   341          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   342                  throw new AbortException('upload failed')
   343              }
   344          )
   345  
   346          stepRule.step.transportRequestUploadFile(script: nullScript,
   347                   applicationUrl: 'http://example.org/blobstore/xyz.zip',
   348                   codePage: 'UTF-9',
   349                   acceptUnixStyleLineEndings: true,
   350                   transportRequestId: '123456',
   351                   changeManagement: [
   352                       type: 'RFC',
   353                       rfc: [
   354                           docker: [
   355                               image: 'rfc',
   356                               options: [],
   357                               envVars: [:],
   358                               pullImage: false,
   359                           ],
   360                           developmentClient: '002',
   361                           developmentInstance: '001',
   362                           ]
   363                       ],
   364                   applicationName: '42',
   365                   applicationDescription: 'Lorem ipsum',
   366                   abapPackage: 'XYZ',
   367                   credentialsId: 'CM'
   368          )
   369      }
   370  
   371      @Test
   372      public void uploadFileToTransportRequestRFCDockerParams() {
   373  
   374          def calledWithParameters
   375  
   376          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   377              params, stepName, metaData, creds ->
   378                  calledWithParameters = params
   379              }
   380          )
   381  
   382          stepRule.step.transportRequestUploadFile(script: nullScript,
   383                   applicationUrl: 'http://example.org/blobstore/xyz.zip',
   384                   codePage: 'UTF-9',
   385                   acceptUnixStyleLineEndings: true,
   386                   transportRequestId: '123456',
   387                   changeManagement: [
   388                       type: 'RFC',
   389                       endpoint: 'https://example.org/cm',
   390                       rfc: [
   391                           developmentClient: '002',
   392                           developmentInstance: '001',
   393                           docker: [
   394                                image: 'rfcImage',
   395                                options: ['-o1', 'opt1', '-o2', 'opt2'],
   396                                envVars: [env1: 'env1', env2: 'env2'],
   397                                pullImage: false,
   398                           ],
   399                       ]
   400                   ],
   401                   applicationName: '42',
   402                   applicationDescription: 'Lorem ipsum',
   403                   abapPackage: 'XYZ',
   404                   credentialsId: 'CM'
   405          )
   406  
   407          assertThat(calledWithParameters.dockerImage, is('rfcImage'))
   408          assertThat(calledWithParameters.dockerOptions, is(['-o1', 'opt1', '-o2', 'opt2']))
   409          assertThat(calledWithParameters.dockerEnvVars, is([env1: 'env1', env2: 'env2']))
   410          assertThat(calledWithParameters.dockerPullImage, is(false))
   411      }
   412  
   413      @Test
   414      public void uploadFileToTransportRequestSOLMANSuccessTest() {
   415  
   416          loggingRule.expect("[INFO] Uploading file '/path' to transport request '002' of change document '001'.")
   417          loggingRule.expect("[INFO] File '/path' has been successfully uploaded to transport request '002' of change document '001'.")
   418  
   419          def calledWithParameters,
   420              calledWithStepName,
   421              calledWithMetadata,
   422              calledWithCredentials
   423  
   424          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   425              params, stepName, metaData, creds ->
   426                  calledWithParameters = params
   427                  calledWithStepName = stepName
   428                  calledWithMetadata = metaData
   429                  calledWithCredentials = creds
   430              }
   431          )
   432  
   433          stepRule.step.transportRequestUploadFile(script: nullScript,
   434                        changeDocumentId: '001',
   435                        transportRequestId: '002',
   436                        applicationId: 'app',
   437                        filePath: '/path',
   438                        changeManagement: [
   439                            type: 'SOLMAN',
   440                            endpoint: 'https://example.org/cm',
   441                            clientOpts: '--client opts'
   442                        ],
   443                        credentialsId: 'CM'
   444          )
   445  
   446          assertThat(calledWithStepName, is('transportRequestUploadSOLMAN'))
   447          assertThat(calledWithParameters.changeDocumentId, is('001'))
   448          assertThat(calledWithParameters.transportRequestId, is('002'))
   449          assertThat(calledWithParameters.applicationId, is('app'))
   450          assertThat(calledWithParameters.filePath, is('/path'))
   451          assertThat(calledWithParameters.endpoint, is('https://example.org/cm'))
   452          assertThat(calledWithParameters.cmClientOpts, is('--client opts'))
   453          assertThat(calledWithParameters.uploadCredentialsId, is('CM'))
   454      }
   455  
   456      @Test
   457      public void uploadFileToTransportRequestSOLMANSuccessApplicationIdFromConfigurationTest() {
   458  
   459          def calledWithParameters,
   460              calledWithStepName,
   461              calledWithMetadata,
   462              calledWithCredentials
   463  
   464          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   465              params, stepName, metaData, creds ->
   466                  calledWithParameters = params
   467                  calledWithStepName = stepName
   468                  calledWithMetadata = metaData
   469                  calledWithCredentials = creds
   470              }
   471          )
   472  
   473          nullScript.commonPipelineEnvironment.configuration.put(['steps',
   474                                                                     [transportRequestUploadFile:
   475                                                                         [applicationId: 'AppIdfromConfig']]])
   476  
   477          stepRule.step.transportRequestUploadFile(
   478                        script: nullScript,
   479                        changeDocumentId: '001',
   480                        transportRequestId: '002',
   481                        filePath: '/path',
   482                        changeManagement: [
   483                            type: 'SOLMAN',
   484                            endpoint: 'https://example.org/cm',
   485                            clientOpts: '--client opts'
   486                        ],
   487                        credentialsId: 'CM'
   488          )
   489  
   490          assertThat(calledWithParameters.applicationId, is('AppIdfromConfig'))
   491      }
   492  
   493      @Test
   494      public void uploadFileToTransportRequestSOLMANFilePathFromParameters() {
   495  
   496          // this one is not used when file path is provided via signature
   497          nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
   498  
   499          def calledWithParameters,
   500              calledWithStepName,
   501              calledWithMetadata,
   502              calledWithCredentials
   503  
   504          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   505              params, stepName, metaData, creds ->
   506                  calledWithParameters = params
   507                  calledWithStepName = stepName
   508                  calledWithMetadata = metaData
   509                  calledWithCredentials = creds
   510              }
   511          )
   512  
   513          stepRule.step.transportRequestUploadFile(script: nullScript,
   514                        changeDocumentId: '001',
   515                        transportRequestId: '002',
   516                        applicationId: 'app',
   517                        filePath: '/pathByParam',
   518                        changeManagement: [
   519                            type: 'SOLMAN',
   520                            endpoint: 'https://example.org/cm',
   521                            clientOpts: '--client opts'
   522                        ],
   523                        credentialsId: 'CM'
   524          )
   525  
   526          assertThat(calledWithParameters.filePath, is('/pathByParam'))
   527      }
   528  
   529      @Test
   530      public void uploadFileToTransportRequestSOLMANFilePathFromCommonPipelineEnvironment() {
   531  
   532          // this one is used since there is nothing in the signature
   533          nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
   534  
   535          def calledWithParameters,
   536              calledWithStepName,
   537              calledWithMetadata,
   538              calledWithCredentials
   539  
   540          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   541              params, stepName, metaData, creds ->
   542                  calledWithParameters = params
   543                  calledWithStepName = stepName
   544                  calledWithMetadata = metaData
   545                  calledWithCredentials = creds
   546              }
   547          )
   548  
   549          stepRule.step.transportRequestUploadFile(script: nullScript,
   550                        changeDocumentId: '001',
   551                        transportRequestId: '002',
   552                        applicationId: 'app',
   553                        changeManagement: [
   554                            type: 'SOLMAN',
   555                            endpoint: 'https://example.org/cm',
   556                            clientOpts: '--client opts'
   557                        ],
   558                        credentialsId: 'CM'
   559          )
   560  
   561          assertThat(calledWithParameters.filePath, is('/path2'))
   562      }
   563  
   564      @Test
   565      public void uploadFileToTransportRequestSOLMANDockerParams() {
   566  
   567          // this one is used since there is nothing in the signature
   568          nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
   569  
   570          def calledWithParameters
   571  
   572          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   573              params, stepName, metaData, creds ->
   574                  calledWithParameters = params
   575              }
   576          )
   577  
   578          stepRule.step.transportRequestUploadFile(script: nullScript,
   579                        changeDocumentId: '001',
   580                        transportRequestId: '002',
   581                        applicationId: 'app',
   582                        filePath: '/pathByParam',
   583                        changeManagement: [
   584                            type: 'SOLMAN',
   585                            endpoint: 'https://example.org/cm',
   586                            clientOpts: '--client opts',
   587                            solman: [
   588                                docker: [
   589                                    image: 'solmanImage',
   590                                    options: ['-o1', 'opt1', '-o2', 'opt2'],
   591                                    envVars: [env1: 'env1', env2: 'env2'],
   592                                    pullImage: false,
   593                                ],
   594                            ],
   595                        ],
   596                        credentialsId: 'CM'
   597          )
   598  
   599          assertThat(calledWithParameters.dockerImage, is('solmanImage'))
   600          assertThat(calledWithParameters.dockerOptions, is(['-o1', 'opt1', '-o2', 'opt2']))
   601          assertThat(calledWithParameters.dockerEnvVars, is([env1: 'env1', env2: 'env2']))
   602          assertThat(calledWithParameters.dockerPullImage, is(false))
   603      }
   604  
   605      @Test
   606      public void invalidBackendTypeTest() {
   607          thrown.expect(AbortException)
   608          thrown.expectMessage('Invalid backend type: \'DUMMY\'. Valid values: [SOLMAN, CTS, RFC, NONE]. ' +
   609                               'Configuration: \'changeManagement/type\'.')
   610  
   611          stepRule.step.transportRequestUploadFile(script: nullScript,
   612                        applicationId: 'app',
   613                        filePath: '/path',
   614                        changeManagement: [type: 'DUMMY'])
   615  
   616      }
   617  
   618      @Test
   619      public void cmIntegrationSwichtedOffTest() {
   620  
   621          loggingRule.expect('[INFO] Change management integration intentionally switched off.')
   622  
   623          stepRule.step.transportRequestUploadFile(script: nullScript,
   624              changeManagement: [type: 'NONE'])
   625      }
   626  
   627  }