Java Code Examples for org.jenkinsci.plugins.workflow.cps.CpsFlowExecution#waitForSuspension()

The following examples show how to use org.jenkinsci.plugins.workflow.cps.CpsFlowExecution#waitForSuspension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("JENKINS-49297")
public void submitInputPostBlock() throws Exception {
    WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    URL resource = Resources.getResource(getClass(), "stepsFromPost.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    job.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    QueueTaskFuture<WorkflowRun> buildTask = job.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();

    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }

    List<Map> nodes = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);

    assertEquals(1, nodes.size());

    List<Map> steps = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + nodes.get(0).get("id") + "/steps/", List.class);

    assertEquals(3, steps.size());

    assertEquals("7", steps.get(0).get("id"));
    assertEquals("Hello World", steps.get(0).get("displayDescription"));
    assertEquals("12", steps.get(1).get("id"));
    assertEquals("Hello World from post", steps.get(1).get("displayDescription"));
    assertEquals("13", steps.get(2).get("id"));
    assertEquals("Wait for interactive input", steps.get(2).get("displayName"));
}
 
Example 2
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("JENKINS-48884")
public void submitInputPostBlockWithParallelStages() throws Exception {
    WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    URL resource = Resources.getResource(getClass(), "parallelStepsFromPost.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    job.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    QueueTaskFuture<WorkflowRun> buildTask = job.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();

    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }

    List<Map> nodes = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);

    assertEquals(6, nodes.size());

    List<Map> steps = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + nodes.get(0).get("id") + "/steps/", List.class);

    assertEquals(4, steps.size());

    assertEquals("15", steps.get(0).get("id"));
    assertEquals("exit 1", steps.get(0).get("displayDescription"));
    assertEquals("17", steps.get(1).get("id"));
    assertEquals("hello stable", steps.get(1).get("displayDescription"));
    assertEquals("47", steps.get(2).get("id"));
    assertEquals("Hello World from post", steps.get(2).get("displayDescription"));
    assertEquals("48", steps.get(3).get("id"));
    assertEquals("Wait for interactive input", steps.get(3).get("displayName"));
}
 
Example 3
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void waitForInputTest() throws Exception {
    String script = "node {\n" +
        "    stage(\"parallelStage\"){\n" +
        "      parallel left : {\n" +
        "            echo \"running\"\n" +
        "            def branchInput = input message: 'Please input branch to test against', parameters: [[$class: 'StringParameterDefinition', defaultValue: 'master', description: '', name: 'branch']]\n" +
        "            echo \"BRANCH NAME: ${branchInput}\"\n" +
        "        }, \n" +
        "        right : {\n" +
        "            sh 'sleep 100000'\n" +
        "        }\n" +
        "    }\n" +
        "}";

    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script, false));
    QueueTaskFuture<WorkflowRun> buildTask = job1.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();

    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }

    Map runResp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/");

    Assert.assertEquals("PAUSED", runResp.get("state"));

    List<FlowNodeWrapper> nodes = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(run).getPipelineNodes();

    List<Map> nodesResp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);

    Assert.assertEquals("PAUSED", nodesResp.get(0).get("state"));
    Assert.assertEquals("UNKNOWN", nodesResp.get(0).get("result"));
    Assert.assertEquals("parallelStage", nodesResp.get(0).get("displayName"));

    Assert.assertEquals("PAUSED", nodesResp.get(1).get("state"));
    Assert.assertEquals("UNKNOWN", nodesResp.get(1).get("result"));
    Assert.assertEquals("left", nodesResp.get(1).get("displayName"));

    Assert.assertEquals("RUNNING", nodesResp.get(2).get("state"));
    Assert.assertEquals("UNKNOWN", nodesResp.get(2).get("result"));
    Assert.assertEquals("right", nodesResp.get(2).get("displayName"));

    List<Map> stepsResp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);

    Assert.assertEquals("RUNNING", stepsResp.get(0).get("state"));
    Assert.assertEquals("UNKNOWN", stepsResp.get(0).get("result"));
    Assert.assertEquals("13", stepsResp.get(0).get("id"));

    Assert.assertEquals("PAUSED", stepsResp.get(2).get("state"));
    Assert.assertEquals("UNKNOWN", stepsResp.get(2).get("result"));
    Assert.assertEquals("12", stepsResp.get(2).get("id"));
}
 
Example 4
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void submitInput() throws Exception {
    String script = "node {\n" +
        "    stage(\"first\"){\n" +
        "            def branchInput = input message: 'Please input branch to test against', parameters: [[$class: 'StringParameterDefinition', defaultValue: 'master', description: '', name: 'branch']]\n" +
        "            echo \"BRANCH NAME: ${branchInput}\"\n" +
        "    }\n" +
        "}";

    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script, false));
    QueueTaskFuture<WorkflowRun> buildTask = job1.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();

    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }


    List<Map> stepsResp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);


    Assert.assertEquals("PAUSED", stepsResp.get(0).get("state"));
    Assert.assertEquals("UNKNOWN", stepsResp.get(0).get("result"));
    Assert.assertEquals("7", stepsResp.get(0).get("id"));

    Map<String, Object> input = (Map<String, Object>) stepsResp.get(0).get("input");
    Assert.assertNotNull(input);
    String id = (String) input.get("id");
    Assert.assertNotNull(id);

    List<Map<String, Object>> params = (List<Map<String, Object>>) input.get("parameters");

    post("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/7/",
         ImmutableMap.of("id", id,
                         PARAMETERS_ELEMENT,
                         ImmutableList.of(ImmutableMap.of("name", params.get(0).get("name"), "value", "master"))
         )
        , 200);

    if (waitForBuildCount(job1, 1, Result.SUCCESS)) {
        Map<String, Object> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/7/");
        Assert.assertEquals("FINISHED", resp.get("state"));
        Assert.assertEquals("SUCCESS", resp.get("result"));
        Assert.assertEquals("7", resp.get("id"));
    }
}
 
