mirror of
https://github.com/koichixD/uptime-kuma.git
synced 2026-09-04 17:23:21 +00:00
Added systemd support to auto-test.yml
This commit is contained in:
@@ -3,15 +3,35 @@ const assert = require("node:assert");
|
||||
const { SystemServiceMonitorType } = require("../../server/monitor-types/system-service");
|
||||
const { DOWN, UP } = require("../../src/util");
|
||||
const process = require("process");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const os = require("os");
|
||||
const { execSync } = require("child_process");
|
||||
|
||||
// GUARD CLAUSE: Skip test if not on Systemd (Linux) or Windows
|
||||
let shouldRun = false;
|
||||
|
||||
if (process.platform === "win32") {
|
||||
shouldRun = true;
|
||||
} else if (process.platform === "linux") {
|
||||
try {
|
||||
// Check if PID 1 is systemd (or init which maps to systemd in our container)
|
||||
const pid1Comm = execSync("ps -p 1 -o comm=", { encoding: "utf-8" }).trim();
|
||||
if (pid1Comm === "systemd" || pid1Comm === "init") {
|
||||
shouldRun = true;
|
||||
}
|
||||
} catch (e) {
|
||||
// Command failed, likely not systemd
|
||||
}
|
||||
}
|
||||
|
||||
if (!shouldRun) {
|
||||
console.log("⚠️ Skipping System Service test: Environment does not support systemd/services.");
|
||||
// We return early or just don't define tests, so the runner sees 0 failures.
|
||||
// In node:test, we can just exit gracefully or simply not call 'test()'.
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
describe("SystemServiceMonitorType", () => {
|
||||
let monitorType;
|
||||
let heartbeat;
|
||||
let tempDir;
|
||||
let originalPath;
|
||||
let originalPlatform;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -20,65 +40,32 @@ describe("SystemServiceMonitorType", () => {
|
||||
status: DOWN,
|
||||
msg: "",
|
||||
};
|
||||
originalPath = process.env.PATH;
|
||||
originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.env.PATH = originalPath;
|
||||
if (originalPlatform) {
|
||||
Object.defineProperty(process, "platform", originalPlatform);
|
||||
}
|
||||
if (tempDir && fs.existsSync(tempDir)) {
|
||||
try {
|
||||
fs.rmSync(tempDir, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
});
|
||||
} catch (e) {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper to create a fake executable that prints specific output.
|
||||
* @param {string} baseName The name of the executable (e.g. systemctl)
|
||||
* @param {string} outputText The text to echo to stdout
|
||||
* @param {number} exitCode The exit code
|
||||
* @returns {void}
|
||||
*/
|
||||
function createMockCommand(baseName, outputText, exitCode = 0) {
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "uptime-kuma-test-"));
|
||||
it("should detect a running service", async (t) => {
|
||||
// Requirement: Use REAL system tools (no mocks).
|
||||
// Therefore, we must skip this test on platforms that lack systemd/PowerShell (like macOS).
|
||||
if (process.platform !== "linux" && process.platform !== "win32") {
|
||||
t.skip("Skipping integration test: Real systemd/PowerShell not available on this platform");
|
||||
return;
|
||||
}
|
||||
|
||||
// Only needed for non-Windows mocks (Linux/Mac)
|
||||
const content = `#!/bin/sh\necho "${outputText}"\nexit ${exitCode}`;
|
||||
const scriptPath = path.join(tempDir, baseName);
|
||||
|
||||
fs.writeFileSync(scriptPath, content);
|
||||
fs.chmodSync(scriptPath, 0o755);
|
||||
process.env.PATH = tempDir + path.delimiter + process.env.PATH;
|
||||
}
|
||||
|
||||
it("should detect a running service", async () => {
|
||||
const isWin = process.platform === "win32";
|
||||
let serviceName = "myservice";
|
||||
|
||||
if (isWin) {
|
||||
// Windows CI has real PowerShell and real services.
|
||||
// We test against 'Dnscache', a core service guaranteed to be running.
|
||||
// Windows: Test against 'Dnscache' (DNS Client), guaranteed to be running.
|
||||
serviceName = "Dnscache";
|
||||
} else {
|
||||
// Linux CI (Docker) lacks systemd. We must mock the tool to pass the test.
|
||||
createMockCommand("systemctl", "active", 0);
|
||||
}
|
||||
|
||||
// If on macOS, mock Linux platform so the code tries to use systemctl
|
||||
if (process.platform === "darwin") {
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: "linux",
|
||||
configurable: true,
|
||||
});
|
||||
// Linux: Test against 'systemd-journald', a core service of systemd.
|
||||
serviceName = "systemd-journald";
|
||||
}
|
||||
|
||||
const monitor = {
|
||||
@@ -91,24 +78,15 @@ describe("SystemServiceMonitorType", () => {
|
||||
assert.ok(heartbeat.msg.includes("is running"));
|
||||
});
|
||||
|
||||
it("should detect a stopped service", async () => {
|
||||
const isWin = process.platform === "win32";
|
||||
let serviceName = "myservice";
|
||||
|
||||
if (isWin) {
|
||||
// Real Windows: Query a non-existent service to force an error/down state
|
||||
serviceName = "non-existent-service-12345";
|
||||
} else {
|
||||
// Mocked Linux: Create a mock that returns "inactive" (exit code 1)
|
||||
createMockCommand("systemctl", "inactive", 1);
|
||||
it("should detect a stopped service", async (t) => {
|
||||
if (process.platform !== "linux" && process.platform !== "win32") {
|
||||
t.skip("Skipping integration test: Real systemd/PowerShell not available on this platform");
|
||||
return;
|
||||
}
|
||||
|
||||
if (process.platform === "darwin") {
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: "linux",
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
// Query a non-existent service to force an error/down state.
|
||||
// This works correctly on both 'systemctl' and 'Get-Service'.
|
||||
const serviceName = "non-existent-service-12345";
|
||||
|
||||
const monitor = {
|
||||
system_service_name: serviceName,
|
||||
@@ -143,6 +121,7 @@ describe("SystemServiceMonitorType", () => {
|
||||
});
|
||||
|
||||
it("should throw error on unsupported platforms", async () => {
|
||||
// This test mocks the platform, so it can run anywhere.
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: "darwin",
|
||||
configurable: true,
|
||||
|
||||
Reference in New Issue
Block a user