Search in sources :

Example 61 with WorkflowRun

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

the class AbstractRunImplTest method pipelineLatestRunIncludesRunning.

@Test
public void pipelineLatestRunIncludesRunning() throws Exception {
    WorkflowJob p = createWorkflowJobWithJenkinsfile(getClass(), "latestRunIncludesQueued.jenkinsfile");
    // Ensure null before first run
    Map pipeline = request().get(String.format("/organizations/jenkins/pipelines/%s/", p.getName())).build(Map.class);
    Assert.assertNull(pipeline.get("latestRun"));
    // Run until completed
    Run r = p.scheduleBuild2(0).waitForStart();
    j.waitForCompletion(r);
    // Make the next runs queue
    j.jenkins.setNumExecutors(0);
    // Schedule another run so it goes in the queue
    WorkflowRun r2 = p.scheduleBuild2(0).waitForStart();
    j.waitForMessage("Still waiting to schedule task", r2);
    // Get latest run for this pipeline
    pipeline = request().get(String.format("/organizations/jenkins/pipelines/%s/", p.getName())).build(Map.class);
    Map latestRun = (Map) pipeline.get("latestRun");
    Assert.assertEquals("QUEUED", latestRun.get("state"));
    Assert.assertEquals("2", latestRun.get("id"));
    Assert.assertEquals("Waiting for next available executor", latestRun.get("causeOfBlockage"));
    String idOfSecondRun = (String) latestRun.get("id");
    // Replay this - with limited retry
    String replayURL = String.format("/organizations/jenkins/pipelines/%s/runs/%s/replay/", p.getName(), idOfSecondRun);
    try {
        Thread.sleep(200);
        request().post(replayURL).build(String.class);
    } catch (Exception e) {
        Thread.sleep(200);
        request().post(replayURL).build(String.class);
    }
    // Sleep to make sure the build actually gets launched.
    Thread.sleep(1000);
    WorkflowRun r3 = p.getLastBuild();
    j.waitForMessage("Still waiting to schedule task", r3);
    // Get latest run for this pipeline
    pipeline = request().get(String.format("/organizations/jenkins/pipelines/%s/", p.getName())).build(Map.class);
    latestRun = (Map) pipeline.get("latestRun");
    // It should be running
    Assert.assertEquals("QUEUED", latestRun.get("state"));
    Assert.assertEquals("3", latestRun.get("id"));
    Assert.assertEquals("Waiting for next available executor", latestRun.get("causeOfBlockage"));
}
Also used : Run(hudson.model.Run) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 62 with WorkflowRun

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

the class MultiBranchTest method getMultiBranchPipelineRuns.

@Test
public void getMultiBranchPipelineRuns() throws Exception {
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(3, mp.getItems().size());
    // execute feature/ux-1 branch build
    p = scheduleAndFindBranchProject(mp, "feature%2Fux-1");
    j.waitUntilNoActivity();
    WorkflowRun b2 = p.getLastBuild();
    assertEquals(1, b2.getNumber());
    // execute feature 2 branch build
    p = scheduleAndFindBranchProject(mp, "feature2");
    j.waitUntilNoActivity();
    WorkflowRun b3 = p.getLastBuild();
    assertEquals(1, b3.getNumber());
    List<Map> br = get("/organizations/jenkins/pipelines/p/branches", List.class);
    List<String> branchNames = new ArrayList<>();
    List<Integer> weather = new ArrayList<>();
    for (Map b : br) {
        branchNames.add((String) b.get("name"));
        weather.add((int) b.get("weatherScore"));
    }
    Assert.assertTrue(branchNames.contains(((Map) (br.get(0).get("latestRun"))).get("pipeline")));
    for (String n : branches) {
        assertTrue(branchNames.contains(n));
    }
    WorkflowRun[] runs = { b1, b2, b3 };
    int i = 0;
    for (String n : branches) {
        WorkflowRun b = runs[i];
        j.waitForCompletion(b);
        Map run = get("/organizations/jenkins/pipelines/p/branches/" + Util.rawEncode(n) + "/runs/" + b.getId());
        validateRun(b, run);
        i++;
    }
    Map pr = get("/organizations/jenkins/pipelines/p/");
    validateMultiBranchPipeline(mp, pr, 3, 3, 0);
    sampleRepo.git("checkout", "master");
    sampleRepo.write("file", "subsequent content11");
    sampleRepo.git("commit", "--all", "--message=tweaked11");
    p = scheduleAndFindBranchProject(mp, "master");
    j.waitUntilNoActivity();
    WorkflowRun b4 = p.getLastBuild();
    assertEquals(2, b4.getNumber());
    List<Map> run = get("/organizations/jenkins/pipelines/p/branches/master/runs/", List.class);
    validateRun(b4, run.get(0));
}
Also used : ArrayList(java.util.ArrayList) GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 63 with WorkflowRun

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

