Search in sources :

Example 1 with CpsFlowExecution

use of org.jenkinsci.plugins.workflow.cps.CpsFlowExecution in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method waitForInputTest.

@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));
    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"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) InputAction(org.jenkinsci.plugins.workflow.support.steps.input.InputAction) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) Test(org.junit.Test)

Example 2 with CpsFlowExecution

use of org.jenkinsci.plugins.workflow.cps.CpsFlowExecution in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method abortInput.

@Test
public void abortInput() 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 'echo 'right done''\n" + "        }\n" + "    }\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script));
    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("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"));
    Map<String, Object> input = (Map<String, Object>) stepsResp.get(2).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/12/", req, 200);
    if (waitForBuildCount(job1, 1, Result.ABORTED)) {
        Map<String, Object> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/12/");
        Assert.assertEquals("FINISHED", resp.get("state"));
        Assert.assertEquals("ABORTED", resp.get("result"));
        Assert.assertEquals("12", resp.get("id"));
    }
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) JSONObject(net.sf.json.JSONObject) InputAction(org.jenkinsci.plugins.workflow.support.steps.input.InputAction) JSONObject(net.sf.json.JSONObject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) Test(org.junit.Test)

Example 3 with CpsFlowExecution

use of org.jenkinsci.plugins.workflow.cps.CpsFlowExecution in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method submitInput.

@Test
public void submitInput() 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 'echo \"right done\"'\n" + "        }\n" + "    }\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script));
    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("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"));
    Map<String, Object> input = (Map<String, Object>) stepsResp.get(2).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/12/", 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/12/");
        Assert.assertEquals("FINISHED", resp.get("state"));
        Assert.assertEquals("SUCCESS", resp.get("result"));
        Assert.assertEquals("12", resp.get("id"));
    }
}
Also used : InputAction(org.jenkinsci.plugins.workflow.support.steps.input.InputAction) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) JSONObject(net.sf.json.JSONObject) RunList(hudson.util.RunList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)3 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)3 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)3 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)3 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)3 Test (org.junit.Test)3 JSONObject (net.sf.json.JSONObject)2 ImmutableList (com.google.common.collect.ImmutableList)1 RunList (hudson.util.RunList)1 List (java.util.List)1