Search in sources :

Example 26 with WorkflowJob

use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method getPipelineJobRunNodesTest.

@Test
public void getPipelineJobRunNodesTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("stage 'build'\n" + "node{\n" + "  echo \"Building...\"\n" + "}\n" + "\n" + "stage 'test'\n" + "parallel 'unit':{\n" + "  node{\n" + "    echo \"Unit testing...\"\n" + "  }\n" + "},'integration':{\n" + "  node{\n" + "    echo \"Integration testing...\"\n" + "  }\n" + "}, 'ui':{\n" + "  node{\n" + "    echo \"UI testing...\"\n" + "  }\n" + "}\n" + "\n" + "stage 'deploy'\n" + "node{\n" + "  echo \"Deploying\"\n" + "}" + "\n" + "stage 'deployToProd'\n" + "node{\n" + "  echo \"Deploying to production\"\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    NodeGraphBuilder builder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1);
    List<FlowNode> nodes = getStagesAndParallels(builder);
    List<FlowNode> parallelNodes = getParallelNodes(builder);
    Assert.assertEquals(7, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(nodes.size(), resp.size());
    for (int i = 0; i < nodes.size(); i++) {
        FlowNode n = nodes.get(i);
        Map rn = resp.get(i);
        Assert.assertEquals(n.getId(), rn.get("id"));
        Assert.assertEquals(getNodeName(n), rn.get("displayName"));
        Assert.assertEquals("SUCCESS", rn.get("result"));
        List<Map> edges = (List<Map>) rn.get("edges");
        Assert.assertTrue((int) rn.get("durationInMillis") > 0);
        if (n.getDisplayName().equals("test")) {
            Assert.assertEquals(parallelNodes.size(), edges.size());
            Assert.assertEquals(edges.get(i).get("id"), parallelNodes.get(i).getId());
        } else if (n.getDisplayName().equals("build")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(i).get("id"), nodes.get(i + 1).getId());
        } else if (n.getDisplayName().equals("deploy")) {
            Assert.assertEquals(1, edges.size());
        } else if (n.getDisplayName().equals("deployToProd")) {
            Assert.assertEquals(0, edges.size());
        } else {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(0).get("id"), nodes.get(nodes.size() - 2).getId());
        }
    }
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) 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) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 27 with WorkflowJob

use of org.jenkinsci.plugins.workflow.job.WorkflowJob 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 28 with WorkflowJob

use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method nodesWithFutureTest.

@Test
public void nodesWithFutureTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("node {\n" + "  stage 'build'\n" + "  sh 'echo s1'\n" + "  stage 'test'\n" + "  echo 'Hello World 2'\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, b1);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    job1.setDefinition(new CpsFlowDefinition("node {\n" + "  stage 'build'\n" + "  sh 'echo s1'\n" + "  stage 'test'\n" + "  echo 'Hello World 2'\n" + "}\n" + "parallel firstBranch: {\n" + "  echo 'Hello first'\n" + "}, secondBranch: {\n" + " echo 'Hello second'\n" + "}"));
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, b2);
    job1.setDefinition(new CpsFlowDefinition("node {\n" + "  stage 'build'\n" + "  sh 'echo s1'\n" + "  stage 'test'\n" + "  echo 'Hello World 2'\n" + "}\n" + "parallel firstBranch: {\n" + "  echo 'Hello first'\n" + "}, secondBranch: {\n" + " sh 'Hello second'\n" + "}"));
    WorkflowRun b3 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b3);
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(2, resp.size());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 29 with WorkflowJob

use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method KyotoNodesFailureTest1.

