Search in sources :

Example 31 with WorkflowRun

use of org.jenkinsci.plugins.workflow.job.WorkflowRun 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 = Resources.getResource(getClass(), "downstreamBuildLinks.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.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) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) FreeStyleProject(hudson.model.FreeStyleProject) 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) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 32 with WorkflowRun

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

Example 33 with WorkflowRun

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

the class PipelineNodeTest method testBlockStage.

@Test
public void testBlockStage() throws Exception {
    String pipeline = "" + "node {" + // start
    "   stage ('dev');" + "     echo ('development'); " + "   stage ('Build') { " + "     echo ('Building'); " + "   } \n" + "   stage ('test') { " + "     echo ('Testing'); " + // 1
    "     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" + "   } \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(2, parallels.size());
    // TODO: complete test
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(6, resp.size());
    String testStageId = 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(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, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("test")) {
            testStageId = (String) rn.get("id");
            Assert.assertEquals(2, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("firstBranch")) {
            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(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("deploy")) {
            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(12, resp.size());
    Assert.assertNotNull(testStageId);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + testStageId + "/steps/", List.class);
    Assert.assertEquals(7, 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 34 with WorkflowRun

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

the class PipelineNodeTest method syntheticParallelFlowNodeNotSaved.

@Issue("JENKINS-47158")
@Test
public void syntheticParallelFlowNodeNotSaved() throws Exception {
    WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    p.setDefinition(new CpsFlowDefinition("parallel a: {\n" + "    node {\n" + "        echo 'a'\n" + "    }\n" + "}, b: {\n" + "    node {\n" + "        echo 'b'\n" + "    }\n" + "}\n", true));
    WorkflowRun b = j.buildAndAssertSuccess(p);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    FlowExecution rawExec = b.getExecution();
    assert (rawExec instanceof CpsFlowExecution);
    CpsFlowExecution execution = (CpsFlowExecution) rawExec;
    File storage = execution.getStorageDir();
    // Nodes 5 and 6 are the parallel branch start nodes. Make sure no "5-parallel-synthetic.xml" and "6..." files
    // exist in the storage directory, showing we haven't saved them.
    assertFalse(new File(storage, "5-parallel-synthetic.xml").exists());
    assertFalse(new File(storage, "6-parallel-synthetic.xml").exists());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) File(java.io.File) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 35 with WorkflowRun

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

the class JobAnalyticsTest method createMultiBranch.

private void createMultiBranch(GitSampleRepoRule rule) throws Exception {
    WorkflowMultiBranchProject mp = j.createProject(WorkflowMultiBranchProject.class, UUID.randomUUID().toString());
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, rule.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    mp.save();
    mp.scheduleBuild2(0).getFuture().get();
    mp.getIndexing().getLogText().writeLogTo(0, System.out);
    j.waitUntilNoActivity();
    WorkflowJob master = mp.getItemByBranchName("master");
    assertNotNull(master);
    WorkflowRun lastBuild = master.getLastBuild();
    assertNotNull(lastBuild);
    assertEquals(Result.SUCCESS, lastBuild.getResult());
}
Also used : WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun)

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