提交 5600aa9b 编写于 作者: M mindless

[FIXED HUDSON-4739] Fixed "even if unstable" option for triggering other projects

from a Maven type project; added unit test.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@23455 71c3de6d-444a-0410-be80-ed276b4c234a
上级 f527d22a
...@@ -32,6 +32,7 @@ import hudson.model.Hudson; ...@@ -32,6 +32,7 @@ import hudson.model.Hudson;
import hudson.model.Result; import hudson.model.Result;
import hudson.model.Run; import hudson.model.Run;
import hudson.model.Cause.UpstreamCause; import hudson.model.Cause.UpstreamCause;
import hudson.tasks.BuildTrigger;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
...@@ -41,7 +42,7 @@ import java.util.Set; ...@@ -41,7 +42,7 @@ import java.util.Set;
public abstract class AbstractMavenBuild<P extends AbstractMavenProject<P,B>,B extends AbstractMavenBuild<P,B>> extends AbstractBuild<P, B> { public abstract class AbstractMavenBuild<P extends AbstractMavenProject<P,B>,B extends AbstractMavenBuild<P,B>> extends AbstractBuild<P, B> {
/** /**
* Extra versbose debug switch. * Extra verbose debug switch.
*/ */
public static boolean debug = false; public static boolean debug = false;
...@@ -59,11 +60,16 @@ public abstract class AbstractMavenBuild<P extends AbstractMavenProject<P,B>,B e ...@@ -59,11 +60,16 @@ public abstract class AbstractMavenBuild<P extends AbstractMavenProject<P,B>,B e
/** /**
* Schedules all the downstream builds. * Schedules all the downstream builds.
* Returns immediately if build result doesn't meet the required level
* (as specified by {@link BuildTrigger}, or {@link Result#SUCCESS} if none).
* *
* @param listener * @param listener
* Where the progress reports go. * Where the progress reports go.
*/ */
protected final void scheduleDownstreamBuilds(BuildListener listener) { protected final void scheduleDownstreamBuilds(BuildListener listener) {
BuildTrigger bt = getParent().getPublishersList().get(BuildTrigger.class);
if (getResult().isWorseThan(bt!=null ? bt.getThreshold() : Result.SUCCESS)) return;
// trigger dependency builds // trigger dependency builds
for( AbstractProject<?,?> down : getParent().getDownstreamProjects()) { for( AbstractProject<?,?> down : getParent().getDownstreamProjects()) {
if(debug) if(debug)
......
...@@ -226,6 +226,7 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> { ...@@ -226,6 +226,7 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
/** /**
* Backdoor for {@link MavenModuleSetBuild} to assign workspaces for modules. * Backdoor for {@link MavenModuleSetBuild} to assign workspaces for modules.
*/ */
@Override
protected void setWorkspace(FilePath path) { protected void setWorkspace(FilePath path) {
super.setWorkspace(path); super.setWorkspace(path);
} }
...@@ -255,6 +256,7 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> { ...@@ -255,6 +256,7 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
super(buildProxy); super(buildProxy);
} }
@Override
public void executeAsync(final BuildCallable<?,?> program) throws IOException { public void executeAsync(final BuildCallable<?,?> program) throws IOException {
futures.add(Channel.current().callAsync(new AsyncInvoker(core,program))); futures.add(Channel.current().callAsync(new AsyncInvoker(core,program)));
} }
...@@ -547,9 +549,9 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> { ...@@ -547,9 +549,9 @@ public class MavenBuild extends AbstractMavenBuild<MavenModule,MavenBuild> {
reporter.end(MavenBuild.this,launcher,listener); reporter.end(MavenBuild.this,launcher,listener);
} }
@Override
public void cleanUp(BuildListener listener) throws Exception { public void cleanUp(BuildListener listener) throws Exception {
if(getResult().isBetterOrEqualTo(Result.SUCCESS)) scheduleDownstreamBuilds(listener);
scheduleDownstreamBuilds(listener);
} }
} }
......
...@@ -234,6 +234,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven ...@@ -234,6 +234,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
return r; return r;
} }
@Override
public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) { public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
// map corresponding module build under this object // map corresponding module build under this object
if(token.indexOf('$')>0) { if(token.indexOf('$')>0) {
...@@ -608,9 +609,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven ...@@ -608,9 +609,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
if(project.isAggregatorStyleBuild()) { if(project.isAggregatorStyleBuild()) {
// schedule downstream builds. for non aggregator style builds, // schedule downstream builds. for non aggregator style builds,
// this is done by each module // this is done by each module
if(getResult().isBetterOrEqualTo(Result.SUCCESS)) { scheduleDownstreamBuilds(listener);
scheduleDownstreamBuilds(listener);
}
} }
MavenMailer mailer = project.getReporters().get(MavenMailer.class); MavenMailer mailer = project.getReporters().get(MavenMailer.class);
...@@ -660,6 +659,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven ...@@ -660,6 +659,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
super(core); super(core);
} }
@Override
public void executeAsync(final BuildCallable<?,?> program) throws IOException { public void executeAsync(final BuildCallable<?,?> program) throws IOException {
futures.add(Channel.current().callAsync(new AsyncInvoker(core,program))); futures.add(Channel.current().callAsync(new AsyncInvoker(core,program)));
} }
...@@ -683,6 +683,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven ...@@ -683,6 +683,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
} }
} }
@Override
public Result call() throws IOException { public Result call() throws IOException {
try { try {
return super.call(); return super.call();
...@@ -771,6 +772,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven ...@@ -771,6 +772,7 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
super(cause); super(cause);
} }
@Override
public Exception getCause() { public Exception getCause() {
return (Exception)super.getCause(); return (Exception)super.getCause();
} }
......
...@@ -25,9 +25,13 @@ package hudson.tasks; ...@@ -25,9 +25,13 @@ package hudson.tasks;
import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.Build; import hudson.model.Build;
import hudson.model.Project; import hudson.model.FreeStyleProject;
import hudson.model.Result; import hudson.model.Result;
import hudson.model.Run;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase; import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.MockBuilder; import org.jvnet.hudson.test.MockBuilder;
...@@ -36,13 +40,9 @@ import org.jvnet.hudson.test.MockBuilder; ...@@ -36,13 +40,9 @@ import org.jvnet.hudson.test.MockBuilder;
* @author Alan.Harder@sun.com * @author Alan.Harder@sun.com
*/ */
public class BuildTriggerTest extends HudsonTestCase { public class BuildTriggerTest extends HudsonTestCase {
private void doTriggerTest(boolean evenWhenUnstable, Result triggerResult,
Result dontTriggerResult) throws Exception { private FreeStyleProject createDownstreamProject() throws Exception {
Project p = createFreeStyleProject(), FreeStyleProject dp = createFreeStyleProject("downstream");
dp = createFreeStyleProject("downstream");
p.getPublishersList().add(new BuildTrigger("downstream", evenWhenUnstable));
p.getBuildersList().add(new MockBuilder(dontTriggerResult));
hudson.rebuildDependencyGraph();
// Hm, no setQuietPeriod, have to submit form.. // Hm, no setQuietPeriod, have to submit form..
WebClient webClient = new WebClient(); WebClient webClient = new WebClient();
...@@ -53,17 +53,36 @@ public class BuildTriggerTest extends HudsonTestCase { ...@@ -53,17 +53,36 @@ public class BuildTriggerTest extends HudsonTestCase {
submit(form); submit(form);
assertEquals("set quiet period", 0, dp.getQuietPeriod()); assertEquals("set quiet period", 0, dp.getQuietPeriod());
return dp;
}
private void doTriggerTest(boolean evenWhenUnstable, Result triggerResult,
Result dontTriggerResult) throws Exception {
FreeStyleProject p = createFreeStyleProject(),
dp = createDownstreamProject();
p.getPublishersList().add(new BuildTrigger("downstream", evenWhenUnstable));
p.getBuildersList().add(new MockBuilder(dontTriggerResult));
hudson.rebuildDependencyGraph();
// First build should not trigger downstream job // First build should not trigger downstream job
Build b = (Build)p.scheduleBuild2(0).get(); Build b = p.scheduleBuild2(0).get();
assertNoDownstreamBuild(dp, b);
// Next build should trigger downstream job
p.getBuildersList().replace(new MockBuilder(triggerResult));
b = p.scheduleBuild2(0).get();
assertDownstreamBuild(dp, b);
}
private void assertNoDownstreamBuild(FreeStyleProject dp, Run b) throws Exception {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
Thread.sleep(200); Thread.sleep(200);
assertTrue("downstream build should not run! upstream log: " + getLog(b), assertTrue("downstream build should not run! upstream log: " + getLog(b),
!dp.isInQueue() && !dp.isBuilding() && dp.getLastBuild()==null); !dp.isInQueue() && !dp.isBuilding() && dp.getLastBuild()==null);
} }
}
// Next build should trigger downstream job private void assertDownstreamBuild(FreeStyleProject dp, Run b) throws Exception {
p.getBuildersList().replace(new MockBuilder(triggerResult));
b = (Build)p.scheduleBuild2(0).get();
// Wait for downstream build // Wait for downstream build
for (int i = 0; dp.getLastBuild()==null && i < 20; i++) Thread.sleep(100); for (int i = 0; dp.getLastBuild()==null && i < 20; i++) Thread.sleep(100);
assertNotNull("downstream build didn't run.. upstream log: " + getLog(b), dp.getLastBuild()); assertNotNull("downstream build didn't run.. upstream log: " + getLog(b), dp.getLastBuild());
...@@ -76,4 +95,40 @@ public class BuildTriggerTest extends HudsonTestCase { ...@@ -76,4 +95,40 @@ public class BuildTriggerTest extends HudsonTestCase {
public void testTriggerEvenWhenUnstable() throws Exception { public void testTriggerEvenWhenUnstable() throws Exception {
doTriggerTest(true, Result.UNSTABLE, Result.FAILURE); doTriggerTest(true, Result.UNSTABLE, Result.FAILURE);
} }
private void doMavenTriggerTest(boolean evenWhenUnstable) throws Exception {
FreeStyleProject dp = createDownstreamProject();
configureDefaultMaven();
MavenModuleSet m = createMavenProject();
m.getPublishersList().add(new BuildTrigger("downstream", evenWhenUnstable));
if (!evenWhenUnstable) {
// Configure for UNSTABLE
m.setGoals("clean test");
m.setScm(new ExtractResourceSCM(getClass().getResource("maven-test-failure.zip")));
} // otherwise do nothing which gets FAILURE
// First build should not trigger downstream project
MavenModuleSetBuild b = m.scheduleBuild2(0).get();
assertNoDownstreamBuild(dp, b);
if (evenWhenUnstable) {
// Configure for UNSTABLE
m.setGoals("clean test");
m.setScm(new ExtractResourceSCM(getClass().getResource("maven-test-failure.zip")));
} else {
// Configure for SUCCESS
m.setGoals("clean");
m.setScm(new ExtractResourceSCM(getClass().getResource("maven-empty.zip")));
}
// Next build should trigger downstream project
b = m.scheduleBuild2(0).get();
assertDownstreamBuild(dp, b);
}
public void testMavenBuildTrigger() throws Exception {
doMavenTriggerTest(false);
}
public void testMavenTriggerEvenWhenUnstable() throws Exception {
doMavenTriggerTest(true);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册