From eaeb45bdaaed37c34d0eca13b10af85eea65e09f Mon Sep 17 00:00:00 2001 From: Marco Miller Date: Tue, 22 Oct 2013 15:13:51 -0400 Subject: [PATCH] Null check for if build scheduling Refused by extension => not in Queue. New test in hudson/cli/BuildCommandTest.groovy, as well as companion @TestExtension class, thanks to Jesse' suggestion. This is so that we test this hereby fixed case of a QueueDecisionHandler Refusing a build, leading to that null value now checked and handled accordingly. Commit also includes sorted imports and a few minor indentation fixes, as well as another (very) minor string change. --- .../main/java/hudson/cli/BuildCommand.java | 4 ++ .../groovy/hudson/cli/BuildCommandTest.groovy | 53 +++++++++++++------ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/hudson/cli/BuildCommand.java b/core/src/main/java/hudson/cli/BuildCommand.java index b6712ccdbb..9282b47dff 100644 --- a/core/src/main/java/hudson/cli/BuildCommand.java +++ b/core/src/main/java/hudson/cli/BuildCommand.java @@ -141,6 +141,10 @@ public class BuildCommand extends CLICommand { QueueTaskFuture f = job.scheduleBuild2(0, new CLICause(Jenkins.getAuthentication().getName()), a); if (wait || sync || follow) { + if (f == null) { + stderr.println("Build scheduling Refused by an extension, hence not in Queue"); + return -1; + } AbstractBuild b = f.waitForStart(); // wait for the start stdout.println("Started "+b.getFullDisplayName()); diff --git a/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy b/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy index 80ae8c56d3..1018821fd2 100644 --- a/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy +++ b/test/src/test/groovy/hudson/cli/BuildCommandTest.groovy @@ -23,22 +23,27 @@ */ package hudson.cli -import org.jvnet.hudson.test.JenkinsRule -import org.junit.Rule -import org.junit.Test +import org.apache.commons.io.output.TeeOutputStream import static org.junit.Assert.* import org.junit.Assume +import org.junit.Rule +import org.junit.Test +import org.jvnet.hudson.test.JenkinsRule +import org.jvnet.hudson.test.TestBuilder +import org.jvnet.hudson.test.TestExtension + +import hudson.Launcher +import hudson.model.AbstractBuild +import hudson.model.Action +import hudson.model.BuildListener +import hudson.model.FreeStyleProject; +import hudson.model.ParametersAction +import hudson.model.ParametersDefinitionProperty +import hudson.model.Queue.QueueDecisionHandler +import hudson.model.Queue.Task; +import hudson.model.StringParameterDefinition import hudson.tasks.Shell import hudson.util.OneShotEvent -import org.jvnet.hudson.test.TestBuilder -import hudson.model.AbstractBuild -import hudson.model.FreeStyleProject; -import hudson.Launcher -import hudson.model.BuildListener -import hudson.model.ParametersDefinitionProperty -import hudson.model.StringParameterDefinition -import hudson.model.ParametersAction -import org.apache.commons.io.output.TeeOutputStream /** * {@link BuildCommand} test. @@ -72,7 +77,6 @@ public class BuildCommandTest { } finally { cli.close(); } - } /** @@ -89,7 +93,6 @@ public class BuildCommandTest { } finally { cli.close(); } - } /** @@ -152,6 +155,26 @@ public class BuildCommandTest { } } + @Test void consoleOutputWhenBuildSchedulingRefused() { + Assume.assumeFalse("Started test0 #1", "https://jenkins.ci.cloudbees.com/job/core/job/jenkins_main_trunk/".equals(System.getenv("JOB_URL"))) + def p = j.createFreeStyleProject() + def cli = new CLI(j.URL) + try { + def o = new ByteArrayOutputStream() + cli.execute(["build","-s","-v",p.name],System.in,System.out,new TeeOutputStream(System.err,o)) + assertTrue(o.toString(), o.toString().contains("Build scheduling Refused by an extension, hence not in Queue")) + } finally { + cli.close() + } + } + // <=> + @TestExtension("consoleOutputWhenBuildSchedulingRefused") + static class UnschedulingVetoer extends QueueDecisionHandler { + public boolean shouldSchedule(Task task, List actions) { + return false; + } + } + @Test void refuseToBuildDisabledProject() { def project = j.createFreeStyleProject("the-project"); @@ -180,7 +203,7 @@ public class BuildCommandTest { def project = j.createFreeStyleProject("the-project"); project.addProperty(new ParametersDefinitionProperty([ - new StringParameterDefinition("expr", null) + new StringParameterDefinition("expr", null) ])); def invoker = new CLICommandInvoker(j, new BuildCommand());