mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-22 11:35:56 +00:00
Merge 275ab89e62
into 7dc6191b0a
This commit is contained in:
commit
b53dc5fea3
2 changed files with 56 additions and 13 deletions
|
@ -10,7 +10,7 @@ class MqttMonitorType extends MonitorType {
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
async check(monitor, heartbeat, server) {
|
async check(monitor, heartbeat, server) {
|
||||||
const receivedMessage = await this.mqttAsync(monitor.hostname, monitor.mqttTopic, {
|
const [ messageTopic, receivedMessage ] = await this.mqttAsync(monitor.hostname, monitor.mqttTopic, {
|
||||||
port: monitor.port,
|
port: monitor.port,
|
||||||
username: monitor.mqttUsername,
|
username: monitor.mqttUsername,
|
||||||
password: monitor.mqttPassword,
|
password: monitor.mqttPassword,
|
||||||
|
@ -24,7 +24,7 @@ class MqttMonitorType extends MonitorType {
|
||||||
|
|
||||||
if (monitor.mqttCheckType === "keyword") {
|
if (monitor.mqttCheckType === "keyword") {
|
||||||
if (receivedMessage != null && receivedMessage.includes(monitor.mqttSuccessMessage)) {
|
if (receivedMessage != null && receivedMessage.includes(monitor.mqttSuccessMessage)) {
|
||||||
heartbeat.msg = `Topic: ${monitor.mqttTopic}; Message: ${receivedMessage}`;
|
heartbeat.msg = `Topic: ${messageTopic}; Message: ${receivedMessage}`;
|
||||||
heartbeat.status = UP;
|
heartbeat.status = UP;
|
||||||
} else {
|
} else {
|
||||||
throw Error(`Message Mismatch - Topic: ${monitor.mqttTopic}; Message: ${receivedMessage}`);
|
throw Error(`Message Mismatch - Topic: ${monitor.mqttTopic}; Message: ${receivedMessage}`);
|
||||||
|
@ -101,11 +101,9 @@ class MqttMonitorType extends MonitorType {
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("message", (messageTopic, message) => {
|
client.on("message", (messageTopic, message) => {
|
||||||
if (messageTopic === topic) {
|
client.end();
|
||||||
client.end();
|
clearTimeout(timeoutID);
|
||||||
clearTimeout(timeoutID);
|
resolve([ messageTopic, message.toString("utf8") ]);
|
||||||
resolve(message.toString("utf8"));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -52,7 +54,7 @@ async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("MqttMonitorType", {
|
describe("MqttMonitorType", {
|
||||||
concurrency: true,
|
concurrency: 4,
|
||||||
skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64")
|
skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64")
|
||||||
}, () => {
|
}, () => {
|
||||||
test("valid keywords (type=default)", async () => {
|
test("valid keywords (type=default)", async () => {
|
||||||
|
@ -61,11 +63,51 @@ describe("MqttMonitorType", {
|
||||||
assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-");
|
assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("valid nested topic", async () => {
|
||||||
|
const heartbeat = await testMqtt("KEYWORD", null, "-> KEYWORD <-", "a/b/c", "a/b/c");
|
||||||
|
assert.strictEqual(heartbeat.status, UP);
|
||||||
|
assert.strictEqual(heartbeat.msg, "Topic: a/b/c; Message: -> KEYWORD <-");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("valid wildcard topic (with #)", async () => {
|
||||||
|
const heartbeat = await testMqtt("KEYWORD", null, "-> KEYWORD <-", "a/#", "a/b/c");
|
||||||
|
assert.strictEqual(heartbeat.status, UP);
|
||||||
|
assert.strictEqual(heartbeat.msg, "Topic: a/b/c; Message: -> KEYWORD <-");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("valid wildcard topic (with +)", async () => {
|
||||||
|
const heartbeat = await testMqtt("KEYWORD", null, "-> KEYWORD <-", "a/+/c", "a/b/c");
|
||||||
|
assert.strictEqual(heartbeat.status, UP);
|
||||||
|
assert.strictEqual(heartbeat.msg, "Topic: a/b/c; Message: -> KEYWORD <-");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("invalid topic", async () => {
|
||||||
|
await assert.rejects(
|
||||||
|
testMqtt("keyword will not be checked anyway", null, "message", "x/y/z", "a/b/c"),
|
||||||
|
new Error("Timeout, Message not received"),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("invalid wildcard topic (with #)", async () => {
|
||||||
|
await assert.rejects(
|
||||||
|
testMqtt("", null, "# should be last character", "#/c", "a/b/c"),
|
||||||
|
new Error("Timeout, Message not received"),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("invalid wildcard topic (with +)", async () => {
|
||||||
|
await assert.rejects(
|
||||||
|
testMqtt("", null, "message", "x/+/z", "a/b/c"),
|
||||||
|
new Error("Timeout, Message not received"),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("valid keywords (type=keyword)", async () => {
|
test("valid keywords (type=keyword)", async () => {
|
||||||
const heartbeat = await testMqtt("KEYWORD", "keyword", "-> KEYWORD <-");
|
const heartbeat = await testMqtt("KEYWORD", "keyword", "-> KEYWORD <-");
|
||||||
assert.strictEqual(heartbeat.status, UP);
|
assert.strictEqual(heartbeat.status, UP);
|
||||||
assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-");
|
assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("invalid keywords (type=default)", async () => {
|
test("invalid keywords (type=default)", async () => {
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
testMqtt("NOT_PRESENT", null, "-> KEYWORD <-"),
|
testMqtt("NOT_PRESENT", null, "-> KEYWORD <-"),
|
||||||
|
@ -79,12 +121,14 @@ describe("MqttMonitorType", {
|
||||||
new Error("Message Mismatch - Topic: test; Message: -> KEYWORD <-"),
|
new Error("Message Mismatch - Topic: test; Message: -> KEYWORD <-"),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("valid json-query", async () => {
|
test("valid json-query", async () => {
|
||||||
// works because the monitors' jsonPath is hard-coded to "firstProp"
|
// works because the monitors' jsonPath is hard-coded to "firstProp"
|
||||||
const heartbeat = await testMqtt("present", "json-query", "{\"firstProp\":\"present\"}");
|
const heartbeat = await testMqtt("present", "json-query", "{\"firstProp\":\"present\"}");
|
||||||
assert.strictEqual(heartbeat.status, UP);
|
assert.strictEqual(heartbeat.status, UP);
|
||||||
assert.strictEqual(heartbeat.msg, "Message received, expected value is found");
|
assert.strictEqual(heartbeat.msg, "Message received, expected value is found");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("invalid (because query fails) json-query", async () => {
|
test("invalid (because query fails) json-query", async () => {
|
||||||
// works because the monitors' jsonPath is hard-coded to "firstProp"
|
// works because the monitors' jsonPath is hard-coded to "firstProp"
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
|
@ -92,6 +136,7 @@ describe("MqttMonitorType", {
|
||||||
new Error("Message received but value is not equal to expected value, value was: [undefined]"),
|
new Error("Message received but value is not equal to expected value, value was: [undefined]"),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("invalid (because successMessage fails) json-query", async () => {
|
test("invalid (because successMessage fails) json-query", async () => {
|
||||||
// works because the monitors' jsonPath is hard-coded to "firstProp"
|
// works because the monitors' jsonPath is hard-coded to "firstProp"
|
||||||
await assert.rejects(
|
await assert.rejects(
|
||||||
|
|
Loading…
Add table
Reference in a new issue