Skip to content

Commit

Permalink
Test more getters and more branches of doListNodes
Browse files Browse the repository at this point in the history
The doListNodes checks now cover failure cases
  • Loading branch information
MarkEWaite committed Dec 25, 2024
1 parent fc63c02 commit c7c07db
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand All @@ -14,6 +15,8 @@
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreOfflineNodeEligibility;

public class LabelParameterDefinitionTest {

Expand Down Expand Up @@ -46,6 +49,7 @@ public void testNodeParameterDefinitionDeprecated() {
assertThat(nodeParameterDefinition.getDescription(), is(description));
assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue));
assertThat(nodeParameterDefinition.getTriggerIfResult(), is(triggerIfResult));
assertThat(nodeParameterDefinition.getNodeEligibility(), is(instanceOf(IgnoreOfflineNodeEligibility.class)));
assertTrue(nodeParameterDefinition.isAllNodesMatchingLabel());
}

Expand All @@ -64,6 +68,7 @@ public void testNodeParameterDefinitionDeprecated3Arg() {
assertThat(nodeParameterDefinition.getDescription(), is(description));
assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue));
assertThat(nodeParameterDefinition.getTriggerIfResult(), is("allCases"));
assertThat(nodeParameterDefinition.getNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
assertFalse(nodeParameterDefinition.isAllNodesMatchingLabel());
}

Expand All @@ -82,12 +87,14 @@ public void testNodeParameterDefinition() {
assertThat(nodeParameterDefinition.getDescription(), is(description));
assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue));
assertThat(nodeParameterDefinition.getTriggerIfResult(), is(triggerIfResult));
assertThat(nodeParameterDefinition.getNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
assertFalse(nodeParameterDefinition.isAllNodesMatchingLabel());
}

@Test
public void testDoListNodesForAgentLabel() throws Exception {
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(LABEL_NAME);
String msg = validation.getMessage();
assertThat(msg, allOf(containsString("Matching nodes"), containsString(agent.getNodeName())));
Expand All @@ -97,6 +104,7 @@ public void testDoListNodesForAgentLabel() throws Exception {
public void testDoListNodesForControllerLabel() throws Exception {
String controllerLabel = "built-in";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(controllerLabel);
String msg = validation.getMessage();
assertThat(msg, allOf(containsString("Matching nodes"), containsString(controllerLabel)));
Expand All @@ -106,8 +114,33 @@ public void testDoListNodesForControllerLabel() throws Exception {
public void testDoListNodesForNonExistentLabel() throws Exception {
String badLabel = "this-label-does-not-exist";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(badLabel);
String msg = validation.getMessage();
assertThat(msg, allOf(containsString("The label expression"), containsString(badLabel)));
}

@Test
public void testDoListNodesForBlankLabel() throws Exception {
String blankLabel = "";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(blankLabel);
String msg = validation.getMessage();
assertThat(msg, containsString("a label is required"));
}

@Test
public void testDoListNodesForInvalidLabelExpression() throws Exception {
String invalidLabel = "a||";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(invalidLabel);
String msg = validation.getMessage();
assertThat(
msg,
allOf(
containsString("The label expression"),
containsString(invalidLabel),
containsString("is not valid")));
}
}

0 comments on commit c7c07db

Please sign in to comment.