Search in sources :

Example 86 with WorkflowJob

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

the class PipelineApiTest method getPipelineJobRuns.

@Test
public void getPipelineJobRuns() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Build1'); " + "   echo ('Building'); " + "   stage ('Test1'); " + "   sleep 10000      " + "   echo ('Testing'); " + "}"));
    job1.setConcurrentBuild(false);
    WorkflowRun r = job1.scheduleBuild2(0).waitForStart();
    job1.scheduleBuild2(0);
    List l = request().get("/organizations/jenkins/pipelines/pipeline1/runs").build(List.class);
    Assert.assertEquals(2, l.size());
    Assert.assertEquals("io.jenkins.blueocean.service.embedded.rest.QueuedBlueRun", ((Map) l.get(0)).get("_class"));
    Assert.assertEquals("io.jenkins.blueocean.rest.impl.pipeline.PipelineRunImpl", ((Map) l.get(1)).get("_class"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) List(java.util.List) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 87 with WorkflowJob

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

the class PipelineNodeTest method nodesTest1.

// TODO: Enable this test if there is way to determine when test starts running and not waiting till launched
@Test
@Ignore
public void nodesTest1() throws IOException, ExecutionException, InterruptedException {
    WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "p1");
    job.setDefinition(new CpsFlowDefinition("node {\n" + "   stage 'Stage 1a'\n" + "    echo 'Stage 1a'\n" + "\n" + "   stage 'Stage 2'\n" + "   echo 'Stage 2'\n" + "}\n" + "node {\n" + "    stage 'testing'\n" + "    echo 'testing'\n" + "}\n" + "\n" + "node {\n" + "    parallel firstBranch: {\n" + "    echo 'first Branch'\n" + "    sh 'sleep 1'\n" + "    echo 'first Branch end'\n" + "    }, secondBranch: {\n" + "       echo 'Hello second Branch'\n" + "    sh 'sleep 1'   \n" + "    echo 'second Branch end'\n" + "       \n" + "    },\n" + "    failFast: false\n" + "}"));
    job.scheduleBuild2(0).waitForStart();
    Thread.sleep(1000);
    List<Map> resp = get("/organizations/jenkins/pipelines/p1/runs/1/nodes/", List.class);
    for (int i = 0; i < resp.size(); i++) {
        Map rn = resp.get(i);
        List<Map> edges = (List<Map>) rn.get("edges");
        if (rn.get("displayName").equals("Stage 1a")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("Stage 2")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("testing")) {
            Assert.assertEquals(2, edges.size());
            Assert.assertEquals(rn.get("result"), "UNKNOWN");
            Assert.assertEquals(rn.get("state"), "RUNNING");
        } else if (rn.get("displayName").equals("firstBranch")) {
            Assert.assertEquals(0, edges.size());
            Assert.assertEquals(rn.get("result"), "UNKNOWN");
            Assert.assertEquals(rn.get("state"), "RUNNING");
        } else if (rn.get("displayName").equals("secondBranch")) {
            Assert.assertEquals(0, edges.size());
            Assert.assertEquals(rn.get("result"), "UNKNOWN");
            Assert.assertEquals(rn.get("state"), "RUNNING");
        }
    }
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 88 with WorkflowJob

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

the class PipelineNodeTest method declarativeSyntheticSkippedStage.

@Test
public void declarativeSyntheticSkippedStage() throws Exception {
    setupScm("pipeline {\n" + "    agent any\n" + "    stages {\n" + "        stage(\"build\") {\n" + "            steps{\n" + "              sh 'echo \"Start Build\"'\n" + "              echo 'End Build'\n" + "            }\n" + "        }\n" + "        stage(\"SkippedStage\") {\n" + "            when {\n" + "        expression {\n" + "                return false\n" + "        }\n" + "            }\n" + "            steps {\n" + "                script {\n" + "                    echo \"World\"\n" + "                    echo \"Heal it\"\n" + "                }\n" + "\n" + "            }\n" + "        }\n" + "        stage(\"deploy\") {\n" + "            steps{\n" + "              sh 'echo \"Start Deploy\"'\n" + "              sh 'echo \"Deploying...\"'\n" + "              sh 'echo \"End Deploy\"'\n" + "            }           \n" + "        }\n" + "    }\n" + "    post {\n" + "        failure {\n" + "            echo \"failed\"\n" + "        }\n" + "        success {\n" + "            echo \"success\"\n" + "        }\n" + "    }\n" + "}");
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    mp.scheduleBuild2(0).getFuture().get();
    j.waitUntilNoActivity();
    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    Assert.assertEquals(Result.SUCCESS, b1.getResult());
    List<FlowNode> stages = getStages(NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1));
    Assert.assertEquals(3, stages.size());
    Assert.assertEquals("build", stages.get(0).getDisplayName());
    Assert.assertEquals("SkippedStage", stages.get(1).getDisplayName());
    Assert.assertEquals("deploy", stages.get(2).getDisplayName());
    List<Map> resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/", List.class);
    Assert.assertEquals(3, resp.size());
    Assert.assertEquals("build", resp.get(0).get("displayName"));
    Assert.assertEquals("SkippedStage", resp.get(1).get("displayName"));
    Assert.assertEquals("deploy", resp.get(2).get("displayName"));
    // check status
    Assert.assertEquals("SUCCESS", resp.get(0).get("result"));
    Assert.assertEquals("FINISHED", resp.get(0).get("state"));
    Assert.assertEquals("NOT_BUILT", resp.get(1).get("result"));
    Assert.assertEquals("SKIPPED", resp.get(1).get("state"));
    Assert.assertEquals("SUCCESS", resp.get(2).get("result"));
    Assert.assertEquals("FINISHED", resp.get(2).get("state"));
    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/steps/", List.class);
    Assert.assertEquals(7, resp.size());
    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(0).getId() + "/steps/", List.class);
    Assert.assertEquals(3, resp.size());
    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(1).getId() + "/steps/", List.class);
    Assert.assertEquals(0, resp.size());
    resp = get("/organizations/jenkins/pipelines/p/pipelines/master/runs/" + b1.getId() + "/nodes/" + stages.get(2).getId() + "/steps/", List.class);
    Assert.assertEquals(4, resp.size());
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) RunList(hudson.util.RunList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) 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 89 with WorkflowJob

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

