Search in sources :

Example 21 with WorkflowJob

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

the class PipelineNodeTest method parameterizedPipeline.

@Test
public void parameterizedPipeline() throws Exception {
    String script = "properties([parameters([string(defaultValue: 'xyz', description: 'string param', name: 'param1'), string(description: 'string param', name: 'param2')]), pipelineTriggers([])])\n" + "\n" + "node(){\n" + "    stage('build'){\n" + "        echo \"building\"\n" + "    }\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    Map resp = get("/organizations/jenkins/pipelines/pipeline1/");
    List<Map<String, Object>> parameters = (List<Map<String, Object>>) resp.get("parameters");
    Assert.assertEquals(2, parameters.size());
    Assert.assertEquals("param1", parameters.get(0).get("name"));
    Assert.assertEquals("StringParameterDefinition", parameters.get(0).get("type"));
    Assert.assertEquals("string param", parameters.get(0).get("description"));
    Assert.assertEquals("xyz", ((Map) parameters.get(0).get("defaultParameterValue")).get("value"));
    Assert.assertEquals("param2", parameters.get(1).get("name"));
    Assert.assertEquals("StringParameterDefinition", parameters.get(1).get("type"));
    Assert.assertEquals("string param", parameters.get(1).get("description"));
    Assert.assertNull(((Map) parameters.get(1).get("defaultParameterValue")).get("value"));
    resp = post("/organizations/jenkins/pipelines/pipeline1/runs/", ImmutableMap.of("parameters", ImmutableList.of(ImmutableMap.of("name", "param1", "value", "abc"), ImmutableMap.of("name", "param2", "value", "def"))), 200);
    Assert.assertEquals("pipeline1", resp.get("pipeline"));
    Thread.sleep(1000);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/2/");
    Assert.assertEquals("SUCCESS", resp.get("result"));
    Assert.assertEquals("FINISHED", resp.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) JSONObject(net.sf.json.JSONObject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 22 with WorkflowJob

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

the class PipelineNodeTest method nodesTest.

@Test
public void nodesTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("stage \"Build\"\n" + "    node {\n" + "       sh \"echo here\"\n" + "    }\n" + "\n" + "stage \"Test\"\n" + "    parallel (\n" + "        \"Firefox\" : {\n" + "            node {\n" + "                sh \"echo ffox\"\n" + "            }\n" + "        },\n" + "        \"Chrome\" : {\n" + "            node {\n" + "                sh \"echo chrome\"\n" + "            }\n" + "        }\n" + "    )\n" + "\n" + "stage \"CrashyMcgee\"\n" + "  parallel (\n" + "    \"SlowButSuccess\" : {\n" + "        node {\n" + "            echo 'This is time well spent.'\n" + "        }\n" + "    },\n" + "    \"DelayThenFail\" : {\n" + "        node {\n" + "            echo 'Not yet.'\n" + "        }\n" + "    }\n" + "  )\n" + "\n" + "\n" + "stage \"Deploy\"\n" + "    node {\n" + "        sh \"echo deploying\"\n" + "    }"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    job1.setDefinition(new CpsFlowDefinition("stage \"Build\"\n" + "    node {\n" + "       sh \"echo here\"\n" + "    }\n" + "\n" + "stage \"Test\"\n" + "    parallel (\n" + "        \"Firefox\" : {\n" + "            node {\n" + "                sh \"echo ffox\"\n" + "            }\n" + "        },\n" + "        \"Chrome\" : {\n" + "            node {\n" + "                sh \"echo chrome\"\n" + "            }\n" + "        }\n" + "    )\n" + "\n" + "stage \"CrashyMcgee\"\n" + "  parallel (\n" + "    \"SlowButSuccess\" : {\n" + "        node {\n" + "            echo 'This is time well spent.'\n" + "            sh 'sleep 3;'\n" + "        }\n" + "    },\n" + "    \"DelayThenFail\" : {\n" + "        node {\n" + "            echo 'Fail soon.'\n" + "            echo 'KABOOM!'\n" + "            sh '11exit 1'\n" + "        }\n" + "    }\n" + "  )\n" + "\n" + "\n" + "stage \"Deploy\"\n" + "    node {\n" + "        sh \"echo deploying\"\n" + "    }"));
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b2);
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/2/nodes/", List.class);
    Assert.assertEquals(resp.size(), 8);
    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("Test")) {
            Assert.assertEquals(2, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("Firefox")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("Chrome")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("CrashyMcgee")) {
            Assert.assertEquals(2, edges.size());
            Assert.assertEquals(rn.get("result"), "FAILURE");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("SlowButSuccess")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "SUCCESS");
            Assert.assertEquals(rn.get("state"), "FINISHED");
        } else if (rn.get("displayName").equals("DelayThenFail")) {
            Assert.assertEquals(1, edges.size());
            Assert.assertEquals(rn.get("result"), "FAILURE");
            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("Deploy")) {
            Assert.assertEquals(0, edges.size());
            Assert.assertNull(rn.get("result"));
            Assert.assertNull(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) Test(org.junit.Test)

Example 23 with WorkflowJob

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

the class PipelineNodeTest method BlockStageNodesFailureTest2.

@Test
public void BlockStageNodesFailureTest2() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("node{\n" + "    stage ('Build') {\n" + "            sh 'echo \"Building\"'\n" + "    }\n" + "    stage ('Test') {\n" + "            sh 'echo1 testing'\n" + "    }\n" + "    stage ('Deploy') {\n" + "            sh 'echo deploy'\n" + "    }\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b1);
    List<Map> nodes = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(2, nodes.size());
    Assert.assertEquals("SUCCESS", nodes.get(0).get("result"));
    Assert.assertEquals("FINISHED", nodes.get(0).get("state"));
    Assert.assertEquals("FAILURE", nodes.get(1).get("result"));
    Assert.assertEquals("FINISHED", nodes.get(1).get("state"));
}
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) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 24 with WorkflowJob

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

the class PipelineNodeTest method orphanParallels2.

@Test
public void orphanParallels2() throws Exception {
    String script = "stage(\"stage1\"){\n" + "    echo \"stage 1...\"\n" + "}\n" + "parallel('branch1':{\n" + "        node {\n" + "            stage('Setup') {\n" + "                sh 'echo \"Setup...\"'\n" + "            }\n" + "            stage('Unit and Integration Tests') {\n" + "                sh 'echo \"Unit and Integration Tests...\"'\n" + "            }\n" + "        }\n" + "}, 'branch3': {\n" + "        node {\n" + "            stage('Setup') {\n" + "                sh 'echo \"Branch3 setup...\"'\n" + "            }\n" + "            stage('Unit and Integration Tests') {\n" + "                echo '\"my command to execute tests\"'\n" + "            }\n" + "        }\n" + "}, 'branch2': {\n" + "        node {\n" + "            stage('Setup') {\n" + "                sh 'echo \"Branch2 setup...\"'\n" + "            }\n" + "            stage('Unit and Integration Tests') {\n" + "                echo '\"my command to execute tests\"'\n" + "            }\n" + "        }\n" + "})\n" + "stage(\"stage2\"){\n" + "    echo \"stage 2...\"\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, b1);
    List<Map> nodes = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(6, nodes.size());
    for (int i = 0; i < nodes.size(); i++) {
        Map n = nodes.get(i);
        List<Map> edges = (List<Map>) n.get("edges");
        if (i == 0) {
            assertEquals("stage1", n.get("displayName"));
            assertEquals(1, edges.size());
            assertEquals(nodes.get(i + 1).get("id"), edges.get(0).get("id"));
        }
        if (i == 1) {
            assertEquals("Parallel", n.get("displayName"));
            assertEquals(nodes.get(i + 1).get("id") + "-parallel-synthetic", n.get("id"));
            Assert.assertEquals(3, edges.size());
            assertEquals(nodes.get(i + 1).get("id"), edges.get(0).get("id"));
            assertEquals(nodes.get(i + 2).get("id"), edges.get(1).get("id"));
            assertEquals(nodes.get(i + 3).get("id"), edges.get(2).get("id"));
        }
        if (i == 2) {
            assertEquals("branch1", n.get("displayName"));
            assertEquals(1, edges.size());
            assertEquals(nodes.get(5).get("id"), edges.get(0).get("id"));
        }
        if (i == 3) {
            assertEquals("branch2", n.get("displayName"));
            assertEquals(1, edges.size());
            assertEquals(nodes.get(5).get("id"), edges.get(0).get("id"));
        }
        if (i == 4) {
            assertEquals("branch3", n.get("displayName"));
            assertEquals(1, edges.size());
            assertEquals(nodes.get(5).get("id"), edges.get(0).get("id"));
        }
        if (i == 5) {
            assertEquals("stage2", n.get("displayName"));
            assertEquals(0, edges.size());
        }
    }
    Map synNode = nodes.get(1);
    List<Map> edges = (List<Map>) synNode.get("edges");
    Map n = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/" + synNode.get("id") + "/", Map.class);
    List<Map> receivedEdges = (List<Map>) n.get("edges");
    assertNotNull(n);
    assertEquals(synNode.get("displayName"), n.get("displayName"));
    assertEquals(synNode.get("id"), n.get("id"));
    Assert.assertEquals(3, edges.size());
    assertEquals(edges.get(0).get("id"), receivedEdges.get(0).get("id"));
    assertEquals(edges.get(1).get("id"), receivedEdges.get(1).get("id"));
    assertEquals(edges.get(2).get("id"), receivedEdges.get(2).get("id"));
}
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) Test(org.junit.Test)

Example 25 with WorkflowJob

use of org.jenkinsci.plugins.workflow.job.WorkflowJob 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));
    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) 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)68 Test (org.junit.Test)59 Map (java.util.Map)56 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)51 ImmutableMap (com.google.common.collect.ImmutableMap)46 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)46 List (java.util.List)26 ImmutableList (com.google.common.collect.ImmutableList)23 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)19 BranchSource (jenkins.branch.BranchSource)17 GitSCMSource (jenkins.plugins.git.GitSCMSource)17 SCMSource (jenkins.scm.api.SCMSource)17 RunList (hudson.util.RunList)16 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)14 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)13 ArrayList (java.util.ArrayList)8 FlowGraphTable (org.jenkinsci.plugins.workflow.support.visualization.table.FlowGraphTable)4 JSONObject (net.sf.json.JSONObject)3 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)3 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)3