Search in sources :

Example 16 with WorkflowRun

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

the class AbstractRunImplTest method queuedSingleNode.

@Issue("JENKINS-44981")
@Test
public void queuedSingleNode() throws Exception {
    WorkflowJob p = createWorkflowJobWithJenkinsfile(getClass(), "queuedSingleNode.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
    WorkflowRun r = p.scheduleBuild2(0).waitForStart();
    j.waitForMessage("Still waiting to schedule task", r);
    // 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("1", latestRun.get("id"));
    Assert.assertEquals("Jenkins doesn’t have label test", latestRun.get("causeOfBlockage"));
    j.createOnlineSlave(Label.get("test"));
    j.assertBuildStatusSuccess(j.waitForCompletion(r));
}
Also used : WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 17 with WorkflowRun

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

the class AbstractRunImplTest method declarativeQueuedAgent.

@Issue("JENKINS-44981")
@Test
public void declarativeQueuedAgent() throws Exception {
    WorkflowJob p = createWorkflowJobWithJenkinsfile(getClass(), "declarativeQueuedAgent.jenkinsfile");
    j.jenkins.setNumExecutors(0);
    // 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
    WorkflowRun r = p.scheduleBuild2(0).waitForStart();
    j.waitForMessage("Still waiting to schedule task", r);
    // Get latest run for this pipeline
    String url = String.format("/organizations/jenkins/pipelines/%s/runs/%s/", p.getName(), r.getId());
    Map latestRun = request().get(url).build(Map.class);
    Assert.assertEquals("QUEUED", latestRun.get("state"));
    Assert.assertEquals("1", latestRun.get("id"));
    Assert.assertEquals("Waiting for next available executor", latestRun.get("causeOfBlockage"));
    j.jenkins.setNumExecutors(2);
    j.assertBuildStatusSuccess(j.waitForCompletion(r));
    // Disable the executors.
    j.jenkins.setNumExecutors(0);
    // Run until we hang.
    WorkflowRun r2 = p.scheduleBuild2(0).waitForStart();
    j.waitForMessage("Still waiting to schedule task", r2);
    // Get latest run for this pipeline
    url = String.format("/organizations/jenkins/pipelines/%s/runs/%s/", p.getName(), r2.getId());
    latestRun = request().get(url).build(Map.class);
    Assert.assertEquals("2", latestRun.get("id"));
    Assert.assertEquals("QUEUED", latestRun.get("state"));
    Assert.assertEquals("Waiting for next available executor", latestRun.get("causeOfBlockage"));
    j.jenkins.setNumExecutors(2);
    j.assertBuildStatusSuccess(j.waitForCompletion(r2));
}
Also used : WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Map(java.util.Map) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 18 with WorkflowRun

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

the class AbstractRunImplTest method replayRunTest.

// Disabled, see JENKINS-36453
@Test
@Ignore
public void replayRunTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    j.createOnlineSlave(Label.get("remote"));
    job1.setDefinition(new CpsFlowDefinition("node('remote') {\n" + "    ws {\n" + "        git($/" + sampleRepo + "/$)\n" + "    }\n" + "}"));
    WorkflowRun b1 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b1);
    sampleRepo.write("file1", "");
    sampleRepo.git("add", "file1");
    sampleRepo.git("commit", "--message=init");
    WorkflowRun b2 = job1.scheduleBuild2(0).get();
    j.assertBuildStatusSuccess(b2);
    Assert.assertNotEquals(new PipelineRunImpl(b1, null, null).getCommitId(), new PipelineRunImpl(b2, null, null).getCommitId());
    request().post("/organizations/jenkins/pipelines/pipeline1/runs/1/replay").build(String.class);
    j.waitForCompletion(job1.getLastBuild());
    Map r = request().get("/organizations/jenkins/pipelines/pipeline1/runs/3/").build(Map.class);
    assertEquals(r.get("commitId"), new PipelineRunImpl(b2, null, null).getCommitId());
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with WorkflowRun

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

the class MultiBranchTest method testMbpPagination.

@Test
public void testMbpPagination() 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());
    }
    mp.scheduleBuild2(0).getFuture().get();
    this.j.waitUntilNoActivity();
    // create 6 runs
    List<WorkflowRun> launchedItems = new ArrayList<>();
    for (String branch : branches) {
        WorkflowJob job = findBranchProject(mp, branch);
        launchedItems.addAll(job.getBuilds());
        for (int j = 0; j < 2; j++) {
            launchedItems.add(job.scheduleBuild2(0).waitForStart());
            this.j.waitUntilNoActivity();
        }
    }
    // total 9. 3 triggered from indexing and 6 we launched
    assertEquals(9, launchedItems.size());
    // sort runs
    Collections.sort(launchedItems, new Comparator<WorkflowRun>() {

        @Override
        public int compare(WorkflowRun o1, WorkflowRun o2) {
            return new Date(o2.getStartTimeInMillis()).compareTo(new Date(o1.getStartTimeInMillis()));
        }
    });
    // request 10 runs, but should not return 9 runs.
    List<Map> resp = get("/organizations/jenkins/pipelines/p/runs?start=0&limit=10", List.class);
    // max number of runs are 9
    assertEquals(9, resp.size());
    for (int i = 0; i < 9; i++) {
        Assert.assertEquals(launchedItems.get(i).getId(), (resp.get(i).get("id")));
    }
    // now call 3 out of 9 runs and see pagination returns only 3 and in right sort order
    resp = get("/organizations/jenkins/pipelines/p/runs?start=0&limit=3", List.class);
    assertEquals(3, resp.size());
    for (int i = 0; i < 3; i++) {
        Assert.assertEquals(launchedItems.get(i).getId(), (resp.get(i).get("id")));
    }
}
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) Date(java.util.Date) WorkflowMultiBranchProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) 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 20 with WorkflowRun

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

the class PipelineApiTest method getPipelineRunStopTest.

@Test
public void getPipelineRunStopTest() throws Exception {
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition("" + "node {" + "   stage ('Build'); " + "   semaphore 's' " + "   stage ('Test'); " + "   echo ('Testing'); " + "}", false));
    WorkflowRun b1 = job1.scheduleBuild2(0).waitForStart();
    SemaphoreStep.waitForStart("s/1", b1);
    Map r = null;
    for (int i = 0; i < 10; i++) {
        r = request().put("/organizations/jenkins/pipelines/pipeline1/runs/1/stop").build(Map.class);
        if (((String) r.get("state")).equals("FINISHED"))
            break;
        Thread.sleep(1000);
    }
    Assert.assertEquals(r.get("state"), "FINISHED");
    Assert.assertEquals(r.get("result"), "ABORTED");
    j.assertBuildStatus(Result.ABORTED, b1);
    FreeStyleProject p = j.createFreeStyleProject("pipeline5");
    p.getBuildersList().add(new Shell("echo hello!\nsleep 69"));
    FreeStyleBuild b2 = p.scheduleBuild2(0).waitForStart();
    for (int i = 0; i < 10; i++) {
        r = put("/organizations/jenkins/pipelines/pipeline5/runs/1/stop", null);
        if (((String) r.get("state")).equals("FINISHED"))
            break;
        Thread.sleep(1000);
    }
    Assert.assertEquals(r.get("state"), "FINISHED");
    Assert.assertEquals(r.get("result"), "ABORTED");
    j.assertBuildStatus(Result.ABORTED, b2);
}
Also used : Shell(hudson.tasks.Shell) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FreeStyleBuild(hudson.model.FreeStyleBuild) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FreeStyleProject(hudson.model.FreeStyleProject) Map(java.util.Map) 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