Search in sources :

Example 96 with WorkflowJob

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

the class PipelineNodeTest method stepStatusForFailedBuild.

@Test
@Issue("JENKINS-39296")
public void stepStatusForFailedBuild() throws Exception {
    String p = "node {\n" + "   echo 'Hello World'\n" + "   try{\n" + "    echo 'Inside try'\n" + "    sh 'this should fail'" + "   }finally{\n" + "    echo 'this should pass'\n" + "   }\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(p));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatus(Result.FAILURE, b1);
    List<Map> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);
    Assert.assertEquals(resp.size(), 4);
    Map helloWorldStep = resp.get(0);
    Assert.assertEquals("Hello World", helloWorldStep.get("displayDescription"));
    Assert.assertEquals("SUCCESS", helloWorldStep.get("result"));
    Assert.assertEquals("FINISHED", helloWorldStep.get("state"));
    Map insideTryStep = resp.get(1);
    Assert.assertEquals("Inside try", insideTryStep.get("displayDescription"));
    Assert.assertEquals("SUCCESS", insideTryStep.get("result"));
    Assert.assertEquals("FINISHED", insideTryStep.get("state"));
    Map thisShouldFailStep = resp.get(2);
    Assert.assertEquals("this should fail", thisShouldFailStep.get("displayDescription"));
    Assert.assertEquals("FAILURE", thisShouldFailStep.get("result"));
    Assert.assertEquals("FINISHED", thisShouldFailStep.get("state"));
    Map thisShouldPassStep = resp.get(3);
    Assert.assertEquals("this should pass", thisShouldPassStep.get("displayDescription"));
    Assert.assertEquals("SUCCESS", thisShouldPassStep.get("result"));
    Assert.assertEquals("FINISHED", thisShouldPassStep.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) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 97 with WorkflowJob

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

the class PipelineNodeTest method submitInput.

@Test
public void submitInput() throws Exception {
    String script = "node {\n" + "    stage(\"first\"){\n" + "            def branchInput = input message: 'Please input branch to test against', parameters: [[$class: 'StringParameterDefinition', defaultValue: 'master', description: '', name: 'branch']]\n" + "            echo \"BRANCH NAME: ${branchInput}\"\n" + "    }\n" + "}";
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script));
    QueueTaskFuture<WorkflowRun> buildTask = job1.scheduleBuild2(0);
    WorkflowRun run = buildTask.getStartCondition().get();
    CpsFlowExecution e = (CpsFlowExecution) run.getExecutionPromise().get();
    while (run.getAction(InputAction.class) == null) {
        e.waitForSuspension();
    }
    List<Map> stepsResp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/", List.class);
    Assert.assertEquals("PAUSED", stepsResp.get(0).get("state"));
    Assert.assertEquals("UNKNOWN", stepsResp.get(0).get("result"));
    Assert.assertEquals("7", stepsResp.get(0).get("id"));
    Map<String, Object> input = (Map<String, Object>) stepsResp.get(0).get("input");
    Assert.assertNotNull(input);
    String id = (String) input.get("id");
    Assert.assertNotNull(id);
    List<Map<String, Object>> params = (List<Map<String, Object>>) input.get("parameters");
    post("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/7/", ImmutableMap.of("id", id, PARAMETERS_ELEMENT, ImmutableList.of(ImmutableMap.of("name", params.get(0).get("name"), "value", "master"))), 200);
    if (waitForBuildCount(job1, 1, Result.SUCCESS)) {
        Map<String, Object> resp = get("/organizations/jenkins/pipelines/pipeline1/runs/1/steps/7/");
        Assert.assertEquals("FINISHED", resp.get("state"));
        Assert.assertEquals("SUCCESS", resp.get("result"));
        Assert.assertEquals("7", resp.get("id"));
    }
}
Also used : InputAction(org.jenkinsci.plugins.workflow.support.steps.input.InputAction) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) JSONObject(net.sf.json.JSONObject) 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) Test(org.junit.Test)

Example 98 with WorkflowJob

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

the class StageDurationTrendApiTest method getDurationTrend.