the class PipelineNodeTest method testNestedBlockStage.

@Test
public void testNestedBlockStage() throws Exception {
    String pipeline = "" + "node {" + // start
    "   stage ('dev');" + "     echo ('development'); " + "   stage ('Build') { " + "     echo ('Building'); " + "     stage('Packaging') {" + "         echo 'packaging...'" + "     }" + "   } \n" + "   stage ('test') { " + "     echo ('Testing'); " + "     parallel firstBranch: {\n" + "       echo 'Hello first Branch'\n" + "       echo 'first Branch 1'\n" + "       echo 'first Branch end'\n" + "       \n" + "    },\n" + "    thirdBranch: {\n" + "       echo 'Hello third Branch'\n" + "       sh 'sleep 1'   \n" + "       echo 'third Branch 1'\n" + "       echo 'third Branch 2'\n" + "       echo 'third Branch end'\n" + "       \n" + "    },\n" + "    secondBranch: {" + "       echo 'first Branch'\n" + "     stage('firstBranchTest') {" + "       echo 'running firstBranchTest'\n" + "       sh 'sleep 1'\n" + "     }\n" + "       echo 'first Branch end'\n" + "     },\n" + "    failFast: false\n" + "   } \n" + "   stage ('deploy') { " + "     writeFile file: 'file.txt', text:'content'; " + "     archive(includes: 'file.txt'); " + "     echo ('Deploying'); " + "   } \n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(pipeline));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    NodeGraphBuilder builder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1);
    List<FlowNode> stages = getStages(builder);
    List<FlowNode> parallels = getParallelNodes(builder);
    Assert.assertEquals(4, stages.size());
    Assert.assertEquals(3, parallels.size());
    // TODO: complete test
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(7, resp.size());
    String testStageId = null;
    String devNodeId = null;
    for (int i = 0; i < resp.size(); i++) {
        Map rn = resp.get(i);
        List<Map> edges = (List<Map>) rn.get("edges");
        if (rn.get("displayName").equals("dev")) {
            Assert.assertEquals(0, i);
            devNodeId = (String) rn.get("id");
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("build")) {
            Assert.assertEquals(1, i);
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("test")) {
            Assert.assertEquals(2, i);
            testStageId = (String) rn.get("id");
            Assert.assertEquals(3, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("firstBranch")) {
            Assert.assertEquals(3, i);
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("secondBranch")) {
            Assert.assertEquals(4, i);
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("thirdBranch")) {
            Assert.assertEquals(5, i);
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("deploy")) {
            Assert.assertEquals(6, i);
            Assert.assertEquals(0, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        }
    }
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);
    Assert.assertEquals(19, resp.size());
    Assert.assertNotNull(testStageId);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + testStageId + "/steps/", List.class);
    Assert.assertEquals(13, resp.size());
    // firstBranch is parallel with nested stage. firstBranch /steps should also include steps inside nested stage
    FlowNode firstBranch = null;
    FlowNode secondBranch = null;
    FlowNode thirdBranch = null;
    for (FlowNode n : parallels) {
        if (n.getDisplayName().equals("Branch: firstBranch")) {
            firstBranch = n;
        }
        if (n.getDisplayName().equals("Branch: secondBranch")) {
            secondBranch = n;
        }
        if (n.getDisplayName().equals("Branch: thirdBranch")) {
            thirdBranch = n;
        }
    }
    Assert.assertNotNull(firstBranch);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + firstBranch.getId() + "/steps/", List.class);
    Assert.assertEquals(3, resp.size());
    Assert.assertNotNull(secondBranch);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + secondBranch.getId() + "/steps/", List.class);
    Assert.assertEquals(4, resp.size());
    Assert.assertNotNull(thirdBranch);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + thirdBranch.getId() + "/steps/", List.class);
    Assert.assertEquals(5, resp.size());
    Assert.assertNotNull(devNodeId);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + devNodeId + "/steps/", List.class);
    Assert.assertEquals(1, resp.size());
}
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 90 with WorkflowJob

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

