Search in sources :

Example 16 with MsgCheck

use of com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck in project jms-messaging-plugin by jenkinsci.

the class SharedMessagingPluginIntegrationTest method _testDisabledJobDoesNotGetTriggeredWithCheck.

public void _testDisabledJobDoesNotGetTriggeredWithCheck() throws Exception {
    FreeStyleProject jobA = j.createFreeStyleProject();
    attachTrigger(new CIBuildTrigger(false, Collections.singletonList(getSubscriberProviderData(null, null, null, new MsgCheck(MESSAGE_CHECK_FIELD, MESSAGE_CHECK_VALUE)))), jobA);
    jobA.getBuildersList().add(new Shell("echo job ran"));
    jobA.disable();
    FreeStyleProject jobB = j.createFreeStyleProject();
    jobB.getPublishersList().add(new CIMessageNotifier(getPublisherProviderData(null, null, null, MESSAGE_CHECK_CONTENT)));
    j.buildAndAssertSuccess(jobB);
    Thread.sleep(5000);
    assertThat(jobA.getBuilds(), Matchers.iterableWithSize(0));
    jobA.enable();
    Thread.sleep(3000);
    j.buildAndAssertSuccess(jobB);
    waitUntilScheduledBuildCompletes();
    j.assertBuildStatusSuccess(jobA.getLastBuild());
    j.assertLogContains("echo job ran", jobA.getLastBuild());
}
Also used : Shell(hudson.tasks.Shell) CIMessageNotifier(com.redhat.jenkins.plugins.ci.CIMessageNotifier) CIBuildTrigger(com.redhat.jenkins.plugins.ci.CIBuildTrigger) FreeStyleProject(hudson.model.FreeStyleProject) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)

Example 17 with MsgCheck

use of com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck in project jms-messaging-plugin by jenkinsci.

the class SharedMessagingPluginIntegrationTest method _testSimpleCIEventTriggerWithRegExpCheck.

public void _testSimpleCIEventTriggerWithRegExpCheck() throws Exception {
    FreeStyleProject jobA = j.createFreeStyleProject();
    attachTrigger(new CIBuildTrigger(false, Collections.singletonList(getSubscriberProviderData(null, null, null, new MsgCheck("$.compose.compose_id", "Fedora-Atomic.+")))), jobA);
    jobA.getBuildersList().add(new Shell("echo job ran"));
    jobA.getBuildersList().add(new Shell("echo CI_MESSAGE = $CI_MESSAGE"));
    FreeStyleProject jobB = j.createFreeStyleProject();
    jobB.getPublishersList().add(new CIMessageNotifier(getPublisherProviderData(null, null, null, "{ \"compose\": { \"compose_id\": \"Fedora-Atomic-25-20170105.0\" } }")));
    j.buildAndAssertSuccess(jobB);
    waitUntilScheduledBuildCompletes();
    j.assertBuildStatusSuccess(jobA.getLastBuild());
    j.assertLogContains("echo job ran", jobA.getLastBuild());
}
Also used : Shell(hudson.tasks.Shell) CIMessageNotifier(com.redhat.jenkins.plugins.ci.CIMessageNotifier) CIBuildTrigger(com.redhat.jenkins.plugins.ci.CIBuildTrigger) FreeStyleProject(hudson.model.FreeStyleProject) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)

Example 18 with MsgCheck

use of com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck in project jms-messaging-plugin by jenkinsci.

the class SharedMessagingPluginIntegrationTest method _testSimpleCIEventSubscribeWithCheckWithTopicOverride.

