github.com/jaylevin/jenkins-library@v1.230.4/vars/gatlingExecuteTests.groovy (about)

     1  import com.sap.piper.ConfigurationHelper
     2  import com.sap.piper.Utils
     3  
     4  import static com.sap.piper.Prerequisites.checkScript
     5  
     6  import com.sap.piper.GenerateDocumentation
     7  import groovy.transform.Field
     8  
     9  @Field String STEP_NAME = getClass().getName()
    10  
    11  @Field Set GENERAL_CONFIG_KEYS = []
    12  
    13  @Field Set STEP_CONFIG_KEYS = [
    14      /**
    15       * Defines the behavior in case tests fail. When this is set to `true` test results cannot be recorded using the `publishTestResults` step afterwards.
    16       * @possibleValues `true`, `false`
    17       */
    18      'failOnError',
    19      /**
    20       * Path to the pom.xml file containing the performance test Maven module, for example `performance-tests/pom.xml`.
    21       */
    22      'pomPath',
    23      /**
    24       * Optional List of app URLs and corresponding Jenkins credential IDs.
    25       */
    26      'appUrls'
    27  ]
    28  
    29  @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
    30  
    31  /**
    32   * In this step Gatling performance tests are executed.
    33   * Requires the [Jenkins Gatling plugin](https://plugins.jenkins.io/gatling/) to be installed.
    34   */
    35  @GenerateDocumentation
    36  void call(Map parameters = [:]) {
    37  
    38      handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
    39          Script script = checkScript(this, parameters) ?: this
    40          Utils utils = parameters.juStabUtils ?: new Utils()
    41          String stageName = parameters.stageName ?: env.STAGE_NAME
    42  
    43          Map config = ConfigurationHelper.newInstance(this)
    44              .loadStepDefaults([:], stageName)
    45              .mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
    46              .mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
    47              .mixin(parameters, PARAMETER_KEYS)
    48              .withMandatoryProperty('pomPath')
    49              .use()
    50  
    51          utils.pushToSWA([
    52              step: STEP_NAME,
    53              stepParamKey1: 'pomPath',
    54              stepParam1: config.pomPath,
    55          ], config)
    56  
    57          def appUrls = parameters.get('appUrls')
    58          if (appUrls && !(appUrls instanceof List)) {
    59              error "The optional parameter 'appUrls' needs to be a List of Maps, where each Map contains the two entries 'url' and 'credentialsId'."
    60          }
    61  
    62          if (!fileExists(config.pomPath)) {
    63              error "The file '${config.pomPath}' does not exist."
    64          }
    65  
    66          utils.unstashAll(config.stashContent)
    67  
    68          try {
    69              if (appUrls) {
    70                  for (int i = 0; i < appUrls.size(); i++) {
    71                      def appUrl = appUrls.get(i)
    72                      if (!(appUrl instanceof Map)) {
    73                          error "The entry at index $i in 'appUrls' is not a Map. It needs to be a Map containing the two entries 'url' and 'credentialsId'."
    74                      }
    75                      executeTestsWithAppUrlAndCredentials(script, appUrl.url, appUrl.credentialsId, config.pomPath)
    76                  }
    77              } else {
    78                  mavenExecute script: script, flags: ['--update-snapshots'], pomPath: config.pomPath, goals: ['test']
    79              }
    80          } finally {
    81              gatlingArchive()
    82          }
    83      }
    84  }
    85  
    86  void executeTestsWithAppUrlAndCredentials(Script script, url, credentialsId, pomPath) {
    87      withCredentials([
    88          [
    89              $class: 'UsernamePasswordMultiBinding',
    90              credentialsId: credentialsId,
    91              passwordVariable: 'PERFORMANCE_TEST_PASSWORD',
    92              usernameVariable: 'PERFORMANCE_TEST_USERNAME'
    93          ]
    94      ]) {
    95          List defines = [
    96              "-DappUrl=$url",
    97              "-Dusername=$PERFORMANCE_TEST_USERNAME",
    98              "-Dpassword=$PERFORMANCE_TEST_PASSWORD"
    99          ]
   100          mavenExecute script: script, flags: ['--update-snapshots'], pomPath: pomPath, goals: ['test'], defines: defines
   101      }
   102  }