@Test
public void getDurationTrend() throws Exception {
    URL resource = Resources.getResource(getClass(), "declarativeThreeStages.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    WorkflowJob p = j.createProject(WorkflowJob.class, "duration-trend");
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();
    Run r = p.scheduleBuild2(0).waitForStart();
    j.waitForCompletion(r);
    Map response = new RequestBuilder(baseUrl).get("/organizations/jenkins/pipelines/" + p.getName() + "/trends/stageDuration/").build(Map.class);
    Assert.assertNotNull(response);
    List rows = new RequestBuilder(baseUrl).get("/organizations/jenkins/pipelines/" + p.getName() + "/trends/stageDuration/rows/").build(List.class);
    Assert.assertNotNull(rows);
    Assert.assertEquals(1, rows.size());
    Map stageRow = (Map) rows.get(0);
    Map nodes = (Map) stageRow.get(NODES);
    Assert.assertNotNull(nodes.get("first"));
    Assert.assertNotNull(nodes.get("second"));
    Assert.assertNotNull(nodes.get("third"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) Run(hudson.model.Run) List(java.util.List) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 99 with WorkflowJob

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

the class JobAnalyticsTest method testJobAnalytics.

@Test
public void testJobAnalytics() throws Exception {
    // Freestyle jobs
    j.createFreeStyleProject("freestyle1").save();
    j.createFreeStyleProject("freestyle2").save();
    // Matrix job
    j.createProject(MatrixProject.class, "bob");
    // Create single scripted pipeline
    WorkflowJob scriptedSingle = createWorkflowJobWithJenkinsfile(getClass(), "JobAnalyticsTest-scripted.jenkinsfile");
    WorkflowRun scriptedSingleRun = scriptedSingle.scheduleBuild2(0, new CauseAction()).waitForStart();
    j.waitForCompletion(scriptedSingleRun);
    // Create single declarative pipeline
    WorkflowJob declarativeSingle = createWorkflowJobWithJenkinsfile(getClass(), "JobAnalyticsTest-declarative.jenkinsfile");
    WorkflowRun declarativeSingleRun = declarativeSingle.scheduleBuild2(0, new CauseAction()).waitForStart();
    j.waitForCompletion(declarativeSingleRun);
    // Create Scripted MultiBranch
    createMultiBranch(sampleRepo);
    // Create Declarative MultiBranch
    createMultiBranch(sampleRepo2);
    AnalyticsImpl analytics = (AnalyticsImpl) Analytics.get();
    Assert.assertNotNull(analytics);
    JobAnalytics jobAnalytics = new JobAnalytics();
    jobAnalytics.calculateAndSend();
    Assert.assertNotNull(analytics.lastReq);
    Assert.assertEquals("job_stats", analytics.lastReq.name);
    Map<String, Object> properties = analytics.lastReq.properties;
    Assert.assertEquals("singlePipelineDeclarative", 1, properties.get("singlePipelineDeclarative"));
    Assert.assertEquals("singlePipelineScripted", 1, properties.get("singlePipelineScripted"));
    Assert.assertEquals("pipelineDeclarative", 1, properties.get("pipelineDeclarative"));
    Assert.assertEquals("pipelineScripted", 1, properties.get("pipelineScripted"));
    Assert.assertEquals("freestyle", 2, properties.get("freestyle"));
    Assert.assertEquals("matrix", 1, properties.get("matrix"));
    Assert.assertEquals("other", 0, properties.get("other"));
}
Also used : JobAnalytics(io.jenkins.blueocean.service.embedded.analytics.JobAnalytics) CauseAction(hudson.model.CauseAction) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) PipelineBaseTest(io.jenkins.blueocean.rest.impl.pipeline.PipelineBaseTest) Test(org.junit.Test)

Example 100 with WorkflowJob

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

the class PipelinePluginAnalyticsTest method createAndRunPipeline.

private void createAndRunPipeline(String jenkinsFileName) throws java.io.IOException, InterruptedException, java.util.concurrent.ExecutionException {
    // Create the pipeline and run it
    WorkflowJob scriptedSingle = createWorkflowJobWithJenkinsfile(getClass(), jenkinsFileName);
    WorkflowRun scriptedSingleRun = scriptedSingle.scheduleBuild2(0, new CauseAction()).waitForStart();
    j.waitForCompletion(scriptedSingleRun);
    // RunListeners can sometimes be executed after the run is reported as completed. This fixes a race condition.
    AnalyticsImpl analytics = (AnalyticsImpl) Analytics.get();
    Assert.assertNotNull(analytics);
    while (analytics.lastReq == null) {
        Thread.sleep(100);
    }
}
Also used : CauseAction(hudson.model.CauseAction) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun)

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