Skip to content

Commit

Permalink
Expand assertions in NodeParameterDefinitionTest
Browse files Browse the repository at this point in the history
Unexpected that myValue is not returned by parameterValue.getValue().
Seems to be a bug in the NodeParameterDefinition implementation.
NodeParameterDefinition declares a private field 'label' that receives
the value instead of it being stored in LabelParameterValue.label but
does not override the LabelParameterValue implementation of getValue().

Expanded coverage by using different triggerIfResult values that change
the behavior of the constructor.

Prefer agents in local variables rather than slaves.
  • Loading branch information
MarkEWaite committed Dec 26, 2024
1 parent 1439faf commit d924ff1
Showing 1 changed file with 92 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,152 +1,151 @@
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static org.junit.Assert.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import hudson.model.ParameterValue;
import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;

public class NodeParameterDefinitionTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Before
public void setUp() throws Exception {
final DumbSlave node = j.createOnlineSlave(new LabelAtom("node"));
}

@Test
public void testReadResolve_DefaultValueIsNotNull() {
String name = "name";
String description = "description";
List<String> defaultSlaves1 = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
String triggerIfResult = "triggerIfResult";

// deaultValue is not null and defaultSlaves is not null
@ClassRule
public static JenkinsRule j = new JenkinsRule();

NodeParameterDefinition nodeParameterDefinition1 = new NodeParameterDefinition(
name, description, defaultSlaves1, allowedSlaves, triggerIfResult, new AllNodeEligibility());
private static DumbSlave agent;

nodeParameterDefinition1.defaultValue = "defaultValue";

assertNotNull(nodeParameterDefinition1.readResolve());
assertEquals(
nodeParameterDefinition1.getClass(),
nodeParameterDefinition1.readResolve().getClass());

// deaultValue is not null and defaultSlaves is null

List<String> defaultSlaves2 = null;
NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition(
name, description, defaultSlaves2, allowedSlaves, triggerIfResult, new AllNodeEligibility());

nodeParameterDefinition2.defaultValue = "defaultValue";

assertNotNull(nodeParameterDefinition2.readResolve());
assertEquals(
nodeParameterDefinition2.getClass(),
nodeParameterDefinition2.readResolve().getClass());
@BeforeClass
public static void createAgent() throws Exception {
agent = j.createOnlineSlave(new LabelAtom("my-agent-label"));
}

@Test
public void testReadResolve_NodeEligibilityIsNull() {
@Deprecated
public void testNodeParameterDefinitionDeprecatedReordersAllowedAgents() {
String name = "name";
String description = "description";
List<String> defaultSlaves = null;
List<String> allowedSlaves = new ArrayList<>();
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition1 =
new NodeParameterDefinition(name, description, defaultSlaves, allowedSlaves, triggerIfResult, true);

nodeParameterDefinition1.defaultValue = null;

assertNotNull(nodeParameterDefinition1.readResolve());

NodeParameterDefinition nodeParameterDefinition2 =
new NodeParameterDefinition(name, description, defaultSlaves, allowedSlaves, triggerIfResult, false);

nodeParameterDefinition2.defaultValue = null;

assertNotNull(nodeParameterDefinition2.readResolve());
List<String> allowedAgents = new ArrayList<>();
allowedAgents.add(agent.getNodeName());
allowedAgents.add("non-existent-agent");
String triggerIfResult = Constants.CASE_MULTISELECT_DISALLOWED;

assertThat(allowedAgents.get(0), is(agent.getNodeName()));
NodeParameterDefinition parameterDefinition =
new NodeParameterDefinition(name, description, "non-existent-agent", allowedAgents, triggerIfResult);
assertThat(allowedAgents.get(0), is("non-existent-agent")); // List reordered by constructor
assertThat(parameterDefinition.getName(), is(name));
assertThat(parameterDefinition.getDescription(), is(description));
assertThat(parameterDefinition.defaultValue, is(nullValue()));
assertThat(parameterDefinition.getTriggerIfResult(), is(triggerIfResult));
assertFalse(parameterDefinition.getAllowMultiNodeSelection());
assertFalse(parameterDefinition.isTriggerConcurrentBuilds());
}

@Test
public void testNodeParameterDefinition() {
String name = "name";
String description = "description";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("defaultValue");
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition1 =
new NodeParameterDefinition(name, description, "defaultValue", allowedSlaves, triggerIfResult);

assertTrue(allowedSlaves.contains("defaultValue"));

NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());

assertNotNull(nodeParameterDefinition2);
List<String> defaultAgents = Arrays.asList("built-in");
List<String> allowedAgents = new ArrayList<>();
allowedAgents.add(agent.getNodeName());
allowedAgents.add("non-existent-agent");
String triggerIfResult = Constants.CASE_MULTISELECT_CONCURRENT_BUILDS;

NodeParameterDefinition parameterDefinition = new NodeParameterDefinition(
name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility());
assertThat(allowedAgents.get(0), is(agent.getNodeName())); // List not reordered by constructor
assertThat(parameterDefinition.getName(), is(name));
assertThat(parameterDefinition.getDescription(), is(description));
assertThat(parameterDefinition.defaultValue, is(nullValue()));
assertThat(parameterDefinition.getTriggerIfResult(), is(triggerIfResult));
assertTrue(parameterDefinition.getAllowMultiNodeSelection());
assertTrue(parameterDefinition.isTriggerConcurrentBuilds());
}

@Test
public void testCreateValue_String() {
String name = "name";
String description = "description";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("defaultValue");
List<String> defaultAgents = new ArrayList<>();
List<String> allowedAgents = new ArrayList<>();
allowedAgents.add("defaultValue");
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());

assertNotNull(nodeParameterDefinition.createValue("value"));
NodeParameterDefinition parameterDefinition = new NodeParameterDefinition(
name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility());
assertThat(parameterDefinition.getName(), is(name));
assertThat(parameterDefinition.getDescription(), is(description));
assertThat(parameterDefinition.defaultValue, is(nullValue()));
assertThat(parameterDefinition.getTriggerIfResult(), is(triggerIfResult));
assertTrue(parameterDefinition.getAllowMultiNodeSelection());
assertFalse(parameterDefinition.isTriggerConcurrentBuilds());

String myValue = "my-value";
ParameterValue parameterValue = parameterDefinition.createValue(myValue);
assertThat(parameterValue.getName(), is(name));
assertThat(parameterValue.getDescription(), is(description));

// Unexpected that myValue is not returned by parameterValue.getValue()
// Seems to be a bug in the NodeParameterDefinition implementation
// NodeParameterDefinition declares a private field 'label' that receives
// the value instead of it being stored in LabelParameterValue.label but
// does not override the LabelParameterValue implementation of getValue().
assertThat(parameterValue.getValue(), is(nullValue()));
}