Example 5
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void abortInput() throws Exception {
    String script = "node {\n" +
        "    stage(\"thing\"){\n" +
        "            input 'continue'\n" +
        "    }\n" +
        "}";

    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script, false));
    QueueTaskFuture<WorkflowRun> buildTask = job1.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();

    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }

    List<Map> stepsResp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);

    System.out.println(stepsResp);

    Assert.assertEquals("PAUSED", stepsResp.get(0).get("state"));
    Assert.assertEquals("UNKNOWN", stepsResp.get(0).get("result"));
    String stepId = (String) stepsResp.get(0).get("id");
    //Assert.assertEquals("7", stepsResp.get(0).get("id"));

    Map<String, Object> input = (Map<String, Object>) stepsResp.get(0).get("input");
    Assert.assertNotNull(input);
    String id = (String) input.get("id");
    Assert.assertNotNull(id);

    JSONObject req = new JSONObject();
    req.put("id", id);
    req.put("abort", true);

    post("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/" + stepId + "/", req, 200);

    if (waitForBuildCount(job1, 1, Result.ABORTED)) {
        Map<String, Object> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/" + stepId + "/");
        Assert.assertEquals("FINISHED", resp.get("state"));
        Assert.assertEquals("ABORTED", resp.get("result"));
        Assert.assertEquals(stepId, resp.get("id"));
    }
}
 
Example 6
Source File: SseEventTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void pipelineWithInput() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    final OneShotEvent success = new OneShotEvent();

    String script = "node {\n" +
            "  stage(\"build\"){\n" +
            "    echo \"running\"\n" +
            "    input message: 'Please input branch to test against', parameters: [[$class: 'StringParameterDefinition', defaultValue: 'master', description: '', name: 'branch']]\n" +
            "  }\n" +
            "}";

    final boolean[] wasPaused = {false};
    final boolean[] wasUnPaused = {false};
    final AssertionHelper assertionHelper = new AssertionHelper();
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {
        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            if("job".equals(message.get(jenkins_channel))) {
                assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
                assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
                if(message.get(jenkins_event).equals(Events.JobChannel.job_run_queue_left.name())) {
                    assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                    assertionHelper.isNotNull(message.get(Job.job_run_queueId));
                    assertionHelper.isNotNull(message.get(Job.job_run_status));
                }
                assertionHelper.isEquals("pipeline1", message.get(job_name));
                assertionHelper.isEquals("job", message.get(jenkins_channel));
                assertionHelper.isEquals("jenkins", message.get(jenkins_org));
                assertionHelper.isNull(message.get(job_ismultibranch));
                assertionHelper.isNull(message.get(job_multibranch_indexing_result));
                assertionHelper.isNull(message.get(job_multibranch_indexing_status));

                if("job_run_unpaused".equals(message.get(jenkins_event))){
                    wasUnPaused[0] = true;
                }
            }else if("pipeline".equals(message.get(jenkins_channel))){
                assertionHelper.isEquals("1", message.get(pipeline_run_id));
                if(message.get(jenkins_event).equals(pipeline_stage.name())) {
                    assertionHelper.isEquals("build", message.get(pipeline_step_stage_name));
                }
                if("input".equals(message.get(pipeline_step_name))){
                    wasPaused[0] = true;
                    assertionHelper.isEquals("true", message.get(pipeline_step_is_paused));
                }
            }
            if(wasPaused[0] && wasUnPaused[0]){ // signal finish only when both conditions are met
                success.signal();
            }
        }
    });

    con.subscribe("pipeline");
    con.subscribe("job");

    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script, false));
    QueueTaskFuture<WorkflowRun> buildTask = job1.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();

    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }

    //Now that flow is paused, send a signal that it's un-paused
    ExtensionList<PipelineEventListener.InputStepPublisher> inputStepPublisherList =
            ExtensionList.lookup(PipelineEventListener.InputStepPublisher.class);
    assertFalse(inputStepPublisherList.isEmpty());

    InputAction inputAction = run.getAction(InputAction.class);
    List<InputStepExecution> executionList = inputAction.getExecutions();
    assertFalse(executionList.isEmpty());
    InputStep inputStep = executionList.get(0).getInput();
    inputStepPublisherList.get(0).onStepContinue(inputStep,run);

    success.block(5000);
    con.close();
    if(success.isSignaled()){
        assertTrue(wasPaused[0]);
        assertTrue(wasUnPaused[0]);
    }
    if(assertionHelper.totalErrors() > 0){
        fail("There were errors: "+ assertionHelper.totalErrors());
    }
}