1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

rename step

This commit is contained in:
Christopher Fenner 2018-02-02 12:25:28 +01:00
parent 6337134607
commit 99ccf0a516
2 changed files with 300 additions and 115 deletions

View File

@ -2,9 +2,6 @@ import static java.util.Arrays.asList
import com.cloudbees.groovy.cps.NonCPS
import groovy.transform.Field
@Field def STEP_NAME = 'testsPublishResults'
/**
* testResultsPublish
*
@ -12,73 +9,37 @@ import groovy.transform.Field
* @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]
prepareDefaultValues script: script
Map configurationKeys = [
'junit': [
'patter': null,
'updateResults': null,
'allowEmptyResults': null
'archive': null
],
'jacoco': [
'pattern': null,
'include': null,
'exclude': null,
'allowEmptyResults': null,
'archive': null
],
'cobertura': [
'pattern': null,
'onlyStableBuilds': null,
'allowEmptyResults': null,
'archive': null
],
'jmeter': [
'pattern': null,
'errorFailedThreshold': null,
'errorUnstableThreshold': null,
'errorUnstableResponseTimeThreshold': null,
'relativeFailedThresholdPositive': null,
'relativeFailedThresholdNegative': null,
'relativeUnstableThresholdPositive': null,
'relativeUnstableThresholdNegative': null,
'modeOfThreshold': null,
'modeThroughput': null,
'nthBuildNumber': null,
'configType': null,
'failBuildIfNoResultFile': null,
'compareBuildPrevious': null,
'allowEmptyResults': null,
'archive': null
]
]
final Map stepDefaults = ConfigurationLoader.defaultStepConfiguration(script, STEP_NAME)
final Map stepConfiguration = ConfigurationLoader.stepConfiguration(script, STEP_NAME)
prepare(parameters)
Map configuration = ConfigurationMerger.mergeDeepStructure(
parameters, configurationKeys,
stepConfiguration, configurationKeys, stepDefaults)
handlePipelineStepErrors (stepName: 'testResultsPublish', stepParameters: parameters) {
// GENERAL
def allowUnstableBuilds = parameters.get('allowUnstableBuilds', false)
// UNIT TESTS
publishJUnitReport(configuration.get('junit'))
def junit = parameters.get('junit', false)
// CODE COVERAGE
publishJacocoReport(configuration.get('jacoco'))
publishCoberturaReport(configuration.get('cobertura'))
def jacoco = parameters.get('jacoco', false)
def cobertura = parameters.get('cobertura', false)
// PERFORMANCE
publishJMeterReport(configuration.get('jmeter'))
def jmeter = parameters.get('jmeter', false)
// jUnit
publishJUnitReport(junit)
publishJacocoReport(jacoco)
publishCoberturaReport(cobertura)
publishJMeterReport(jmeter)
if (!allowUnstableBuilds)
failUnstableBuild(currentBuild)
}
}
def publishJUnitReport(Map settings = [:]) {
if(settings.active){
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
if(!Boolean.FALSE.equals(settings)){
settings = asMap(settings)
def pattern = settings.get('pattern', '**/target/surefire-reports/*.xml')
def archive = settings.get('archive', false)
def allowEmpty = settings.get('allowEmptyResults', true)
def updateResults = settings.get('updateResults', false)
if (settings.get('updateResults')
if (updateResults)
touchFiles(pattern)
junit(
testResults: pattern,
@ -86,35 +47,42 @@ def publishJUnitReport(Map settings = [:]) {
healthScaleFactor: 100.0,
)
// archive results
archiveResults(settings.get('archive'), pattern, allowEmpty)
archiveResults(archive, pattern, allowEmpty)
}
}
def publishJacocoReport(Map settings = [:]) {
if(settings.active){
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
if(!Boolean.FALSE.equals(settings)){
settings = asMap(settings)
def pattern = settings.get('pattern', '**/target/*.exec')
def archive = settings.get('archive', false)
def allowEmpty = settings.get('allowEmptyResults', true)
def include = settings.get('include', '')
def exclude = settings.get('exclude', '')
jacoco(
execPattern: pattern,
inclusionPattern: settings.get('include'),
exclusionPattern: settings.get('exclude')
inclusionPattern: include,
exclusionPattern: exclude
)
// archive results
archiveResults(settings.get('archive'), pattern, allowEmpty)
archiveResults(archive, pattern, allowEmpty)
}
}
def publishCoberturaReport(Map settings = [:]) {
if(settings.active){
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
if(!Boolean.FALSE.equals(settings)){
settings = asMap(settings)
def pattern = settings.get('pattern', '**/target/coverage/cobertura-coverage.xml')
def archive = settings.get('archive', false)
def allowEmpty = settings.get('allowEmptyResults', true)
def onlyStable = settings.get('onlyStableBuilds', true)
cobertura(
coberturaReportFile: pattern,
onlyStable: settings.get('onlyStableBuilds'),
failNoReports: !allowEmpty,
onlyStable: (onlyStable?true:false),
failNoReports: (allowEmpty?false:true),
failUnstable: false,
failUnhealthy: false,
autoUpdateHealth: false,
@ -123,47 +91,58 @@ def publishCoberturaReport(Map settings = [:]) {
)
// archive results
archiveResults(settings.get('archive'), pattern, allowEmpty)
archiveResults(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')
if(!Boolean.FALSE.equals(settings)){
settings = asMap(settings)
def pattern = settings.get('pattern', '**/*.jtl')
def archive = settings.get('archive', false)
def allowEmpty = settings.get('allowEmptyResults', true)
step([
$class: 'PerformancePublisher',
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'),
errorFailedThreshold: settings.get('errorFailedThreshold', 20),
errorUnstableThreshold: settings.get('errorUnstableThreshold', 10),
errorUnstableResponseTimeThreshold: settings.get('errorUnstableResponseTimeThreshold', ""),
relativeFailedThresholdPositive: settings.get('relativeFailedThresholdPositive', 0),
relativeFailedThresholdNegative: settings.get('relativeFailedThresholdNegative', 0),
relativeUnstableThresholdPositive: settings.get('relativeUnstableThresholdPositive', 0),
relativeUnstableThresholdNegative: settings.get('relativeUnstableThresholdNegative', 0),
modePerformancePerTestCase: false,
modeOfThreshold: settings.get('modeOfThreshold'),
modeThroughput: settings.get('modeThroughput'),
nthBuildNumber: settings.get('nthBuildNumber'),
configType: settings.get('configType'),
failBuildIfNoResultFile: settings.get('failBuildIfNoResultFile'),
compareBuildPrevious: settings.get('compareBuildPrevious'),
modeOfThreshold: settings.get('modeOfThreshold', false),
modeThroughput: settings.get('modeThroughput', false),
nthBuildNumber: settings.get('nthBuildNumber', 0),
configType: settings.get('configType', "PRT"),
failBuildIfNoResultFile: settings.get('failBuildIfNoResultFile', false),
compareBuildPrevious: settings.get('compareBuildPrevious', true),
parsers: asList(getJMeterParser().newInstance(pattern))
])
// archive results
archiveResults(settings.get('archive'), pattern, settings.get('allowEmptyResults')
archiveResults(archive, pattern, allowEmpty)
}
}
def touchFiles(){
echo "[${STEP_NAME}] update test results"
echo 'update test results'
def patternArray = pattern.split(',')
for(def i = 0; i < patternArray.length; i++){
sh "find . -wholename '${patternArray[i].trim()}' -exec touch {} \\;"
}
}
@NonCPS
def failUnstableBuild(currentBuild) {
if (currentBuild.result == 'UNSTABLE') {
echo "Current Build Status: ${currentBuild.result}"
currentBuild.result = 'FAILURE'
error 'Some tests failed!'
}
}
def getJMeterParser(){
// handle package renaming of JMeterParser class
try {
@ -173,30 +152,16 @@ def getJMeterParser(){
}
}
@NonCPS
def asMap(parameter) {
return Boolean.TRUE.equals(parameter)
?[:]
:parameter
}
def archiveResults(archive, pattern, allowEmpty) {
if(archive){
echo "[${STAP_NAME}] archive ${pattern}"
echo "archive ${pattern}"
archiveArtifacts artifacts: pattern, allowEmptyArchive: allowEmpty
}
}
@NonCPS
def prepare(parameters){
// ensure tool maps are initialized
parameters.junit = toMap(parameters.junit)
parameters.jacoco = toMap(parameters.jacoco)
parameters.cobertura = toMap(parameters.cobertura)
parameters.jmeter = toMap(parameters.jmeter)
return parameters
}
@NonCPS
def toMap(settings){
if(isMap(settings))
settings.put('active', true)
else if(Boolean.TRUE.equals(parameter))
settings = [active: true]
else
settings = [active: false]
return settings
}

View File

@ -0,0 +1,220 @@
import static java.util.Arrays.asList
import com.cloudbees.groovy.cps.NonCPS
import com.sap.piper.ConfigurationLoader
import com.sap.piper.ConfigurationMerger
import groovy.transform.Field
@Field def STEP_NAME = 'testsPublishResults'
/**
* 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]
prepareDefaultValues script: script
Map configurationKeys = [
'junit': [
'patter': null,
'updateResults': null,
'allowEmptyResults': null,
'archive': null,
'active': null
],
'jacoco': [
'pattern': null,
'include': null,
'exclude': null,
'allowEmptyResults': null,
'archive': null,
'active': null
],
'cobertura': [
'pattern': null,
'onlyStableBuilds': null,
'allowEmptyResults': null,
'archive': null,
'active': null
],
'jmeter': [
'pattern': null,
'errorFailedThreshold': null,
'errorUnstableThreshold': null,
'errorUnstableResponseTimeThreshold': null,
'relativeFailedThresholdPositive': null,
'relativeFailedThresholdNegative': null,
'relativeUnstableThresholdPositive': null,
'relativeUnstableThresholdNegative': null,
'modeOfThreshold': null,
'modeThroughput': null,
'nthBuildNumber': null,
'configType': null,
'failBuildIfNoResultFile': null,
'compareBuildPrevious': null,
'allowEmptyResults': null,
'archive': null,
'active': null
]
]
final Map stepDefaults = ConfigurationLoader.defaultStepConfiguration(script, STEP_NAME)
final Map stepConfiguration = ConfigurationLoader.stepConfiguration(script, STEP_NAME)
println("ORIGINAL PARAMETERS: " + parameters)
prepare(parameters)
println("PREPARED PARAMETERS: " + parameters)
Map configuration = ConfigurationMerger.mergeDeepStructure(
parameters, configurationKeys,
stepConfiguration, configurationKeys, stepDefaults)
println("CONFIG MERGED: " + configuration)
// 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(
//step([
// $class: 'JUnitResultArchiver',
testResults: pattern,
allowEmptyResults: allowEmpty,
healthScaleFactor: 100.0,
)
// archive results
archiveResults(settings.get('archive'), pattern, allowEmpty)
}
}
def publishJacocoReport(Map settings = [:]) {
if(settings.active){
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
jacoco(
//step([
// $class: 'JacocoPublisher',
execPattern: pattern,
inclusionPattern: settings.get('include'),
exclusionPattern: settings.get('exclude')
)
// archive results
archiveResults(settings.get('archive'), pattern, allowEmpty)
}
}
def publishCoberturaReport(Map settings = [:]) {
if(settings.active){
def pattern = settings.get('pattern')
def allowEmpty = settings.get('allowEmptyResults')
cobertura(
//step([
// $class: 'CoberturaPublisher',
coberturaReportFile: pattern,
onlyStable: settings.get('onlyStableBuilds'),
failNoReports: !allowEmpty,
failUnstable: false,
failUnhealthy: false,
autoUpdateHealth: false,
autoUpdateStability: false,
maxNumberOfBuilds: 0
)
// archive results
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')
step([
$class: 'PerformancePublisher',
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'),
compareBuildPrevious: settings.get('compareBuildPrevious'),
parsers: asList(getJMeterParser().newInstance(pattern))
])
// archive results
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 {} \\;"
}
}
@NonCPS
def getJMeterParser(){
// handle package renaming of JMeterParser class
try {
return this.class.classLoader.loadClass("hudson.plugins.performance.parsers.JMeterParser")
} catch (Exception e) {
return this.class.classLoader.loadClass("hudson.plugins.performance.JMeterParser")
}
}
def archiveResults(archive, pattern, allowEmpty) {
if(archive){
echo "[${STAP_NAME}] archive ${pattern}"
archiveArtifacts artifacts: pattern, allowEmptyArchive: allowEmpty
}
}
@NonCPS
def prepare(parameters){
// ensure tool maps are initialized
parameters.junit = toMap(parameters.junit)
parameters.jacoco = toMap(parameters.jacoco)
parameters.cobertura = toMap(parameters.cobertura)
parameters.jmeter = toMap(parameters.jmeter)
return parameters
}
@NonCPS
def toMap(settings){
if(isMap(settings))
settings.put('active', true)
else if(Boolean.TRUE.equals(settings))
settings = [active: true]
else
settings = [active: false]
return settings
}
@NonCPS
def isMap(object){
return object in Map
}