the class PipelineNodeTest method getPipelineJobRunNodeTest.

@Test
public void getPipelineJobRunNodeTest() 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" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    FlowGraphTable nodeGraphTable = new FlowGraphTable(b1.getExecution());
    nodeGraphTable.build();
    List<FlowNode> nodes = getStages(nodeGraphTable);
    List<FlowNode> parallelNodes = getParallelNodes(nodeGraphTable);
    Assert.assertEquals(6, nodes.size());
    Assert.assertEquals(3, parallelNodes.size());
    // get all nodes for pipeline1
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(nodes.size(), resp.size());
    // Get a node detail
    FlowNode n = nodes.get(0);
    Map node = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + n.getId());
    List<Map> edges = (List<Map>) node.get("edges");
    Assert.assertEquals(n.getId(), node.get("id"));
    Assert.assertEquals(getNodeName(n), node.get("displayName"));
    Assert.assertEquals("SUCCESS", node.get("result"));
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(nodes.get(1).getId(), edges.get(0).get("id"));
    // Get a parallel node detail
    node = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + parallelNodes.get(0).getId());
    n = parallelNodes.get(0);
    edges = (List<Map>) node.get("edges");
    Assert.assertEquals(n.getId(), node.get("id"));
    Assert.assertEquals(getNodeName(n), node.get("displayName"));
    Assert.assertEquals("SUCCESS", node.get("result"));
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(nodes.get(nodes.size() - 1).getId(), edges.get(0).get("id"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FlowGraphTable(org.jenkinsci.plugins.workflow.support.visualization.table.FlowGraphTable) 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)100 Test (org.junit.Test)91 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)74 Map (java.util.Map)73 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)62 ImmutableMap (com.google.common.collect.ImmutableMap)55 List (java.util.List)34 ImmutableList (com.google.common.collect.ImmutableList)28 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)26 BranchSource (jenkins.branch.BranchSource)24 GitSCMSource (jenkins.plugins.git.GitSCMSource)24 SCMSource (jenkins.scm.api.SCMSource)23 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)20 RunList (hudson.util.RunList)19 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)17 ArrayList (java.util.ArrayList)10 URL (java.net.URL)9 Run (hudson.model.Run)8 Issue (org.jvnet.hudson.test.Issue)8 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)7