Java Code Examples for org.jenkinsci.plugins.workflow.job.WorkflowJob#save()

The following examples show how to use org.jenkinsci.plugins.workflow.job.WorkflowJob#save() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RunMultiThreadLoadTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void load_runs_multi_threaded() throws Exception {

    URL resource = Resources.getResource( getClass(), "RunMultiThreadLoadTest.jenkinsfile");
    String jenkinsFile = Resources.toString( resource, Charsets.UTF_8);
    WorkflowJob p = j.createProject( WorkflowJob.class, "project1");
    p.setDefinition(new CpsFlowDefinition( jenkinsFile, false));
    p.save();

    for (int i = 0; i < 10; i++) {
        j.waitForCompletion(p.scheduleBuild2(0).waitForStart());
    }
    Iterable<BlueRun> blueRuns = RunSearch.findRuns( p, null, 0, 20 );
    List<BlueRun> runs = StreamSupport.stream( blueRuns.spliterator(), false )
                            .collect(Collectors.toList());
    Assert.assertFalse( runs.isEmpty() );
    Assert.assertEquals( 10, runs.size() );
}
 
Example 2
Source File: RunMultiThreadLoadTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
@Issue( "JENKINS-52101" )
public void load_runs_multi_threaded_no_runs() throws Exception {

    URL resource = Resources.getResource( getClass(), "RunMultiThreadLoadTest.jenkinsfile");
    String jenkinsFile = Resources.toString( resource, Charsets.UTF_8);
    WorkflowJob p = j.createProject( WorkflowJob.class, "project2");
    p.setDefinition(new CpsFlowDefinition( jenkinsFile, false));
    p.save();

    Iterable<BlueRun> blueRuns = RunSearch.findRuns( p, null, 0, 0 );
    List<BlueRun> runs = StreamSupport.stream( blueRuns.spliterator(), false )
        .collect(Collectors.toList());
    Assert.assertTrue( runs.isEmpty() );
    Assert.assertEquals( 0, runs.size() );
}
 
Example 3
Source File: BlueJUnitTestResultTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void createsTestResult() throws Exception {
    URL resource = Resources.getResource(getClass(), "BlueJUnitTestResultTest.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);

    WorkflowJob p = j.createProject(WorkflowJob.class, "project");
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, false));
    p.save();

    Run r = p.scheduleBuild2(0).waitForStart();
    j.waitUntilNoActivity();

    BlueRun test = BlueRunFactory.getRun(r, new Reachable() {
        @Override
        public Link getLink() {
            return new Link("test");
        }
    });

    Assert.assertEquals(3, Iterators.size(test.getTests().iterator()));

    BluePipelineNode node = mock(BluePipelineNode.class);
    when(node.getId()).thenReturn("6");
}
 
Example 4
Source File: HTMLArtifactTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void resolveArtifact() throws Exception {
    WorkflowJob p = j.createProject(WorkflowJob.class, "project");

    URL resource = Resources.getResource(getClass(), "HTMLArtifactTest.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();

    Run r = p.scheduleBuild2(0).waitForStart();
    j.waitForCompletion(r);

    BluePipeline  bluePipeline = (BluePipeline) BluePipelineFactory.resolve(p);
    BlueArtifactContainer artifacts = bluePipeline.getLatestRun().getArtifacts();

    Assert.assertEquals(1, Iterators.size(artifacts.iterator()));

    BlueArtifact artifact = Iterators.getOnlyElement(artifacts.iterator());
    Assert.assertEquals("/blue/rest/organizations/jenkins/pipelines/project/runs/1/artifacts/io.jenkins.blueocean.htmlpublisher.HTMLArtifact%253AMy%252520Cool%252520report/", artifact.getLink().getHref());
    Assert.assertEquals("My Cool report", artifact.getName());
    Assert.assertEquals("My Cool report", artifact.getPath());
    Assert.assertNotNull(artifact.getUrl());
    Assert.assertEquals(-1, artifact.getSize());
    Assert.assertFalse(artifact.isDownloadable());
}
 
Example 5
Source File: PipelineImplTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
@Issue("JENKINS-55497")
public void testPipelineRunSummaryHasChangeSet() throws Exception {
    String jenkinsFile = Resources.toString(Resources.getResource(getClass(), "singleScm.jenkinsfile"), Charsets.UTF_8).replaceAll("%REPO%", sampleRepo.toString());

    WorkflowJob p = j.createProject(WorkflowJob.class, "project");
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();

    sampleRepo.init();

    j.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));
    
    // create a commit to populate the changeSet for the second run
    sampleRepo.write("file1", "");
    sampleRepo.git("add", "file1");
    sampleRepo.git("commit", "--message=init");

    Run r = j.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));

    // confirm the changeSet retrieved from the latestRun details for the project is not empty
    Map<String, Object> runDetails = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/" + r.getId() + "/");
    List<Object> changeSet = (ArrayList) runDetails.get("changeSet");
    assertTrue(!changeSet.isEmpty());
}
 
