Skip to content

Commit d19126e

Browse files
committed
Broaden the temp offline eligibility tests
Start the Jenkins controller only once for the entire test so that it runs faster. This is more important on Windows than on Linux, but is not harmful because the tests configure their needed Jenkins configuration at the start of each test. Use nodeName as the isEligible argument rather than label string. Label string did not match a node so would always return false. Remove the label declaration since it is not needed for these tests. Confirm that the Jenkins controller is eligible when it has executors and is not eligible when it does not have executors. Check more details of the descriptors.
1 parent 7d72974 commit d19126e

File tree

1 file changed

+57
-23
lines changed

1 file changed

+57
-23
lines changed
Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,94 @@
11
package org.jvnet.jenkins.plugins.nodelabelparameter;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.hasItem;
5+
import static org.hamcrest.Matchers.hasProperty;
6+
import static org.hamcrest.Matchers.is;
37
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertNotNull;
58
import static org.junit.Assert.assertTrue;
69

710
import hudson.DescriptorExtensionList;
811
import hudson.model.Node;
9-
import hudson.model.labels.LabelAtom;
1012
import hudson.slaves.DumbSlave;
11-
import org.junit.Before;
12-
import org.junit.Rule;
13+
import java.util.Random;
14+
import org.junit.BeforeClass;
15+
import org.junit.ClassRule;
1316
import org.junit.Test;
1417
import org.jvnet.hudson.test.JenkinsRule;
1518
import org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreTempOfflineNodeEligibility;
1619
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility;
1720
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility.NodeEligibilityDescriptor;
1821

1922
public class IgnoreTempOfflineNodeEligibilityTest {
20-
@Rule
21-
public JenkinsRule j = new JenkinsRule();
23+
@ClassRule
24+
public static JenkinsRule j = new JenkinsRule();
2225

23-
private DumbSlave onlineNode1;
26+
private static DumbSlave onlineNode1;
2427

25-
@Before
26-
public void setUp() throws Exception {
27-
onlineNode1 = j.createOnlineSlave(new LabelAtom("label"));
28+
@BeforeClass
29+
public static void createAgent() throws Exception {
30+
onlineNode1 = j.createOnlineSlave();
2831
}
2932

30-
IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility = new IgnoreTempOfflineNodeEligibility();
33+
private final IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility =
34+
new IgnoreTempOfflineNodeEligibility();
35+
private final Random random = new Random();
3136

3237
@Test
3338
public void testGetComputer() throws Exception {
39+
// Does not matter if executors on the Jenkins controller are enabled
40+
j.jenkins.setNumExecutors(random.nextBoolean() ? 1 : 0);
41+
42+
// Null node is never eligible
3443
Node node = null;
35-
// Node is null
3644
assertFalse(ignoreTempOfflineNodeEligibility.isEligible(node));
37-
// Node is not null
38-
assertFalse(ignoreTempOfflineNodeEligibility.isEligible(onlineNode1.getLabelString()));
39-
// Node is null and nodeName is empty
45+
// Non-existent node name is never eligible
46+
assertFalse(ignoreTempOfflineNodeEligibility.isEligible("not-a-valid-node"));
47+
48+
// Online node is always eligible
49+
assertTrue(ignoreTempOfflineNodeEligibility.isEligible(onlineNode1));
50+
// Online node name is always eligible
51+
assertTrue(ignoreTempOfflineNodeEligibility.isEligible(onlineNode1.getNodeName()));
52+
}
53+
54+
@Test
55+
public void testGetComputerWithControllerExecutor() throws Exception {
56+
// Enable executors on the Jenkins controller
57+
j.jenkins.setNumExecutors(1);
58+
59+
// Node of the Jenkins controller is eligible because it has executors
60+
assertTrue(ignoreTempOfflineNodeEligibility.isEligible(j.jenkins));
61+
// Empty node name is eligible because empty string matches the controller
4062
assertTrue(ignoreTempOfflineNodeEligibility.isEligible(""));
63+
// Node name of the Jenkins controller is eligible because it has executors
64+
assertTrue(ignoreTempOfflineNodeEligibility.isEligible("built-in"));
65+
}
66+
67+
@Test
68+
public void testGetComputerWithoutControllerExecutor() throws Exception {
69+
// Disable executors on the Jenkins controller
70+
j.jenkins.setNumExecutors(0);
71+
72+
// Node of the Jenkins controller is not eligible because it has no executors
73+
assertFalse(ignoreTempOfflineNodeEligibility.isEligible(j.jenkins));
74+
// Empty node name is not eligible because empty string matches the controller
75+
assertFalse(ignoreTempOfflineNodeEligibility.isEligible(""));
76+
// Node name of the Jenkins controller is not eligible because it has no executors
77+
assertFalse(ignoreTempOfflineNodeEligibility.isEligible("built-in"));
4178
}
4279

4380
@Test
4481
public void testGetDescriptor() {
4582
NodeEligibilityDescriptor descriptor = ignoreTempOfflineNodeEligibility.getDescriptor();
46-
// Check if descriptor is not null
47-
assertNotNull(descriptor);
48-
// Check if descriptor is an instance of NodeEligibilityDescriptor
49-
assertTrue(descriptor instanceof NodeEligibilityDescriptor);
83+
assertThat(descriptor.getDisplayName(), is("Ignore Temp Offline Nodes"));
5084
}
5185

5286
@Test
5387
public void testAll() {
5488
DescriptorExtensionList<NodeEligibility, NodeEligibilityDescriptor> descriptors = NodeEligibility.all();
55-
// Check if descriptors is not empty
56-
assertTrue(!descriptors.isEmpty());
57-
// Check if descriptors is not null
58-
assertNotNull(descriptors);
89+
assertThat(descriptors, hasItem(hasProperty("displayName", is("All Nodes"))));
90+
assertThat(descriptors, hasItem(hasProperty("displayName", is("Ignore Offline Nodes"))));
91+
assertThat(descriptors, hasItem(hasProperty("displayName", is("Ignore Temp Offline Nodes"))));
92+
assertThat(descriptors.size(), is(3));
5993
}
6094
}

0 commit comments

Comments
 (0)