Search in sources :

Example 46 with WorkflowJob

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

the class PipelineNodeTest method successfulStepWithBlockFailureAfterward.

@Test
@Issue("JENKINS-44742")
public void successfulStepWithBlockFailureAfterward() throws Exception {
    WorkflowJob p = j.createProject(WorkflowJob.class, "project");
    URL resource = Resources.getResource(getClass(), "successfulStepWithBlockFailureAfterward.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();
    Run r = p.scheduleBuild2(0).waitForStart();
    j.waitForCompletion(r);
    List<Map> resp = get("/organizations/jenkins/pipelines/project/runs/" + r.getId() + "/steps/", List.class);
    Map firstStep = resp.get(0);
    Assert.assertEquals("SUCCESS", firstStep.get("result"));
    Assert.assertEquals("FINISHED", firstStep.get("state"));
    Map secondStep = resp.get(1);
    Assert.assertEquals("FAILURE", secondStep.get("result"));
    Assert.assertEquals("FINISHED", secondStep.get("state"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Run(hudson.model.Run) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) URL(java.net.URL) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 47 with WorkflowJob

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

Example 49 with WorkflowJob

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

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

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