Skip to content

Commit

Permalink
Backport of #642 to 2.426.x (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Apr 30, 2024
1 parent 4f494a0 commit ea384d8
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 61 deletions.
13 changes: 4 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.79</version>
<version>4.81</version>
<relativePath />
</parent>

Expand All @@ -28,11 +28,6 @@
</licenses>

<developers>
<developer>
<id>basil</id>
<name>Basil Crow</name>
<email>[email protected]</email>
</developer>
<developer>
<id>TobiX</id>
<name>Tobias Gruetzmacher</name>
Expand All @@ -59,7 +54,7 @@

<properties>
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.387.3</jenkins.version>
<jenkins.version>2.426.3</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<spotbugs.effort>Max</spotbugs.effort>
<spotbugs.threshold>Low</spotbugs.threshold>
Expand All @@ -70,8 +65,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.387.x</artifactId>
<version>2543.vfb_1a_5fb_9496d</version>
<artifactId>bom-2.426.x</artifactId>
<version>2982.vdce2153031a_0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ THE SOFTWARE.

<div class="table-responsive">
<table
class="jenkins-table jenkins-!-margin-bottom-4 data-table"
class="jenkins-!-margin-bottom-4 table table-striped display data-table"
id="lockable-resources-labels"
isLoaded="true"
data-remember-search-text="true"
Expand All @@ -47,7 +47,7 @@ THE SOFTWARE.
}'
>
<thead>
<th>${%labels.table.column.labels}</th>
<th class="width-100">${%labels.table.column.labels}</th>
<th>${%labels.table.column.assigned}</th>
<th>${%labels.table.column.free}</th>
<th>${%labels.table.column.percentage}</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ THE SOFTWARE.
<link rel="stylesheet" href="${resURL}/plugin/lockable-resources/css/style.css"/>
<div class="table-responsive">
<table
class="jenkins-table jenkins-!-margin-bottom-4 data-table"
class="jenkins-!-margin-bottom-4 table table-striped display data-table"
id="lockable-resources-queue"
isLoaded="true"
data-remember-search-text="true"
Expand All @@ -69,7 +69,7 @@ THE SOFTWARE.
<th>${%queue.table.column.index}</th>
<th>${%queue.table.column.request.type}</th>
<th>${%queue.table.column.request.info}</th>
<th>${%queue.table.column.reason}</th>
<th class="width-100">${%queue.table.column.reason}</th>
<th>${%queue.table.column.requested.by}</th>
<th>${%queue.table.column.requested.at}</th>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ THE SOFTWARE.

<div class="table-responsive">
<table
class="jenkins-table jenkins-!-margin-bottom-4 data-table"
class="jenkins-!-margin-bottom-4 table table-striped display data-table"
id="lockable-resources"
data-remember-search-text="true"
isLoaded="true"
Expand All @@ -59,28 +59,12 @@ THE SOFTWARE.
]
}'
>
<colgroup>
<!-- index -->
<col class="text-end" />
<!-- resouce -->
<col class="width-100 text-end" />
<!-- status -->
<col class="width-100 text-end" />
<!-- timestamp -->
<col class="width-100 text-end" />
<!-- labels -->
<col class="width-100 text-end" />
<!-- properties -->
<col class="width-100 text-end" />
<!-- actions -->
<col action="width-100 text-end" />
</colgroup>
<thead>
<th data-class-name="index">${%resources.table.column.index}</th>
<th data-class-name="resource">${%resources.table.column.resource}</th>
<th data-class-name="resource" class="width-100">${%resources.table.column.resource}</th>
<th data-class-name="status">${%resources.table.column.status}</th>
<th data-class-name="timestamp">${%resources.table.column.timestamp}</th>
<th data-class-name="labels">${%resources.table.column.labels}</th>
<th data-class-name="labels" class="width-100">${%resources.table.column.labels}</th>
<th data-class-name="properties">${%resources.table.column.properties}</th>
<th data-class-name="action">${%resources.table.column.action}</th>
</thead>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,14 @@ public void unlockButtonWithWaitingRuns() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("lock('resource1') { semaphore('wait-inside') }", true));

JenkinsRule.WebClient wc = j.createWebClient();

