Search in sources :

Example 6 with Message

use of org.jenkinsci.plugins.pubsub.Message in project blueocean-plugin by jenkinsci.

the class SseEventTest method pipelineWithInput.

@Test
public void pipelineWithInput() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    final OneShotEvent success = new OneShotEvent();
    String script = "node {\n" + "  stage(\"build\"){\n" + "    echo \"running\"\n" + "    input message: 'Please input branch to test against', parameters: [[$class: 'StringParameterDefinition', defaultValue: 'master', description: '', name: 'branch']]\n" + "  }\n" + "}";
    final boolean[] wasPaused = { false };
    final boolean[] wasUnPaused = { false };
    final AssertionHelper assertionHelper = new AssertionHelper();
    SSEConnection con = new SSEConnection(j.getURL(), "me", new ChannelSubscriber() {

        @Override
        public void onMessage(@Nonnull Message message) {
            System.out.println(message);
            if ("job".equals(message.get(jenkins_channel))) {
                assertionHelper.isEquals("/blue/rest/organizations/jenkins/pipelines/pipeline1/", message.get(blueocean_job_rest_url));
                assertionHelper.isEquals("pipeline1", message.get(blueocean_job_pipeline_name));
                if (message.get(jenkins_event).equals(Events.JobChannel.job_run_queue_left.name())) {
                    assertionHelper.isEquals("1", message.get(blueocean_queue_item_expected_build_number));
                    assertionHelper.isNotNull(message.get(Job.job_run_queueId));
                    assertionHelper.isNotNull(message.get(Job.job_run_status));
                }
                assertionHelper.isEquals("pipeline1", message.get(job_name));
                assertionHelper.isEquals("job", message.get(jenkins_channel));
                assertionHelper.isEquals("jenkins", message.get(jenkins_org));
                assertionHelper.isNull(message.get(job_ismultibranch));
                assertionHelper.isNull(message.get(job_multibranch_indexing_result));
                assertionHelper.isNull(message.get(job_multibranch_indexing_status));
                if ("job_run_unpaused".equals(message.get(jenkins_event))) {
                    wasUnPaused[0] = true;
                }
            } else if ("pipeline".equals(message.get(jenkins_channel))) {
                assertionHelper.isEquals("1", message.get(pipeline_run_id));
                if (message.get(jenkins_event).equals(pipeline_stage.name())) {
                    assertionHelper.isEquals("build", message.get(pipeline_step_stage_name));
                }
                if ("input".equals(message.get(pipeline_step_name))) {
                    wasPaused[0] = true;
                    assertionHelper.isEquals("true", message.get(pipeline_step_is_paused));
                }
            }
            if (wasPaused[0] && wasUnPaused[0]) {
                // signal finish only when both conditions are met
                success.signal();
            }
        }
    });
    con.subscribe("pipeline");
    con.subscribe("job");
    WorkflowJob job1 = j.jenkins.createProject(WorkflowJob.class, "pipeline1");
    job1.setDefinition(new CpsFlowDefinition(script, false));
    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();
    }
    // Now that flow is paused, send a signal that it's un-paused
    ExtensionList<PipelineEventListener.InputStepPublisher> inputStepPublisherList = ExtensionList.lookup(PipelineEventListener.InputStepPublisher.class);
    assertFalse(inputStepPublisherList.isEmpty());
    InputAction inputAction = run.getAction(InputAction.class);
    List<InputStepExecution> executionList = inputAction.getExecutions();
    assertFalse(executionList.isEmpty());
    InputStep inputStep = executionList.get(0).getInput();
    inputStepPublisherList.get(0).onStepContinue(inputStep, run);
    success.block(5000);
    con.close();
    if (success.isSignaled()) {
        assertTrue(wasPaused[0]);
        assertTrue(wasUnPaused[0]);
    }
    if (assertionHelper.totalErrors() > 0) {
        fail("There were errors: " + assertionHelper.totalErrors());
    }
}
Also used : Message(org.jenkinsci.plugins.pubsub.Message) InputAction(org.jenkinsci.plugins.workflow.support.steps.input.InputAction) ChannelSubscriber(org.jenkinsci.plugins.pubsub.ChannelSubscriber) SSEConnection(io.jenkins.blueocean.events.sse.SSEConnection) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) InputStepExecution(org.jenkinsci.plugins.workflow.support.steps.input.InputStepExecution) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) InputStep(org.jenkinsci.plugins.workflow.support.steps.input.InputStep) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) OneShotEvent(hudson.util.OneShotEvent) Test(org.junit.Test)

Aggregations

Message (org.jenkinsci.plugins.pubsub.Message)6 OneShotEvent (hudson.util.OneShotEvent)5 SSEConnection (io.jenkins.blueocean.events.sse.SSEConnection)5 ChannelSubscriber (org.jenkinsci.plugins.pubsub.ChannelSubscriber)5 Test (org.junit.Test)5 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)3 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)3 BranchSource (jenkins.branch.BranchSource)2 DefaultBranchPropertyStrategy (jenkins.branch.DefaultBranchPropertyStrategy)2 GitSCMSource (jenkins.plugins.git.GitSCMSource)2 SCMSource (jenkins.scm.api.SCMSource)2 WorkflowMultiBranchProject (org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject)2 InputAction (org.jenkinsci.plugins.workflow.support.steps.input.InputAction)2 FreeStyleProject (hudson.model.FreeStyleProject)1 RunMessage (org.jenkinsci.plugins.pubsub.RunMessage)1 SimpleMessage (org.jenkinsci.plugins.pubsub.SimpleMessage)1 QueueItemAction (org.jenkinsci.plugins.workflow.actions.QueueItemAction)1 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)1 CpsFlowExecution (org.jenkinsci.plugins.workflow.cps.CpsFlowExecution)1 StepAtomNode (org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode)1