1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/vars/testsPublishResults.groovy

231 lines
8.0 KiB
Groovy
Raw Normal View History

import static com.sap.piper.Prerequisites.checkScript
2019-04-03 11:29:57 +02:00
import com.sap.piper.GenerateDocumentation
2018-04-05 09:27:32 +02:00
import com.sap.piper.ConfigurationHelper
import com.sap.piper.JenkinsUtils
import com.sap.piper.MapUtils
import com.sap.piper.Utils
2018-02-02 13:25:28 +02:00
import groovy.transform.Field
2018-02-19 11:26:50 +02:00
@Field List TOOLS = [
2019-04-03 11:29:57 +02:00
/**
* Publishes test results files in JUnit format with the [JUnit Plugin](https://plugins.jenkins.io/junit).
* @possibleValues `true`, `false`, `Map`
*/
'junit',
/**
* Publishes code coverage with the [JaCoCo plugin](https://plugins.jenkins.io/jacoco).
* @possibleValues `true`, `false`, `Map`
*/
'jacoco',
/**
* Publishes code coverage with the [Cobertura plugin](https://plugins.jenkins.io/cobertura).
* @possibleValues `true`, `false`, `Map`
*/
'cobertura',
/**
* Publishes performance test results with the [Performance plugin](https://plugins.jenkins.io/performance).
* @possibleValues `true`, `false`, `Map`
*/
'jmeter',
/**
* Publishes test results with the [Cucumber plugin](https://plugins.jenkins.io/cucumber-testresult-plugin/).
* @possibleValues `true`, `false`, `Map`
*/
'cucumber',
/**
* Publishes test results with the [HTML Publisher plugin](https://plugins.jenkins.io/htmlpublisher/).
* @possibleValues `true`, `false`, `Map`
*/
'htmlPublisher'
2018-02-19 11:26:50 +02:00
]
2018-04-05 09:27:32 +02:00
@Field def STEP_NAME = getClass().getName()
2019-04-03 11:29:57 +02:00
@Field Set GENERAL_CONFIG_KEYS = TOOLS
2019-04-03 11:29:57 +02:00
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
2019-04-03 11:29:57 +02:00
/**
* If it is set to `true` the step will fail the build if JUnit detected any failing tests.
* @possibleValues `true`, `false`
*/
'failOnError'
])
2019-04-03 11:29:57 +02:00
2018-04-05 09:27:32 +02:00
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2018-02-02 13:25:28 +02:00
/**
2019-04-03 11:29:57 +02:00
* This step can publish test results from various sources.
2018-02-02 13:25:28 +02:00
*/
2019-04-03 11:29:57 +02:00
@GenerateDocumentation
2018-08-30 16:33:07 +02:00
void call(Map parameters = [:]) {
2018-02-02 13:25:28 +02:00
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def script = checkScript(this, parameters) ?: this
String stageName = parameters.stageName ?: env.STAGE_NAME
prepare(parameters)
2018-04-05 09:27:32 +02:00
// load default & individual configuration
Map configuration = ConfigurationHelper.newInstance(this)
.loadStepDefaults([:], stageName)
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
2018-04-05 09:27:32 +02:00
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
2018-04-05 09:27:32 +02:00
.mixin(parameters, PARAMETER_KEYS)
.use()
new Utils().pushToSWA([
step: STEP_NAME,
stepParamKey1: 'scriptMissing',
stepParam1: parameters?.script == null
], configuration)
2018-02-02 13:25:28 +02:00
publishJUnitReport(configuration.get('junit'))
publishJacocoReport(configuration.get('jacoco'))
publishCoberturaReport(configuration.get('cobertura'))
publishJMeterReport(configuration.get('jmeter'))
publishCucumberReport(configuration.get('cucumber'))
publishHtmlReport(configuration.get('htmlPublisher'))
if (configuration.failOnError && (JenkinsUtils.hasTestFailures(script.currentBuild) || 'FAILURE' == script.currentBuild?.result)){
script.currentBuild.result = 'FAILURE'
error "[${STEP_NAME}] Some tests failed!"
}
2018-02-02 13:25:28 +02:00
}
}
def publishJUnitReport(Map settings = [:]) {
if (settings.active) {
2018-02-02 13:25:28 +02:00
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
if (settings.get('updateResults'))
touchFiles(pattern)
junit(
testResults: pattern,
allowEmptyResults: allowEmpty,
healthScaleFactor: 100.0,
)
archiveResults(settings.get('archive'), pattern, allowEmpty)
}
}
def publishJacocoReport(Map settings = [:]) {
if (settings.active) {
2018-02-02 13:25:28 +02:00
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
jacoco(
execPattern: pattern,
inclusionPattern: settings.get('include', ''),
exclusionPattern: settings.get('exclude', '')
2018-02-02 13:25:28 +02:00
)
archiveResults(settings.get('archive'), pattern, allowEmpty)
}
}
def publishCoberturaReport(Map settings = [:]) {
if (settings.active) {
2018-02-02 13:25:28 +02:00
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
cobertura(
coberturaReportFile: pattern,
onlyStable: settings.get('onlyStableBuilds'),
failNoReports: !allowEmpty,
failUnstable: false,
failUnhealthy: false,
autoUpdateHealth: false,
autoUpdateStability: false,
maxNumberOfBuilds: 0
)
archiveResults(settings.get('archive'), pattern, allowEmpty)
}
}
// publish Performance Report using "Jenkins Performance Plugin" https://wiki.jenkins.io/display/JENKINS/Performance+Plugin
def publishJMeterReport(Map settings = [:]){
if (settings.active) {
2018-02-02 13:25:28 +02:00
def pattern = settings.get('pattern')
2018-02-19 11:11:12 +02:00
perfReport(
sourceDataFiles: pattern,
2018-02-02 13:25:28 +02:00
errorFailedThreshold: settings.get('errorFailedThreshold'),
errorUnstableThreshold: settings.get('errorUnstableThreshold'),
errorUnstableResponseTimeThreshold: settings.get('errorUnstableResponseTimeThreshold'),
relativeFailedThresholdPositive: settings.get('relativeFailedThresholdPositive'),
relativeFailedThresholdNegative: settings.get('relativeFailedThresholdNegative'),
relativeUnstableThresholdPositive: settings.get('relativeUnstableThresholdPositive'),
relativeUnstableThresholdNegative: settings.get('relativeUnstableThresholdNegative'),
modePerformancePerTestCase: false,
modeOfThreshold: settings.get('modeOfThreshold'),
modeThroughput: settings.get('modeThroughput'),
nthBuildNumber: settings.get('nthBuildNumber'),
configType: settings.get('configType'),
failBuildIfNoResultFile: settings.get('failBuildIfNoResultFile'),
2018-02-19 11:11:12 +02:00
compareBuildPrevious: settings.get('compareBuildPrevious')
)
2018-02-02 13:25:28 +02:00
archiveResults(settings.get('archive'), pattern, settings.get('allowEmptyResults'))
}
}
def publishCucumberReport(Map settings = [:]) {
if (settings.active) {
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
cucumber(
testResults: pattern
)
archiveResults(settings.get('archive'), pattern, allowEmpty)
}
}
def publishHtmlReport(Map settings = [:]) {
if (settings.active) {
publishHTML(target: [
allowMissing : settings.get('allowMissing'),
alwaysLinkToLastBuild: settings.get('alwaysLinkToLastBuild'),
keepAll : settings.get('keepAll'),
reportDir : settings.get('reportDir'),
reportFiles : settings.get('pattern'),
reportName : settings.get('reportName')
])
}
}
2018-10-10 12:05:41 +02:00
void touchFiles(pattern){
2018-02-02 13:25:28 +02:00
echo "[${STEP_NAME}] update test results"
def patternArray = pattern.split(',')
for(def i = 0; i < patternArray.length; i++){
sh "find . -wholename '${patternArray[i].trim()}' -exec touch {} \\;"
}
}
def archiveResults(archive, pattern, allowEmpty) {
if(archive){
2018-02-19 11:26:50 +02:00
echo "[${STEP_NAME}] archive ${pattern}"
2018-02-02 13:25:28 +02:00
archiveArtifacts artifacts: pattern, allowEmptyArchive: allowEmpty
}
}
def prepare(parameters){
// ensure tool maps are initialized correctly
2018-02-19 11:26:50 +02:00
for(String tool : TOOLS){
parameters[tool] = toMap(parameters[tool])
}
2018-02-02 13:25:28 +02:00
return parameters
}
def toMap(parameters){
if(MapUtils.isMap(parameters))
parameters.put('active', parameters.active == null?true:parameters.active)
else if(Boolean.TRUE.equals(parameters))
parameters = [active: true]
else if(Boolean.FALSE.equals(parameters))
parameters = [active: false]
2018-02-02 13:25:28 +02:00
else
parameters = [:]
2018-02-19 11:26:50 +02:00
return parameters
2018-02-02 13:25:28 +02:00
}