Skip to content
Permalink
Browse files

[JENKINS-18804] Added a test to reproduce JENKINS-18804.

  • Loading branch information...
ikedam committed Sep 29, 2014
1 parent a7ce03c commit 72435901fdaea4c9202a771b752f205e6eb69462
@@ -33,11 +33,13 @@
import hudson.model.FreeStyleBuild;
import hudson.model.Result;
import hudson.plugins.copyartifact.testutils.FileWriteBuilder;
import hudson.plugins.copyartifact.testutils.RemoveUpstreamBuilder;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BuildTrigger;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

@@ -611,4 +613,151 @@ public void testIsUseNewest() throws Exception {
d.setGlobalUpstreamFilterStrategy(TriggeredBuildSelector.UpstreamFilterStrategy.UseNewest);
assertFalse(new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseOldest).isUseNewest());
}

@Bug(18804)
@Test
public void testUpstreamWasRemoved() throws Exception {
// upstream -> downstream
// Remove the build of upstream.
{
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();

upstream.getBuildersList().add(new FileWriteBuilder(
"artifact.txt",
"foobar"
));
upstream.getPublishersList().add(new ArtifactArchiver(
"**/*",
"",
false,
false
));
upstream.getPublishersList().add(new BuildTrigger(
downstream.getFullName(),
false
));

downstream.getBuildersList().add(new RemoveUpstreamBuilder());
downstream.getBuildersList().add(new CopyArtifact(
upstream.getFullName(),
"",
new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting),
"**/*",
"",
"",
false,
true, // This results build succeed even if the upstream build has been removed.
false
));

j.jenkins.rebuildDependencyGraph();

upstream.scheduleBuild2(0);
j.waitUntilNoActivity();

assertNull(upstream.getLastBuild());
j.assertBuildStatusSuccess(downstream.getLastBuild());
}

// upstream -> intermediate -> downstream
// Remove the build of upstream.
{
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject intermediate = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();

upstream.getBuildersList().add(new FileWriteBuilder(
"artifact.txt",
"foobar"
));
upstream.getPublishersList().add(new ArtifactArchiver(
"**/*",
"",
false,
false
));
upstream.getPublishersList().add(new BuildTrigger(
intermediate.getFullName(),
false
));

intermediate.getBuildersList().add(new RemoveUpstreamBuilder());
intermediate.getPublishersList().add(new BuildTrigger(
downstream.getFullName(),
false
));

downstream.getBuildersList().add(new CopyArtifact(
upstream.getFullName(),
"",
new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting),
"**/*",
"",
"",
false,
true, // This results build succeed even if the upstream build has been removed.
false
));

j.jenkins.rebuildDependencyGraph();

upstream.scheduleBuild2(0);
j.waitUntilNoActivity();

assertNull(upstream.getLastBuild());
j.assertBuildStatusSuccess(intermediate.getLastBuild());
j.assertBuildStatusSuccess(downstream.getLastBuild());
}

// upstream -> intermediate -> downstream
// Remove the build of intermediate.
{
FreeStyleProject upstream = j.createFreeStyleProject();
FreeStyleProject intermediate = j.createFreeStyleProject();
FreeStyleProject downstream = j.createFreeStyleProject();

upstream.getBuildersList().add(new FileWriteBuilder(
"artifact.txt",
"foobar"
));
upstream.getPublishersList().add(new ArtifactArchiver(
"**/*",
"",
false,
false
));
upstream.getPublishersList().add(new BuildTrigger(
intermediate.getFullName(),
false
));

intermediate.getPublishersList().add(new BuildTrigger(
downstream.getFullName(),
false
));

downstream.getBuildersList().add(new RemoveUpstreamBuilder());
downstream.getBuildersList().add(new CopyArtifact(
upstream.getFullName(),
"",
new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting),
"**/*",
"",
"",
false,
true, // This results build succeed even if the upstream build has been removed.
false
));

j.jenkins.rebuildDependencyGraph();

upstream.scheduleBuild2(0);
j.waitUntilNoActivity();

j.assertBuildStatusSuccess(upstream.getLastBuild());
assertNull(intermediate.getLastBuild());
j.assertBuildStatusSuccess(downstream.getLastBuild());
}
}
}
@@ -0,0 +1,65 @@
/*
* The MIT License
*
* Copyright (c) 2014 IKEDA Yasuyuki
*
* 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 hudson.plugins.copyartifact.testutils;

import java.io.IOException;

import jenkins.model.Jenkins;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.BuildListener;
import hudson.tasks.Builder;

/**
* Removes the upstream build.
* (Only removes the direct upstream build).
*/
public class RemoveUpstreamBuilder extends Builder {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
for (Cause.UpstreamCause c: Util.filter(build.getCauses(), Cause.UpstreamCause.class)) {
Job<?,?> upstreamProject = Jenkins.getInstance().getItemByFullName(c.getUpstreamProject(), Job.class);
if (upstreamProject == null) {
listener.getLogger().println(String.format("Not Found: %s", c.getUpstreamProject()));
continue;
}

Run<?,?> upstreamBuild = upstreamProject.getBuildByNumber(c.getUpstreamBuild());
if (upstreamBuild == null) {
listener.getLogger().println(String.format("Not Found: %s - %d", upstreamProject.getFullName(), c.getUpstreamBuild()));
continue;
}

listener.getLogger().println(String.format("Removed: %s - %s", upstreamProject.getFullName(), upstreamBuild.getFullDisplayName()));
upstreamBuild.delete();
}
return true;
}
}

0 comments on commit 7243590

Please sign in to comment.
You can’t perform that action at this time.