提交 23758d49 编写于 作者: J Jesse Glick

[JENKINS-26964] Merging #1577.

......@@ -55,6 +55,9 @@ Upcoming changes</a>
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=bug>
Handle AbortException publisher status in the same way as deprecated false boolean status
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-26964">issue 26964</a>)
<li class=bug>
Ensures GlobalSettingsProvider does not swallow fatal exceptions
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-26604">issue 26604</a>)
......
......@@ -715,6 +715,8 @@ public abstract class AbstractBuild<P extends AbstractProject<P,R>,R extends Abs
*
* @param phase
* true for the post build processing, and false for the final "run after finished" execution.
*
* @return false if any build step failed
*/
protected final boolean performAllBuildSteps(BuildListener listener, Iterable<? extends BuildStep> buildSteps, boolean phase) throws InterruptedException, IOException {
boolean r = true;
......@@ -724,20 +726,33 @@ public abstract class AbstractBuild<P extends AbstractProject<P,R>,R extends Abs
if (!perform(bs,listener)) {
LOGGER.log(Level.FINE, "{0} : {1} failed", new Object[] {AbstractBuild.this, bs});
r = false;
if (phase) {
setResult(Result.FAILURE);
}
}
} catch (Exception e) {
reportError(bs, e, listener, phase);
r = false;
} catch (LinkageError e) {
reportError(bs, e, listener, phase);
r = false;
}
}
return r;
}
private void reportError(BuildStep bs, Throwable e, BuildListener listener, boolean phase) {
String msg = "Publisher " + bs.getClass().getName() + " aborted due to exception";
e.printStackTrace(listener.error(msg));
LOGGER.log(WARNING, msg, e);
final String publisher = ((Publisher) bs).getDescriptor().getDisplayName();
if (e instanceof AbortException) {
LOGGER.log(Level.FINE, "{0} : {1} failed", new Object[] {AbstractBuild.this, publisher});
listener.error("Publisher '" + publisher + "' failed: " + e.getMessage());
} else {
String msg = "Publisher '" + publisher + "' aborted due to exception: ";
e.printStackTrace(listener.error(msg));
LOGGER.log(WARNING, msg, e);
}
if (phase) {
setResult(Result.FAILURE);
}
......
package hudson.model;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.utils.AbortExceptionPublisher;
import hudson.model.utils.IOExceptionPublisher;
import hudson.model.utils.ResultWriterPublisher;
import hudson.model.utils.TrueFalsePublisher;
import hudson.tasks.ArtifactArchiver;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Freestyle publishers statuses tests
*
* @author Kanstantsin Shautsou
*/
public class FreestyleJobPublisherTest {
@Rule
public JenkinsRule j = new JenkinsRule();
/**
* Execute all publishers even one of publishers return false.
*/
@Issue("JENKINS-26964")
@Test
public void testFreestyleWithFalsePublisher() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.getPublishersList().add(new TrueFalsePublisher(true)); // noop
p.getPublishersList().add(new TrueFalsePublisher(false)); // FAIL build with false
p.getPublishersList().add(new ResultWriterPublisher("result.txt")); // catch result to file
final ArtifactArchiver artifactArchiver = new ArtifactArchiver("result.txt");
artifactArchiver.setOnlyIfSuccessful(false);
p.getPublishersList().add(artifactArchiver); // transfer file to build dir
FreeStyleBuild b = p.scheduleBuild2(0).get();
assertEquals("Build must fail, because we used FalsePublisher", b.getResult(), Result.FAILURE);
File file = new File(b.getArtifactsDir(), "result.txt");
assertTrue("ArtifactArchiver is executed even prior publisher fails", file.exists());
assertTrue("Publisher, after publisher with return false status, must see FAILURE status",
FileUtils.readFileToString(file).equals(Result.FAILURE.toString()));
}
/**
* Execute all publishers even one of them throws AbortException.
*/
@Issue("JENKINS-26964")
@Test
public void testFreestyleWithExceptionPublisher() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.getPublishersList().add(new TrueFalsePublisher(true)); // noop
p.getPublishersList().add(new AbortExceptionPublisher()); // FAIL build with AbortException
p.getPublishersList().add(new ResultWriterPublisher("result.txt")); // catch result to file
final ArtifactArchiver artifactArchiver = new ArtifactArchiver("result.txt");
artifactArchiver.setOnlyIfSuccessful(false);
p.getPublishersList().add(artifactArchiver); // transfer file to build dir
FreeStyleBuild b = p.scheduleBuild2(0).get();
assertEquals("Build must fail, because we used AbortExceptionPublisher", b.getResult(), Result.FAILURE);
j.assertLogNotContains("\tat", b); // log must not contain stacktrace
j.assertLogContains("Threw AbortException from publisher!", b); // log must contain exact error message
File file = new File(b.getArtifactsDir(), "result.txt");
assertTrue("ArtifactArchiver is executed even prior publisher fails", file.exists());
assertTrue("Third publisher must see FAILURE status",
FileUtils.readFileToString(file).equals(Result.FAILURE.toString()));
}
/**
* Execute all publishers even one of them throws any Exceptions.
*/
@Issue("JENKINS-26964")
@Test
public void testFreestyleWithIOExceptionPublisher() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.getPublishersList().add(new TrueFalsePublisher(true)); // noop
p.getPublishersList().add(new IOExceptionPublisher()); // fail with IOException
p.getPublishersList().add(new ResultWriterPublisher("result.txt")); //catch result to file
final ArtifactArchiver artifactArchiver = new ArtifactArchiver("result.txt");
artifactArchiver.setOnlyIfSuccessful(false);
p.getPublishersList().add(artifactArchiver); // transfer file to build dir
FreeStyleBuild b = p.scheduleBuild2(0).get();
assertEquals("Build must fail, because we used FalsePublisher", b.getResult(), Result.FAILURE);
j.assertLogContains("\tat hudson.model.utils.IOExceptionPublisher", b); // log must contain stacktrace
j.assertLogContains("Threw IOException from publisher!", b); // log must contain exact error message
File file = new File(b.getArtifactsDir(), "result.txt");
assertTrue("ArtifactArchiver is executed even prior publisher fails", file.exists());
assertTrue("Third publisher must see FAILURE status",
FileUtils.readFileToString(file).equals(Result.FAILURE.toString()));
}
}
package hudson.model.utils;
import hudson.AbortException;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import java.io.IOException;
/**
* Publisher that throws AbortException
*/
public class AbortExceptionPublisher extends Recorder {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
throw new AbortException("Threw AbortException from publisher!");
}
@Override
public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; }
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public String getDisplayName() { return "ThrowAbortExceptionRecorder"; }
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) { return true; }
}
}
package hudson.model.utils;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import java.io.IOException;
/**
* Publisher that throw IOException
* @author Kanstantsin Shautsou
*/
public class IOExceptionPublisher extends Recorder {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
throw new IOException("Threw IOException from publisher!");
}
@Override
public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; }
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public String getDisplayName() { return "Throw IOException Publisher"; }
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) { return true; }
}
}
package hudson.model.utils;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* Wrote build status to file
* @author Kanstantsin Shautsou
*/
public class ResultWriterPublisher extends Recorder {
private final String fileName;
public ResultWriterPublisher(String fileName) {
this.fileName = fileName;
}
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
FilePath file = build.getWorkspace().child(fileName);
file.write(build.getResult().toString(), Charset.defaultCharset().name());
return true;
}
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public String getDisplayName() { return "wrote result to file"; }
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) { return true; }
}
}
package hudson.model.utils;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.IOException;
/**
* @author Kanstantsin Shautsou
*/
public class TrueFalsePublisher extends Recorder {
private final boolean b;
@DataBoundConstructor
public TrueFalsePublisher(boolean b) {
this.b = b;
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
return b;
}
@Override
public BuildStepMonitor getRequiredMonitorService() { return BuildStepMonitor.NONE; }
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public String getDisplayName() { return "return true or false"; }
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) { return true; }
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册