Example 6
Source File: PipelineNodeTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
@Issue("JENKINS-44742")
public void successfulStepWithBlockFailureAfterward() throws Exception {
    WorkflowJob p = j.createProject(WorkflowJob.class, "project");

    URL resource = Resources.getResource(getClass(), "successfulStepWithBlockFailureAfterward.jenkinsfile");
    String jenkinsFile = Resources.toString(resource, Charsets.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"));
}
 
Example 7
Source File: PipelineBaseTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
protected WorkflowJob createWorkflowJobWithJenkinsfile(Class<?> contextClass, String jenkinsFileName) throws java.io.IOException {
    WorkflowJob p = j.createProject(WorkflowJob.class, "project-" + UUID.randomUUID().toString());
    URL resource = Resources.getResource(contextClass, jenkinsFileName);
    String jenkinsFile = Resources.toString(resource, Charsets.UTF_8);
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();
    return p;
}
 
Example 8
Source File: PipelineRunImplTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("JENKINS-53019")
public void testMultipleRepoChangeSet() throws Exception {
    String jenkinsFile = Resources.toString(Resources.getResource(getClass(), "mulitpleScms.jenkinsfile"), Charsets.UTF_8).replaceAll("%REPO1%", sampleRepo1.toString()).replaceAll("%REPO2%", sampleRepo2.toString());

    WorkflowJob p = j.createProject(WorkflowJob.class, "project");
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();

    sampleRepo1.init();
    sampleRepo2.init();

    j.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));

    updateREADME(sampleRepo1);
    updateREADME(sampleRepo2);
    TimeUnit.SECONDS.sleep(1);
    updateREADME(sampleRepo2);
    TimeUnit.SECONDS.sleep(1);

    Run r = j.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));

    Map<String, Object> runDetails = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/" + r.getId() + "/");
    HashSet<String> commitIds = new HashSet<>();
    for (Object o : (ArrayList) runDetails.get("changeSet")) {
        commitIds.add(((Map) o).get("checkoutCount").toString());
    }
    assertEquals(3, ((ArrayList) runDetails.get("changeSet")).size());
    assertEquals(new HashSet<>(Arrays.asList("0", "1")), commitIds);
}
 
Example 9
Source File: PipelineRunImplTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("JENKINS-53019")
public void changelogFromReplayDeleted() throws Exception {
    String jenkinsFile = Resources.toString(Resources.getResource(getClass(), "mulitpleScms.jenkinsfile"), Charsets.UTF_8).replaceAll("%REPO1%", sampleRepo1.toString()).replaceAll("%REPO2%", sampleRepo2.toString());

    WorkflowJob p = j.createProject(WorkflowJob.class, "project");
    p.setDefinition(new CpsFlowDefinition(jenkinsFile, true));
    p.save();

    sampleRepo1.init();
    sampleRepo2.init();

    j.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));

    updateREADME(sampleRepo1);
    updateREADME(sampleRepo2);
    TimeUnit.SECONDS.sleep( 1);
    updateREADME(sampleRepo2);
    TimeUnit.SECONDS.sleep(1);

    Run run2 = j.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0));

    ReplayAction replayAction = run2.getAction(ReplayAction.class);
    Run run3 = j.assertBuildStatus(Result.SUCCESS, replayAction.run(replayAction.getOriginalScript(), replayAction.getOriginalLoadedScripts()));
    run2.delete();


    Map<String, Object> runDetails = get("/organizations/jenkins/pipelines/" + p.getName() + "/runs/" + run3.getId() + "/");
    assertEquals(0, ((ArrayList) runDetails.get("changeSet")).size());
}
 
