Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-12205] : recent versions of MatrixProject no longer in…
…clude the test results of their child jobs. Iterate over them manually.
  • Loading branch information
Willem Verstraeten committed Sep 15, 2015
1 parent 09948c5 commit dabdd8e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/main/java/hudson/plugins/view/dashboard/test/TestUtil.java
@@ -1,10 +1,10 @@
package hudson.plugins.view.dashboard.test;

import hudson.matrix.MatrixProject;
import hudson.maven.reporters.SurefireAggregatedReport;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResultProjectAction;

Expand All @@ -15,54 +15,67 @@ public class TestUtil {
/**
* Summarize the last test results from the passed set of jobs. If a job
* doesn't include any tests, add a 0 summary.
*
*
* @param jobs
* @return
*/
public static TestResultSummary getTestResultSummary(Collection<TopLevelItem> jobs) {
TestResultSummary summary = new TestResultSummary();

for (TopLevelItem item : jobs) {
if (item instanceof Job) {
Job job = (Job) item;
boolean addBlank = true;
TestResultProjectAction testResults = job.getAction(TestResultProjectAction.class);
for (TopLevelItem item : jobs) {
if (item instanceof MatrixProject) {
MatrixProject mp = (MatrixProject) item;

if (testResults != null) {
AbstractTestResultAction tra = testResults.getLastTestResultAction();
for (Job job : mp.getAllJobs()) {
if (job != mp) { //getAllJobs includes the parent job too, so skip that
summarizeJob(job, summary);
}
}
}
else if (item instanceof Job) {
Job job = (Job) item;
summarizeJob(job, summary);
}
}

return summary;
}

private static void summarizeJob(Job job, TestResultSummary summary) {
boolean addBlank = true;
TestResultProjectAction testResults = job.getAction(TestResultProjectAction.class);

if (tra != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, tra.getTotalCount(), tra.getFailCount(), tra.getSkipCount()));
}
} else {
SurefireAggregatedReport surefireTestResults = job.getAction(SurefireAggregatedReport.class);
if (surefireTestResults != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, surefireTestResults.getTotalCount(), surefireTestResults.getFailCount(), surefireTestResults.getSkipCount()));
}
}
if (testResults != null) {
AbstractTestResultAction tra = testResults.getLastTestResultAction();

if (addBlank) {
summary.addTestResult(new TestResult(job, 0, 0, 0));
}
}
if (tra != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, tra.getTotalCount(), tra.getFailCount(), tra.getSkipCount()));
}
} else {
SurefireAggregatedReport surefireTestResults = job.getAction(SurefireAggregatedReport.class);
if (surefireTestResults != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, surefireTestResults.getTotalCount(), surefireTestResults.getFailCount(), surefireTestResults.getSkipCount()));
}
}

return summary;
if (addBlank) {
summary.addTestResult(new TestResult(job, 0, 0, 0));
}
}

public static TestResult getTestResult(Run run) {
AbstractTestResultAction tra = run.getAction(AbstractTestResultAction.class);
if (tra != null) {
return new TestResult(run.getParent(), tra.getTotalCount(), tra.getFailCount(), tra.getSkipCount());
}
}

SurefireAggregatedReport surefireTestResults = run.getAction(SurefireAggregatedReport.class);
if (surefireTestResults != null) {
return new TestResult(run.getParent(), surefireTestResults.getTotalCount(), surefireTestResults.getFailCount(), surefireTestResults.getSkipCount());
}

return new TestResult(run.getParent(), 0, 0, 0);
}
}
@@ -0,0 +1,60 @@
package hudson.plugins.view.dashboard.test;

import hudson.Launcher;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.TopLevelItem;
import hudson.tasks.junit.JUnitResultArchiver;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;

import java.io.IOException;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class TestSummaryForMatrixJobs {
@Rule
public JenkinsRule jenkins = new JenkinsRule();

private MatrixProject matrixProject;

@Before
public void createMatrixJob() throws IOException {
matrixProject = jenkins.createMatrixProject("top");
matrixProject.getAxes().add(new TextAxis("param", "one", "two"));
matrixProject.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("testresult.xml").copyFrom(TestSummaryForMatrixJobs.class.getResource("/hudson/plugins/view/dashboard/test_failure.xml"));
return true;
}
});
matrixProject.getPublishersList().add(new JUnitResultArchiver("testresult.xml", true, null));
}

@Test
public void summaryIncludesMatrixJobs() throws Exception {
MatrixBuild result = matrixProject.scheduleBuild2(0).get();
jenkins.assertBuildStatus(Result.UNSTABLE, result);

TestResultSummary testSummary = TestUtil.getTestResultSummary(Collections.singleton((TopLevelItem) matrixProject));
assertThat(testSummary.getFailed(), is((2)));
assertThat(testSummary.getSuccess(), is((2)));
}

@After
public void cleanupMatrixJob() throws IOException, InterruptedException {
matrixProject.delete();
}

}
14 changes: 14 additions & 0 deletions src/test/resources/hudson/plugins/view/dashboard/test_failure.xml
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="1" hostname="anniesue" name="com.luciad.format.aixmcommon.view.lightspeed.TLspAIXMStylerAcceptanceTest" skipped="0" tests="2" time="12.829" timestamp="2015-09-14T14:37:11">
<properties/>
<testcase classname="test" name="failure" time="0.376">
<failure message="description" type="junit.framework.AssertionFailedError">
detail message
</failure>
</testcase>
<testcase classname="test.case" name="success" time="0.893" />
<system-out><![CDATA[System out
]]></system-out>
<system-err><![CDATA[System err
]]></system-err>
</testsuite>

0 comments on commit dabdd8e

Please sign in to comment.