Skip to content

Commit

Permalink
wip: Add unit test for Jenkinsfile
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Mar 31, 2017
1 parent a91e668 commit 86f4245
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 28 deletions.
53 changes: 25 additions & 28 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

// Import pipeline library for utility methods:
// ansicolor(), info.*, notify.*, pullRequests.*, strings.*
@Library('github.com/zanata/zanata-pipeline-library@master')
// You can't put @Library in front of try (eg), so this dummy
// ensures that won't happen again:
def dummyForLibraryAnnotation
@Library('github.com/zanata/zanata-pipeline-library@pipelineunit') _

notify.env = env
//notify.steps = steps

// Define project properties: general properties for the Pipeline-defined jobs.
// 1. discard old artifacts and build logs
Expand Down Expand Up @@ -68,7 +67,8 @@ timestamps {

stage('Checkout') {
// info methods output diagnostic information about the node
info.printNode()
info.printNode(env)
info.printEnv()
// notify methods send instant messages about the build progress
notify.started()

Expand All @@ -83,8 +83,6 @@ timestamps {
// Build and Unit Tests
// The built files are stashed for integration tests in other nodes.
stage('Build') {
info.printNode()
info.printEnv()

// validate translations
sh """./run-clean.sh ./mvnw -e -V \
Expand Down Expand Up @@ -117,15 +115,15 @@ timestamps {

setJUnitPrefix("UNIT", surefireTestReports)
// gather surefire results; mark build as unstable in case of failures
junit([
testResults: "**/${surefireTestReports}"
])
junit(testResults: "**/${surefireTestReports}")

// TODO send to codecov.io (NB: need to get correct token for zanata-platform, configured by env var)

// notify if compile+unit test successful
// TODO update notify (in pipeline library) to support Rocket.Chat webhook integration
notify.testResults("UNIT")
notify.testResults("UNIT", currentBuild)

// TODO ensure the pipeline aborts in case of test failures

// archive build artifacts (and cross-referenced source code)
archive "**/${jarFiles},**/${warFiles},**/target/site/xref/**"
Expand All @@ -143,6 +141,7 @@ timestamps {
} catch (e) {
notify.failed()
currentBuild.result = 'FAILURE'
// abort the rest of the pipeline
throw e
}
}
Expand All @@ -155,22 +154,20 @@ timestamps {
stage('Integration tests') {
try {
// define tasks which will run in parallel
def tasks = [:]

tasks["Integration tests: WILDFLY"] = {
integrationTests('wildfly8')
}
tasks["Integration tests: JBOSSEAP"] = {
integrationTests('jbosseap6')
}
// abort other tasks (for faster feedback) as soon as one fails
tasks.failFast = true
def tasks = [
// FIXME enable this
// "WILDFLY": { integrationTests('wildfly8') },
"JBOSSEAP": { integrationTests('jbosseap6') },
// abort other tasks (for faster feedback) as soon as one fails
// disabled; not currently handled by pipeline-unit
// failFast: true
]

// run integration test tasks in parallel
parallel tasks
// notify.successful()
} catch (e) {
// When it cannot find the failfast report
// When it cannot find the failsafe xml files
echo "ERROR integrationTests: ${e.toString()}"
}

Expand Down Expand Up @@ -200,7 +197,7 @@ void debugChromeDriver() {
void integrationTests(String appserver) {
def failsafeTestReports='target/failsafe-reports/TEST-*.xml'
node(LABEL) {
info.printNode()
info.printNode(env)
info.printEnv()
echo "WORKSPACE=${env.WORKSPACE}"
checkout scm
Expand Down Expand Up @@ -275,12 +272,12 @@ void integrationTests(String appserver) {
excludes: '**/BACKUP-*.log')
} finally {
setJUnitPrefix(appserver, failsafeTestReports)
junit([
testResults: "**/${failsafeTestReports}"
junit(testResults: "**/${failsafeTestReports}"
// TODO enable after https://issues.jenkins-ci.org/browse/JENKINS-33168 is fixed
// testDataPublishers: [[$class: 'StabilityTestDataPublisher']]
])
notify.testResults(appserver.toUpperCase())
// , testDataPublishers: [[$class: 'StabilityTestDataPublisher']]
)
notify.testResults(appserver.toUpperCase(), currentBuild)
// TODO ensure the pipeline aborts in case of test failures
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions build-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
</developer>
</developers>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
Expand All @@ -69,6 +74,21 @@
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.lesfurets</groupId>
<artifactId>jenkins-pipeline-unit</artifactId>
<version>0.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>release</id>
Expand Down
85 changes: 85 additions & 0 deletions build-tools/src/test/java/TestJenkinsfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import com.cloudbees.groovy.cps.impl.CpsCallableInvocation;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.lesfurets.jenkins.unit.BasePipelineTest;
import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS;
import com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration;
import groovy.lang.Closure;
import groovy.lang.Script;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static com.lesfurets.jenkins.unit.MethodSignature.method;
import static com.lesfurets.jenkins.unit.global.lib.GitSource.gitSource;
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library;
import static com.lesfurets.jenkins.unit.global.lib.LocalSource.localSource;

public class TestJenkinsfile extends BasePipelineTestCPS {
//public class TestJenkinsfile extends BasePipelineTest {

private static final String LIB_PATH = "target/libs";

@Before
@Override
public void setUp() throws Exception {
super.setUp();

LibraryConfiguration library = library()
.name("github.com/zanata/zanata-pipeline-library")
.retriever(gitSource("https://github.com/zanata/zanata-pipeline-library"))
// uncomment to use already-downloaded (perhaps modified) copy instead of git:
// .retriever(localSource(LIB_PATH))
.targetPath(LIB_PATH)
.defaultVersion("master")
.allowOverride(true)
.implicit(false)
.build();
getHelper().registerSharedLibrary(library);

// set up mock methods
getHelper().registerAllowedMethod("timestamps", ImmutableList.of(Closure.class), null);
getHelper().registerAllowedMethod("wrap", ImmutableList.of(Map.class, Closure.class), null);
getHelper().registerAllowedMethod("hipchatSend", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("junit", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("archive", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("archive", ImmutableList.of(Object.class), null);
getHelper().registerAllowedMethod("stash", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("unstash", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("unstash", ImmutableList.of(String.class), null);
getHelper().registerAllowedMethod(method("findFiles", Map.class), args -> {
return ImmutableList.of();
});

// environment variables
Map<String, String> env = new HashMap<>();
env.put("BUILD_URL", "http://example.com/job/JobName/123");
env.put("JOB_NAME", "JobName");
env.put("BUILD_NUMBER", "123");
env.put("DEFAULT_NODE", "master");
env.put("NODE_NAME", "jenkins-pipeline-unit");

// global variables
getBinding().setProperty("env", env);
getBinding().setProperty("LABEL", "master");
getBinding().setProperty("scm", ImmutableMap.of());
getBinding().setProperty("currentBuild", new HashMap<>());
}

@Test
public void should_execute_without_errors() throws Exception {

try {
Script script = loadScript("../Jenkinsfile");
printCallStack();
} catch (CpsCallableInvocation e) {
// if the script fails, we need the call stack to tell us where the problem is
// (CpsCallableInvocation tells us very little)
System.err.println("CPS call stack:");
getHelper().getCallStack().forEach(System.err::println);
throw e;
}
}
}

0 comments on commit 86f4245

Please sign in to comment.