Skip to content

Commit

Permalink
Adapt plugin for Data Table API 2.0.x (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
mPokornyETM committed Apr 26, 2024
1 parent 0d6a204 commit f48745d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 54 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,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.440.x</artifactId>
<version>2977.vdf61ecb_fb_e2d</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -81,6 +81,7 @@
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>data-tables-api</artifactId>
<version>2.0.5-1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
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 @@ -62,7 +62,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 @@ -81,7 +81,7 @@ THE SOFTWARE.
<th>${%queue.table.column.action}</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>
<th>${%queue.table.column.priority}</th>
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 @@ -745,9 +745,8 @@ 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();
LOGGER.info("start build " + rNext);
Expand All @@ -756,11 +755,8 @@ public void unlockButtonWithWaitingRuns() throws Exception {
j.waitForMessage("[resource1] is locked by build " + prevBuild.getFullDisplayName(), rNext);
LOGGER.info("is paused " + rNext);
isPaused(rNext, 1, 1);
// List<LockableResource> resources = new ArrayList<>();
// resources.add(LockableResourcesManager.get().fromName("resource1"));
LOGGER.info("unlock resource1");
// LockableResourcesManager.get().unlock(resources, null);
TestHelpers.clickButton(wc, "unlock");
testHelpers.clickButton("unlock", "resource1");
}

j.waitForMessage("Trying to acquire lock on [Resource: 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 f48745d

Please sign in to comment.