Skip to content
Permalink
Browse files
[JENKINS-15094] - Unit tests for the issue
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
(cherry picked from commit cd23fac)
  • Loading branch information
oleg-nenashev authored and olivergondza committed Jul 2, 2014
1 parent b813d47 commit 5cfcebf722ed027cb9dbbd1b5b3142196fc58161
Showing with 126 additions and 0 deletions.
  1. +41 −0 core/src/test/java/hudson/model/ParametersActionTest.java
  2. +85 −0 test/src/test/groovy/hudson/cli/BuildCommandTest.groovy
@@ -1,11 +1,21 @@
package hudson.model;

import hudson.EnvVars;
import hudson.model.queue.SubTask;
import hudson.tasks.BuildWrapper;
import java.util.LinkedList;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Bug;
import static org.powermock.api.mockito.PowerMockito.mock;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class ParametersActionTest {

private ParametersAction baseParamsAB;
@@ -76,4 +86,35 @@ public void createUpdatedShouldReturnNewInstanceWithNullOverride() {

assertNotSame(baseParamsAB, params);
}

@Test
@Bug(15094)
public void checkNullParamaterValues() {
SubTask subtask = mock(SubTask.class);
Build build = mock(Build.class);

// Prepare parameters Action
StringParameterValue A = new StringParameterValue("A", "foo");
StringParameterValue B = new StringParameterValue("B", "bar");
ParametersAction parametersAction = new ParametersAction(A, null, B);
ParametersAction parametersAction2 = new ParametersAction(A,null);

// Non existent parameter
assertNull(parametersAction.getParameter("C"));
assertNull(parametersAction.getAssignedLabel(subtask));

// Interaction with build
EnvVars vars = new EnvVars();
parametersAction.buildEnvVars(build, vars);
assertEquals(2, vars.size());
parametersAction.createVariableResolver(build);

LinkedList<BuildWrapper> wrappers = new LinkedList<BuildWrapper>();
parametersAction.createBuildWrappers(build, wrappers);
assertEquals(0, wrappers.size());

// Merges and overrides
assertEquals(3, parametersAction.createUpdated(parametersAction2.getParameters()).getParameters().size());
assertEquals(3, parametersAction.merge(parametersAction2).getParameters().size());
}
}
@@ -25,25 +25,37 @@ package hudson.cli

import org.apache.commons.io.output.TeeOutputStream
import static org.junit.Assert.*
import hudson.Extension
import org.junit.Rule
import org.junit.Test
import org.jvnet.hudson.test.Bug
import org.jvnet.hudson.test.CaptureEnvironmentBuilder
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.RandomlyFails
import org.jvnet.hudson.test.TestBuilder
import org.jvnet.hudson.test.TestExtension
import org.kohsuke.stapler.StaplerRequest

import hudson.Launcher
import hudson.model.AbstractBuild
import hudson.model.Action
import hudson.model.BuildListener
import hudson.model.Executor
import hudson.model.FreeStyleProject;
import hudson.model.ParameterDefinition.ParameterDescriptor
import hudson.model.ParameterValue
import hudson.model.ParametersAction
import hudson.model.ParametersDefinitionProperty
import hudson.model.Queue.QueueDecisionHandler
import hudson.model.Queue.Task;
import hudson.model.SimpleParameterDefinition
import hudson.model.StringParameterDefinition
import hudson.model.StringParameterValue
import hudson.model.labels.LabelAtom
import hudson.tasks.Shell
import hudson.util.OneShotEvent
import java.util.concurrent.Executor
import net.sf.json.JSONObject

/**
* {@link BuildCommand} test.
@@ -212,4 +224,77 @@ public class BuildCommandTest {
assertEquals("Command is expected to succeed", 0, result.returnCode());
assertEquals("a=b", project.getBuildByNumber(1).getBuildVariables().get("expr"));
}

@Bug(15094)
@Test public void executorsAliveOnParameterWithNullDefaultValue() throws Exception {
def slave = j.createSlave();
FreeStyleProject project = j.createFreeStyleProject("foo");
project.setAssignedNode(slave);

// Create test parameter with Null default value
def nullDefaultDefinition = new NullDefaultValueParameterDefinition();
ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(
new StringParameterDefinition("string", "defaultValue", "description"),
nullDefaultDefinition);
project.addProperty(pdp);
CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
project.getBuildersList().add(builder);

// Warmup
j.buildAndAssertSuccess(project);

for (def exec : slave.toComputer().getExecutors()) {
assertTrue("Executor has died before the test start: "+exec, exec.isActive());
}

// Create CLI & run command
def invoker = new CLICommandInvoker(j, new BuildCommand());
def result = invoker
.authorizedTo(jenkins.model.Jenkins.ADMINISTER)
.invokeWithArgs("foo","-p","string=value");
assertEquals("Command is expected to fail with -1 code. \nSTDOUT="+result.stdout()
+"\nSTDERR: "+result.stderr(), -1, result.returnCode());
assertTrue("Unexpected error message",
result.stderr().startsWith("No default value for the parameter \'FOO\'."));

// Give the job 5 seconds to be submitted
def q = j.jenkins.getQueue().getItem(project);
Thread.sleep(5000);

// Check executors health after a timeout
for (def exec : slave.toComputer().getExecutors()) {
assertTrue("Executor is dead: "+exec, exec.isActive());
}
}

public static final class NullDefaultValueParameterDefinition extends SimpleParameterDefinition {

/*package*/ NullDefaultValueParameterDefinition() {
super("FOO", "Always null default value");
}

@Override
public ParameterValue createValue(String value) {
return new StringParameterValue("FOO", "BAR");
}

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
return createValue("BAR");
}

@Override
public ParameterValue getDefaultParameterValue() {
return null; // Equals to super.getDefaultParameterValue();
}

@Extension
public static class DescriptorImpl extends ParameterDescriptor {

@Override
public String getDisplayName() {
return "Parameter with the default NULL value";
}
}
}
}

0 comments on commit 5cfcebf

Please sign in to comment.