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

132 lines
5.4 KiB
Groovy
Raw Normal View History

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
import com.sap.piper.GitUtils
import com.sap.piper.Utils
import groovy.text.GStringTemplateEngine
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@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]]]`.
*/
'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']. */
'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. */
'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.
*/
'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`.
*/
'dockerWorkspace',
/** @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`
*/
'failOnError',
2019-03-13 13:01:05 +02:00
/** The command that is executed to install the test tool. */
'installCommand',
2019-03-13 13:01:05 +02:00
/** Define the paths of the modules to execute tests on. */
'modules',
2019-03-13 13:01:05 +02:00
/** The command that is executed to start the tests. */
'runCommand',
/** @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`. */
'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. */
'sidecarImage',
2019-03-13 13:01:05 +02:00
/**
* as `dockerName` for the sidecar container
*/
'sidecarName',
2019-03-13 13:01:05 +02:00
/** Volumes that should be mounted into the sidecar container. */
'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`. */
'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`)
* * 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
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,
containerPortMappings: config.containerPortMappings,
dockerEnvVars: config.dockerEnvVars,
dockerImage: config.dockerImage,
dockerName: config.dockerName,
dockerWorkspace: config.dockerWorkspace,
dockerOptions: config.dockerOptions,
failOnError: config.failOnError,
seleniumHubCredentialsId: config.seleniumHubCredentialsId,
sidecarEnvVars: config.sidecarEnvVars,
sidecarImage: config.sidecarImage,
sidecarName: config.sidecarName,
sidecarVolumeBind: config.sidecarVolumeBind,
stashContent: config.stashContent
]
for(String path : config.modules){
String modulePath = path
testJobs["Karma - ${modulePath}"] = {
seleniumExecuteTests(options){
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."
}
}
}
}
if(testJobs.size() == 1){
testJobs.each({ key, value -> value() })
}else{
parallel testJobs.plus([failFast: false])
}
}
}