Allow different topics for monitoring and publishing in MQTT tests

pull/5398/head
Eric Duminil 4 weeks ago
parent a8b8a21f4c
commit ae439c2522

@ -9,17 +9,19 @@ const { UP, PENDING } = require("../../src/util");
* Runs an MQTT test with the * Runs an MQTT test with the
* @param {string} mqttSuccessMessage the message that the monitor expects * @param {string} mqttSuccessMessage the message that the monitor expects
* @param {null|"keyword"|"json-query"} mqttCheckType the type of check we perform * @param {null|"keyword"|"json-query"} mqttCheckType the type of check we perform
* @param {string} receivedMessage what message is recieved from the mqtt channel * @param {string} receivedMessage what message is received from the mqtt channel
* @param {string} monitorTopic which MQTT topic is monitored (wildcards are allowed)
* @param {string} publishTopic to which MQTT topic the message is sent
* @returns {Promise<Heartbeat>} the heartbeat produced by the check * @returns {Promise<Heartbeat>} the heartbeat produced by the check
*/ */
async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage) { async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage, monitorTopic = "test", publishTopic = "test") {
const hiveMQContainer = await new HiveMQContainer().start(); const hiveMQContainer = await new HiveMQContainer().start();
const connectionString = hiveMQContainer.getConnectionString(); const connectionString = hiveMQContainer.getConnectionString();
const mqttMonitorType = new MqttMonitorType(); const mqttMonitorType = new MqttMonitorType();
const monitor = { const monitor = {
jsonPath: "firstProp", // always return firstProp for the json-query monitor jsonPath: "firstProp", // always return firstProp for the json-query monitor
hostname: connectionString.split(":", 2).join(":"), hostname: connectionString.split(":", 2).join(":"),
mqttTopic: "test", mqttTopic: monitorTopic,
port: connectionString.split(":")[2], port: connectionString.split(":")[2],
mqttUsername: null, mqttUsername: null,
mqttPassword: null, mqttPassword: null,
@ -35,9 +37,9 @@ async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage) {
const testMqttClient = mqtt.connect(hiveMQContainer.getConnectionString()); const testMqttClient = mqtt.connect(hiveMQContainer.getConnectionString());
testMqttClient.on("connect", () => { testMqttClient.on("connect", () => {
testMqttClient.subscribe("test", (error) => { testMqttClient.subscribe(monitorTopic, (error) => {
if (!error) { if (!error) {
testMqttClient.publish("test", receivedMessage); testMqttClient.publish(publishTopic, receivedMessage);
} }
}); });
}); });

Loading…
Cancel
Save