137 lines
6.2 KiB
Groovy
137 lines
6.2 KiB
Groovy
import org.codehaus.groovy.control.CompilerConfiguration
|
|
|
|
// Execute the actual Jenkinsfile with controlled Pipeline steps. This checks our
|
|
// control flow; Jenkins plugins, SCM checks and controller validation remain external.
|
|
abstract class PipelineFixture extends Script {
|
|
Map env = [:]
|
|
String result = 'SUCCESS'
|
|
String currentStage
|
|
Closure stagesBlock
|
|
Closure postBlock
|
|
boolean failClean = false
|
|
boolean failVerification = false
|
|
boolean junitAvailable = true
|
|
boolean coverageAvailable = true
|
|
boolean htmlAvailable = true
|
|
boolean archiveAvailable = true
|
|
String failedPublisher
|
|
List<Map> publications = []
|
|
List<String> commands = []
|
|
List<String> failures = []
|
|
List<Map> checks = []
|
|
|
|
Object getAny() { 'any' }
|
|
void agent(Object ignored) {}
|
|
void environment(Closure body) {
|
|
body.delegate = env
|
|
body.resolveStrategy = Closure.DELEGATE_FIRST
|
|
body()
|
|
}
|
|
void pipeline(Closure body) {
|
|
body()
|
|
try { stagesBlock() }
|
|
catch (RuntimeException failure) { recordFailure(failure) }
|
|
finally { postBlock() }
|
|
}
|
|
void stages(Closure body) { stagesBlock = body }
|
|
void post(Closure body) { postBlock = body }
|
|
void stage(String name, Closure body) { currentStage = name; body() }
|
|
void steps(Closure body) { body() }
|
|
void script(Closure body) { body() }
|
|
void always(Closure body) { body() }
|
|
void withGradle(Closure body) { body() }
|
|
void withChecks(Map options, Closure body) { checks << options; body() }
|
|
void sh(String command) {
|
|
commands << command
|
|
if (currentStage == 'Clean' && failClean) throw new IllegalStateException('clean failed')
|
|
if (currentStage == 'Tests and coverage' && failVerification) throw new IllegalStateException('verification failed')
|
|
}
|
|
void catchError(Map options, Closure body) {
|
|
assert options.buildResult == 'FAILURE'
|
|
try { body() } catch (RuntimeException failure) { recordFailure(failure) }
|
|
}
|
|
void recordFailure(RuntimeException failure) { result = 'FAILURE'; failures << failure.message }
|
|
boolean fileExists(String path) {
|
|
assert path == 'build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml'
|
|
coverageAvailable
|
|
}
|
|
void echo(String message) {}
|
|
void error(String message) { throw new IllegalStateException(message) }
|
|
void publish(String name, Map options) {
|
|
publications << [name: name, options: options]
|
|
if (failedPublisher == name) throw new IllegalStateException("${name} publisher failed")
|
|
}
|
|
void junit(Map options) {
|
|
publish('junit', options)
|
|
if (!junitAvailable && !options.allowEmptyResults) error('missing JUnit')
|
|
}
|
|
void recordCoverage(Map options) { publish('coverage', options) }
|
|
void publishHTML(Map options) {
|
|
publish('html', options)
|
|
if (!htmlAvailable && !options.target.allowMissing) error('missing HTML')
|
|
}
|
|
void archiveArtifacts(Map options) {
|
|
publish('archive', options)
|
|
if (!archiveAvailable && !options.allowEmptyArchive) error('missing artifacts')
|
|
}
|
|
}
|
|
|
|
assert args.length == 1 : 'Pass the Jenkinsfile path'
|
|
File pipeline = new File(args[0])
|
|
assert pipeline.isFile() : "Missing Jenkinsfile: ${pipeline}"
|
|
def configuration = new CompilerConfiguration(scriptBaseClass: PipelineFixture.name)
|
|
def shell = new GroovyShell(PipelineFixture.classLoader, new Binding(), configuration)
|
|
def runPipeline = { Map conditions = [:] ->
|
|
PipelineFixture fixture = shell.parse(pipeline) as PipelineFixture
|
|
conditions.each { key, value -> fixture.setProperty(key as String, value) }
|
|
fixture.run()
|
|
fixture
|
|
}
|
|
def publishers = ['junit', 'coverage', 'html', 'archive']
|
|
def successful = runPipeline()
|
|
assert successful.result == 'SUCCESS'
|
|
assert successful.publications*.name == publishers
|
|
assert successful.commands.size() == 2
|
|
assert successful.commands[0].startsWith('./gradlew clean ')
|
|
assert successful.commands[1].contains('test jacocoTestReport jenkinsPipelineTest --continue')
|
|
def junit = successful.publications.find { it.name == 'junit' }.options
|
|
assert junit.allowEmptyResults == false
|
|
assert junit.checksName == 'Test / Tests and coverage'
|
|
def coverage = successful.publications.find { it.name == 'coverage' }.options
|
|
assert coverage.enabledForFailure && coverage.failOnError
|
|
assert coverage.qualityGates.collectEntries { [(it.metric): it.threshold] } ==
|
|
[CLASS: 50.0, METHOD: 50.0, LINE: 50.0, INSTRUCTION: 50.0, BRANCH: 40.0]
|
|
assert coverage.qualityGates.every { it.baseline == 'PROJECT' && it.criticality == 'UNSTABLE' }
|
|
assert coverage.tools == [[parser: 'JACOCO', pattern: 'build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml']]
|
|
assert successful.publications.find { it.name == 'archive' }.options.onlyIfSuccessful == false
|
|
|
|
def failedClean = runPipeline(failClean: true)
|
|
assert failedClean.result == 'FAILURE'
|
|
assert failedClean.publications.empty // Even when stale report paths would exist.
|
|
assert failedClean.commands.size() == 1
|
|
|
|
def failedTests = runPipeline(failVerification: true)
|
|
assert failedTests.result == 'FAILURE'
|
|
assert failedTests.publications*.name == publishers
|
|
assert failedTests.env.VERIFICATION_SUCCEEDED == 'false'
|
|
assert failedTests.publications.find { it.name == 'junit' }.options.allowEmptyResults
|
|
assert failedTests.publications.find { it.name == 'coverage' }.options.enabledForFailure
|
|
|
|
def noReports = runPipeline(failVerification: true, junitAvailable: false,
|
|
coverageAvailable: false, htmlAvailable: false, archiveAvailable: false)
|
|
assert noReports.result == 'FAILURE'
|
|
assert noReports.publications*.name == ['junit', 'html', 'archive']
|
|
assert noReports.failures == ['verification failed']
|
|
|
|
['junitAvailable', 'coverageAvailable', 'htmlAvailable'].each { report ->
|
|
def missing = runPipeline([(report): false])
|
|
assert missing.result == 'FAILURE' : "Missing successful-build ${report} was ignored"
|
|
assert missing.publications*.name.containsAll(['junit', 'html', 'archive'])
|
|
}
|
|
publishers.each { publisher ->
|
|
def failed = runPipeline(failedPublisher: publisher)
|
|
assert failed.result == 'FAILURE'
|
|
assert failed.publications*.name == publishers : "${publisher} failure suppressed another publisher"
|
|
}
|
|
println 'Jenkins pipeline fixture passed: 11 success/failure scenarios (no controller or plugins invoked).'
|