Skip to content

Commit

Permalink
[JENKINS-47194] xUnit should not consider report generated by other x…
Browse files Browse the repository at this point in the history
…Unit step

XUnit build step or publisher use an instance of processor than generates output report to a shared common folder. This cause on latest step thresholds consider also reports produced by previous steps. Processor now generate an identifier that permits to generate reports into an unique output folder.
  • Loading branch information
nfalco79 committed May 10, 2018
1 parent 3f23643 commit 10d5280
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 194 deletions.
29 changes: 18 additions & 11 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitProcessor.java
Expand Up @@ -26,8 +26,8 @@

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.UUID;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -66,12 +66,13 @@
/**
* @author Gregory Boissinot
*/
public class XUnitProcessor implements Serializable {
private static final long serialVersionUID = 1L;
private TestType[] tools;
private XUnitThreshold[] thresholds;
private int thresholdMode;
private ExtraConfiguration extraConfiguration;
public class XUnitProcessor {

private final TestType[] tools;
private final XUnitThreshold[] thresholds;
private final int thresholdMode;
private final ExtraConfiguration extraConfiguration;
private final String processorId;

public XUnitProcessor(@Nonnull TestType[] tools, @CheckForNull XUnitThreshold[] thresholds, int thresholdMode, @Nonnull ExtraConfiguration extraConfiguration) {
if (tools == null) {
Expand All @@ -84,6 +85,7 @@ public XUnitProcessor(@Nonnull TestType[] tools, @CheckForNull XUnitThreshold[]
this.thresholds = thresholds != null ? Arrays.copyOf(thresholds, thresholds.length) : new XUnitThreshold[0];
this.thresholdMode = thresholdMode;
this.extraConfiguration = extraConfiguration;
this.processorId = UUID.randomUUID().toString();
}

public boolean performXunit(boolean dryRun, AbstractBuild<?, ?> build, BuildListener listener)
Expand Down Expand Up @@ -265,7 +267,8 @@ private FilePath getCustomStylesheet(final TestType tool, final Run<?, ?> build,
}

private XUnitTransformer getXUnitTransformerObject(final XUnitToolInfo xUnitToolInfo, final TaskListener listener) {
return Guice.createInjector(new AbstractModule() {
// TODO why use Guice in this manner it's the quite the same of instantiate classes directly
XUnitTransformer transformer = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(TaskListener.class).toInstance(listener);
Expand All @@ -276,6 +279,8 @@ protected void configure() {
bind(XUnitReportProcessorService.class).in(Singleton.class);
}
}).getInstance(XUnitTransformer.class);
transformer.setProcessorId(processorId);
return transformer;
}

private TestResultAction getTestResultAction(Run<?, ?> build) {
Expand Down Expand Up @@ -344,7 +349,7 @@ private TestResult getTestResult(final FilePath workspace,
@Override
public TestResult invoke(File ws, VirtualChannel channel) throws IOException {
final long nowSlave = System.currentTimeMillis();
File generatedJunitDir = new File(ws, XUnitDefaultValues.GENERATED_JUNIT_DIR);
File generatedJunitDir = new File(new File(ws, XUnitDefaultValues.GENERATED_JUNIT_DIR), processorId);
IOUtils.mkdirs(generatedJunitDir);
FileSet fs = Util.createFileSet(generatedJunitDir, junitFilePattern);
DirectoryScanner ds = fs.getDirectoryScanner();
Expand Down Expand Up @@ -420,19 +425,21 @@ private Result processResultThreshold(XUnitLog log,

private void processDeletion(boolean dryRun, FilePath workspace, XUnitLog xUnitLog) throws XUnitException {
try {
FilePath generatedJunitDir = workspace.child(XUnitDefaultValues.GENERATED_JUNIT_DIR).child(processorId);

boolean keepJUnitDirectory = false;
for (TestType tool : tools) {
InputMetric inputMetric = tool.getInputMetric();

if (dryRun || tool.isDeleteOutputFiles()) {
workspace.child(XUnitDefaultValues.GENERATED_JUNIT_DIR + "/" + inputMetric.getToolName()).deleteRecursive();
generatedJunitDir.child(inputMetric.getToolName()).deleteRecursive();
} else {
//Mark the tool file parent directory to no deletion
keepJUnitDirectory = true;
}
}
if (!keepJUnitDirectory) {
workspace.child(XUnitDefaultValues.GENERATED_JUNIT_DIR).deleteRecursive();
generatedJunitDir.deleteRecursive();
}
} catch (IOException | InterruptedException e) {
throw new XUnitException("Problem on deletion", e);
Expand Down
Expand Up @@ -26,7 +26,6 @@

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
Expand All @@ -42,17 +41,14 @@
import hudson.remoting.VirtualChannel;
import jenkins.security.Roles;

public class XUnitTransformer extends XUnitService implements FilePath.FileCallable<Boolean>, Serializable {
public class XUnitTransformer extends XUnitService implements FilePath.FileCallable<Boolean> {

private XUnitReportProcessorService xUnitReportProcessorService;

private XUnitConversionService xUnitConversionService;

private XUnitValidationService xUnitValidationService;

private XUnitToolInfo xUnitToolInfo;

private XUnitLog xUnitLog;
private String processorId;

@Inject
void load(
Expand All @@ -79,8 +75,10 @@ void load(
@Override
public Boolean invoke(File ws, VirtualChannel channel) throws IOException, InterruptedException {
try {

File junitOutputDir = new File(ws, XUnitDefaultValues.GENERATED_JUNIT_DIR);
if (processorId != null) {
junitOutputDir = new File(junitOutputDir, processorId);
}
if (!junitOutputDir.exists() && !junitOutputDir.mkdirs()) {
String msg = "Can't create the path " + junitOutputDir + ". Maybe the directory already exists.";
xUnitLog.warningConsoleLogger(msg);
Expand Down Expand Up @@ -191,4 +189,12 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException, Inter
public void checkRoles(RoleChecker checker) throws SecurityException {
checker.check(this, Roles.SLAVE);
}

public String getProcessorId() {
return processorId;
}

public void setProcessorId(String processorId) {
this.processorId = processorId;
}
}
102 changes: 102 additions & 0 deletions src/test/java/org/jenkinsci/plugins/xunit/XUnitPublisherTest.java
@@ -0,0 +1,102 @@
/*
The MIT License (MIT)
Copyright (c) 2016, Andrew Bayer, CloudBees Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package org.jenkinsci.plugins.xunit;

import java.io.IOException;

import org.jenkinsci.lib.dtkit.type.TestType;
import org.jenkinsci.plugins.xunit.threshold.FailedThreshold;
import org.jenkinsci.plugins.xunit.threshold.XUnitThreshold;
import org.jenkinsci.plugins.xunit.types.GoogleTestType;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;

public class XUnitPublisherTest {

@SuppressWarnings("serial")
public static class SpyXUnitPublisher extends XUnitPublisher {

public SpyXUnitPublisher(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin) {
super(tools, thresholds, thresholdMode, testTimeMargin);
}

@Override
public void perform(Run<?, ?> build,
FilePath workspace,
Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {
super.perform(build, workspace, launcher, listener);
Assert.assertEquals("Unexpected build FAILURE setup by the first publisher", build.getResult(), Result.SUCCESS);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public BuildStepDescriptor getDescriptor() {
return new BuildStepDescriptor<Publisher>(SpyXUnitPublisher.class) {

@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
};
}
}

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

@LocalData
@Issue("JENKINS-47194")
@Test
public void different_build_steps_use_separate_output_folders() throws Exception {
// FreeStyleProject job = getBaseJob("JENKINS-47194");
FreeStyleProject job = jenkinsRule.jenkins.createProject(FreeStyleProject.class, "JENKINS-47194");

TestType[] tools = new TestType[] { new GoogleTestType("input.xml", false, false, false, true) };

XUnitThreshold threshold1 = new FailedThreshold();
threshold1.setFailureThreshold("2");
job.getPublishersList().add(new SpyXUnitPublisher(tools, new XUnitThreshold[] { threshold1 }, 1, "3000"));

XUnitThreshold threshold2 = new FailedThreshold();
threshold2.setFailureThreshold("0");
job.getPublishersList().add(new XUnitPublisher(tools, new XUnitThreshold[] { threshold2 }, 1, "3000"));

jenkinsRule.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
}
}
19 changes: 16 additions & 3 deletions src/test/java/org/jenkinsci/plugins/xunit/XUnitWorkflowTest.java
Expand Up @@ -65,7 +65,7 @@ public void xunitBuilderThresholdsAreOptional() throws Exception {
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'XUnitBuilder', tools: [[$class: 'GoogleTestType', pattern: 'input.xml']], thresholdMode: 1])\n"
+ "}"));
+ "}", true));

jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
}
Expand All @@ -80,9 +80,22 @@ public void xunitPublisherWorkflowStepTest() throws Exception {

jenkinsRule.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
}

// node('master') {
// stage('1') {
// step([$class: 'XUnitBuilder',
// thresholds: [[$class: 'FailedThreshold', failureThreshold: "1"]],
// tools: [[$class: 'JUnitType', pattern: "tmp.xml"]]])
// }
// stage('2') {
// step([$class: 'XUnitBuilder',
// thresholds: [[$class: 'FailedThreshold', failureThreshold: "0"]],
// tools: [[$class: 'JUnitType', pattern: "tmp2.xml"]]])
// }
// }

@Issue("JENKINS-37611")
@Test
public void xunit() throws Exception {
public void xunitPipelineStepDefinition() throws Exception {
WorkflowJob job = getBaseJob("readablePublisherPipeline");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
Expand Down

This file was deleted.

0 comments on commit 10d5280

Please sign in to comment.