public void _testSimpleCIEventSubscribeWithCheckWithTopicOverride() throws Exception {
    FreeStyleProject jobA = j.createFreeStyleProject();
    jobA.getBuildersList().add(new CIMessageSubscriberBuilder(getSubscriberProviderData("otopic", "MESSAGE_CONTENT", "CI_TYPE = 'code-quality-checks-done'", new MsgCheck(MESSAGE_CHECK_FIELD, MESSAGE_CHECK_VALUE))));
    jobA.getBuildersList().add(new Shell("echo $MESSAGE_CONTENT"));
    scheduleAwaitStep(jobA);
    FreeStyleProject jobB = j.createFreeStyleProject();
    jobB.getPublishersList().add(new CIMessageNotifier(getPublisherProviderData("otopic", MessageUtils.MESSAGE_TYPE.CodeQualityChecksDone, null, MESSAGE_CHECK_CONTENT)));
    j.buildAndAssertSuccess(jobB);
    waitUntilScheduledBuildCompletes();
    j.assertBuildStatusSuccess(jobA.getLastBuild());
    j.assertLogContains("catch me", jobA.getLastBuild());
}
Also used : Shell(hudson.tasks.Shell) CIMessageNotifier(com.redhat.jenkins.plugins.ci.CIMessageNotifier) CIMessageSubscriberBuilder(com.redhat.jenkins.plugins.ci.CIMessageSubscriberBuilder) FreeStyleProject(hudson.model.FreeStyleProject) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)

Example 19 with MsgCheck

use of com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck in project jms-messaging-plugin by jenkinsci.

the class FedMsgMessageWatcher method watch.

@Override
public String watch() {
    fedMsgMessagingProvider = (FedMsgMessagingProvider) provider;
    log.info("Waiting for message with selector: " + selector);
    for (MsgCheck msgCheck : checks) {
        log.info(" with check: " + msgCheck.toString());
    }
    taskListener.getLogger().println("Waiting for message with selector: " + selector);
    for (MsgCheck msgCheck : checks) {
        taskListener.getLogger().println(" with check: " + msgCheck.toString());
    }
    log.info(" with timeout: " + timeout);
    taskListener.getLogger().println(" with timeout: " + timeout);
    lcontext = ZMQ.context(1);
    lpoller = lcontext.poller(1);
    lsocket = lcontext.socket(ZMQ.SUB);
    topic = PluginUtils.getSubstitutedValue(getTopic(overrides, fedMsgMessagingProvider.getTopic(), DEFAULT_TOPIC), environment);
    lsocket.subscribe(topic.getBytes(StandardCharsets.UTF_8));
    lsocket.setLinger(0);
    lsocket.connect(fedMsgMessagingProvider.getHubAddr());
    lpoller.register(lsocket, ZMQ.Poller.POLLIN);
    ObjectMapper mapper = new ObjectMapper();
    long startTime = new Date().getTime();
    int timeoutInMs = timeout * 60 * 1000;
    interrupted = false;
    try {
        while ((new Date().getTime() - startTime) < timeoutInMs) {
            if (lpoller.poll(1000) > 0) {
                if (lpoller.pollin(0)) {
                    ZMsg z = ZMsg.recvMsg(lpoller.getSocket(0));
                    String json = z.getLast().toString();
                    FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
                    if (!provider.verify(data.getBodyJson(), checks, jobname)) {
                        continue;
                    }
                    return data.getBodyJson();
                }
            }
        }
        if (interrupted) {
            return null;
        }
        log.severe("Timed out waiting for message!");
        taskListener.getLogger().println("Timed out waiting for message!");
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
    } finally {
        try {
            if (lpoller != null) {
                ZMQ.Socket s = lpoller.getSocket(0);
                lpoller.unregister(s);
                s.disconnect(fedMsgMessagingProvider.getHubAddr());
                lsocket.unsubscribe(this.topic.getBytes(StandardCharsets.UTF_8));
                lsocket.close();
            }
            if (lcontext != null) {
                lcontext.term();
            }
        } catch (Exception e) {
            log.fine(e.getMessage());
        }
        lpoller = null;
        lsocket = null;
    }
    return null;
}
Also used : FedmsgMessage(com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck) ZMsg(org.zeromq.ZMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date) ZMQ(org.zeromq.ZMQ)

Example 20 with MsgCheck

use of com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck in project jms-messaging-plugin by jenkinsci.

the class FedMsgMessagingWorker method waitForMessage.

