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
2020-06-12 10:04:03 +02:00
import com.sap.piper.integration.CloudFoundry
2019-10-23 13:38:31 +02:00
import groovy.text.GStringTemplateEngine
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' ,
2019-07-25 11:57:21 +02:00
/** @see dockerExecute*/
'dockerEnvVars' ,
/** @see dockerExecute */
'dockerOptions' ,
/** @see dockerExecute*/
'dockerWorkspace' ,
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 .
* /
2020-06-12 10:04:03 +02:00
'testRepository' ,
/ * *
* Define name array of cloud foundry apps deployed for which secrets ( clientid and clientsecret ) will be appended
* to the newman command that overrides the environment json entries
* ( - - env - var < appName_clientid > = $ { clientid } & - - env - var < appName_clientsecret > = $ { clientsecret } )
* /
'cfAppsWithSecrets' ,
'cloudFoundry' ,
/ * *
* Cloud Foundry API endpoint .
* @parentConfigKey cloudFoundry
* /
'apiEndpoint' ,
/ * *
* Credentials to be used for deployment .
* @parentConfigKey cloudFoundry
* /
'credentialsId' ,
/ * *
* Cloud Foundry target organization .
* @parentConfigKey cloudFoundry
* /
'org' ,
/ * *
* Cloud Foundry target space .
* @parentConfigKey cloudFoundry
* /
'space' ,
/ * *
* Print more detailed information into the log .
* @possibleValues ` true ` , ` false `
* /
'verbose'
2018-07-04 11:12:32 +02:00
]
2018-10-25 15:44:21 +02:00
2020-06-12 10:04:03 +02:00
@Field Map CONFIG_KEY_COMPATIBILITY = [ cloudFoundry: [ apiEndpoint: 'cfApiEndpoint' , credentialsId: 'cfCredentialsId' , org: 'cfOrg' , space: 'cfSpace' ] ]
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-08-08 11:22:08 +02:00
def utils = parameters ? . juStabUtils ? : new Utils ( )
2020-08-26 15:32:58 +02:00
String stageName = parameters . stageName ? : env . STAGE_NAME
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 )
2020-08-26 15:32:58 +02:00
. loadStepDefaults ( [ : ] , stageName )
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 )
2020-08-26 15:32:58 +02:00
. mixinStageConfig ( script . commonPipelineEnvironment , stageName , STEP_CONFIG_KEYS )
2020-06-12 10:04:03 +02:00
. mixin ( parameters , PARAMETER_KEYS , CONFIG_KEY_COMPATIBILITY )
2018-07-03 14:16:17 +02:00
. 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 ,
2019-07-25 11:57:21 +02:00
dockerEnvVars: config . dockerEnvVars ,
dockerOptions: config . dockerOptions ,
dockerWorkspace: config . dockerWorkspace ,
2018-08-08 11:22:08 +02:00
stashContent: config . stashContent
) {
2020-02-17 14:24:56 +02:00
sh returnStatus: true , script: "" "
node - - version
npm - - version
"" "
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
2019-10-23 13:38:31 +02:00
def command = GStringTemplateEngine . newInstance ( )
2018-08-08 11:22:08 +02:00
. createTemplate ( config . newmanRunCommand )
. make ( [
config: config . plus ( [ newmanCollection: collection ] ) ,
collectionDisplayName: collectionDisplayName
] ) . toString ( )
2020-06-12 10:04:03 +02:00
def command_secrets = ''
if ( config . cfAppsWithSecrets ) {
CloudFoundry cfUtils = new CloudFoundry ( script ) ;
config . cfAppsWithSecrets . each { appName - >
def xsuaaCredentials = cfUtils . getXsuaaCredentials ( config . cloudFoundry . apiEndpoint ,
config . cloudFoundry . org ,
config . cloudFoundry . space ,
config . cloudFoundry . credentialsId ,
appName ,
config . verbose ? true : false ) //to avoid config.verbose as "null" if undefined in yaml and since function parameter boolean
command_secrets + = " --env-var ${appName}_clientid=${xsuaaCredentials.clientid} --env-var ${appName}_clientsecret=${xsuaaCredentials.clientsecret}"
echo "Exposing client id and secret for ${appName}: as ${appName}_clientid and ${appName}_clientsecret to newman"
}
}
2018-08-08 11:22:08 +02:00
if ( ! config . failOnError ) command + = ' --suppress-exit-code'
2020-04-16 09:14:39 +02:00
try {
2020-06-12 10:04:03 +02:00
if ( config . cfAppsWithSecrets & & command_secrets ) {
echo "PATH=\$PATH:~/.npm-global/bin newman ${command} **env/secrets**"
sh "" "
set + x
PATH = \ $PATH : ~ /.npm-global/ bin newman $ { command } $ { command_secrets }
"" "
}
else {
sh "PATH=\$PATH:~/.npm-global/bin newman ${command}"
}
}
catch ( e ) {
2020-04-17 11:59:51 +02:00
error "[${STEP_NAME}] ERROR: The execution of the newman tests failed, see the log for details."
2020-04-16 09:14:39 +02:00
}
2018-07-03 14:16:17 +02:00
}
}
}
}