2018-11-08 13:15:15 +02:00
import static com . sap . piper . Prerequisites . checkScript
import com.sap.piper.ConfigurationHelper
2019-03-13 13:01:05 +02:00
import com.sap.piper.GenerateDocumentation
2018-11-08 13:15:15 +02:00
import com.sap.piper.GitUtils
import com.sap.piper.Utils
2019-10-23 13:38:31 +02:00
import groovy.text.GStringTemplateEngine
2018-11-08 13:15:15 +02:00
import groovy.transform.Field
2018-11-29 10:54:05 +02:00
@Field String STEP_NAME = getClass ( ) . getName ( )
2018-11-08 13:15:15 +02:00
@Field Set GENERAL_CONFIG_KEYS = [
2019-03-13 13:01:05 +02:00
/ * *
* Map which defines per docker image the port mappings , e . g . ` containerPortMappings: [ 'selenium/standalone-chrome' : [ [ name: 'selPort' , containerPort: 4444 , hostPort: 4444 ] ] ] ` .
* /
2018-11-08 13:15:15 +02:00
'containerPortMappings' ,
2019-03-13 13:01:05 +02:00
/** A map of environment variables to set in the container, e.g. [http_proxy:'proxy:8080']. */
2018-11-08 13:15:15 +02:00
'dockerEnvVars' ,
2019-03-13 13:01:05 +02:00
/** The name of the docker image that should be used. If empty, Docker is not used and the command is executed directly on the Jenkins system. */
2018-11-08 13:15:15 +02:00
'dockerImage' ,
2019-03-13 13:01:05 +02:00
/ * *
* Kubernetes only:
* Name of the container launching ` dockerImage ` .
* SideCar only:
* Name of the container in local network .
* /
2018-11-08 13:15:15 +02:00
'dockerName' ,
2019-03-13 13:01:05 +02:00
/ * *
* Kubernetes only:
* Specifies a dedicated user home directory for the container which will be passed as value for environment variable ` HOME ` .
* /
2018-11-08 13:15:15 +02:00
'dockerWorkspace' ,
2019-07-25 11:57:21 +02:00
/** @see dockerExecute */
'dockerOptions' ,
2019-03-13 13:01:05 +02:00
/ * *
* With ` failOnError ` the behavior in case tests fail can be defined .
* @possibleValues ` true ` , ` false `
* /
2018-11-08 13:15:15 +02:00
'failOnError' ,
2019-03-13 13:01:05 +02:00
/** The command that is executed to install the test tool. */
2018-11-08 13:15:15 +02:00
'installCommand' ,
2019-03-13 13:01:05 +02:00
/** Define the paths of the modules to execute tests on. */
2018-11-08 13:15:15 +02:00
'modules' ,
2019-03-13 13:01:05 +02:00
/** The command that is executed to start the tests. */
2018-11-08 13:15:15 +02:00
'runCommand' ,
2020-03-11 14:35:49 +02:00
/** @see seleniumExecuteTests */
'seleniumHubCredentialsId' ,
2019-03-13 13:01:05 +02:00
/** A map of environment variables to set in the sidecar container, similar to `dockerEnvVars`. */
2018-11-08 13:15:15 +02:00
'sidecarEnvVars' ,
2019-03-13 13:01:05 +02:00
/** The name of the docker image of the sidecar container. If empty, no sidecar container is started. */
2018-11-08 13:15:15 +02:00
'sidecarImage' ,
2019-03-13 13:01:05 +02:00
/ * *
* as ` dockerName ` for the sidecar container
* /
2018-11-08 13:15:15 +02:00
'sidecarName' ,
2019-03-13 13:01:05 +02:00
/** Volumes that should be mounted into the sidecar container. */
2018-11-08 13:15:15 +02:00
'sidecarVolumeBind' ,
2019-03-13 13:01:05 +02:00
/** If specific stashes should be considered for the tests, their names need to be passed via the parameter `stashContent`. */
2018-11-08 13:15:15 +02:00
'stashContent'
]
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2019-03-13 13:01:05 +02:00
/ * *
* In this step the ( [ Karma test runner ] ( http: //karma-runner.github.io)) is executed.
*
* The step is using the ` seleniumExecuteTest ` step to spin up two containers in a Docker network:
*
* * a Selenium /Chrome container (`selenium/ standalone - chrome ` )
2020-01-29 12:17:56 +02:00
* * a NodeJS container ( ` node: lts - stretch ` )
2019-03-13 13:01:05 +02:00
*
* In the Docker network , the containers can be referenced by the values provided in ` dockerName ` and ` sidecarName ` , the default values are ` karma ` and ` selenium ` . These values must be used in the ` hostname ` properties of the test configuration ( [ Karma ] ( https: //karma-runner.github.io/1.0/config/configuration-file.html) and [WebDriver](https://github.com/karma-runner/karma-webdriver-launcher#usage)).
*
* ! ! ! note
* In a Kubernetes environment , the containers both need to be referenced with ` localhost ` .
* /
@GenerateDocumentation
2018-11-08 13:15:15 +02:00
void call ( Map parameters = [ : ] ) {
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
final 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 ( )
utils . pushToSWA ( [ step: STEP_NAME ] , config )
def testJobs = [ : ]
def options = [
2018-11-08 14:25:11 +02:00
script: script ,
2018-11-08 13:15:15 +02:00
containerPortMappings: config . containerPortMappings ,
dockerEnvVars: config . dockerEnvVars ,
dockerImage: config . dockerImage ,
dockerName: config . dockerName ,
dockerWorkspace: config . dockerWorkspace ,
2019-07-25 11:57:21 +02:00
dockerOptions: config . dockerOptions ,
2018-11-08 13:15:15 +02:00
failOnError: config . failOnError ,
2020-03-11 14:35:49 +02:00
seleniumHubCredentialsId: config . seleniumHubCredentialsId ,
2018-11-08 13:15:15 +02:00
sidecarEnvVars: config . sidecarEnvVars ,
sidecarImage: config . sidecarImage ,
sidecarName: config . sidecarName ,
sidecarVolumeBind: config . sidecarVolumeBind ,
stashContent: config . stashContent
]
for ( String path : config . modules ) {
2019-06-24 15:29:32 +02:00
String modulePath = path
testJobs [ "Karma - ${modulePath}" ] = {
2018-11-08 13:15:15 +02:00
seleniumExecuteTests ( options ) {
2020-04-17 11:59:51 +02:00
try {
sh "cd '${modulePath}' && ${config.installCommand}"
sh "cd '${modulePath}' && ${config.runCommand}"
} catch ( e ) {
error "[${STEP_NAME}] ERROR: The execution of the karma tests failed, see the log for details."
}
2018-11-08 13:15:15 +02:00
}
}
}
if ( testJobs . size ( ) = = 1 ) {
testJobs . each ( { key , value - > value ( ) } )
} else {
parallel testJobs . plus ( [ failFast: false ] )
}
}
}