1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-05 15:15:44 +02:00

Adapt npmExecuteEndToEndTests (#4008)

* Adapt step

* Adapt comment
This commit is contained in:
Linda Siebert 2022-09-16 17:30:19 +02:00 committed by GitHub
parent 25bfc61e95
commit 3ba7a8dfa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 131 additions and 78 deletions

View File

@ -70,17 +70,6 @@ class NpmExecuteEndToEndTestsTest extends BasePiperTest {
Utils.metaClass = null Utils.metaClass = null
} }
@Test
void noAppUrl() {
thrown.expect(hudson.AbortException)
thrown.expectMessage('[npmExecuteEndToEndTests] The execution failed, since no appUrls are defined. Please provide appUrls as a list of maps.')
stepRule.step.npmExecuteEndToEndTests(
script: nullScript,
stageName: "myStage"
)
}
@Test @Test
void appUrlsNoList() { void appUrlsNoList() {
def appUrl = "http://my-url.com" def appUrl = "http://my-url.com"
@ -153,6 +142,54 @@ class NpmExecuteEndToEndTestsTest extends BasePiperTest {
assert npmExecuteScriptsRule.hasParameter('scriptOptions', ["--launchUrl=${appUrl.url}"]) assert npmExecuteScriptsRule.hasParameter('scriptOptions', ["--launchUrl=${appUrl.url}"])
} }
@Test
void baseUrl() {
nullScript.commonPipelineEnvironment.configuration = [
stages: [
myStage: [
baseUrl: "http://my-url.com"
]
]
]
stepRule.step.npmExecuteEndToEndTests(
script: nullScript,
stageName: "myStage"
)
assertFalse(executedInParallel)
assert npmExecuteScriptsRule.hasParameter('script', nullScript)
assert npmExecuteScriptsRule.hasParameter('parameters', [dockerOptions: ['--shm-size 512MB']])
assert npmExecuteScriptsRule.hasParameter('virtualFrameBuffer', true)
assert npmExecuteScriptsRule.hasParameter('runScripts', ["ci-e2e"])
assert npmExecuteScriptsRule.hasParameter('scriptOptions', ["--baseUrl=http://my-url.com"])
}
@Test
void chooseScript() {
nullScript.commonPipelineEnvironment.configuration = [
stages: [
myStage: [
runScript: "wdio"
]
]
]
stepRule.step.npmExecuteEndToEndTests(
script: nullScript,
stageName: "myStage"
)
assertFalse(executedInParallel)
assert npmExecuteScriptsRule.hasParameter('script', nullScript)
assert npmExecuteScriptsRule.hasParameter('parameters', [dockerOptions: ['--shm-size 512MB']])
assert npmExecuteScriptsRule.hasParameter('virtualFrameBuffer', true)
assert npmExecuteScriptsRule.hasParameter('runScripts', ["wdio"])
assert npmExecuteScriptsRule.hasParameter('scriptOptions', [])
}
@Test @Test
void oneAppUrlWithCredentials() { void oneAppUrlWithCredentials() {
def appUrl = [url: "http://my-url.com", credentialId: 'testCred'] def appUrl = [url: "http://my-url.com", credentialId: 'testCred']

View File

@ -41,10 +41,18 @@ import static com.sap.piper.Prerequisites.checkScript
* @possibleValues `true`, `false` * @possibleValues `true`, `false`
*/ */
'onlyRunInProductiveBranch', 'onlyRunInProductiveBranch',
/** /**
* Docker image on which end to end tests should be executed * Docker image on which end to end tests should be executed
*/ */
'dockerImage' 'dockerImage',
/**
* Base URL of the application to be tested
*/
'baseUrl',
/**
* Credentials to access the application to be tested
*/
'credentialsId'
]) ])
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
@ -81,83 +89,91 @@ void call(Map parameters = [:]) {
def npmParameters = [:] def npmParameters = [:]
npmParameters.dockerOptions = ['--shm-size 512MB'] npmParameters.dockerOptions = ['--shm-size 512MB']
if (!config.appUrls) { if (config.appUrls && !(config.appUrls instanceof List)) {
error "[${STEP_NAME}] The execution failed, since no appUrls are defined. Please provide appUrls as a list of maps.\n"
}
if (!(config.appUrls instanceof List)) {
error "[${STEP_NAME}] The execution failed, since appUrls is not a list. Please provide appUrls as a list of maps. For example:\n" + error "[${STEP_NAME}] The execution failed, since appUrls is not a list. Please provide appUrls as a list of maps. For example:\n" +
"appUrls: \n" + " - url: 'https://my-url.com'\n" + " credentialId: myCreds" "appUrls: \n" + " - url: 'https://my-url.com'\n" + " credentialId: myCreds"
}
if (!config.runScript) {
error "[${STEP_NAME}] No runScript was defined."
} }
if (config.onlyRunInProductiveBranch && (config.productiveBranch != env.BRANCH_NAME)) { if (config.onlyRunInProductiveBranch && (config.productiveBranch != env.BRANCH_NAME)) {
return return
} }
List credentials = []
for (int i = 0; i < config.appUrls.size(); i++) { if (config.appUrls){
List credentials = [] for (int i = 0; i < config.appUrls.size(); i++) {
def appUrl = config.appUrls[i] def appUrl = config.appUrls[i]
if (!(appUrl instanceof Map)) {
if (!(appUrl instanceof Map)) { error "[${STEP_NAME}] The element ${appUrl} is not of type map. Please provide appUrls as a list of maps. For example:\n" +
error "[${STEP_NAME}] The element ${appUrl} is not of type map. Please provide appUrls as a list of maps. For example:\n" + "appUrls: \n" + " - url: 'https://my-url.com'\n" + " credentialId: myCreds"
"appUrls: \n" + " - url: 'https://my-url.com'\n" + " credentialId: myCreds" }
} if (!appUrl.url) {
if (!appUrl.url) { error "[${STEP_NAME}] No url property was defined for the following element in appUrls: ${appUrl}"
error "[${STEP_NAME}] No url property was defined for the following element in appUrls: ${appUrl}" }
} if (appUrl.credentialId) {
if (appUrl.credentialId) { credentials.add(usernamePassword(credentialsId: appUrl.credentialId, passwordVariable: 'e2e_password', usernameVariable: 'e2e_username'))
credentials.add(usernamePassword(credentialsId: appUrl.credentialId, passwordVariable: 'e2e_password', usernameVariable: 'e2e_username')) }
} List scriptOptions = ["--launchUrl=${appUrl.url}"]
if (appUrl.parameters) {
Closure e2eTest = { if (appUrl.parameters instanceof List) {
Utils utils = new Utils() scriptOptions = scriptOptions + appUrl.parameters
utils.unstashStageFiles(script, stageName)
try {
withCredentials(credentials) {
List scriptOptions = ["--launchUrl=${appUrl.url}"]
if (appUrl.parameters) {
if (appUrl.parameters instanceof List) {
scriptOptions = scriptOptions + appUrl.parameters
} else {
error "[${STEP_NAME}] The parameters property is not of type list. Please provide parameters as a list of strings."
}
}
npmExecuteScripts(script: script, parameters: npmParameters, virtualFrameBuffer: true, runScripts: [config.runScript], dockerImage: config.dockerImage, scriptOptions: scriptOptions, buildDescriptorExcludeList: config.buildDescriptorExcludeList)
}
} catch (Exception e) {
error "[${STEP_NAME}] The execution failed with error: ${e.getMessage()}"
} finally {
List cucumberFiles = findFiles(glob: "**/e2e/*.json")
List junitFiles = findFiles(glob: "**/e2e/*.xml")
if (cucumberFiles.size() > 0) {
testsPublishResults script: script, cucumber: [active: true, archive: true]
} else if (junitFiles.size() > 0) {
testsPublishResults script: script, junit: [active: true, archive: true]
} else { } else {
echo "[${STEP_NAME}] No JUnit or cucumber report files found, skipping report visualization." error "[${STEP_NAME}] The parameters property is not of type list. Please provide parameters as a list of strings."
}
utils.stashStageFiles(script, stageName)
}
}
e2ETests["E2E Tests ${index > 1 ? index : ''}"] = {
if (env.POD_NAME || env.ON_K8S) {
dockerExecuteOnKubernetes(script: script, containerMap: ContainerMap.instance.getMap().get(stageName) ?: [:]) {
e2eTest.call()
}
} else {
node(env.NODE_NAME) {
e2eTest.call()
} }
} }
e2ETests = runTest(scriptOptions, credentials, e2ETests, index, script, config, npmParameters, stageName)
index++
} }
index++ }else{
if (config.credentialsId) {
credentials.add(usernamePassword(credentialsId: config.credentialsId, passwordVariable: 'e2e_password', usernameVariable: 'e2e_username'))
}
List scriptOptions = []
if (config.baseUrl){
scriptOptions = ["--baseUrl=${config.baseUrl}"]
}
e2ETests = runTest(scriptOptions, credentials, e2ETests, index, script, config, npmParameters, stageName)
} }
runClosures(script, e2ETests, config.parallelExecution, "end to end tests") runClosures(script, e2ETests, config.parallelExecution, "end to end tests")
} }
} }
def runTest(scriptOptions, credentials, e2ETests, index, script, config, npmParameters, stageName){
Closure e2eTest = {
Utils utils = new Utils()
utils.unstashStageFiles(script, stageName)
try {
withCredentials(credentials) {
npmExecuteScripts(script: script, parameters: npmParameters, virtualFrameBuffer: true, runScripts: [config.runScript], dockerImage: config.dockerImage, scriptOptions: scriptOptions, buildDescriptorExcludeList: config.buildDescriptorExcludeList)
}
} catch (Exception e) {
error "[${STEP_NAME}] The execution failed with error: ${e.getMessage()}"
} finally {
List cucumberFiles = findFiles(glob: "**/e2e/*.json")
List junitFiles = findFiles(glob: "**/e2e/*.xml")
if (cucumberFiles.size() > 0) {
testsPublishResults script: script, cucumber: [active: true, archive: true]
} else if (junitFiles.size() > 0) {
testsPublishResults script: script, junit: [active: true, archive: true]
} else {
echo "[${STEP_NAME}] No JUnit or cucumber report files found, skipping report visualization."
}
utils.stashStageFiles(script, stageName)
}
}
e2ETests["E2E Tests ${index > 1 ? index : ''}"] = {
if (env.POD_NAME || env.ON_K8S) {
dockerExecuteOnKubernetes(script: script, containerMap: ContainerMap.instance.getMap().get(stageName) ?: [:]) {
e2eTest.call()
}
} else {
node(env.NODE_NAME) {
e2eTest.call()
}
}
}
return e2ETests
}