Search in sources :

Example 66 with CpsFlowDefinition

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

the class PipelineNodeTest method downstreamBuildLinks.

@Test
@Issue("JENKINS-38339")
public void downstreamBuildLinks() throws Exception {
    FreeStyleProject downstream1 = j.createFreeStyleProject("downstream1");
    FreeStyleProject downstream2 = j.createFreeStyleProject("downstream2");
    WorkflowJob upstream = j.createProject(WorkflowJob.class, "upstream");
    URL resource = getClass().getResource("downstreamBuildLinks.jenkinsfile");
    String jenkinsFile = IOUtils.toString(resource, StandardCharsets.UTF_8);
    upstream.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    j.assertBuildStatus(Result.SUCCESS, upstream.scheduleBuild2(0));
    WorkflowRun r = upstream.getLastBuild();
    List<Map> resp = get("/organizations/jenkins/pipelines/upstream/runs/" + r.getId() + "/nodes/", List.class);
    assertEquals("number of nodes", 5, resp.size());
    Matcher actionMatcher1 = new NodeDownstreamBuildActionMatcher("downstream1");
    Matcher actionMatcher2 = new NodeDownstreamBuildActionMatcher("downstream2");
    List<Map> actions = (List<Map>) resp.get(2).get("actions");
    assertThat("node #2 contains a link to downstream1", actions, hasItem(actionMatcher1));
    actions = (List<Map>) resp.get(3).get("actions");
    assertThat("node #3 contains a link to downstream2", actions, hasItem(actionMatcher2));
    actions = (List<Map>) resp.get(4).get("actions");
    assertThat("node #4 contains a link to downstream1", actions, hasItem(actionMatcher1));
    assertThat("node #4 contains a link to downstream1", actions, hasItem(actionMatcher2));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Matcher(org.hamcrest.Matcher) RunList(hudson.util.RunList) List(java.util.List) ExtensionList(hudson.ExtensionList) FreeStyleProject(hudson.model.FreeStyleProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) URL(java.net.URL) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 67 with CpsFlowDefinition

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

the class PipelineNodeTest method downstreamBuildLinksSequential.

@Test
@Issue("JENKINS-38339")
public void downstreamBuildLinksSequential() throws Exception {
    FreeStyleProject downstream1 = j.createFreeStyleProject("downstream1");
    FreeStyleProject downstream2 = j.createFreeStyleProject("downstream2");
    FreeStyleProject downstream3 = j.createFreeStyleProject("downstream3");
    FreeStyleProject downstream4 = j.createFreeStyleProject("downstream4");
    WorkflowJob upstream = j.createProject(WorkflowJob.class, "upstream");
    URL resource = getClass().getResource("downstreamBuildLinksSeq.jenkinsfile");
    String jenkinsFile = IOUtils.toString(resource, StandardCharsets.UTF_8);
    upstream.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    j.assertBuildStatus(Result.SUCCESS, upstream.scheduleBuild2(0));
    WorkflowRun r = upstream.getLastBuild();
    List<Map> resp = get("/organizations/jenkins/pipelines/upstream/runs/" + r.getId() + "/nodes/", List.class);
    assertEquals("number of nodes", 9, resp.size());
    // Find the nodes we're interested in
    Map node1 = null, node2 = null, node3 = null, node4 = null;
    for (Map node : resp) {
        String displayName = (String) node.get("displayName");
        if ("Single stage branch".equals(displayName)) {
            node1 = node;
        } else if ("Inner".equals(displayName)) {
            node2 = node;
        } else if ("build-ds3".equals(displayName)) {
            node3 = node;
        } else if ("build-ds4".equals(displayName)) {
            node4 = node;
        }
    }
    // Check they all have downstream links
    assertNotNull("missing node1", node1);
    assertThat("node1 contains a link to downstream1", (List<Map>) node1.get("actions"), hasItem(new NodeDownstreamBuildActionMatcher("downstream1")));
    assertNotNull("missing node2", node2);
    assertThat("node2 contains a link to downstream2", (List<Map>) node2.get("actions"), hasItem(new NodeDownstreamBuildActionMatcher("downstream2")));
    assertNotNull("missing node3", node3);
    assertThat("node3 contains a link to downstream3", (List<Map>) node3.get("actions"), hasItem(new NodeDownstreamBuildActionMatcher("downstream3")));
    assertNotNull("missing node4", node4);
    assertThat("node4 contains a link to downstream4", (List<Map>) node4.get("actions"), hasItem(new NodeDownstreamBuildActionMatcher("downstream4")));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FreeStyleProject(hudson.model.FreeStyleProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) URL(java.net.URL) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 68 with CpsFlowDefinition

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

the class PipelineNodeTest method testNonblockStageSteps.

@Test
public void testNonblockStageSteps() throws Exception {
    String pipeline = "node {\n" + "  stage 'Checkout'\n" + "      echo 'checkingout'\n" + "  stage 'Build'\n" + "      echo 'building'\n" + "  stage 'Archive'\n" + "      echo 'archiving...'\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(pipeline, false));
    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(3, stages.size());
    Assert.assertEquals(0, parallels.size());
    // TODO: complete test
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(3, resp.size());
    String checkoutId = (String) resp.get(0).get("id");
    String buildId = (String) resp.get(0).get("id");
    String archiveId = (String) resp.get(0).get("id");
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);
    Assert.assertEquals(3, resp.size());
    Assert.assertNotNull(checkoutId);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + checkoutId + "/steps/", List.class);
    Assert.assertEquals(1, resp.size());
    Assert.assertNotNull(buildId);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + buildId + "/steps/", List.class);
    Assert.assertEquals(1, resp.size());
    Assert.assertNotNull(archiveId);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + archiveId + "/steps/", List.class);
    Assert.assertEquals(1, resp.size());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) RunList(hudson.util.RunList) List(java.util.List) ExtensionList(hudson.ExtensionList) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 69 with CpsFlowDefinition

