Skip to content

Commit

Permalink
Remove system log that are accessible only for administrator and some…
Browse files Browse the repository at this point in the history
… serialisation improvements.
  • Loading branch information
Nikolas Falco committed Jun 6, 2018
1 parent d714f7a commit f8a62e9
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 217 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Expand Up @@ -42,7 +42,6 @@
<properties>
<dtkit.frmk.version>2.0.0</dtkit.frmk.version>
<saxon.version>9.1.0.8</saxon.version>
<dry.run.version>0.1</dry.run.version>
<xmlunit.version>1.6</xmlunit.version>
<mockito.version>2.17.0</mockito.version>
<jenkins.version>1.651.3</jenkins.version>
Expand Down Expand Up @@ -112,12 +111,6 @@
<version>${dtkit.frmk.version}</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.lib</groupId>
<artifactId>dry-run-lib</artifactId>
<version>${dry.run.version}</version>
</dependency>

<dependency>
<groupId>net.sourceforge.saxon</groupId>
<artifactId>saxon</artifactId>
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitProcessor.java
Expand Up @@ -143,22 +143,22 @@ public boolean performXUnit(boolean dryRun, Run<?, ?> build, FilePath workspace,
throws IOException, InterruptedException {
final XUnitLog xUnitLog = getXUnitLogObject(listener);
try {
xUnitLog.infoConsoleLogger("Starting to record.");
xUnitLog.info("Starting to record.");

boolean continueTestProcessing;
try {
continueTestProcessing = performTests(xUnitLog, build, workspace, listener);
} catch (StopTestProcessingException e) {
build.setResult(Result.FAILURE);
xUnitLog.infoConsoleLogger("There are errors when processing test results.");
xUnitLog.infoConsoleLogger("Skipping tests recording.");
xUnitLog.infoConsoleLogger("Stop build.");
xUnitLog.info("There are errors when processing test results.");
xUnitLog.info("Skipping tests recording.");
xUnitLog.info("Stop build.");
return true;
}

if (!continueTestProcessing) {
xUnitLog.infoConsoleLogger("There are errors when processing test results.");
xUnitLog.infoConsoleLogger("Skipping tests recording.");
xUnitLog.info("There are errors when processing test results.");
xUnitLog.info("Skipping tests recording.");
return true;
}

Expand All @@ -167,17 +167,17 @@ public boolean performXUnit(boolean dryRun, Run<?, ?> build, FilePath workspace,
Result result = getBuildStatus(build, xUnitLog);
if (result != null) {
if (!dryRun) {
xUnitLog.infoConsoleLogger("Setting the build status to " + result);
xUnitLog.info("Setting the build status to " + result);
build.setResult(result);
} else {
xUnitLog.infoConsoleLogger("Through the xUnit plugin, the build status will be set to " + result.toString());
xUnitLog.info("Through the xUnit plugin, the build status will be set to " + result.toString());
}
}
xUnitLog.infoConsoleLogger("Stopping recording.");
xUnitLog.info("Stopping recording.");
return true;

} catch (XUnitException xe) {
xUnitLog.errorConsoleLogger("The plugin hasn't been performed correctly: " + xe.getMessage());
xUnitLog.error("The plugin hasn't been performed correctly: " + xe.getMessage());
build.setResult(Result.FAILURE);
return false;
}
Expand All @@ -196,7 +196,7 @@ private boolean performTests(XUnitLog xUnitLog, Run<?, ?> build, FilePath worksp
XUnitReportProcessorService xUnitReportService = getXUnitReportProcessorServiceObject(listener);
boolean findTest = false;
for (TestType tool : tools) {
xUnitLog.infoConsoleLogger("Processing " + tool.getDescriptor().getDisplayName());
xUnitLog.info("Processing " + tool.getDescriptor().getDisplayName());

if (!isEmptyGivenPattern(xUnitReportService, tool)) {
String expandedPattern = getExpandedResolvedPattern(tool, build, listener);
Expand All @@ -218,26 +218,26 @@ private boolean performTests(XUnitLog xUnitLog, Run<?, ?> build, FilePath worksp
ie = (InterruptedException) originalException;

if (ie instanceof NoFoundTestException) {
xUnitLog.infoConsoleLogger("Failing BUILD.");
xUnitLog.info("Failing BUILD.");
throw new StopTestProcessingException();
}

if (ie instanceof SkipTestException) {
xUnitLog.infoConsoleLogger("Skipping the metric tool processing.");
xUnitLog.info("Skipping the metric tool processing.");
continue;
}

if (ie instanceof OldTestReportException) {
xUnitLog.infoConsoleLogger("Failing BUILD.");
xUnitLog.info("Failing BUILD.");
throw new StopTestProcessingException();
}

xUnitLog.warningConsoleLogger("Caught exception of unexpected type " + ie.getClass() + ", rethrowing");
xUnitLog.warn("Caught exception of unexpected type " + ie.getClass() + ", rethrowing");
throw ie;
}

if (!result && xUnitToolInfo.isStopProcessingIfError()) {
xUnitLog.infoConsoleLogger("Failing BUILD because 'set build failed if errors' option is activated.");
xUnitLog.info("Failing BUILD because 'set build failed if errors' option is activated.");
throw new StopTestProcessingException();
}
}
Expand Down Expand Up @@ -361,7 +361,7 @@ private void recordTestResult(Run<?, ?> build, FilePath workspace, TaskListener
}

if (result.getPassCount() == 0 && result.getFailCount() == 0) {
xUnitLog.warningConsoleLogger("All test reports are empty.");
xUnitLog.warn("All test reports are empty.");
}

if (existingAction == null) {
Expand Down Expand Up @@ -423,7 +423,7 @@ private Result processResultThreshold(XUnitLog log,

if (thresholds != null) {
for (XUnitThreshold threshold : thresholds) {
log.infoConsoleLogger(String.format("Check '%s' threshold.", threshold.getDescriptor().getDisplayName()));
log.info(String.format("Check '%s' threshold.", threshold.getDescriptor().getDisplayName()));
Result result;
if (XUnitDefaultValues.MODE_PERCENT == thresholdMode) {
result = threshold.getResultThresholdPercent(log, build, testResultAction, previousTestResultAction);
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitPublisher.java
Expand Up @@ -25,13 +25,11 @@
package org.jenkinsci.plugins.xunit;

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

import javax.annotation.CheckForNull;

import org.jenkinsci.Symbol;
import org.jenkinsci.lib.dryrun.DryRun;
import org.jenkinsci.lib.dtkit.descriptor.TestTypeDescriptor;
import org.jenkinsci.lib.dtkit.type.TestType;
import org.jenkinsci.plugins.xunit.threshold.FailedThreshold;
Expand All @@ -46,11 +44,8 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
Expand All @@ -66,7 +61,7 @@
*
* @author Gregory Boissinot
*/
public class XUnitPublisher extends Recorder implements DryRun, Serializable, SimpleBuildStep {
public class XUnitPublisher extends Recorder implements SimpleBuildStep {

@XStreamAlias("types")
private TestType[] tools;
Expand Down Expand Up @@ -129,20 +124,6 @@ public void perform(final Run<?, ?> build, FilePath workspace, Launcher launcher
xUnitProcessor.performXUnit(false, build, workspace, listener);
}

@Override
public boolean performDryRun(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
try {
XUnitProcessor xUnitProcessor = new XUnitProcessor(getTools(), getThresholds(), getThresholdMode(), getExtraConfiguration());
xUnitProcessor.performXUnit(true, build, build.getWorkspace(), listener);
} catch (Throwable t) {
listener.getLogger().println("[ERROR] - There is an error: " + t.getCause().getMessage());
}
//Always exit on success (returned code and status)
build.setResult(Result.SUCCESS);
return true;
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
Expand Down
Expand Up @@ -41,11 +41,11 @@

import hudson.FilePath;

public class XUnitConversionService extends XUnitService implements Serializable {
public class XUnitConversionService implements Serializable {
private XUnitLog xUnitLog;

@Inject
void load(XUnitLog xUnitLog) {
public XUnitConversionService(XUnitLog xUnitLog) {
this.xUnitLog = xUnitLog;
}

Expand All @@ -71,7 +71,7 @@ public File convert(XUnitToolInfo xUnitToolInfo, File inputFile, File junitOutpu
throw new XUnitException("Can't create " + parent);
}

File junitTargetFile = getTargetFile(parent, inputFile);
File junitTargetFile = getTargetFile(parent);
try {
if (inputMetric instanceof CustomInputMetric) {
return convertCustomInputMetric(xUnitToolInfo, inputFile, inputMetric, junitTargetFile);
Expand All @@ -84,12 +84,8 @@ public File convert(XUnitToolInfo xUnitToolInfo, File inputFile, File junitOutpu
inputMetric.convert(inputFile, junitTargetFile);
return junitTargetFile;

} catch (ConversionException ce) {
throw new XUnitException("Conversion error " + ce.getMessage(), ce);
} catch (InterruptedException ie) {
throw new XUnitException("Conversion error " + ie.getMessage(), ie);
} catch (IOException ie) {
throw new XUnitException("Conversion error " + ie.getMessage(), ie);
} catch (ConversionException | InterruptedException | IOException e) {
throw new XUnitException("Conversion error " + e.getMessage(), e);
}
}

Expand All @@ -102,15 +98,17 @@ public File convert(XUnitToolInfo xUnitToolInfo, File inputFile, File junitOutpu
* same workspace is enough. Version 1 UUID use MAC address but since
* {@code XUnitProcessor} does not copy slave node report files back to
* master is not necessary ensure file be globally unique.
*
*
* @param parent the parent folder under which create converted reports
*
* @return a node workspace unique file name
*/
private File getTargetFile(File parent, File inputFile) {
private File getTargetFile(File parent) {
String uniqueTestName = UUID.randomUUID().toString();
return new File(parent, XUnitDefaultValues.JUNIT_FILE_PREFIX + uniqueTestName + XUnitDefaultValues.JUNIT_FILE_EXTENSION);
}

private File convertCustomInputMetric(XUnitToolInfo xUnitToolInfo, File inputFile, InputMetric inputMetric, File junitTargetFile) throws IOException, InterruptedException, XUnitException {
private File convertCustomInputMetric(XUnitToolInfo xUnitToolInfo, File inputFile, InputMetric inputMetric, File junitTargetFile) {
CustomInputMetric customInputMetric = (CustomInputMetric) inputMetric;
customInputMetric.setCustomXSLFile(new File(xUnitToolInfo.getCusXSLFile().getRemote()));
inputMetric.convert(inputFile, junitTargetFile);
Expand All @@ -122,12 +120,12 @@ private File convertInputMetricXSL(XUnitToolInfo xUnitToolInfo, File inputFile,
FilePath userXSLFilePath = xUnitToolInfo.getUserContentRoot().child(inputMetricXSL.getUserContentXSLDirRelativePath());

if (userXSLFilePath.exists()) {
xUnitLog.infoConsoleLogger("Using the native embedded stylesheet in JENKINS_HOME.");
xUnitLog.info("Using the native embedded stylesheet in JENKINS_HOME.");
try {
return convertInputMetricXSLWithUserXSL(inputFile, junitTargetFile, inputMetricXSL, userXSLFilePath);
} catch (XUnitException xe) {
xUnitLog.errorConsoleLogger("Error occurs on the use of the user stylesheet: " + xe.getMessage());
xUnitLog.infoConsoleLogger("Trying to use the native embedded stylesheet.");
xUnitLog.error("Error occurs on the use of the user stylesheet: " + xe.getMessage());
xUnitLog.info("Trying to use the native embedded stylesheet.");
inputMetric.convert(inputFile, junitTargetFile);
return junitTargetFile;
}
Expand All @@ -138,9 +136,7 @@ private File convertInputMetricXSL(XUnitToolInfo xUnitToolInfo, File inputFile,
}

private File convertInputMetricXSLWithUserXSL(File inputFile, File junitTargetFile, InputMetricXSL inputMetricXSL, FilePath userXSLFilePath) throws XUnitException {

try {

List<FilePath> filePathList = userXSLFilePath.list();
if (filePathList.isEmpty()) {
throw new XUnitException(String.format("There are no XSLs in '%s'", userXSLFilePath.getRemote()));
Expand All @@ -155,9 +151,7 @@ private File convertInputMetricXSLWithUserXSL(File inputFile, File junitTargetFi

throw new XUnitException(String.format("There are no XSLs in '%s'", userXSLFilePath.getRemote()));

} catch (IOException e) {
throw new XUnitException("Error in the use of the user stylesheet", e);
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
throw new XUnitException("Error in the use of the user stylesheet", e);
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/jenkinsci/plugins/xunit/service/XUnitLog.java
Expand Up @@ -31,39 +31,40 @@
import hudson.model.TaskListener;

public class XUnitLog implements Serializable {
private static final long serialVersionUID = 1L;

private TaskListener buildListener;

@Inject
void set(TaskListener buildListener) {
public XUnitLog(TaskListener buildListener) {
this.buildListener = buildListener;
}

/**
* Log an info output to the console logger
* Log an info output to the console logger.
*
* @param message The message to be outputted
*/
public void infoConsoleLogger(String message) {
buildListener.getLogger().println("[xUnit] [INFO] - " + message);
public void info(String message) {
buildListener.getLogger().println("INFO: " + message);
}

/**
* Log an error output to the console logger
* Log an error output to the console logger.
*
* @param message The message to be outputted
*/
public void errorConsoleLogger(String message) {
buildListener.getLogger().println("[xUnit] [ERROR] - " + message);
public void error(String message) {
buildListener.error(message);
}

/**
* Log a warning output to the console logger
* Log a warning output to the console logger.
*
* @param message The message to be outputted
*/
public void warningConsoleLogger(String message) {
buildListener.getLogger().println("[xUnit] [WARNING] - " + message);
public void warn(String message) {
buildListener.getLogger().println("WARNING: " + message);
}

}
Expand Up @@ -39,12 +39,12 @@
import hudson.Util;


public class XUnitReportProcessorService extends XUnitService implements Serializable {
public class XUnitReportProcessorService implements Serializable {

private XUnitLog xUnitLog;

@Inject
void set(XUnitLog xUnitLog) {
public XUnitReportProcessorService(XUnitLog xUnitLog) {
this.xUnitLog = xUnitLog;
}

Expand Down Expand Up @@ -79,13 +79,11 @@ public List<String> findReports(XUnitToolInfo xUnitToolInfo, File parentPath, St
+ pattern + "' relative to '" + parentPath + "' for the testing framework '" + toolName + "'."
+ " Did you enter a pattern relative to (and within) the workspace directory?"
+ " Did you generate the result report(s) for '" + toolName + "'?";
xUnitLog.infoConsoleLogger(msg);
infoSystemLogger(msg);
xUnitLog.info(msg);
} else {
String msg = "[" + toolName + "] - " + xunitFiles.length + " test report file(s) were found with the pattern '"
+ pattern + "' relative to '" + parentPath + "' for the testing framework '" + toolName + "'.";
xUnitLog.infoConsoleLogger(msg);
infoSystemLogger(msg);
xUnitLog.info(msg);
}
return Arrays.asList(xunitFiles);
}
Expand All @@ -97,12 +95,12 @@ public List<String> findReports(XUnitToolInfo xUnitToolInfo, File parentPath, St
* @param xUnitToolInfo the wrapped object
* @param files the file list
* @param workspace the root location of the file list
* @throws OldTestReportException when the report file is not updated during this build is setup to fail
* @throws OldTestReportException when the report file is not updated during this build is setup to fail
*/
public void checkIfFindsFilesNewFiles(XUnitToolInfo xUnitToolInfo, List<String> files, File workspace) throws OldTestReportException {

if (xUnitToolInfo.isFailIfNotNew()) {
ArrayList<File> oldResults = new ArrayList<File>();
ArrayList<File> oldResults = new ArrayList<>();
for (String value : files) {
File reportFile = new File(workspace, value);
// if the file was not updated this build, that is a problem
Expand All @@ -118,8 +116,7 @@ public void checkIfFindsFilesNewFiles(XUnitToolInfo xUnitToolInfo, List<String>
String msg = "Clock on this slave is out of sync with the master, and therefore \n" +
"I can't figure out what test results are new and what are old.\n" +
"Please keep the slave clock in sync with the master.";
xUnitLog.errorConsoleLogger(msg);
errorSystemLogger(msg);
xUnitLog.error(msg);
throw new OldTestReportException();
}

Expand All @@ -133,8 +130,7 @@ public void checkIfFindsFilesNewFiles(XUnitToolInfo xUnitToolInfo, List<String>
);
}
String msg = stringBuilder.toString();
xUnitLog.errorConsoleLogger(msg);
errorSystemLogger(msg);
xUnitLog.error(msg);
throw new OldTestReportException();
}
}
Expand Down

0 comments on commit f8a62e9

Please sign in to comment.