@Test
public void testGetAllowedNodesOrAll() {
String name = "name";
String description = "description";
List<String> defaultSlaves = new ArrayList<>();
List<String> allowedSlaves = new ArrayList<>();
allowedSlaves.add("node");
List<String> defaultAgents = new ArrayList<>();
List<String> allowedAgents = new ArrayList<>();
allowedAgents.add(agent.getNodeName());
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition1 = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());
NodeParameterDefinition parameterDefinition = new NodeParameterDefinition(
name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility());

assertEquals(allowedSlaves, nodeParameterDefinition1.getAllowedNodesOrAll());
assertThat(parameterDefinition.getAllowedNodesOrAll(), is(allowedAgents));
}

allowedSlaves = new ArrayList<>();
allowedSlaves.add("master");
@Test
public void testGetAllowedNodesOrAllWithBuiltIn() {
String name = "name";
String description = "description";
List<String> defaultAgents = new ArrayList<>();
List<String> allowedAgents = new ArrayList<>();
allowedAgents.add("built-in");
String triggerIfResult = "triggerIfResult";

NodeParameterDefinition nodeParameterDefinition2 = new NodeParameterDefinition(
name, description, defaultSlaves, allowedSlaves, triggerIfResult, new AllNodeEligibility());
NodeParameterDefinition parameterDefinition = new NodeParameterDefinition(
name, description, defaultAgents, allowedAgents, triggerIfResult, new AllNodeEligibility());

assertEquals(allowedSlaves, nodeParameterDefinition2.getAllowedNodesOrAll());
assertThat(parameterDefinition.getAllowedNodesOrAll(), is(allowedAgents));
}

@Test
public void testGetHelpFile() {
NodeParameterDefinition.DescriptorImpl descriptorImpl = new NodeParameterDefinition.DescriptorImpl();

assertEquals(descriptorImpl.getHelpFile(), "/plugin/nodelabelparameter/nodeparam.html");
assertThat(descriptorImpl.getHelpFile(), is("/plugin/nodelabelparameter/nodeparam.html"));
}

@Test
public void testGetDefaultNodeEligibility() {
NodeParameterDefinition.DescriptorImpl descriptorImpl = new NodeParameterDefinition.DescriptorImpl();

assertNotNull(descriptorImpl.getDefaultNodeEligibility());
assertThat(descriptorImpl.getDefaultNodeEligibility(), instanceOf(AllNodeEligibility.class));
}
}

0 comments on commit d924ff1

Please sign in to comment.