From 287c3b8b3a4b3c8d31db0f6cbda26a11556dd7c5 Mon Sep 17 00:00:00 2001 From: Yash Pal <112522911+yashpal2104@users.noreply.github.com> Date: Mon, 23 Dec 2024 19:52:50 +0530 Subject: [PATCH] Add test for AllNodesBuildParameterFactory class (#369) * Add test for AllNodesBuildParameterFactory class * removed unsused object nodeName * method to maintain a clean state for subsequent tests * Apply mvn:spotless * Apply phrasing changes on the comments --------- Co-authored-by: Mark Waite --- .../AllNodesBuildParameterFactoryTest.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/AllNodesBuildParameterFactoryTest.java diff --git a/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/AllNodesBuildParameterFactoryTest.java b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/AllNodesBuildParameterFactoryTest.java new file mode 100644 index 0000000..d7742e1 --- /dev/null +++ b/src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/parameterizedtrigger/AllNodesBuildParameterFactoryTest.java @@ -0,0 +1,111 @@ +package org.jvnet.jenkins.plugins.nodelabelparameter.parameterizedtrigger; + +import static org.junit.Assert.*; + +import hudson.model.Computer; +import hudson.model.Node; +import hudson.plugins.parameterizedtrigger.AbstractBuildParameters; +import hudson.slaves.DumbSlave; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import jenkins.model.Jenkins; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +public class AllNodesBuildParameterFactoryTest { + + @Rule + public JenkinsRule j = new JenkinsRule(); + + private AllNodesBuildParameterFactory factory; + + @Before + public void setUp() throws Exception { + factory = new AllNodesBuildParameterFactory(); + } + + @After + public void tearDown() throws Exception { + Computer[] computers = Jenkins.get().getComputers(); + for (Computer computer : computers) { + computer.setTemporarilyOffline(false, null); + } + } + + @Test + public void getParameters_withAllNodesOnline() throws Exception { + DumbSlave node1 = j.createOnlineSlave(); + DumbSlave node2 = j.createOnlineSlave(); + + List params = factory.getParameters(null, j.createTaskListener()); + + // Parameters should include 2 agents and the Jenkins controller (built-in) + assertEquals(3, params.size()); + } + + @Test + public void getParameters_withAllNodesOffline() throws Exception { + DumbSlave node1 = j.createSlave(); + DumbSlave node2 = j.createSlave(); + + Objects.requireNonNull(node1.toComputer()).disconnect(null); + Objects.requireNonNull(node2.toComputer()).disconnect(null); + + List params = factory.getParameters(null, j.createTaskListener()); + // Parameters should only include the controller, since 2 agents are offline + // AllNodes factory only returns online agents with 1 or more executors + assertEquals(1, params.size()); + } + + @Test + public void getParameters_withMixedNodeStates() throws Exception { + DumbSlave node1 = j.createOnlineSlave(); + DumbSlave node2 = j.createSlave(); + + Objects.requireNonNull(node2.toComputer()).disconnect(null); + + List params = factory.getParameters(null, j.createTaskListener()); + // Parameters should include the controller and the 1 online agent + assertEquals(2, params.size()); + } + + @Test + public void getParameters_withNoNodes() throws Exception { + Computer[] computers = Jenkins.get().getComputers(); + for (Computer computer : computers) { + computer.setTemporarilyOffline(true, null); + } + + List params = factory.getParameters(null, j.createTaskListener()); + + assertTrue(params.isEmpty()); + } + + @Test + public void getParameters_setOneExecutorAgentToZero() throws Exception { + DumbSlave node1 = j.createOnlineSlave(); + DumbSlave node2 = j.createOnlineSlave(); + + node1.setNumExecutors(0); + + List params = factory.getParameters(null, j.createTaskListener()); + + // node1 will not be able to execute any builds but it will be included in the list of nodes + assertEquals(3, params.size()); + + // Create a list of nodes that can execute builds + List buildableNodes = new ArrayList<>(); + for (Computer c : Jenkins.get().getComputers()) { + Node n = c.getNode(); + if (n != null && c.isOnline() && c.getNumExecutors() > 0) { + buildableNodes.add(n); + } + } + // Check that the number of buildable nodes is equal to the number of parameters + assertEquals(buildableNodes.size(), params.size()); + } +}