use of com.gargoylesoftware.htmlunit.ElementNotFoundException in project workflow-cps-plugin by jenkinsci.
the class CpsFlowExecutionTest method pause.
@Issue("JENKINS-25736")
@Test
public void pause() throws Throwable {
sessions.then(r -> {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.READ, Item.READ).everywhere().toEveryone().grant(Jenkins.ADMINISTER).everywhere().to("admin").grant(Item.BUILD, Item.CANCEL).onItems(p).to("dev"));
r.jenkins.save();
p.setDefinition(new CpsFlowDefinition("echo 'before'; semaphore 'one'; echo 'after'", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("one/1", b);
final CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
assertFalse(e.isPaused());
JenkinsRule.WebClient wc = r.createWebClient();
String toggleUrlRel = b.getUrl() + PauseUnpauseAction.URL + "/toggle";
WebRequest wrs = new WebRequest(wc.createCrumbedUrl(toggleUrlRel), HttpMethod.POST);
try {
// like JenkinsRule.assertFails but taking a WebRequest:
fail("should have been rejected but produced: " + wc.getPage(wrs).getWebResponse().getContentAsString());
} catch (FailingHttpStatusCodeException x) {
// link not even offered
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, x.getStatusCode());
}
wc.login("admin").getPage(wrs);
assertTrue(e.isPaused());
r.waitForMessage("before", b);
SemaphoreStep.success("one/1", null);
// not a very strong way of ensuring that the pause actually happens
Thread.sleep(1000);
assertTrue(b.isBuilding());
assertTrue(e.isPaused());
// link should only be displayed conditionally:
String toggleUrlAbs = r.contextPath + "/" + toggleUrlRel;
r.createWebClient().login("admin").getPage(b).getAnchorByHref(toggleUrlAbs);
try {
r.createWebClient().getPage(b).getAnchorByHref(toggleUrlAbs);
fail("link should not be present for anonymous user without CANCEL");
} catch (ElementNotFoundException x) {
// good
}
});
sessions.then(r -> {
WorkflowJob p = r.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
assertTrue(b.isBuilding());
CpsFlowExecution e = (CpsFlowExecution) b.getExecution();
assertTrue(e.isPaused());
JenkinsRule.WebClient wc = r.createWebClient();
WebRequest wrs = new WebRequest(wc.createCrumbedUrl(b.getUrl() + PauseUnpauseAction.URL + "/toggle"), HttpMethod.POST);
wc.login("dev").getPage(wrs);
assertFalse(e.isPaused());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
assertFalse(e.isPaused());
});
}
Aggregations