Java Code Examples for org.jenkinsci.plugins.workflow.job.WorkflowRun#addOrReplaceAction()

The following examples show how to use org.jenkinsci.plugins.workflow.job.WorkflowRun#addOrReplaceAction() . 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: ScriptedPipelineTest.java    From github-autostatus-plugin with MIT License 6 votes vote down vote up
/**
 * Verifies a labelled step isn't reported as a stage
 * @throws Exception
 */
@Test
public void testLabel() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "node {\n" +
                    "    stage('The stage') {\n" +
                    "          sh(script: \"echo 'hello'\", label: 'echo')\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).addBuildStatus(any());

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("The stage"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 2
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies a simple pipeline that succeeds sends the correct notifications
 * @throws Exception
 */
@Test
public void testSuccess() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "    agent any\n" +
                    "    stages {\n" +
                    "        stage('The stage') {\n" +
                    "            steps {\n" +
                    "                echo 'hello'\n" +
                    "                echo 'goodbye'\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("The stage"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedSuccess), any());

    verify(buildStatus, times(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 3
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies notifications are sent for nested stages
 * @throws Exception
 */
@Test
public void testNested() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "agent any\n" +
                    "stages {\n" +
                        "stage('Parallel') {\n" +
                            "parallel {\n" +
                                "stage ('Stage A') {\n" +
                                    "steps {\n" +
                                        "sh 'echo hello'\n" +
                                    "}\n" +
                                "}\n" +
                                "stage ('Stage B') {\n" +
                                    "steps {\n" +
                                        "sh 'echo hello'\n" +
                                    "}\n" +
                                "}\n" +
                            "}\n" +
                        "}\n" +
                    "}\n" +
                "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Parallel"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage A"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage B"), eq(BuildStage.State.CompletedSuccess), anyLong());

    verify(buildStatus, times(3)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 4
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies a simple pipeline that fails sends the correct notifications
 * @throws Exception
 */
@Test
public void testFail() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "    agent any\n" +
                    "    stages {\n" +
                    "        stage('The stage') {\n" +
                    "            steps {\n" +
                    "                echo 'hello'\n" +
                    "                error('fail')\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("The stage"), eq(BuildStage.State.CompletedError), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedError), any());

    verify(buildStatus, atMost(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, atMost(1)).updateBuildStatusForJob(any(), any());
}
 
Example 5
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies stages with caught errors are reported as success
 * @throws Exception
 */
@Test
public void testCaughtException() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "    agent any\n" +
                    "    stages {\n" +
                    "        stage('The stage') {\n" +
                    "            steps {\n" +
                    "                script {\n" +
                    "                    try {\n" +
                    "                        sh 'exit 1'\n" +
                    "                    } catch (Exception ignored) {}\n" +
                    "                }\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("The stage"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedSuccess), any());

    verify(buildStatus, times(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 6
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies stage status can be reported correctly when set to ABORTED in catchError
 * @throws Exception
 */
@Test
public void testCaughtExceptionSetStageAbort() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "    agent any\n" +
                    "    stages {\n" +
                    "        stage('Error stage') {\n" +
                    "            steps {\n" +
                    "                catchError(buildResult: 'SUCCESS', stageResult: 'ABORTED') {\n" +
                    "                    error 'fail on purpose'\n" +
                    "                }\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Error stage"), eq(BuildStage.State.Aborted), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedSuccess), any());

    verify(buildStatus, times(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 7
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies an aborted stage is reported correctly
 * @throws Exception
 */
@Test
public void testAbort() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "    agent any\n" +
                    "    stages {\n" +
                    "        stage('Error stage') {\n" +
                    "            steps {\n" +
                    "                sleep time: 5, unit: 'MINUTES'\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);

    WorkflowRun runningBuild = null;
    while (runningBuild == null) {
        runningBuild = p.getBuildByNumber(1);
        Thread.sleep(10);
    }
    Thread.sleep(5000);
    runningBuild.getExecution().interrupt(Result.ABORTED, new CauseOfInterruption.UserInterruption("xxxx"));

    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Error stage"), eq(BuildStage.State.Aborted), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.Aborted), any());

    verify(buildStatus, times(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 8
Source File: ScriptedPipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies a simple scripted pipeline that succeeds sends the correct notifications
 * @throws Exception
 */
@Test
public void testScriptedSuccess() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "node {\n" +
                    "    stage('Stage 1') {\n" +
                    "        echo 'hi'\n" +
                    "    }\n" +
                    "    stage('Stage 2') {\n" +
                    "        echo 'bye'\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage 1"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage 2"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedSuccess), any());

    verify(buildStatus, times(2)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 9
Source File: ScriptedPipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies a simple scripted pipeline that fails sends the correct notifications
 * @throws Exception
 */
@Test
public void testScriptedFail() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "node {\n" +
                    "    stage('Stage 1') {\n" +
                    "        echo 'hi'\n" +
                    "    }\n" +
                    "    stage('Stage fail') {\n" +
                    "        error 'fail on purpose'\n" +
                    "    }\n" +
                    "    stage('Stage 2') {\n" +
                    "        echo 'bye'\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage 1"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage fail"), eq(BuildStage.State.CompletedError), anyLong());
    verify(buildStatus, times(0)).updateBuildStatusForStage(eq("Stage 2"), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedError), any());

    verify(buildStatus, times(2)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 10
Source File: ScriptedPipelineTest.java    From github-autostatus-plugin with MIT License 5 votes vote down vote up
/**
 * Verifies an error that occurs outside of a stage is sent as an out of stage error
 * @throws Exception
 */
@Test
public void testScriptedOutOfStageError() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "node {\n" +
                    "    stage('Stage 1') {\n" +
                    "        echo 'hi'\n" +
                    "    }\n" +
                    "    sh 'exit(1)'\n" +
                    "    stage('Stage 2') {\n" +
                    "        echo 'bye'\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Stage 1"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, atLeast(1)).sendNonStageError("script returned exit code 2");
    verify(buildStatus, times(0)).updateBuildStatusForStage(eq("Stage 2"), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedError), any());

    verify(buildStatus, times(1)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}
 
Example 11
Source File: DeclarativePipelineTest.java    From github-autostatus-plugin with MIT License 4 votes vote down vote up
/**
 * Verifies stage status can be reported correctly when set to FAILED in catchError
 * @throws Exception
 */
@Test
public void testCaughtExceptionSetStageFail() throws Exception {

    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    p.setDefinition(new CpsFlowDefinition(
            "pipeline {\n" +
                    "    agent any\n" +
                    "    stages {\n" +
                    "        stage('Before stage') {\n" +
                    "            steps {\n" +
                    "                echo 'hello'\n" +
                    "            }\n" +
                    "        }\n" +
                    "        stage('Error stage') {\n" +
                    "            steps {\n" +
                    "                echo 'before step'\n" +
                    "                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {\n" +
                    "                    error 'fail on purpose'\n" +
                    "                }\n" +
                    "                echo 'after step'\n" +
                    "            }\n" +
                    "        }\n" +
                    "        stage('After stage') {\n" +
                    "            steps {\n" +
                    "                echo 'goodbye'\n" +
                    "            }\n" +
                    "        }\n" +
                    "    }\n" +
                    "}",
            true));

    WorkflowRun b = p.scheduleBuild2(0).waitForStart();
    BuildStatusAction buildStatus = mock(BuildStatusAction.class);
    b.addOrReplaceAction(buildStatus);
    r.waitForCompletion(b);
    Thread.sleep(500);

    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Before stage"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("Error stage"), eq(BuildStage.State.CompletedError), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForStage(eq("After stage"), eq(BuildStage.State.CompletedSuccess), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(eq(BuildStage.State.CompletedSuccess), any());

    verify(buildStatus, times(3)).updateBuildStatusForStage(any(), any(), anyLong());
    verify(buildStatus, times(1)).updateBuildStatusForJob(any(), any());
}