Search in sources :

Example 86 with CpsFlowDefinition

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

the class PipelineNodeTest method nodesWithFutureTest.

@Test
public void nodesWithFutureTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("node {\n" + "  stage 'build'\n" + "  sh 'echo s1'\n" + "  stage 'test'\n" + "  echo 'Hello World 2'\n" + "}", false));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, b1);
    get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    job1.setDefinition(new CpsFlowDefinition("node {\n" + "  stage 'build'\n" + "  sh 'echo s1'\n" + "  stage 'test'\n" + "  echo 'Hello World 2'\n" + "}\n" + "parallel firstBranch: {\n" + "  echo 'Hello first'\n" + "}, secondBranch: {\n" + " echo 'Hello second'\n" + "}", false));
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, b2);
    job1.setDefinition(new CpsFlowDefinition("node {\n" + "  stage 'build'\n" + "  sh 'echo s1'\n" + "  stage 'test'\n" + "  echo 'Hello World 2'\n" + "}\n" + "parallel firstBranch: {\n" + "  echo 'Hello first'\n" + "}, secondBranch: {\n" + " sh 'Hello second'\n" + "}", false));
    WorkflowRun b3 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b3);
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/nodes/", List.class);
    Assert.assertEquals(2, resp.size());
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 87 with CpsFlowDefinition

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

the class PipelineNodeTest method BlockStageStepsWithDesc.

@Test
public void BlockStageStepsWithDesc() 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 'echo testing'\n" + "    }\n" + "    stage ('Deploy') {\n" + "            sh 'echo deploy'\n" + "    }\n" + "}", false));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.SUCCESS, b1);
    List<Map> steps = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);
    Assert.assertEquals(3, steps.size());
    Assert.assertEquals("Shell Script", steps.get(0).get("displayName"));
    Assert.assertEquals("Shell Script", steps.get(1).get("displayName"));
    Assert.assertEquals("Shell Script", steps.get(2).get("displayName"));
    Assert.assertEquals("echo \"Building\"", steps.get(0).get("displayDescription"));
    Assert.assertEquals("echo testing", steps.get(1).get("displayDescription"));
    Assert.assertEquals("echo deploy", steps.get(2).get("displayDescription"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 88 with CpsFlowDefinition

use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition 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 = getClass().getResource("successfulStepWithBlockFailureAfterward.jenkinsfile");
    String jenkinsFile = IOUtils.toString(resource, StandardCharsets.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) BlueRun(io.jenkins.blueocean.rest.model.BlueRun) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) URL(java.net.URL) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 89 with CpsFlowDefinition

use of org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition 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, false));
    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(Util.fixEmpty((String) ((Map) parameters.get(1).get("defaultParameterValue")).get("value")));
    resp = post("/organizations/jenkins/pipelines/pipeline1/runs/", MapsHelper.of("parameters", Arrays.asList(MapsHelper.of("name", "param1", "value", "abc"), MapsHelper.of("name", "param2", "value", "def"))), 200);
    Assert.assertEquals("pipeline1", resp.get("pipeline"));
    Thread.sleep(5000);
    resp = get("/organizations/jenkins/pipelines/pipeline1/runs/2/");
    Assert.assertEquals("Response should be SUCCESS: " + resp.toString(), "SUCCESS", resp.get("result"));
    Assert.assertEquals("Response should be FINISHED: " + resp.toString(), "FINISHED", resp.get("state"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) RunList(hudson.util.RunList) List(java.util.List) ExtensionList(hudson.ExtensionList) JSONObject(net.sf.json.JSONObject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 90 with CpsFlowDefinition

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

the class PipelineNodeTest method testBlockStage.

@Test
public void testBlockStage() throws Exception {
    String pipeline = "" + "node {\n" + // 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, 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(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) 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