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());
}
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());
}
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());
}
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;
}
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;
}
Aggregations