WorkflowRun prevBuild = null;
TestHelpers testHelpers = new TestHelpers();
for (int i = 0; i < 3; i++) {
WorkflowRun rNext = p.scheduleBuild2(0).waitForStart();
if (prevBuild != null) {
j.waitForMessage("[resource1] is locked by build " + prevBuild.getFullDisplayName(), rNext);
isPaused(rNext, 1, 1);
// List<LockableResource> resources = new ArrayList<>();
// resources.add(LockableResourcesManager.get().fromName("resource1"));
// LockableResourcesManager.get().unlock(resources, null);
TestHelpers.clickButton(wc, "unlock");
testHelpers.clickButton("unlock", "resource1");
}

j.waitForMessage("Lock acquired on [resource1]", rNext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class LockStepTest_manualUnreserveUnblocksJob extends LockStepTestBase {
@Test
public void manualUnreserveUnblocksJob() throws Exception {
LockableResourcesManager.get().createResource("resource1");
JenkinsRule.WebClient wc = j.createWebClient();

TestHelpers.clickButton(wc, "reserve");
TestHelpers testHelpers = new TestHelpers();
testHelpers.clickButton("reserve", "resource1");
LockableResource resource1 = LockableResourcesManager.get().fromName("resource1");
assertNotNull(resource1);
resource1.setReservedBy("someone");
Expand All @@ -45,7 +45,7 @@ public void manualUnreserveUnblocksJob() throws Exception {
WorkflowRun r = p.scheduleBuild2(0).waitForStart();
j.waitForMessage("[resource1] is not free, waiting for execution ...", r);
j.assertLogNotContains("I am inside", r);
TestHelpers.clickButton(wc, "unreserve");
testHelpers.clickButton("unreserve", "resource1");
j.waitForMessage("I am inside", r);
j.assertLogContains("I am inside", r);
j.assertBuildStatusSuccess(j.waitForCompletion(r));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ public void checkQueueAfterRestart() throws Throwable {

JenkinsRule.WebClient wc = j.createWebClient();
wc.login("user");
TestHelpers.clickButton(wc, "unreserve");

TestHelpers testHelpers = new TestHelpers();
testHelpers.clickButton("unreserve", "resource1");

lrm.unreserve(Collections.singletonList(lrm.fromName("resource1")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public void reserveUnreserveApi() throws Exception {

JenkinsRule.WebClient wc = j.createWebClient();
wc.login("user");
TestHelpers.clickButton(wc, "reserve");
TestHelpers testHelpers = new TestHelpers();
testHelpers.clickButton("reserve", "a1");
assertThat(LockableResourcesManager.get().fromName("a1").isReserved(), is(true));
TestHelpers.clickButton(wc, "unreserve");
testHelpers.clickButton("unreserve", "a1");
assertThat(LockableResourcesManager.get().fromName("a1").isReserved(), is(false));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;

import hudson.model.FreeStyleProject;
import hudson.model.Queue;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlElementUtil;
import org.htmlunit.html.HtmlPage;
import org.jenkins.plugins.lockableresources.actions.LockableResourcesRootAction;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public final class TestHelpers {

Expand All @@ -29,8 +31,18 @@ public final class TestHelpers {
private static final int SLEEP_TIME = 100;
private static final int MAX_WAIT = 5000;

@Mock
private StaplerRequest req;

@Mock
private StaplerResponse rsp;

private AutoCloseable mocks;

// Utility class
private TestHelpers() {}
public TestHelpers() {
this.mocks = MockitoAnnotations.openMocks(this);
}

public static void waitForQueue(Jenkins jenkins, FreeStyleProject job) throws InterruptedException {
waitForQueue(jenkins, job, Queue.Item.class);
Expand Down Expand Up @@ -71,20 +83,31 @@ public static JSONObject getApiData(JenkinsRule rule) throws IOException {
return rule.getJSON("plugin/lockable-resources/api/json").getJSONObject();
}

// Currently assumes one resource or only clicks the button for the first resource
public static void clickButton(JenkinsRule.WebClient wc, String action) throws Exception {
// disable exceptions, otherwise it will not parse jQuery scripts (used ba DataTable plugin)
wc.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage htmlPage = wc.goTo("lockable-resources");
List<HtmlElement> allButtons = htmlPage.getDocumentElement().getElementsByTagName("button");
/** SImulate the click on the button in the LRM page
* note: Currently does not click on the button. Just simulate the doAction (stapler request)
* on the given resource.
* We shall provide some better solution like selenium tests. But for now it is fine.
*/
public void clickButton(String action, String resourceName) throws Exception {
LOGGER.info(action + " on " + resourceName);
LockableResourcesRootAction doAction = new LockableResourcesRootAction();
when(req.getMethod()).thenReturn("POST");
when(req.getParameter("resource")).thenReturn(resourceName);

HtmlElement reserveButton = null;
for (HtmlElement b : allButtons) {
String onClick = b.getAttribute("onClick");
if (onClick != null && onClick.contains(action)) {
reserveButton = b;
switch (action) {
case "reserve": {
doAction.doReserve(req, rsp);
break;
}
case "unreserve": {
doAction.doUnreserve(req, rsp);
break;
}
case "unlock": {
LOGGER.info("doUnlock");
doAction.doUnlock(req, rsp);
break;
}
}
HtmlElementUtil.click(reserveButton);
}
}

0 comments on commit ea384d8

Please sign in to comment.