diff --git a/src/com/sap/piper/Utils.groovy b/src/com/sap/piper/Utils.groovy index 47f1e8f49..c8587bb7f 100644 --- a/src/com/sap/piper/Utils.groovy +++ b/src/com/sap/piper/Utils.groovy @@ -2,6 +2,7 @@ package com.sap.piper import com.cloudbees.groovy.cps.NonCPS import com.sap.piper.analytics.Telemetry +import groovy.text.SimpleTemplateEngine import java.nio.charset.StandardCharsets import java.security.MessageDigest @@ -95,3 +96,10 @@ void pushToSWA(Map parameters, Map config) { // some error occured in telemetry reporting. This should not break anything though. } } + +@NonCPS +static String fillTemplate(String templateText, Map binding){ + def engine = new SimpleTemplateEngine() + String result = engine.createTemplate(templateText).make(binding) + return result +} diff --git a/src/com/sap/piper/testutils/JenkinsController.groovy b/src/com/sap/piper/testutils/JenkinsController.groovy new file mode 100644 index 000000000..1e4d0476d --- /dev/null +++ b/src/com/sap/piper/testutils/JenkinsController.groovy @@ -0,0 +1,122 @@ +package com.sap.piper.jenkins + +import com.cloudbees.groovy.cps.NonCPS + +class JenkinsController implements Serializable { + def script + String jenkinsUrl + def timeout + + JenkinsController(script, String jenkinsUrl = "http://localhost:8080", timeout = 3600) { + this.script = script + this.jenkinsUrl = jenkinsUrl + this.timeout = timeout + } + + def waitForJenkinsStarted() { + def timeout = 120 + def timePerLoop = 5 + + for (int i = 0; i < timeout; i += timePerLoop) { + script.sleep timePerLoop + try { + if (retrieveJenkinsStatus() == 'NORMAL') { + return true + } + } catch (Exception e) { + script.echo "Could not retrieve status for Jenkins at ${jenkinsUrl}/api/json. Message: ${e.getMessage()}. Retrying..." + e.printStackTrace() + continue + } + return false + } + script.error("Timeout: Jenkins did not start within the expected time frame.") + } + + private retrieveJenkinsStatus() { + def apiUrl = "${jenkinsUrl}/api/json" + script.echo "Checking Jenkins Status" + def response = getTextFromUrl(apiUrl) + def result = script.readJSON text: response + return result.mode + } + + //Trigger scanning of the multi branch builds + def buildJob(String jobName) { + script.sh "curl -s -X POST ${jenkinsUrl}/job/${URLEncoder.encode(jobName, 'UTF-8')}/build" + } + + def waitForSuccess(String jobName, String branch) { + if (this.waitForJobStatus(jobName, branch, 'SUCCESS')) { + this.printConsoleText(jobName, branch) + script.echo "Build was successful" + } else { + this.printConsoleText(jobName, branch) + script.error("Build of ${jobName} ${branch} was not successfull") + } + } + + def getBuildUrl(String jobName, String branch) { + return "${jenkinsUrl}/job/${URLEncoder.encode(jobName, 'UTF-8')}/job/${URLEncoder.encode(branch, 'UTF-8')}/lastBuild/" + } + + def waitForJobStatus(String jobName, String branch, String status) { + def buildUrl = getBuildUrl(jobName, branch) + def timePerLoop = 10 + + for (int i = 0; i < timeout; i += timePerLoop) { + script.sleep timePerLoop + try { + script.echo "Checking Build Status of ${jobName} ${branch}" + def buildInformation = retrieveBuildInformation(jobName, branch) + + if (buildInformation.building) { + script.echo "Build is still in progress" + continue + } + if (buildInformation.result == status) { + return true + } + } catch (Exception e) { + script.echo "Could not retrieve status for ${buildUrl}. Message: ${e.getMessage()}. Retrying..." + continue + } + return false + } + script.error("Timeout: Build of job ${jobName}, branch ${branch} did not finish in the expected time frame.") + } + + def getConsoleText(String jobName, String branch) { + def consoleUrl = this.getBuildUrl(jobName, branch) + "/consoleText" + return getTextFromUrl(consoleUrl) + } + + def printConsoleText(String jobName, String branch) { + String consoleOutput = getConsoleText(jobName, branch) + + script.echo '***********************************************' + script.echo '** Begin Output of Example Application Build **' + script.echo '***********************************************' + + script.echo consoleOutput + + script.echo '*********************************************' + script.echo '** End Output of Example Application Build **' + script.echo '*********************************************' + } + + def retrieveBuildInformation(String jobName, String branch) { + def buildUrl = getBuildUrl(jobName, branch) + def url = "${buildUrl}/api/json" + script.echo "Checking Build Status of ${jobName} ${branch}" + script.echo "${jenkinsUrl}/job/${URLEncoder.encode(jobName, 'UTF-8')}/job/${URLEncoder.encode(branch, 'UTF-8')}/" + def response = getTextFromUrl(url) + def result = script.readJSON text: response + return result + } + + @NonCPS + private static String getTextFromUrl(url) { + return new URL(url).getText() + } +}