Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve NodeParameterValue and LabelParameterValue test coverage #352

Merged
merged 8 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.model.Run;
Expand Down Expand Up @@ -264,6 +265,26 @@ public void testTriggerViaCurlWithLabel() throws Exception {
null,
new AllNodeEligibility());
String json = "{\"parameter\":[{\"name\":\"NODE\",\"label\":[\"" + controllerLabel + "\"]}]}";
LabelParameterValue lpv = new LabelParameterValue("test");
ParameterDefinition copy = parameterDefinition.copyWithDefaultValue(lpv);
assertThat(copy.getDescription(), is(parameterDefinition.getDescription()));
assertThat(copy.getType(), is(parameterDefinition.getType()));
assertThat(copy.getName(), is(parameterDefinition.getName()));
assertThat(
NodeParameterDefinition.getSlaveNamesForSelection(),
contains(
Constants.ALL_NODES,
controllerLabel,
onlineNode1.getNodeName(),
onlineNode2.getNodeName(),
offlineNode.getNodeName()));
assertThat(
NodeParameterDefinition.getSlaveNames(),
contains(
controllerLabel,
onlineNode1.getNodeName(),
onlineNode2.getNodeName(),
offlineNode.getNodeName()));
runTestViaCurl(projectA, parameterDefinition, json, 1, Result.SUCCESS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import hudson.model.AutoCompletionCandidates;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Project;
import hudson.model.labels.LabelAtom;
Expand All @@ -34,13 +35,16 @@
import hudson.plugins.parameterizedtrigger.ResultCondition;
import hudson.slaves.DumbSlave;
import hudson.util.FormValidation;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterDefinition;
import org.jvnet.jenkins.plugins.nodelabelparameter.LabelParameterValue;
import org.jvnet.jenkins.plugins.nodelabelparameter.NodeParameterValue;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;

public class NodeLabelBuildParameterTest {

Expand Down Expand Up @@ -75,13 +79,29 @@ public void test() throws Exception {

LabelParameterDefinition lb1 =
new LabelParameterDefinition(paramName, "some desc", "wrongNodeName", false, null, "");
Assert.assertTrue(lb1.isTriggerConcurrentBuilds());
Assert.assertTrue(lb1.getNodeEligibility() instanceof AllNodeEligibility);

LabelParameterDefinition lb2 =
new LabelParameterDefinition(paramName2, "some desc", "wrongNodeName", false, false, "");
Assert.assertTrue(lb2.isTriggerConcurrentBuilds());
Assert.assertTrue(lb2.getNodeEligibility() instanceof AllNodeEligibility);

LabelParameterDefinition lb3 = new LabelParameterDefinition(paramName3, "some desc", "wrongNodeName");
Assert.assertTrue(lb3.isTriggerConcurrentBuilds());
Assert.assertTrue(lb3.getNodeEligibility() instanceof AllNodeEligibility);

LabelParameterDefinition lb4 =
new LabelParameterDefinition(paramName, "some desc", "wrongNodeName", true, null, "");
Assert.assertTrue(lb4.isTriggerConcurrentBuilds());
Assert.assertTrue(lb4.getNodeEligibility() instanceof AllNodeEligibility);

ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(lb1);
ParametersDefinitionProperty pdp2 = new ParametersDefinitionProperty(lb2);
ParametersDefinitionProperty pdp3 = new ParametersDefinitionProperty(lb3);
ParameterValue pv1 = lb4.createValue("test");
LabelParameterValue lpv1 = new LabelParameterValue("test");
LabelParameterValue lpv2 = new LabelParameterValue("test2");

projectB.addProperty(pdp);
projectB.addProperty(pdp2);
Expand All @@ -108,10 +128,18 @@ public void test() throws Exception {
final AutoCompletionCandidates candidates = descriptor.doAutoCompleteDefaultValue(paramName);
final FormValidation doListNodesForLabel = descriptor.doListNodesForLabel(nodeName);

Assert.assertEquals(
lb1.copyWithDefaultValue(new LabelParameterValue("")).getName(), paramName);
Assert.assertTrue(lpv1.equals(lpv1));
Assert.assertFalse(lpv1.equals(lpv2));
Assert.assertFalse(lpv1.equals(new NodeParameterValue("test", "description", "TestLabel")));

Assert.assertEquals(paramName, pv1.getName());
Assert.assertEquals("allCases", lb1.getTriggerIfResult());

Assert.assertEquals(lb1.copyWithDefaultValue(null).getName(), paramName);
List<String> labels = new ArrayList<>();
labels.add("wrongNodeName");
LabelParameterValue lpv3 = new LabelParameterValue("test", labels, lb1.getNodeEligibility());
Assert.assertEquals(paramName, lb1.copyWithDefaultValue(lpv3).getName());
Assert.assertEquals(paramName, lb1.copyWithDefaultValue(null).getName());

Assert.assertEquals(doListNodesForLabel.kind, FormValidation.Kind.OK);
Assert.assertEquals(candidates.getValues(), List.of(nodeName));
Expand Down