@Override
public String waitForMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    FedMsgSubscriberProviderData pd = (FedMsgSubscriberProviderData) pdata;
    log.info("Waiting for message.");
    listener.getLogger().println("Waiting for message.");
    for (MsgCheck msgCheck : pd.getChecks()) {
        log.info(" with check: " + msgCheck.toString());
        listener.getLogger().println(" with check: " + msgCheck);
    }
    Integer timeout = (pd.getTimeout() != null ? pd.getTimeout() : FedMsgSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES);
    log.info(" with timeout: " + timeout);
    listener.getLogger().println(" with timeout: " + timeout);
    ZMQ.Context lcontext = ZMQ.context(1);
    ZMQ.Poller lpoller = lcontext.poller(1);
    ZMQ.Socket lsocket = lcontext.socket(ZMQ.SUB);
    String ltopic = getTopic(provider);
    try {
        ltopic = PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener));
    } catch (IOException | InterruptedException e) {
        log.warning(e.getMessage());
    }
    lsocket.subscribe(ltopic.getBytes(StandardCharsets.UTF_8));
    lsocket.setLinger(0);
    lsocket.connect(provider.getHubAddr());
    lpoller.register(lsocket, ZMQ.Poller.POLLIN);
    ObjectMapper mapper = new ObjectMapper();
    long startTime = new Date().getTime();
    int timeoutInMs = timeout * 60 * 1000;
    try {
        while ((new Date().getTime() - startTime) < timeoutInMs) {
            if (lpoller.poll(1000) > 0) {
                if (lpoller.pollin(0)) {
                    ZMsg z = ZMsg.recvMsg(lpoller.getSocket(0));
                    listener.getLogger().println("Received a message");
                    String json = z.getLast().toString();
                    FedmsgMessage data = mapper.readValue(json, FedmsgMessage.class);
                    String body = data.getBodyJson();
                    if (!provider.verify(body, pd.getChecks(), jobname)) {
                        continue;
                    }
                    if (StringUtils.isNotEmpty(pd.getVariable())) {
                        EnvVars vars = new EnvVars();
                        vars.put(pd.getVariable(), body);
                        build.addAction(new CIEnvironmentContributingAction(vars));
                    }
                    return body;
                }
            }
        }
        log.severe("Timed out waiting for message!");
        listener.getLogger().println("Timed out waiting for message!");
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
    } finally {
        try {
            ZMQ.Socket s = lpoller.getSocket(0);
            lpoller.unregister(s);
            s.disconnect(provider.getHubAddr());
            lsocket.unsubscribe(ltopic.getBytes(StandardCharsets.UTF_8));
            lsocket.close();
            lcontext.term();
        } catch (Exception e) {
            listener.getLogger().println("exception in finally");
        }
    }
    return null;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) IOException(java.io.IOException) MsgCheck(com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck) ZMsg(org.zeromq.ZMsg) Date(java.util.Date) IOException(java.io.IOException) EnvVars(hudson.EnvVars) FedmsgMessage(com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ZMQ(org.zeromq.ZMQ) CIEnvironmentContributingAction(com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)

Aggregations

MsgCheck (com.redhat.jenkins.plugins.ci.messaging.checks.MsgCheck)20 FreeStyleProject (hudson.model.FreeStyleProject)17 CIBuildTrigger (com.redhat.jenkins.plugins.ci.CIBuildTrigger)14 Shell (hudson.tasks.Shell)14 CIMessageNotifier (com.redhat.jenkins.plugins.ci.CIMessageNotifier)12 CIMessageSubscriberBuilder (com.redhat.jenkins.plugins.ci.CIMessageSubscriberBuilder)4 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)4 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)4 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)3 StringParameterDefinition (hudson.model.StringParameterDefinition)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 CIEnvironmentContributingAction (com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)2 CIMessageBuilder (com.redhat.jenkins.plugins.ci.CIMessageBuilder)2 FedmsgRelayContainer (com.redhat.jenkins.plugins.ci.integration.fixtures.FedmsgRelayContainer)2 MessagingProviderOverrides (com.redhat.jenkins.plugins.ci.messaging.MessagingProviderOverrides)2 FedmsgMessage (com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage)2 FedMsgSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData)2 ProviderData (com.redhat.jenkins.plugins.ci.provider.data.ProviderData)2 EnvVars (hudson.EnvVars)2