use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition 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");
    // This is now returning
    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" + "    },\n" + "    'integration':{\n" + "        node {\n" + "            echo \"Integration testing...\"\n" + "        }\n" + "    },\n" + "    'ui':{\n" + "        node{\n" + "            echo \"UI testing...\"\n" + "        }\n" + "    }\n" + "}\n" + "stage ('deploy') {\n" + "    node {\n" + "        echo \"Deploying\"\n" + "    }\n" + "}\n" + "stage ('deployToProd') {\n" + "    node {\n" + "        echo \"Deploying to production\"\n" + "    }\n" + "}", false));
    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" + "    },\n" + "    'integration':{\n" + "        node {\n" + "            echo \"Integration testing...\"\n" + "        }\n" + "    },\n" + "    'ui':{\n" + "        node {\n" + "            echo \"UI testing...\"\n" + "        }\n" + "    }\n" + "}\n" + "stage ('deploy') {\n" + "    node {\n" + "        echo \"Deploying\"\n" + "    }\n" + "}\n" + "stage ('deployToProd') {\n" + "    node{\n" + "        echo \"Deploying to production\"\n" + "    }\n" + "}", false));
    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) List(java.util.List) ExtensionList(hudson.ExtensionList) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Example 70 with CpsFlowDefinition

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

the class PipelineNodeTest method getPipelineJobRunNodesWithFailureTest.

@Test
public void getPipelineJobRunNodesWithFailureTest() 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" + // 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" + "}", false));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b1);
    NodeGraphBuilder builder = NodeGraphBuilder.NodeGraphBuilderFactory.getInstance(b1);
    List<FlowNode> nodes = getStagesAndParallels(builder);
    List<FlowNode> parallelNodes = getParallelNodes(builder);
    Assert.assertEquals(5, 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());
    String unitNodeId = null;
    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");
        switch(n.getDisplayName()) {
            case "test":
                Assert.assertEquals(parallelNodes.size(), edges.size());
                Assert.assertEquals(edges.get(i).get("id"), parallelNodes.get(i).getId());
                Assert.assertEquals("FAILURE", rn.get("result"));
                break;
            case "build":
                Assert.assertEquals(1, edges.size());
                Assert.assertEquals(edges.get(i).get("id"), nodes.get(i + 1).getId());
                Assert.assertEquals("SUCCESS", rn.get("result"));
                break;
            case "Branch: unit":
                unitNodeId = n.getId();
                Assert.assertEquals(0, edges.size());
                Assert.assertEquals("FAILURE", rn.get("result"));
                break;
            default:
                Assert.assertEquals(0, edges.size());
                Assert.assertEquals("SUCCESS", rn.get("result"));
                break;
        }
    }
    assertNotNull(unitNodeId);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + unitNodeId + "/steps/", List.class);
    String log = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + unitNodeId + "/log/", String.class);
    assertNotNull(log);
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) RunList(hudson.util.RunList) List(java.util.List) ExtensionList(hudson.ExtensionList) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) Test(org.junit.Test)

Aggregations

CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)220 Test (org.junit.Test)210 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)182 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)124 Issue (org.jvnet.hudson.test.Issue)63 Map (java.util.Map)60 List (java.util.List)26 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 RunList (hudson.util.RunList)20 ExtensionList (hudson.ExtensionList)19 Statement (org.junit.runners.model.Statement)19 Run (hudson.model.Run)17 URL (java.net.URL)16 AmazonCloudFormation (com.amazonaws.services.cloudformation.AmazonCloudFormation)12 TaskListener (hudson.model.TaskListener)11 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)10 NodeStepTypePredicate (org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate)9 DescribeChangeSetResult (com.amazonaws.services.cloudformation.model.DescribeChangeSetResult)7 Parameter (com.amazonaws.services.cloudformation.model.Parameter)6