|
1 | 1 | package org.jvnet.jenkins.plugins.nodelabelparameter;
|
2 | 2 |
|
| 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; |
3 | 7 | import static org.junit.Assert.assertFalse;
|
4 |
| -import static org.junit.Assert.assertNotNull; |
5 | 8 | import static org.junit.Assert.assertTrue;
|
6 | 9 |
|
7 | 10 | import hudson.DescriptorExtensionList;
|
8 | 11 | import hudson.model.Node;
|
9 |
| -import hudson.model.labels.LabelAtom; |
10 | 12 | 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; |
13 | 16 | import org.junit.Test;
|
14 | 17 | import org.jvnet.hudson.test.JenkinsRule;
|
15 | 18 | import org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreTempOfflineNodeEligibility;
|
16 | 19 | import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility;
|
17 | 20 | import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility.NodeEligibilityDescriptor;
|
18 | 21 |
|
19 | 22 | public class IgnoreTempOfflineNodeEligibilityTest {
|
20 |
| - @Rule |
21 |
| - public JenkinsRule j = new JenkinsRule(); |
| 23 | + @ClassRule |
| 24 | + public static JenkinsRule j = new JenkinsRule(); |
22 | 25 |
|
23 |
| - private DumbSlave onlineNode1; |
| 26 | + private static DumbSlave onlineNode1; |
24 | 27 |
|
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(); |
28 | 31 | }
|
29 | 32 |
|
30 |
| - IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility = new IgnoreTempOfflineNodeEligibility(); |
| 33 | + private final IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility = |
| 34 | + new IgnoreTempOfflineNodeEligibility(); |
| 35 | + private final Random random = new Random(); |
31 | 36 |
|
32 | 37 | @Test
|
33 | 38 | 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 |
34 | 43 | Node node = null;
|
35 |
| - // Node is null |
36 | 44 | 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 |
40 | 62 | 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")); |
41 | 78 | }
|
42 | 79 |
|
43 | 80 | @Test
|
44 | 81 | public void testGetDescriptor() {
|
45 | 82 | 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")); |
50 | 84 | }
|
51 | 85 |
|
52 | 86 | @Test
|
53 | 87 | public void testAll() {
|
54 | 88 | 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)); |
59 | 93 | }
|
60 | 94 | }
|
0 commit comments