the class MultiBranchTest method getMultiBranchPipelineActivityRuns.

@Test
public void getMultiBranchPipelineActivityRuns() throws Exception {
    WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
    mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
    for (SCMSource source : mp.getSCMSources()) {
        assertEquals(mp, source.getOwner());
    }
    scheduleAndFindBranchProject(mp);
    j.waitUntilNoActivity();
    WorkflowJob p = findBranchProject(mp, "master");
    WorkflowRun b1 = p.getLastBuild();
    assertEquals(1, b1.getNumber());
    assertEquals(3, mp.getItems().size());
    // execute feature/ux-1 branch build
    p = findBranchProject(mp, "feature%2Fux-1");
    WorkflowRun b2 = p.getLastBuild();
    assertEquals(1, b2.getNumber());
    // execute feature 2 branch build
    p = findBranchProject(mp, "feature2");
    WorkflowRun b3 = p.getLastBuild();
    assertEquals(1, b3.getNumber());
    List<Map> resp = get("/organizations/jenkins/pipelines/p/runs", List.class);
    Assert.assertEquals(3, resp.size());
    Date d1 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String) resp.get(0).get("startTime"));
    Date d2 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String) resp.get(1).get("startTime"));
    Date d3 = new SimpleDateFormat(DATE_FORMAT_STRING).parse((String) resp.get(2).get("startTime"));
    Assert.assertTrue(d1.compareTo(d2) >= 0);
    Assert.assertTrue(d2.compareTo(d3) >= 0);
    for (Map m : resp) {
        BuildData d;
        WorkflowRun r;
        if (m.get("pipeline").equals("master")) {
            r = b1;
            d = b1.getAction(BuildData.class);
        } else if (m.get("pipeline").equals("feature2")) {
            r = b3;
            d = b3.getAction(BuildData.class);
        } else {
            r = b2;
            d = b2.getAction(BuildData.class);
        }
        validateRun(r, m);
        String commitId = "";
        if (d != null) {
            commitId = d.getLastBuiltRevision().getSha1String();
            Assert.assertEquals(commitId, m.get("commitId"));
        }
    }
}
Also used : GitSCMSource(jenkins.plugins.git.GitSCMSource) SCMSource(jenkins.scm.api.SCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) BranchSource(jenkins.branch.BranchSource) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Date(java.util.Date) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) BuildData(hudson.plugins.git.util.BuildData) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) DefaultBranchPropertyStrategy(jenkins.branch.DefaultBranchPropertyStrategy) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 64 with WorkflowRun

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

the class PipelineApiTest method getPipelineJobAbortTest.

@Test
public void getPipelineJobAbortTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Build1'); " + "   sh('sleep 120') " + "   stage ('Test1'); " + "   echo ('Testing'); " + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).waitForStart();
    // Give the job some time to really start (allocate nodes and so on)
    Thread.sleep(5000);
    for (int i = 0; i < 10; i++) {
        b1.doStop();
        if (b1.getResult() != null) {
            break;
        }
        Thread.sleep(1000);
    }
    j.assertBuildStatus(Result.ABORTED, b1);
    Map r = get("/organizations/jenkins/pipelines/pipeline1/runs/1");
    validateRun(b1, r);
}
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 65 with WorkflowRun

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

the class PipelineApiTest method getPipelineJobRuns.

@Test
public void getPipelineJobRuns() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Build1'); " + "   echo ('Building'); " + "   stage ('Test1'); " + "   sleep 10000      " + "   echo ('Testing'); " + "}"));
    job1.setConcurrentBuild(false);
    WorkflowRun r = job1.scheduleBuild2(0).waitForStart();
    job1.scheduleBuild2(0);
    List l = request().get("/organizations/jenkins/pipelines/pipeline1/runs").build(List.class);
    Assert.assertEquals(2, l.size());
    Assert.assertEquals("io.jenkins.blueocean.service.embedded.rest.QueuedBlueRun", ((Map) l.get(0)).get("_class"));
    Assert.assertEquals("io.jenkins.blueocean.rest.impl.pipeline.PipelineRunImpl", ((Map) l.get(1)).get("_class"));
}
Also used : CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) List(java.util.List) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

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