2018-09-21 16:55:31 +02:00
import static com . sap . piper . Prerequisites . checkScript
2019-04-09 08:39:20 +02:00
import com.sap.piper.GenerateDocumentation
2018-08-15 11:41:01 +02:00
import com.sap.piper.ConfigurationHelper
2018-10-08 11:30:42 +02:00
import com.sap.piper.GitUtils
import com.sap.piper.Utils
2019-04-11 11:39:41 +02:00
import com.sap.piper.analytics.InfluxData
2019-10-23 13:38:31 +02:00
import groovy.text.GStringTemplateEngine
2018-08-15 11:41:01 +02:00
import groovy.transform.Field
2018-11-29 10:54:05 +02:00
@Field String STEP_NAME = getClass ( ) . getName ( )
2018-10-25 15:00:11 +02:00
@Field Set GENERAL_CONFIG_KEYS = STEP_CONFIG_KEYS
2018-08-15 11:41:01 +02:00
@Field Set STEP_CONFIG_KEYS = [
2019-04-09 08:39:20 +02:00
/** @see dockerExecute */
2018-10-08 11:30:42 +02:00
'dockerImage' ,
2019-04-09 08:39:20 +02:00
/** @see dockerExecute */
2019-07-25 11:57:21 +02:00
'dockerEnvVars' ,
/** @see dockerExecute */
'dockerOptions' ,
/** @see dockerExecute */
2018-08-15 11:41:01 +02:00
'dockerWorkspace' ,
2019-04-09 08:39:20 +02:00
/** @see dockerExecute */
'stashContent' ,
/** Defines the environment variables to pass to the test execution.*/
2018-08-15 11:41:01 +02:00
'envVars' ,
2019-04-09 08:39:20 +02:00
/** Defines the behavior, in case tests fail. For example, in case of `outputFormat: 'junit'` you should set it to `false`. Otherwise test results cannot be recorded using the `testsPublishhResults` step afterwards.*/
2018-08-15 11:41:01 +02:00
'failOnError' ,
2019-04-09 08:39:20 +02:00
/ * *
* Defines the format of the test result output . ` junit ` would be the standard for automated build environments but you could use also the option ` tap ` .
2019-07-05 16:32:24 +02:00
* @possibleValues ` junit ` , ` tap `
2019-04-09 08:39:20 +02:00
* /
2018-08-15 11:41:01 +02:00
'outputFormat' ,
2019-04-09 08:39:20 +02:00
/ * *
* Defines the version of * * bats - core * * to be used . By default we use the version from the master branch .
* /
2018-08-15 11:41:01 +02:00
'repository' ,
2019-04-09 08:39:20 +02:00
/** For the transformation of the test result to xUnit format the node module **tap-xunit** is used. This parameter defines the name of the test package used in the xUnit result file.*/
2018-08-15 11:41:01 +02:00
'testPackage' ,
2019-04-09 08:39:20 +02:00
/** Defines either the directory which contains the test files (`*.bats`) or a single file. You can find further details in the [Bats-core documentation](https://github.com/bats-core/bats-core#usage).*/
2018-08-15 11:41:01 +02:00
'testPath' ,
2019-04-09 08:39:20 +02:00
/** Allows to load tests from another repository.*/
'testRepository' ,
/** Defines the branch where the tests are located, in case the tests are not located in the master branch.*/
'gitBranch' ,
/ * *
* Defines the access credentials for protected repositories .
* Note: In case of using a protected repository , ` testRepository ` should include the ssh link to the repository .
* /
'gitSshKeyCredentialsId'
2018-08-15 11:41:01 +02:00
]
2018-10-25 15:00:11 +02:00
2018-08-15 11:41:01 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2019-04-09 08:39:20 +02:00
/** This step executes tests using the [Bash Automated Testing System - bats-core](https://github.com/bats-core/bats-core)*/
@GenerateDocumentation
2018-08-30 16:33:07 +02:00
void call ( Map parameters = [ : ] ) {
2018-08-15 11:41:01 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
def utils = parameters . juStabUtils ? : new Utils ( )
2018-09-21 16:55:31 +02:00
2018-10-31 09:40:12 +02:00
def script = checkScript ( this , parameters ) ? : this
2018-08-15 11:41:01 +02:00
2020-08-26 15:32:58 +02:00
String stageName = parameters . stageName ? : env . STAGE_NAME
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:00:11 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS )
2018-08-15 11:41:01 +02:00
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS )
2020-08-26 15:32:58 +02:00
. mixinStageConfig ( script . commonPipelineEnvironment , stageName , STEP_CONFIG_KEYS )
2018-08-15 11:41:01 +02:00
. mixin ( parameters , PARAMETER_KEYS )
. use ( )
// report to SWA
2019-01-21 09:47:34 +02:00
utils . pushToSWA ( [
step: STEP_NAME ,
stepParamKey1: 'scriptMissing' ,
stepParam1: parameters ? . script = = null
] , config )
2018-08-15 11:41:01 +02:00
2019-04-11 11:39:41 +02:00
InfluxData . addField ( 'step_data' , 'bats' , false )
2018-08-15 11:41:01 +02:00
2018-10-08 11:30:42 +02:00
config . stashContent = config . testRepository
? [ GitUtils . handleTestRepository ( this , config ) ]
: utils . unstashAll ( config . stashContent )
2018-08-15 11:41:01 +02:00
//resolve commonPipelineEnvironment references in envVars
config . envVarList = [ ]
config . envVars . each { e - >
2019-10-23 13:38:31 +02:00
def envValue = GStringTemplateEngine . newInstance ( ) . createTemplate ( e . getValue ( ) ) . make ( commonPipelineEnvironment: script . commonPipelineEnvironment ) . toString ( )
2018-08-15 11:41:01 +02:00
config . envVarList . add ( "${e.getKey()}=${envValue}" )
}
withEnv ( config . envVarList ) {
sh "git clone ${config.repository}"
try {
sh "bats-core/bin/bats --recursive --tap ${config.testPath} > 'TEST-${config.testPackage}.tap'"
2019-04-11 11:39:41 +02:00
InfluxData . addField ( 'step_data' , 'bats' , true )
2018-08-15 11:41:01 +02:00
} catch ( err ) {
echo "[${STEP_NAME}] One or more tests failed"
2020-04-17 11:59:51 +02:00
if ( config . failOnError ) error "[${STEP_NAME}] ERROR: The execution of the bats tests failed, see the log for details."
2018-08-15 11:41:01 +02:00
} finally {
sh "cat 'TEST-${config.testPackage}.tap'"
if ( config . outputFormat = = 'junit' ) {
2019-07-25 11:57:21 +02:00
dockerExecute (
script: script ,
dockerImage: config . dockerImage ,
dockerEnvVars: config . dockerEnvVars ,
dockerOptions: config . dockerOptions ,
dockerWorkspace: config . dockerWorkspace ,
stashContent: config . stashContent
) {
2019-07-05 16:32:24 +02:00
sh "NPM_CONFIG_PREFIX=~/.npm-global npm install tap-xunit -g"
sh "cat 'TEST-${config.testPackage}.tap' | PATH=\$PATH:~/.npm-global/bin tap-xunit --package='${config.testPackage}' > TEST-${config.testPackage}.xml"
2018-08-15 11:41:01 +02:00
}
}
}
}
}
}