Search in sources :

Example 66 with WorkflowRun

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

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

use of org.jenkinsci.plugins.workflow.job.WorkflowRun 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)

Example 69 with WorkflowRun

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

the class PipelineNodeTest method testTestsInStage.

@Test
public void testTestsInStage() throws Exception {
    String pipeline = "" + "node {\n" + "  stage ('dev') {\n" + "    junit('*.xml')\n" + "  }\n" + "  stage ('prod') {\n" + "    junit('*.xml')\n" + "  }\n" + "  stage ('testing') {\n" + "    parallel(first: {\n" + "        junit('*.xml')\n" + "      },\n" + "      second: {\n" + "        junit('*.xml')\n" + "      })\n" + "  }\n" + "}\n";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(pipeline, true));
    FilePath ws = j.jenkins.getWorkspaceFor(job1);
    FilePath testFile = ws.child("test-result.xml");
    testFile.copyFrom(PipelineNodeTest.class.getResource("testResult.xml"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    NodeGraphBuilder builder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1);
    List<FlowNode> stages = getStages(builder);
    Assert.assertEquals(3, stages.size());
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(5, resp.size());
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/tests/", List.class);
    Assert.assertEquals(4, resp.size());
    Assert.assertEquals("dev / testDummyMethod – DummyTest", resp.get(0).get("name"));
    Assert.assertEquals("prod / testDummyMethod – DummyTest", resp.get(1).get("name"));
    Assert.assertEquals("testing / first / testDummyMethod – DummyTest", resp.get(2).get("name"));
    Assert.assertEquals("testing / second / testDummyMethod – DummyTest", resp.get(3).get("name"));
}
Also used : FilePath(hudson.FilePath) 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 70 with WorkflowRun

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

the class PipelineNodeTest method testDynamicInnerStage.

@Test
public void testDynamicInnerStage() throws Exception {
    WorkflowJob job = j.jenkins.createProject(WorkflowJob.class, "p");
    URL resource = Resources.getResource(getClass(), "testDynamicInnerStage.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    job.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    WorkflowRun build = job.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, build);
    List<Map> nodes = get("/organizations/jenkins/pipelines/p/runs/1/nodes/", List.class);
    assertEquals(4, nodes.size());
    assertEquals(FlowNodeWrapper.NodeType.STAGE.name(), nodes.get(0).get("type"));
    assertEquals(FlowNodeWrapper.NodeType.STAGE.name(), nodes.get(1).get("type"));
    assertEquals(FlowNodeWrapper.NodeType.PARALLEL.name(), nodes.get(2).get("type"));
    assertEquals(FlowNodeWrapper.NodeType.STAGE.name(), nodes.get(3).get("type"));
}
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) URL(java.net.URL) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Aggregations

WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)77 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)72 Test (org.junit.Test)70 Map (java.util.Map)58 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)53 ImmutableMap (com.google.common.collect.ImmutableMap)45 List (java.util.List)22 ImmutableList (com.google.common.collect.ImmutableList)20 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)19 RunList (hudson.util.RunList)18 BranchSource (jenkins.branch.BranchSource)12 GitSCMSource (jenkins.plugins.git.GitSCMSource)12 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)12 SCMSource (jenkins.scm.api.SCMSource)11 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)8 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)7 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)6 Issue (org.jvnet.hudson.test.Issue)6 Run (hudson.model.Run)4 JSONObject (net.sf.json.JSONObject)4