mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-02-11 13:53:53 +02:00
Add mavenExecuteIntegration and writeTemporaryCredentials to piperPipelineStageIntegration (#1927)
This change add the support for running integration tests using the recently introduced step mavenExecuteIntegration in piperPipelineStageIntegration. In addition, capabilities to provide temporary credentials to these tests is added using the writeTemporaryCredentials step.
This commit is contained in:
parent
0b805bac24
commit
ce96c379ef
@ -45,3 +45,10 @@ stages:
|
||||
githubPublishRelease:
|
||||
configKeys:
|
||||
- 'githubTokenCredentialsId'
|
||||
backendIntegrationTests:
|
||||
stepConditions:
|
||||
npmExecuteScripts:
|
||||
configKeys:
|
||||
- 'runScripts'
|
||||
mavenExecuteIntegration:
|
||||
filePattern: 'integration-tests/pom.xml'
|
||||
|
@ -578,6 +578,10 @@ steps:
|
||||
- 'tests'
|
||||
testOptions: ''
|
||||
runCommand: "uiveri5 --seleniumAddress='http://${config.seleniumHost}:${config.seleniumPort}/wd/hub'"
|
||||
writeTemporaryCredentials:
|
||||
credentialsDirectories:
|
||||
- './'
|
||||
- 'integration-tests/src/test/resources'
|
||||
|
||||
#defaults for stage wrapper
|
||||
piperStageWrapper:
|
||||
|
@ -8,47 +8,69 @@ class TemporaryCredentialsUtils implements Serializable {
|
||||
this.script = script
|
||||
}
|
||||
|
||||
void handleTemporaryCredentials(List credentialItems, String credentialsDirectory, Closure body) {
|
||||
void handleTemporaryCredentials(List credentialItems, List credentialsDirectories, Closure body) {
|
||||
final String credentialsFileName = 'credentials.json'
|
||||
|
||||
if (!credentialsDirectory) {
|
||||
script.error("This should not happen: Directory for credentials file not specified.")
|
||||
if (!credentialsDirectories) {
|
||||
script.error("This should not happen: Directories for credentials files not specified.")
|
||||
}
|
||||
|
||||
final boolean useCredentials = credentialItems
|
||||
try {
|
||||
if (useCredentials) {
|
||||
writeCredentials(credentialItems, credentialsDirectory, credentialsFileName)
|
||||
writeCredentials(credentialItems, credentialsDirectories, credentialsFileName)
|
||||
}
|
||||
body()
|
||||
}
|
||||
finally {
|
||||
if (useCredentials) {
|
||||
deleteCredentials(credentialsDirectory, credentialsFileName)
|
||||
deleteCredentials(credentialsDirectories, credentialsFileName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writeCredentials(List credentialItems, String credentialsDirectory, String credentialsFileName) {
|
||||
private void writeCredentials(List credentialItems, List credentialsDirectories, String credentialsFileName) {
|
||||
if (!credentialItems) {
|
||||
script.echo "Not writing any credentials."
|
||||
return
|
||||
}
|
||||
|
||||
assertSystemsFileExists(credentialsDirectory)
|
||||
Boolean systemsFileFound = false
|
||||
for (int i = 0; i < credentialsDirectories.size(); i++) {
|
||||
if (!credentialsDirectories[i]) {
|
||||
continue
|
||||
}
|
||||
if (!credentialsDirectories[i].endsWith("/")) {
|
||||
credentialsDirectories[i] += '/'
|
||||
}
|
||||
if (script.fileExists("${credentialsDirectories[i]}systems.yml") || script.fileExists("${credentialsDirectories[i]}systems.yaml") || script.fileExists("${credentialsDirectories[i]}systems.json")) {
|
||||
String credentialJson = returnCredentialsAsJSON(credentialItems)
|
||||
|
||||
String credentialJson = returnCredentialsAsJSON(credentialItems)
|
||||
script.echo "Writing credentials file with ${credentialItems.size()} items to ${credentialsDirectories[i]}."
|
||||
script.writeFile file: credentialsDirectories[i] + credentialsFileName, text: credentialJson
|
||||
|
||||
script.echo "Writing credentials file with ${credentialItems.size()} items."
|
||||
script.dir(credentialsDirectory) {
|
||||
script.writeFile file: credentialsFileName, text: credentialJson
|
||||
systemsFileFound = true
|
||||
}
|
||||
}
|
||||
if (!systemsFileFound) {
|
||||
script.error("None of the directories ${credentialsDirectories} contains any of the files systems.yml, systems.yaml or systems.json. " +
|
||||
"One of those files is required in order to activate the integration test credentials configured in the pipeline configuration file of this project. " +
|
||||
"Please add the file as explained in the SAP Cloud SDK documentation.")
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteCredentials(String credentialsDirectory, String credentialsFileName) {
|
||||
script.echo "Deleting credentials file."
|
||||
script.dir(credentialsDirectory) {
|
||||
script.sh "rm -f ${credentialsFileName}"
|
||||
private void deleteCredentials(List credentialsDirectories, String credentialsFileName) {
|
||||
for (int i = 0; i < credentialsDirectories.size(); i++) {
|
||||
if (!credentialsDirectories[i]) {
|
||||
continue
|
||||
}
|
||||
if(!credentialsDirectories[i].endsWith('/'))
|
||||
credentialsDirectories[i] += '/'
|
||||
|
||||
if (script.fileExists(credentialsDirectories[i] + credentialsFileName)) {
|
||||
script.echo "Deleting credentials file in ${credentialsDirectories[i]}."
|
||||
script.sh "rm -f ${credentialsDirectories[i] + credentialsFileName}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,14 +90,4 @@ class TemporaryCredentialsUtils implements Serializable {
|
||||
}
|
||||
return new JsonUtils().groovyObjectToJsonString(credentialCollection)
|
||||
}
|
||||
|
||||
private assertSystemsFileExists(String credentialsDirectory){
|
||||
script.dir(credentialsDirectory) {
|
||||
if (!script.fileExists("systems.yml") && !script.fileExists("systems.yaml") && !script.fileExists("systems.json")) {
|
||||
script.error("The directory ${credentialsDirectory} does not contain any of the files systems.yml, systems.yaml or systems.json. " +
|
||||
"One of those files is required in order to activate the integration test credentials configured in the pipeline configuration file of this project. " +
|
||||
"Please add the file as explained in the SAP Cloud SDK documentation.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ class WriteTemporaryCredentialsTest extends BasePiperTest {
|
||||
@Test
|
||||
void noCredentials() {
|
||||
nullScript.commonPipelineEnvironment.configuration = [stages: [myStage:[
|
||||
credentialsDirectory: './',
|
||||
credentialsDirectories: ['./', 'integration-test/'],
|
||||
]]]
|
||||
stepRule.step.writeTemporaryCredentials(
|
||||
script: nullScript,
|
||||
@ -89,15 +89,37 @@ class WriteTemporaryCredentialsTest extends BasePiperTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void noCredentialsDirectory() {
|
||||
void noCredentialsDirectories() {
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
|
||||
nullScript.commonPipelineEnvironment.configuration = [stages: [myStage:[
|
||||
credentials: [credential]
|
||||
credentials: [credential],
|
||||
credentialsDirectories: []
|
||||
]]]
|
||||
|
||||
thrown.expect(hudson.AbortException)
|
||||
thrown.expectMessage("[writeTemporaryCredentials] The execution failed, since no credentialsDirectory is defined. Please provide the path for the credentials file.")
|
||||
thrown.expectMessage("[writeTemporaryCredentials] The execution failed, since no credentialsDirectories are defined. Please provide a list of paths for the credentials files.")
|
||||
|
||||
stepRule.step.writeTemporaryCredentials(
|
||||
script: nullScript,
|
||||
stageName: "myStage",
|
||||
){
|
||||
bodyExecuted = true
|
||||
}
|
||||
assertFalse(bodyExecuted)
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsDirectoriesNoList() {
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
|
||||
nullScript.commonPipelineEnvironment.configuration = [stages: [myStage:[
|
||||
credentials: [credential],
|
||||
credentialsDirectories: './',
|
||||
]]]
|
||||
|
||||
thrown.expect(hudson.AbortException)
|
||||
thrown.expectMessage("[writeTemporaryCredentials] The execution failed, since credentialsDirectories is not a list. Please provide credentialsDirectories as a list of paths.")
|
||||
|
||||
stepRule.step.writeTemporaryCredentials(
|
||||
script: nullScript,
|
||||
@ -111,11 +133,12 @@ class WriteTemporaryCredentialsTest extends BasePiperTest {
|
||||
@Test
|
||||
void credentialsFileWrittenAndRemoved() {
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
fileExistsRule.registerExistingFile('systems.yml')
|
||||
fileExistsRule.registerExistingFile('./systems.yml')
|
||||
fileExistsRule.registerExistingFile('./credentials.json')
|
||||
|
||||
nullScript.commonPipelineEnvironment.configuration = [stages: [myStage:[
|
||||
credentials: [credential],
|
||||
credentialsDirectory: './',
|
||||
credentialsDirectories: ['./', 'integration-test/'],
|
||||
]]]
|
||||
|
||||
stepRule.step.writeTemporaryCredentials(
|
||||
@ -126,7 +149,8 @@ class WriteTemporaryCredentialsTest extends BasePiperTest {
|
||||
}
|
||||
|
||||
assertTrue(bodyExecuted)
|
||||
assertThat(writeFileRule.files['credentials.json'], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
assertThat(shellRule.shell, hasItem('rm -f credentials.json'))
|
||||
assertThat(writeFileRule.files['./credentials.json'], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
assertThat(shellRule.shell, hasItem('rm -f ./credentials.json'))
|
||||
assertThat(writeFileRule.files.size(), is(1))
|
||||
}
|
||||
}
|
||||
|
@ -56,77 +56,96 @@ class TemporaryCredentialsUtilsTest extends BasePiperTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsFileWritten() {
|
||||
void singleCredentialsFileWritten() {
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
def directory = './'
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
def filename = 'credentials.json'
|
||||
fileExistsRule.registerExistingFile('systems.yml')
|
||||
fileExistsRule.registerExistingFile('./systems.yml')
|
||||
|
||||
credUtils.writeCredentials([credential], directory, filename )
|
||||
credUtils.writeCredentials([credential], directories, filename )
|
||||
|
||||
assertThat(writeFileRule.files['credentials.json'], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
assertThat(writeFileRule.files['./credentials.json'], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoCredentialsFilesWritten() {
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
def filename = 'credentials.json'
|
||||
fileExistsRule.registerExistingFile('./systems.yml')
|
||||
fileExistsRule.registerExistingFile('integration-tests/src/test/resources/systems.yml')
|
||||
|
||||
credUtils.writeCredentials([credential], directories, filename )
|
||||
|
||||
assertThat(writeFileRule.files["./credentials.json"], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
assertThat(writeFileRule.files["integration-tests/src/test/resources/credentials.json"], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsFileNotWrittenWithEmptyList() {
|
||||
def directory = './'
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
def filename = 'credentials.json'
|
||||
fileExistsRule.registerExistingFile('systems.yml')
|
||||
|
||||
credUtils.writeCredentials([], directory, filename )
|
||||
credUtils.writeCredentials([], directories, filename )
|
||||
|
||||
loggingRule.expect('Not writing any credentials.')
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsFileDeleted() {
|
||||
def directory = './'
|
||||
def filename = 'credentials.json'
|
||||
fileExistsRule.registerExistingFile('systems.yml')
|
||||
|
||||
credUtils.deleteCredentials(directory, filename )
|
||||
|
||||
assertThat(shellRule.shell, hasItem('rm -f credentials.json'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void systemsFileNotExists() {
|
||||
def directory = './'
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
def filename = 'credentials.json'
|
||||
thrown.expect(hudson.AbortException)
|
||||
thrown.expectMessage("The directory ${directory} does not contain any of the files systems.yml, systems.yaml or systems.json. " +
|
||||
thrown.expectMessage("None of the directories [./, integration-tests/src/test/resources/] contains any of the files systems.yml, systems.yaml or systems.json. " +
|
||||
"One of those files is required in order to activate the integration test credentials configured in the pipeline configuration file of this project. " +
|
||||
"Please add the file as explained in the SAP Cloud SDK documentation.")
|
||||
|
||||
credUtils.assertSystemsFileExists(directory)
|
||||
credUtils.writeCredentials([credential], directories, filename )
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsFileDeleted() {
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
def filename = 'credentials.json'
|
||||
fileExistsRule.registerExistingFile('systems.yml')
|
||||
fileExistsRule.registerExistingFile('./credentials.json')
|
||||
|
||||
credUtils.deleteCredentials(directories, filename )
|
||||
|
||||
assertThat(shellRule.shell, hasItem('rm -f ./credentials.json'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleTemporaryCredentials() {
|
||||
def credential = [alias: 'ERP', credentialId: 'erp-credentials']
|
||||
def directory = './'
|
||||
fileExistsRule.registerExistingFile('systems.yml')
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
fileExistsRule.registerExistingFile('./systems.yml')
|
||||
fileExistsRule.registerExistingFile('./credentials.json')
|
||||
|
||||
credUtils.handleTemporaryCredentials([credential], directory) {
|
||||
credUtils.handleTemporaryCredentials([credential], directories) {
|
||||
bodyExecuted = true
|
||||
}
|
||||
assertTrue(bodyExecuted)
|
||||
assertThat(writeFileRule.files['credentials.json'], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
assertThat(shellRule.shell, hasItem('rm -f credentials.json'))
|
||||
assertThat(writeFileRule.files['./credentials.json'], containsString('"alias":"ERP","username":"test_user","password":"********"'))
|
||||
assertThat(shellRule.shell, hasItem('rm -f ./credentials.json'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleTemporaryCredentialsNoDirectory() {
|
||||
void handleTemporaryCredentialsNoDirectories() {
|
||||
thrown.expect(hudson.AbortException)
|
||||
thrown.expectMessage("This should not happen: Directory for credentials file not specified.")
|
||||
thrown.expectMessage("This should not happen: Directories for credentials files not specified.")
|
||||
|
||||
credUtils.handleTemporaryCredentials([], ""){
|
||||
credUtils.handleTemporaryCredentials([], []){
|
||||
bodyExecuted = true
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleTemporaryCredentialsNoCredentials() {
|
||||
credUtils.handleTemporaryCredentials([], "./"){
|
||||
def directories = ['./', 'integration-tests/src/test/resources']
|
||||
credUtils.handleTemporaryCredentials([], directories){
|
||||
bodyExecuted = true
|
||||
}
|
||||
assertTrue(bodyExecuted)
|
||||
|
@ -11,6 +11,8 @@ import static com.sap.piper.Prerequisites.checkScript
|
||||
@Field STAGE_STEP_KEYS = [
|
||||
/** Runs npm scripts to run generic integration tests written on JavaScript */
|
||||
'npmExecuteScripts',
|
||||
/** Runs backend integration tests via the Jacoco Maven-plugin */
|
||||
'mavenExecuteIntegration',
|
||||
/** Publishes test results to Jenkins. It will automatically be active in cases tests are executed. */
|
||||
'testsPublishResults',
|
||||
]
|
||||
@ -35,6 +37,7 @@ void call(Map parameters = [:]) {
|
||||
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
|
||||
.mixin(parameters, PARAMETER_KEYS)
|
||||
.addIfEmpty('npmExecuteScripts', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.npmExecuteScripts)
|
||||
.addIfEmpty('mavenExecuteIntegration', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.mavenExecuteIntegration)
|
||||
.use()
|
||||
|
||||
piperStageWrapper (script: script, stageName: stageName) {
|
||||
@ -44,9 +47,15 @@ void call(Map parameters = [:]) {
|
||||
|
||||
boolean publishResults = false
|
||||
try {
|
||||
if (config.npmExecuteScripts) {
|
||||
publishResults = true
|
||||
npmExecuteScripts script: script
|
||||
writeTemporaryCredentials(script: script) {
|
||||
if (config.npmExecuteScripts) {
|
||||
publishResults = true
|
||||
npmExecuteScripts script: script
|
||||
}
|
||||
if (config.mavenExecuteIntegration) {
|
||||
publishResults = true
|
||||
mavenExecuteIntegration script: script
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
|
@ -20,9 +20,9 @@ import static com.sap.piper.Prerequisites.checkScript
|
||||
*/
|
||||
'credentials',
|
||||
/**
|
||||
* The path to the directory where the credentials file has to be placed.
|
||||
* The list of paths to directories where credentials files need to be placed.
|
||||
*/
|
||||
'credentialsDirectory'
|
||||
'credentialsDirectories'
|
||||
]
|
||||
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
|
||||
|
||||
@ -55,13 +55,16 @@ void call(Map parameters = [:], Closure body) {
|
||||
error "[${STEP_NAME}] The execution failed, since credentials is not a list. Please provide credentials as a list of maps. For example:\n" +
|
||||
"credentials: \n" + " - alias: 'ERP'\n" + " credentialId: 'erp-credentials'"
|
||||
}
|
||||
if (!config.credentialsDirectory) {
|
||||
error "[${STEP_NAME}] The execution failed, since no credentialsDirectory is defined. Please provide the path for the credentials file.\n"
|
||||
if (!config.credentialsDirectories) {
|
||||
error "[${STEP_NAME}] The execution failed, since no credentialsDirectories are defined. Please provide a list of paths for the credentials files.\n"
|
||||
}
|
||||
if (!(config.credentialsDirectories instanceof List)) {
|
||||
error "[${STEP_NAME}] The execution failed, since credentialsDirectories is not a list. Please provide credentialsDirectories as a list of paths.\n"
|
||||
}
|
||||
|
||||
TemporaryCredentialsUtils credUtils = new TemporaryCredentialsUtils(script)
|
||||
|
||||
credUtils.handleTemporaryCredentials(config.credentials, config.credentialsDirectory) {
|
||||
credUtils.handleTemporaryCredentials(config.credentials, config.credentialsDirectories) {
|
||||
body()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user