Search in sources :

Example 1 with ElementNotFoundException

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());
    });
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)1 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)1 Test (org.junit.Test)1 Issue (org.jvnet.hudson.test.Issue)1 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)1 MockAuthorizationStrategy (org.jvnet.hudson.test.MockAuthorizationStrategy)1