Skip to content

Commit

Permalink
[JENKINS-26936] Added tests for JENKINS-29636, Flexible Publish abort…
Browse files Browse the repository at this point in the history
…s execution if a publisher throws Exception.
  • Loading branch information
ikedam committed Feb 15, 2015
1 parent 92a773f commit f027caa
Showing 1 changed file with 331 additions and 0 deletions.
Expand Up @@ -25,20 +25,33 @@
package org.jenkins_ci.plugins.flexible_publish;

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

import hudson.AbortException;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.tasks.BuildStep;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.tasks.ArtifactArchiver;

import org.jenkins_ci.plugins.flexible_publish.testutils.FileWriteBuilder;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;
import org.jenkins_ci.plugins.run_condition.core.AlwaysRun;
import org.jenkins_ci.plugins.run_condition.core.StringsMatchCondition;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.recipes.LocalData;
Expand Down Expand Up @@ -158,4 +171,322 @@ public void testMultipleConditionsMultipleActions() throws Exception {
assertFalse(new File(b.getArtifactsDir(), "artifact4.txt").exists());
}
}

public static class FailurePublisher extends Recorder {
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
return false;
}

@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return "FailurePublisher";
}
}
}

public static class ThorwAbortExceptionPublisher extends Recorder {
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
throw new AbortException("Intended abort");
//return true;
}

@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return "ThorwAbortExceptionPublisher";
}
}
}

public static class ThorwGeneralExceptionPublisher extends Recorder {
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
throw new IOException("Intended abort");
//return true;
}

@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return "ThorwGeneralExceptionPublisher";
}
}
}

public void testRunAllPublishers() throws Exception {
// Jenkins executes all publishers even one of them failed.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FailurePublisher());
p.getPublishersList().add(new ArtifactArchiver("**/*", "", false));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

// Jenkins executes all publishers even one of them throws AbortException.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new ThorwAbortExceptionPublisher());
p.getPublishersList().add(new ArtifactArchiver("**/*", "", false));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());

// Somehow Jenkins prints stacktrace for AbortException. You can see that here.
// System.out.println(b.getLog());
}

// Jenkins executes all publishers even one of them throws any Exceptions.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new ThorwGeneralExceptionPublisher());
p.getPublishersList().add(new ArtifactArchiver("**/*", "", false));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

//// Flexible Publish should run as Jenkins core do.

// Flexible Publish executes all publishers even one of them failed.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new FailurePublisher()
),
new BuildStepRunner.Fail(),
false,
null,
null
),
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ArtifactArchiver("**/*", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

// Flexible Publish executes all publishers even one of them throws AbortException.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ThorwAbortExceptionPublisher()
),
new BuildStepRunner.Fail(),
false,
null,
null
),
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ArtifactArchiver("**/*", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

// Flexible Publish executes all publishers even one of them throws any Exceptions.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ThorwGeneralExceptionPublisher()
),
new BuildStepRunner.Fail(),
false,
null,
null
),
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ArtifactArchiver("**/*", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}


//// Of course, ConditionalPublisher should do so.

// Flexible Publish executes all publishers in a condition even one of them failed.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new FailurePublisher(),
new ArtifactArchiver("**/*", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

// Flexible Publish executes all publishers even one of them throws AbortException.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ThorwAbortExceptionPublisher(),
new ArtifactArchiver("**/*", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}

// Flexible Publish executes all publishers even one of them throws any Exceptions.
{
FreeStyleProject p = createFreeStyleProject();

p.getBuildersList().add(new FileWriteBuilder("artifact.txt", "blahblahblah"));
p.getPublishersList().add(new FlexiblePublisher(Arrays.asList(
new ConditionalPublisher(
new AlwaysRun(),
Arrays.<BuildStep>asList(
new ThorwGeneralExceptionPublisher(),
new ArtifactArchiver("**/*", "", false)
),
new BuildStepRunner.Fail(),
false,
null,
null
)
)));

FreeStyleBuild b = p.scheduleBuild2(0).get();
assertBuildStatus(Result.FAILURE, b);

// ArtifactArchiver is executed even prior publisher fails.
assertTrue(new File(b.getArtifactsDir(), "artifact.txt").exists());
}
}
}

0 comments on commit f027caa

Please sign in to comment.