diff options
author | Daniel Baumann <daniel@debian.org> | 2024-11-26 09:28:28 +0100 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-11-26 12:25:58 +0100 |
commit | a1882b67c41fe9901a0cd8059b5cc78a5beadec0 (patch) | |
tree | 2a24507c67aa99a15416707b2f7e645142230ed8 /test/backend-test/test-mqtt.js | |
parent | Initial commit. (diff) | |
download | uptime-kuma-a1882b67c41fe9901a0cd8059b5cc78a5beadec0.tar.xz uptime-kuma-a1882b67c41fe9901a0cd8059b5cc78a5beadec0.zip |
Adding upstream version 2.0.0~beta.0+dfsg.upstream/2.0.0_beta.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'test/backend-test/test-mqtt.js')
-rw-r--r-- | test/backend-test/test-mqtt.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/test/backend-test/test-mqtt.js b/test/backend-test/test-mqtt.js new file mode 100644 index 0000000..4503102 --- /dev/null +++ b/test/backend-test/test-mqtt.js @@ -0,0 +1,102 @@ +const { describe, test } = require("node:test"); +const assert = require("node:assert"); +const { HiveMQContainer } = require("@testcontainers/hivemq"); +const mqtt = require("mqtt"); +const { MqttMonitorType } = require("../../server/monitor-types/mqtt"); +const { UP, PENDING } = require("../../src/util"); + +/** + * Runs an MQTT test with the + * @param {string} mqttSuccessMessage the message that the monitor expects + * @param {null|"keyword"|"json-query"} mqttCheckType the type of check we perform + * @param {string} receivedMessage what message is recieved from the mqtt channel + * @returns {Promise<Heartbeat>} the heartbeat produced by the check + */ +async function testMqtt(mqttSuccessMessage, mqttCheckType, receivedMessage) { + const hiveMQContainer = await new HiveMQContainer().start(); + const connectionString = hiveMQContainer.getConnectionString(); + const mqttMonitorType = new MqttMonitorType(); + const monitor = { + jsonPath: "firstProp", // always return firstProp for the json-query monitor + hostname: connectionString.split(":", 2).join(":"), + mqttTopic: "test", + port: connectionString.split(":")[2], + mqttUsername: null, + mqttPassword: null, + interval: 20, // controls the timeout + mqttSuccessMessage: mqttSuccessMessage, // for keywords + expectedValue: mqttSuccessMessage, // for json-query + mqttCheckType: mqttCheckType, + }; + const heartbeat = { + msg: "", + status: PENDING, + }; + + const testMqttClient = mqtt.connect(hiveMQContainer.getConnectionString()); + testMqttClient.on("connect", () => { + testMqttClient.subscribe("test", (error) => { + if (!error) { + testMqttClient.publish("test", receivedMessage); + } + }); + }); + + try { + await mqttMonitorType.check(monitor, heartbeat, {}); + } finally { + testMqttClient.end(); + hiveMQContainer.stop(); + } + return heartbeat; +} + +describe("MqttMonitorType", { + concurrency: true, + skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64") +}, () => { + test("valid keywords (type=default)", async () => { + const heartbeat = await testMqtt("KEYWORD", null, "-> KEYWORD <-"); + assert.strictEqual(heartbeat.status, UP); + assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-"); + }); + + test("valid keywords (type=keyword)", async () => { + const heartbeat = await testMqtt("KEYWORD", "keyword", "-> KEYWORD <-"); + assert.strictEqual(heartbeat.status, UP); + assert.strictEqual(heartbeat.msg, "Topic: test; Message: -> KEYWORD <-"); + }); + test("invalid keywords (type=default)", async () => { + await assert.rejects( + testMqtt("NOT_PRESENT", null, "-> KEYWORD <-"), + new Error("Message Mismatch - Topic: test; Message: -> KEYWORD <-"), + ); + }); + + test("invalid keyword (type=keyword)", async () => { + await assert.rejects( + testMqtt("NOT_PRESENT", "keyword", "-> KEYWORD <-"), + new Error("Message Mismatch - Topic: test; Message: -> KEYWORD <-"), + ); + }); + test("valid json-query", async () => { + // works because the monitors' jsonPath is hard-coded to "firstProp" + const heartbeat = await testMqtt("present", "json-query", "{\"firstProp\":\"present\"}"); + assert.strictEqual(heartbeat.status, UP); + assert.strictEqual(heartbeat.msg, "Message received, expected value is found"); + }); + test("invalid (because query fails) json-query", async () => { + // works because the monitors' jsonPath is hard-coded to "firstProp" + await assert.rejects( + testMqtt("[not_relevant]", "json-query", "{}"), + new Error("Message received but value is not equal to expected value, value was: [undefined]"), + ); + }); + test("invalid (because successMessage fails) json-query", async () => { + // works because the monitors' jsonPath is hard-coded to "firstProp" + await assert.rejects( + testMqtt("[wrong_success_messsage]", "json-query", "{\"firstProp\":\"present\"}"), + new Error("Message received but value is not equal to expected value, value was: [present]") + ); + }); +}); |