Example 10
Source File: StageDurationTrendApiTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@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"));
}
 
Example 11
Source File: WorkflowFailTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@WithTimeout(value = 0)
    @Test
    public void testWFHSError() throws Exception {
        final WorkflowJob workflow = j.getInstance().createProject(WorkflowJob.class, "workflow");
        workflow.save();
        LOG.info(workflow.getAbsoluteUrl());
        j.configRoundtrip(workflow); // fails
//        j.pause();
    }
 
Example 12
Source File: WorkflowITest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void workflowTest() throws Exception {
    final WorkflowJob workflowJob = jRule.jenkins.createProject(WorkflowJob.class, "it-job");

    workflowJob.addProperty(getPreconfiguredProperty(ghRule.getGhRepo()));
    workflowJob.addTrigger(getPreconfiguredPRTrigger());
    workflowJob.setQuietPeriod(10);

    workflowJob.setDefinition(
            new CpsFlowDefinition(classpath(this.getClass(), "workflowTest.groovy"), false)
    );
    workflowJob.save();

    basicTest(workflowJob);
}
 
Example 13
Source File: WorkflowITest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
public void testContextStatuses() throws Exception {
    final WorkflowJob workflowJob = jRule.jenkins.createProject(WorkflowJob.class, "testContextStatuses");

    workflowJob.addProperty(getPreconfiguredProperty(ghRule.getGhRepo()));
    workflowJob.addTrigger(getPreconfiguredPRTrigger());

    workflowJob.setDefinition(
            new CpsFlowDefinition(classpath(this.getClass(), "testContextStatuses.groovy"), false)
    );
    workflowJob.save();

    basicTest(workflowJob);

    GHPullRequest pullRequest = ghRule.getGhRepo().getPullRequest(1);
    assertThat(pullRequest, notNullValue());

    WorkflowRun lastBuild = workflowJob.getLastBuild();
    assertThat(lastBuild, notNullValue());

    GitHubPRCause cause = lastBuild.getCause(GitHubPRCause.class);
    assertThat(cause, notNullValue());

    List<GHCommitStatus> statuses = pullRequest.getRepository()
            .listCommitStatuses(cause.getHeadSha())
            .asList();
    assertThat(statuses, hasSize(7));

    // final statuses
    assertThat("workflow Run.getResult() strange",
            statuses,
            hasItem(commitStatus("testContextStatuses", GHCommitState.ERROR, "Run #2 ended normally"))
    );
    assertThat(statuses, hasItem(commitStatus("custom-context1", GHCommitState.SUCCESS, "Tests passed")));
    assertThat(statuses, hasItem(commitStatus("custom-context2", GHCommitState.SUCCESS, "Tests passed")));
}
 
Example 14
Source File: AbortRunningJobRunnerCauseTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Test
@RandomlyFails(value = "No idea why workflow doesn't work normally")
public void testAbortRunningWorkflow() throws Exception {

    MockFolder folder = j.createFolder("workflow_folder");

    WorkflowJob job1 = folder.createProject(WorkflowJob.class, "job1");
    job1.setDisplayName("WorkflowJob 1 display name");
    job1.setConcurrentBuild(true);
    job1.setDefinition(new SleepFlow());
    configRoundTripUnsecure(job1);
    job1.save();

    WorkflowJob job2 = folder.createProject(WorkflowJob.class, "job2");
    job2.setDisplayName("WorkflowJob 2 display name");
    job2.setConcurrentBuild(true);
    job2.setDefinition(new SleepFlow());
    configRoundTripUnsecure(job2);
    job2.save();

    WorkflowJob job3 = folder.createProject(WorkflowJob.class, "job3");
    job3.setDisplayName("WorkflowJob 3 display name");
    job3.setConcurrentBuild(true);
    job3.setDefinition(new SleepFlow());
    configRoundTripUnsecure(job3);
    job3.save();

    Thread.sleep(30 * 1000); // allow get jobs to executors
    testAbortRunning(job1, job2, job3);
}