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

161 lines
5.4 KiB
Groovy
Raw Normal View History

2018-02-02 13:25:28 +02:00
import com.cloudbees.groovy.cps.NonCPS
2018-04-05 09:27:32 +02:00
import com.sap.piper.ConfigurationHelper
2018-02-02 13:25:28 +02:00
import com.sap.piper.ConfigurationMerger
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 = [
'junit','jacoco','cobertura','jmeter'
]
2018-04-05 09:27:32 +02:00
@Field def STEP_NAME = 'testsPublishResults'
@Field Set STEP_CONFIG_KEYS = TOOLS
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2018-02-02 13:25:28 +02:00
/**
* testResultsPublish
*
* @param script global script environment of the Jenkinsfile run
* @param others document all parameters
*/
def call(Map parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def script = parameters.script
if (script == null)
script = [commonPipelineEnvironment: commonPipelineEnvironment]
prepare(parameters)
2018-04-05 09:27:32 +02:00
// load default & individual configuration
Map configuration = ConfigurationHelper
.loadStepDefaults(this)
.mixinGeneralConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
2018-04-05 09:27:32 +02:00
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.use()
new Utils().pushToSWA([step: STEP_NAME], configuration)
2018-02-02 13:25:28 +02:00
// UNIT TESTS
publishJUnitReport(configuration.get('junit'))
// CODE COVERAGE
publishJacocoReport(configuration.get('jacoco'))
publishCoberturaReport(configuration.get('cobertura'))
// PERFORMANCE
publishJMeterReport(configuration.get('jmeter'))
}
}
def publishJUnitReport(Map settings = [:]) {
if(settings.active){
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){
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){
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){
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 touchFiles(){
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
}
}
@NonCPS
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
}
@NonCPS
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
}