1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-16 11:09:33 +02:00
sap-jenkins-library/vars/newmanExecute.groovy

120 lines
4.6 KiB
Groovy
Raw Normal View History

import static com.sap.piper.Prerequisites.checkScript
import com.sap.piper.ConfigurationHelper
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.text.SimpleTemplateEngine
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
2018-07-04 11:12:32 +02:00
@Field Set STEP_CONFIG_KEYS = [
2018-12-10 12:53:48 +02:00
/**
* Docker image for code execution.
*/
2018-07-04 11:12:32 +02:00
'dockerImage',
2018-12-10 12:53:48 +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
/**
* see `testRepository`
*/
'gitBranch',
2018-12-10 12:53:48 +02:00
/**
* see `testRepository`
*/
'gitSshKeyCredentialsId',
2018-12-10 12:53:48 +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
/**
* 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
/**
* 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
/**
* The shell command that will be executed inside the docker container to install Newman.
*/
'newmanInstallCommand',
2018-12-10 12:53:48 +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
/**
* If specific stashes should be considered for the tests, you can pass this via this parameter.
*/
'stashContent',
2018-12-10 12:53:48 +02:00
/**
* In case the test implementation is stored in a different repository than the code itself, you can define the repository containing the tests using parameter `testRepository` and if required `gitBranch` (for a different branch than master) and `gitSshKeyCredentialsId` (for protected repositories).
* For protected repositories the `testRepository` needs to contain the ssh git url.
*/
2018-07-04 11:12:32 +02:00
'testRepository'
]
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2018-08-30 16:33:07 +02:00
void call(Map parameters = [:]) {
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
def script = checkScript(this, parameters) ?: this
def utils = parameters?.juStabUtils ?: new Utils()
// load default & individual configuration
Map config = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.use()
new Utils().pushToSWA([
step: STEP_NAME,
stepParamKey1: 'scriptMissing',
stepParam1: parameters?.script == null
], config)
config.stashContent = config.testRepository
?[GitUtils.handleTestRepository(this, config)]
:utils.unstashAll(config.stashContent)
List collectionList = findFiles(glob: config.newmanCollection)?.toList()
if (collectionList.isEmpty()) {
error "[${STEP_NAME}] No collection found with pattern '${config.newmanCollection}'"
} else {
echo "[${STEP_NAME}] Found files ${collectionList}"
}
dockerExecute(
script: script,
dockerImage: config.dockerImage,
stashContent: config.stashContent
) {
sh "NPM_CONFIG_PREFIX=~/.npm-global ${config.newmanInstallCommand}"
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'
sh "PATH=\$PATH:~/.npm-global/bin newman ${command}"
}
}
}
}