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

     1  import static org.hamcrest.Matchers.allOf
     2  import static org.hamcrest.Matchers.containsString
     3  
     4  import org.hamcrest.Matchers
     5  import org.junit.After
     6  import org.junit.Before
     7  import org.junit.Rule
     8  import org.junit.Test
     9  import org.junit.rules.ExpectedException
    10  import org.junit.rules.RuleChain
    11  
    12  import com.sap.piper.Utils
    13  import com.sap.piper.cm.BackendType
    14  import com.sap.piper.cm.ChangeManagement
    15  import com.sap.piper.cm.ChangeManagementException
    16  
    17  import util.BasePiperTest
    18  import util.JenkinsCredentialsRule
    19  import util.JenkinsDockerExecuteRule
    20  import util.JenkinsStepRule
    21  import util.JenkinsLoggingRule
    22  import util.JenkinsReadYamlRule
    23  import util.Rules
    24  
    25  import hudson.AbortException
    26  import hudson.scm.NullSCM
    27  
    28  public class TransportRequestReleaseTest extends BasePiperTest {
    29  
    30      private ExpectedException thrown = new ExpectedException()
    31      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    32      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    33  
    34      @Rule
    35      public RuleChain ruleChain = Rules.getCommonRules(this)
    36          .around(new JenkinsReadYamlRule(this))
    37          .around(thrown)
    38          .around(stepRule)
    39          .around(loggingRule)
    40          .around(new JenkinsCredentialsRule(this)
    41              .withCredentials('CM', 'anonymous', '********'))
    42  
    43      @Before
    44      public void setup() {
    45  
    46          nullScript.commonPipelineEnvironment.configuration = [general:
    47                                       [changeManagement:
    48                                           [
    49                                            credentialsId: 'CM',
    50                                            type: 'SOLMAN',
    51                                            endpoint: 'https://example.org/cm'
    52                                           ]
    53                                       ]
    54                                   ]
    55          Utils.metaClass.echo = { def m -> }
    56          helper.registerAllowedMethod('addBadge', [Map], {return})
    57          helper.registerAllowedMethod('createSummary', [Map], {return})
    58      }
    59  
    60      @After
    61      public void tearDown() {
    62          Utils.metaClass = null
    63      }
    64  
    65      @Test
    66      public void changeDocumentIdNotProvidedSOLMANTest() {
    67  
    68          ChangeManagement cm = new ChangeManagement(nullScript) {
    69              String getChangeDocumentId(String from,
    70                                         String to,
    71                                         String label,
    72                                         String format) {
    73                                  throw new ChangeManagementException('Cannot retrieve change documentId')
    74              }
    75          }
    76  
    77          thrown.expect(IllegalArgumentException)
    78          thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' provided to the step call or via commit history).")
    79  
    80          stepRule.step.transportRequestRelease(script: nullScript, transportRequestId: '001', cmUtils: cm)
    81      }
    82  
    83      @Test
    84      public void transportRequestIdNotProvidedTest() {
    85  
    86          ChangeManagement cm = new ChangeManagement(nullScript) {
    87              String getTransportRequestId(String from,
    88                                           String to,
    89                                           String label,
    90                                           String format) {
    91                  throw new ChangeManagementException('Cannot retrieve transportRequestId')
    92              }
    93          }
    94  
    95          thrown.expect(IllegalArgumentException)
    96          thrown.expectMessage("Transport request id not provided (parameter: 'transportRequestId' provided to the step call or via commit history).")
    97  
    98          stepRule.step.transportRequestRelease(script: nullScript, changeDocumentId: '001', cmUtils: cm)
    99      }
   100  
   101      @Test
   102      public void releaseTransportRequestFailsSOLMANTest() {
   103  
   104          thrown.expect(AbortException)
   105          thrown.expectMessage("Something went wrong")
   106  
   107          ChangeManagement cm = new ChangeManagement(nullScript) {
   108  
   109              void releaseTransportRequestSOLMAN(
   110                                           Map docker,
   111                                           String changeId,
   112                                           String transportRequestId,
   113                                           String endpoint,
   114                                           String credentialsId,
   115                                           String clientOpts) {
   116  
   117                  throw new ChangeManagementException('Something went wrong')
   118              }
   119          }
   120  
   121          stepRule.step.transportRequestRelease(script: nullScript, changeDocumentId: '001', transportRequestId: '001', cmUtils: cm)
   122      }
   123  
   124      @Test
   125      public void releaseTransportRequestFailsCTSTest() {
   126  
   127          thrown.expect(AbortException)
   128          thrown.expectMessage("Something went wrong")
   129  
   130          nullScript
   131              .commonPipelineEnvironment
   132                  .configuration
   133                      .general
   134                          .changeManagement
   135                              .type = 'CTS'
   136  
   137          ChangeManagement cm = new ChangeManagement(nullScript) {
   138  
   139              void releaseTransportRequestCTS(
   140                                           Map docker,
   141                                           String transportRequestId,
   142                                           String endpoint,
   143                                           String credentialsId,
   144                                           String clientOpts) {
   145  
   146                  throw new ChangeManagementException('Something went wrong')
   147              }
   148          }
   149  
   150          stepRule.step.transportRequestRelease(
   151              script: nullScript,
   152              transportRequestId: '001',
   153              cmUtils: cm)
   154      }
   155  
   156      @Test
   157      public void releaseTransportRequestSuccessRFCTest() {
   158  
   159          def receivedParameters
   160  
   161          nullScript
   162              .commonPipelineEnvironment
   163                  .configuration
   164                      .general
   165                          .changeManagement =
   166                              [
   167                                  credentialsId: 'CM',
   168                                  type: 'RFC',
   169                                  endpoint: 'https://example.org/rfc',
   170                                  rfc: [
   171                                      dockerImage: 'rfc',
   172                                      dockerOptions: [],
   173                                  ],
   174                              ]
   175  
   176          ChangeManagement cm = new ChangeManagement(nullScript) {
   177              void releaseTransportRequestRFC(
   178                  Map docker,
   179                  String transportRequestId,
   180                  String endpoint,
   181                  String developmentInstance,
   182                  String developmentClient,
   183                  String credentialsId,
   184                  boolean verbose) {
   185  
   186                  receivedParameters = [
   187                      docker: docker,
   188                      transportRequestId: transportRequestId,
   189                      endpoint: endpoint,
   190                      developmentInstance: developmentInstance,
   191                      developmentClient: developmentClient,
   192                      credentialsId: credentialsId,
   193                      verbose: verbose,
   194                  ]
   195              }
   196          }
   197  
   198          stepRule.step.transportRequestRelease(
   199              script: nullScript,
   200              transportRequestId: '002',
   201              changeManagement: [
   202                  rfc: [
   203                      developmentClient: '003',
   204                      developmentInstance: '002',
   205                  ]
   206              ],
   207              verbose: true,
   208              cmUtils: cm)
   209  
   210          assert receivedParameters == [
   211                      docker: [
   212                          image: 'rfc',
   213                          options: [],
   214                          envVars: [:],
   215                          pullImage: true,
   216                      ],
   217                      transportRequestId: '002',
   218                      endpoint: 'https://example.org/rfc',
   219                      developmentInstance: '002',
   220                      developmentClient: '003',
   221                      credentialsId: 'CM',
   222                      'verbose': true,
   223                  ]
   224      }
   225  
   226      @Test
   227      public void releaseTransportRequestSuccessCTSTest() {
   228  
   229          def receivedParameters
   230  
   231          nullScript
   232              .commonPipelineEnvironment
   233                  .configuration
   234                      .general
   235                          .changeManagement =
   236                              [
   237                                  credentialsId: 'CM',
   238                                  type: 'CTS',
   239                                  endpoint: 'https://example.org/cts'
   240                              ]
   241  
   242          ChangeManagement cm = new ChangeManagement(nullScript) {
   243              void releaseTransportRequestCTS(
   244                  Map docker,
   245                  String transportRequestId,
   246                  String endpoint,
   247                  String credentialsId,
   248                  String clientOpts = '') {
   249  
   250                  receivedParameters = [
   251                      docker: docker,
   252                      transportRequestId: transportRequestId,
   253                      endpoint: endpoint,
   254                      credentialsId: credentialsId,
   255                      clientOpts: clientOpts
   256                  ]
   257              }
   258          }
   259  
   260          stepRule.step.transportRequestRelease(
   261              script: nullScript,
   262              transportRequestId: '002',
   263              cmUtils: cm)
   264  
   265          assert receivedParameters == [
   266                      docker: [
   267                          image:'ppiper/cm-client:2.0.1.0',
   268                          options:[],
   269                          envVars:[:],
   270                          pullImage:true,
   271                      ],
   272                      transportRequestId: '002',
   273                      endpoint: 'https://example.org/cts',
   274                      credentialsId: 'CM',
   275                      clientOpts: ''
   276                  ]
   277      }
   278  
   279      @Test
   280      public void releaseTransportRequestFailsRFCTest() {
   281  
   282          thrown.expect(AbortException)
   283          thrown.expectMessage('Failed releasing transport request.')
   284  
   285          nullScript
   286              .commonPipelineEnvironment
   287                  .configuration
   288                      .general
   289                          .changeManagement =
   290                              [
   291                                  credentialsId: 'CM',
   292                                  type: 'RFC',
   293                                  endpoint: 'https://example.org/rfc',
   294                                  rfc: [dockerImage: 'rfc']
   295                              ]
   296  
   297          ChangeManagement cm = new ChangeManagement(nullScript) {
   298              void releaseTransportRequestRFC(
   299                  Map docker,
   300                  String transportRequestId,
   301                  String endpoint,
   302                  String developmentInstance,
   303                  String developmentClient,
   304                  String credentialsId,
   305                  boolean verbose) {
   306  
   307                  throw new ChangeManagementException('Failed releasing transport request.')
   308              }
   309          }
   310  
   311          stepRule.step.transportRequestRelease(
   312              script: nullScript,
   313              transportRequestId: '002',
   314              changeManagement: [
   315                  rfc: [
   316                      developmentClient: '003',
   317                      developmentInstance: '002'
   318                  ]
   319              ],
   320              cmUtils: cm)
   321  
   322      }
   323  
   324      @Test
   325      public void releaseTransportRequestSanityChecksSOLMANTest() {
   326  
   327          thrown.expect(IllegalArgumentException)
   328          thrown.expectMessage(allOf(
   329              containsString('ERROR - NO VALUE AVAILABLE FOR'),
   330              containsString('changeManagement/endpoint')))
   331  
   332          // changeDocumentId and transportRequestId are not checked
   333          // by the sanity checks here since they are looked up from
   334          // commit history in case they are not provided.
   335  
   336          nullScript
   337              .commonPipelineEnvironment
   338                  .configuration = null
   339  
   340          stepRule.step.transportRequestRelease(
   341              script: nullScript,
   342              changeManagement: [type: 'SOLMAN']
   343          )
   344      }
   345  
   346      @Test
   347      public void releaseTransportRequestSanityChecksCTSTest() {
   348  
   349          thrown.expect(IllegalArgumentException)
   350          thrown.expectMessage(allOf(
   351              containsString('ERROR - NO VALUE AVAILABLE FOR'),
   352              containsString('changeManagement/endpoint')))
   353  
   354          nullScript
   355              .commonPipelineEnvironment
   356                  .configuration = null
   357  
   358          stepRule.step.transportRequestRelease(
   359              script: nullScript,
   360              changeManagement: [type: 'CTS']
   361          )
   362      }
   363  
   364      @Test
   365      public void releaseTransportRequestSanityChecksRFCTest() {
   366  
   367          thrown.expect(IllegalArgumentException)
   368          thrown.expectMessage(allOf(
   369              containsString('ERROR - NO VALUE AVAILABLE FOR:'),
   370              containsString('changeManagement/endpoint'),
   371              containsString('developmentClient')))
   372  
   373          nullScript
   374              .commonPipelineEnvironment
   375                  .configuration = null
   376  
   377          stepRule.step.transportRequestRelease(
   378              script: nullScript,
   379              changeManagement: [type: 'RFC'],
   380              transportRequestId: '002')
   381      }
   382  
   383      @Test
   384      public void releaseTransportRequestSuccessSOLMANTest() {
   385  
   386          // Here we test only the case where the transportRequestId is
   387          // provided via parameters. The other cases are tested by
   388          // corresponding tests for StepHelpers#getTransportRequestId(./.)
   389  
   390          loggingRule.expect("[INFO] Closing transport request '002' for change document '001'.")
   391          loggingRule.expect("[INFO] Transport Request '002' has been successfully closed.")
   392  
   393          Map receivedParams = [:]
   394  
   395          ChangeManagement cm = new ChangeManagement(nullScript) {
   396              void releaseTransportRequestSOLMAN(
   397                                           Map docker,
   398                                           String changeId,
   399                                           String transportRequestId,
   400                                           String endpoint,
   401                                           String credentialsId,
   402                                           String clientOpts) {
   403  
   404                  receivedParams.docker = docker
   405                  receivedParams.changeId = changeId
   406                  receivedParams.transportRequestId = transportRequestId
   407                  receivedParams.endpoint = endpoint
   408                  receivedParams.credentialsId = credentialsId
   409                  receivedParams.clientOpts = clientOpts
   410              }
   411          }
   412  
   413          stepRule.step.transportRequestRelease(script: nullScript, changeDocumentId: '001', transportRequestId: '002', cmUtils: cm)
   414  
   415          assert receivedParams == [
   416                                    docker: [
   417                                        image: 'ppiper/cm-client:2.0.1.0',
   418                                        pullImage: true,
   419                                        envVars: [:],
   420                                        options: [],
   421                                    ],
   422                                    changeId: '001',
   423                                    transportRequestId: '002',
   424                                    endpoint: 'https://example.org/cm',
   425                                    credentialsId: 'CM',
   426                                    clientOpts: '']
   427      }
   428  
   429      @Test
   430      public void cmIntegrationSwichtedOffTest() {
   431  
   432          loggingRule.expect('[INFO] Change management integration intentionally switched off.')
   433  
   434          stepRule.step.transportRequestRelease(script: nullScript,
   435              changeManagement: [type: 'NONE'])
   436      }
   437  }