@Test
public void KyotoNodesFailureTest1() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("pipeline {\n" + "    agent any\n" + "    stages {\n" + "        stage ('Build') {\n" + "steps{\n" + "            sh 'echo1 \"Building\"'\n" + "        }\n" + "}\n" + "        stage ('Test') {\n" + "steps{\n" + "            sh 'echo \"Building\"'\n" + "        }\n" + "}\n" + "        stage ('Deploy') {\n" + "steps{\n" + "            sh 'echo \"Building\"'\n" + "        }\n" + "}\n" + "    }\n" + "}\n"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b1);
    List<Map> nodes = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(3, nodes.size());
    Assert.assertEquals("FAILURE", nodes.get(0).get("result"));
    Assert.assertEquals("FINISHED", nodes.get(0).get("state"));
    Assert.assertEquals("NOT_BUILT", nodes.get(1).get("result"));
    Assert.assertEquals("NOT_BUILT", nodes.get(1).get("state"));
    Assert.assertEquals("NOT_BUILT", nodes.get(2).get("result"));
    Assert.assertEquals("NOT_BUILT", nodes.get(2).get("state"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 30 with WorkflowJob

use of org.jenkinsci.plugins.workflow.job.WorkflowJob in project blueocean-plugin by jenkinsci.

the class PipelineNodeTest method getPipelineJobRunNodesTestWithFuture.

@Test
public void getPipelineJobRunNodesTestWithFuture() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("stage 'build'\n" + "node{\n" + "  echo \"Building...\"\n" + "}\n" + "\n" + "stage 'test'\n" + "parallel 'unit':{\n" + "  node{\n" + "    echo \"Unit testing...\"\n" + "  }\n" + "},'integration':{\n" + "  node{\n" + "    echo \"Integration testing...\"\n" + "  }\n" + "}, 'ui':{\n" + "  node{\n" + "    echo \"UI testing...\"\n" + "  }\n" + "}\n" + "\n" + "stage 'deploy'\n" + "node{\n" + "  echo \"Deploying\"\n" + "}" + "\n" + "stage 'deployToProd'\n" + "node{\n" + "  echo \"Deploying to production\"\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    NodeGraphBuilder builder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1);
    List<FlowNode> nodes = getStagesAndParallels(builder);
    List<FlowNode> parallelNodes = getParallelNodes(builder);
    Assert.assertEquals(7, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(nodes.size(), resp.size());
    for (int i = 0; i < nodes.size(); i++) {
        FlowNode n = nodes.get(i);
        Map rn = resp.get(i);
        Assert.assertEquals(n.getId(), rn.get("id"));
        Assert.assertEquals(getNodeName(n), rn.get("displayName"));
        Assert.assertEquals("SUCCESS", rn.get("result"));
        List<Map> edges = (List<Map>) rn.get("edges");
        if (n.getDisplayName().equals("test")) {
            Assert.assertEquals(parallelNodes.size(), edges.size());
            Assert.assertEquals(edges.get(i).get("id"), parallelNodes.get(i).getId());
        } else if (n.getDisplayName().equals("build")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(i).get("id"), nodes.get(i + 1).getId());
        } else if (n.getDisplayName().equals("deploy")) {
            Assert.assertEquals(1, edges.size());
        } else if (n.getDisplayName().equals("deployToProd")) {
            Assert.assertEquals(0, edges.size());
        } else {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(0).get("id"), nodes.get(nodes.size() - 2).getId());
        }
    }
    job1.setDefinition(new CpsFlowDefinition("stage 'build'\n" + "node{\n" + "  echo \"Building...\"\n" + "}\n" + "\n" + "stage 'test'\n" + "parallel 'unit':{\n" + "  node{\n" + "    echo \"Unit testing...\"\n" + //fail the build intentionally
    "    sh \"`fail-the-build`\"\n" + "  }\n" + "},'integration':{\n" + "  node{\n" + "    echo \"Integration testing...\"\n" + "  }\n" + "}, 'ui':{\n" + "  node{\n" + "    echo \"UI testing...\"\n" + "  }\n" + "}\n" + "\n" + "stage 'deploy'\n" + "node{\n" + "  echo \"Deploying\"\n" + "}" + "\n" + "stage 'deployToProd'\n" + "node{\n" + "  echo \"Deploying to production\"\n" + "}"));
    b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b1);
    resp = get(String.format("/organizations/jenkins/pipelines/pipeline1/runs/%s/nodes/", b1.getId()), List.class);
    Assert.assertEquals(nodes.size(), resp.size());
    for (int i = 0; i < nodes.size(); i++) {
        FlowNode n = nodes.get(i);
        Map rn = resp.get(i);
        Assert.assertEquals(n.getId(), rn.get("id"));
        Assert.assertEquals(getNodeName(n), rn.get("displayName"));
        List<Map> edges = (List<Map>) rn.get("edges");
        if (n.getDisplayName().equals("build")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(i).get("id"), nodes.get(i + 1).getId());
            Assert.assertEquals("SUCCESS", rn.get("result"));
            Assert.assertEquals("FINISHED", rn.get("state"));
        } else if (n.getDisplayName().equals("test")) {
            Assert.assertEquals(parallelNodes.size(), edges.size());
            Assert.assertEquals(edges.get(i).get("id"), parallelNodes.get(i).getId());
            Assert.assertEquals("FAILURE", rn.get("result"));
            Assert.assertEquals("FINISHED", rn.get("state"));
        } else if (PipelineNodeUtil.getDisplayName(n).equals("unit")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(0).get("id"), nodes.get(nodes.size() - 2).getId());
            Assert.assertEquals("FAILURE", rn.get("result"));
            Assert.assertEquals("FINISHED", rn.get("state"));
        } else if (n.getDisplayName().equals("deploy")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
            Assert.assertNull(rn.get("startTime"));
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(0).get("id"), nodes.get(nodes.size() - 1).getId());
        } else if (n.getDisplayName().equals("deployToProd")) {
            Assert.assertEquals(0, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(rn.get("state"));
            Assert.assertNull(rn.get("startTime"));
            Assert.assertEquals(0, edges.size());
        } else {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(edges.get(0).get("id"), nodes.get(nodes.size() - 2).getId());
            Assert.assertEquals("SUCCESS", rn.get("result"));
            Assert.assertEquals("FINISHED", rn.get("state"));
        }
    }
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) 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) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Aggregations

WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)68 Test (org.junit.Test)59 Map (java.util.Map)56 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)51 ImmutableMap (com.google.common.collect.ImmutableMap)46 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)46 List (java.util.List)26 ImmutableList (com.google.common.collect.ImmutableList)23 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)19 BranchSource (jenkins.branch.BranchSource)17 GitSCMSource (jenkins.plugins.git.GitSCMSource)17 SCMSource (jenkins.scm.api.SCMSource)17 RunList (hudson.util.RunList)16 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)14 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)13 ArrayList (java.util.ArrayList)8 FlowGraphTable (org.jenkinsci.plugins.workflow.support.visualization.table.FlowGraphTable)4 JSONObject (net.sf.json.JSONObject)3 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)3 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)3