2018-09-21 16:55:31 +02:00
import static com . sap . piper . Prerequisites . checkScript
2018-07-03 14:16:17 +02:00
import com.sap.piper.ConfigurationHelper
2019-03-26 14:26:21 +02:00
import com.sap.piper.GenerateDocumentation
2018-10-08 11:30:42 +02:00
import com.sap.piper.GitUtils
2018-08-09 11:35:33 +02:00
import com.sap.piper.Utils
2018-07-03 14:16:17 +02:00
import groovy.text.SimpleTemplateEngine
2018-10-08 11:30:42 +02:00
import groovy.transform.Field
2018-07-03 14:16:17 +02:00
2018-11-29 10:54:05 +02:00
@Field String STEP_NAME = getClass ( ) . getName ( )
2018-10-25 15:44:21 +02:00
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
2018-07-04 11:12:32 +02:00
@Field Set STEP_CONFIG_KEYS = [
2019-03-26 14:26:21 +02:00
/** @see dockerExecute */
2018-07-04 11:12:32 +02:00
'dockerImage' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* Defines the behavior , in case tests fail .
* @possibleValues ` true ` , ` false `
* /
2018-07-04 11:12:32 +02:00
'failOnError' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* Only if ` testRepository ` is provided: Branch of testRepository , defaults to master .
* /
2018-08-08 11:22:08 +02:00
'gitBranch' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* Only if ` testRepository ` is provided: Credentials for a protected testRepository
* @possibleValues Jenkins credentials id
* /
2018-08-08 11:22:08 +02:00
'gitSshKeyCredentialsId' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* The test collection that should be executed . This could also be a file pattern .
* /
2018-07-04 11:12:32 +02:00
'newmanCollection' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* Specify an environment file path or URL . Environments provide a set of variables that one can use within collections .
* see also [ Newman docs ] ( https: //github.com/postmanlabs/newman#newman-run-collection-file-source-options)
* /
2018-07-04 11:12:32 +02:00
'newmanEnvironment' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* Specify the file path or URL for global variables . Global variables are similar to environment variables but have a lower precedence and can be overridden by environment variables having the same name .
* see also [ Newman docs ] ( https: //github.com/postmanlabs/newman#newman-run-collection-file-source-options)
* /
2018-07-04 11:12:32 +02:00
'newmanGlobals' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* The shell command that will be executed inside the docker container to install Newman .
* /
2018-11-28 10:25:34 +02:00
'newmanInstallCommand' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* The newman command that will be executed inside the docker container .
* /
2018-07-04 11:12:32 +02:00
'newmanRunCommand' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* If specific stashes should be considered for the tests , you can pass this via this parameter .
* /
2018-08-08 11:22:08 +02:00
'stashContent' ,
2018-12-10 12:53:48 +02:00
/ * *
2019-03-26 14:26:21 +02:00
* Define an additional repository where the test implementation is located .
* For protected repositories the ` testRepository ` needs to contain the ssh git url .
* /
2018-07-04 11:12:32 +02:00
'testRepository'
]
2018-10-25 15:44:21 +02:00
2018-07-03 14:16:17 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2019-03-26 14:26:21 +02:00
/ * *
* This script executes [ Postman ] ( https: //www.getpostman.com) tests from a collection via the [Newman](https://www.getpostman.com/docs/v6/postman/collection_runs/command_line_integration_with_newman) command line tool.
* /
@GenerateDocumentation
2018-08-30 16:33:07 +02:00
void call ( Map parameters = [ : ] ) {
2018-07-03 14:16:17 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2018-09-21 16:55:31 +02:00
2018-10-31 09:40:12 +02:00
def script = checkScript ( this , parameters ) ? : this
2018-09-21 16:55:31 +02:00
2018-08-08 11:22:08 +02:00
def utils = parameters ? . juStabUtils ? : new Utils ( )
2018-07-03 14:16:17 +02:00
// load default & individual configuration
2018-10-17 11:05:20 +02:00
Map config = ConfigurationHelper . newInstance ( this )
2018-09-07 10:08:16 +02:00
. loadStepDefaults ( )
2018-10-25 15:44:21 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS )
2018-07-03 14:16:17 +02:00
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS )
. mixinStageConfig ( script . commonPipelineEnvironment , parameters . stageName ? : env . STAGE_NAME , STEP_CONFIG_KEYS )
. mixin ( parameters , PARAMETER_KEYS )
. use ( )
2019-01-21 09:47:34 +02:00
new Utils ( ) . pushToSWA ( [
step: STEP_NAME ,
stepParamKey1: 'scriptMissing' ,
stepParam1: parameters ? . script = = null
] , config )
2018-08-09 11:35:33 +02:00
2018-10-08 11:30:42 +02:00
config . stashContent = config . testRepository
? [ GitUtils . handleTestRepository ( this , config ) ]
: utils . unstashAll ( config . stashContent )
2018-08-08 11:22:08 +02:00
2018-07-03 14:16:17 +02:00
List collectionList = findFiles ( glob: config . newmanCollection ) ? . toList ( )
2018-08-08 11:22:08 +02:00
if ( collectionList . isEmpty ( ) ) {
error "[${STEP_NAME}] No collection found with pattern '${config.newmanCollection}'"
} else {
echo "[${STEP_NAME}] Found files ${collectionList}"
}
2018-07-03 14:16:17 +02:00
2018-08-08 11:22:08 +02:00
dockerExecute (
2018-11-28 10:25:34 +02:00
script: script ,
2018-08-08 11:22:08 +02:00
dockerImage: config . dockerImage ,
stashContent: config . stashContent
) {
2019-01-18 14:14:39 +02:00
sh "NPM_CONFIG_PREFIX=~/.npm-global ${config.newmanInstallCommand}"
2018-08-08 11:22:08 +02:00
for ( String collection : collectionList ) {
def collectionDisplayName = collection . toString ( ) . replace ( File . separatorChar , ( char ) '_' ) . tokenize ( '.' ) . first ( )
// resolve templates
def command = SimpleTemplateEngine . newInstance ( )
. createTemplate ( config . newmanRunCommand )
. make ( [
config: config . plus ( [ newmanCollection: collection ] ) ,
collectionDisplayName: collectionDisplayName
] ) . toString ( )
if ( ! config . failOnError ) command + = ' --suppress-exit-code'
2019-01-18 14:14:39 +02:00
sh "PATH=\$PATH:~/.npm-global/bin newman ${command}"
2018-07-03 14:16:17 +02:00
}
}
}
}