/** * When the operating system a server runs stops being supported: what the table knows, how an agent's answer is read, the alert, and that the agent's own * script says it (the shell part is checked by reading it; the answer it would give is a plain JSON object). * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; const dir = mkdtempSync(join(tmpdir(), "hm-oslife-")); process.env.DATABASE_PATH = join(dir, "test.sqlite"); process.env.SESSION_DIR = join(dir, "sessions"); process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32); process.env.HEALTH_STARTUP_GRACE_MS = "0"; const { db } = await import("../src/db/client.js"); const { servers, maintenanceWindows } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { lifecycleOf, releaseKey, supportState, daysOfSupportLeft } = await import("../src/services/osLifecycle.js"); const { syncServerTasks } = await import("../src/services/taskSync.js"); const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js"); const { updateSettings } = await import("../src/services/settingsStore.js"); const { latestAgentVersions } = await import("../src/services/agentVersions.js"); const { eq } = await import("drizzle-orm"); await runMigrations(); const repo = fileURLToPath(new URL("../../", import.meta.url)); after(() => { try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows may still hold the database file } }); describe("reading what an agent says it runs", () => { test("a release is found by its distribution and version, however the version is written", () => { assert.deepEqual(releaseKey({ id: "ubuntu", version: "22.04" }), { family: "ubuntu", key: "22.04" }); assert.deepEqual(releaseKey({ id: "debian", version: "12" }), { family: "debian", key: "12" }); assert.deepEqual(releaseKey({ id: "raspbian", version: "11" }), { family: "debian", key: "11" }, "Raspberry Pi OS is Debian"); assert.deepEqual(releaseKey({ id: "rocky", version: "9.4" }), { family: "rocky", key: "9" }); assert.deepEqual(releaseKey({ id: "alpine", version: "3.19.1" }), { family: "alpine", key: "3.19" }); assert.deepEqual(releaseKey({ id: "proxmox-ve", version: "8.2" }), { family: "proxmox-ve", key: "8" }); assert.deepEqual(releaseKey({ id: "centos", name: "CentOS Stream 9", version: "9" }), { family: "centos-stream", key: "9" }); assert.deepEqual(releaseKey({ id: "centos", name: "CentOS Linux 7 (Core)", version: "7" }), { family: "centos-classic", key: "7" }); assert.deepEqual(releaseKey({ id: "windows", name: "Windows Server 2019 Standard", version: "17763" }), { family: "windows-server", key: "17763" }); assert.deepEqual(releaseKey({ id: "windows", name: "Windows 11 Pro", version: "22631" }), { family: "windows-client", key: "22631" }); assert.equal(releaseKey({ id: "arch", version: "rolling" }), null, "a rolling distribution has no end"); assert.equal(releaseKey({ id: "", version: "1" }), null); assert.equal(releaseKey({ id: "ubuntu" }), null); }); test("the same build number is a different product on a server and a client", () => { assert.equal(lifecycleOf({ id: "windows", name: "Windows Server 2025 Datacenter", version: "26100" })?.product, "Windows Server 2025"); assert.equal(lifecycleOf({ id: "windows", name: "Windows 11 Pro", version: "26100" })?.product, "Windows 11 24H2"); }); test("what the table knows: a name and a date; a release it does not know is named as the machine named itself, with no date", () => { assert.deepEqual(lifecycleOf({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" }), { product: "Ubuntu 20.04 LTS", endsOn: "2025-05-31" }); assert.deepEqual(lifecycleOf({ id: "debian", version: "12" }), { product: "Debian 12 (bookworm)", endsOn: "2028-06-30" }); assert.deepEqual(lifecycleOf({ id: "ubuntu", name: "Ubuntu 31.04", version: "31.04" }), { product: "Ubuntu 31.04", endsOn: null }); assert.equal(lifecycleOf({ id: "ubuntu", version: "31.04" }), null, "nothing to call it, nothing to say"); assert.equal(lifecycleOf({ id: "templeos", version: "5" }), null); }); test("ended once the day is over, soon within the warning, ok beyond it, unknown with no date", () => { const now = new Date("2026-10-20T12:00:00Z"); assert.equal(supportState("2026-10-19", now, 90), "ended"); assert.equal(supportState("2026-10-20", now, 90), "soon", "the last day is still a day of support"); assert.equal(supportState("2027-01-10", now, 90), "soon"); assert.equal(supportState("2027-06-01", now, 90), "ok"); assert.equal(supportState(null, now, 90), "unknown"); assert.equal(daysOfSupportLeft("2026-10-30", now), 10); assert.equal(daysOfSupportLeft("2026-10-10", now), -10); }); }); describe("what the agent says, and what the Alerts page makes of it", () => { let id = 0; before(async () => { id = (await db.insert(servers).values({ name: "old-box", apiTokenHash: "o", apiTokenPrefix: "o", lastSeenAt: new Date().toISOString() }).returning({ id: servers.id }))[0].id; }); const row = () => db.select().from(servers).where(eq(servers.id, id)).then((r) => r[0]); const report = (os: unknown) => syncServerTasks(id, { osType: "linux", tasks: [], system: { os } as never }); test("it is stored, bounded; an agent that says nothing leaves what was said", async () => { await report({ id: "Ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" }); let r = await row(); assert.deepEqual([r.osId, r.osName, r.osVersion], ["ubuntu", "Ubuntu 20.04.6 LTS", "20.04"]); await syncServerTasks(id, { osType: "linux", tasks: [], system: {} }); r = await row(); assert.equal(r.osId, "ubuntu", "an older agent does not wipe it"); await report({ id: "x".repeat(200), name: "n".repeat(500), version: "v".repeat(200) }); r = await row(); assert.deepEqual([r.osId!.length, r.osName!.length, r.osVersion!.length], [40, 80, 40]); await report({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" }); }); test("a release past its end is a warning with the date, on the Alerts page, under the server", async () => { invalidateAlertsCache(); const alert = (await getAlerts(true)).alerts.find((a) => a.id === `updates:os-support:${id}`)!; assert.ok(alert, "it is raised"); assert.deepEqual([alert.severity, alert.category, alert.link, alert.target?.name, alert.silenced], ["warning", "updates", `/servers/${id}`, "old-box", false]); assert.match(alert.message, /old-box runs Ubuntu 20\.04 LTS, whose support ended on 2025-05-31/); }); test("one with a supported release is not raised, and the warning is a setting (0 turns it off)", async () => { await report({ id: "debian", name: "Debian GNU/Linux 12 (bookworm)", version: "12" }); invalidateAlertsCache(); assert.equal((await getAlerts(true)).alerts.some((a) => a.id === `updates:os-support:${id}`), false); await report({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" }); await updateSettings({ healthChecks: { osSupportWarnDays: 0 } }); invalidateAlertsCache(); assert.equal((await getAlerts(true)).alerts.some((a) => a.id === `updates:os-support:${id}`), false, "off"); await updateSettings({ healthChecks: { osSupportWarnDays: 90 } }); }); test("a maintenance window on the server holds it back", async () => { await db.insert(maintenanceWindows).values({ targetType: "server", targetId: id, startedAt: new Date(Date.now() - 1000).toISOString(), endsAt: new Date(Date.now() + 3600_000).toISOString() }); invalidateAlertsCache(); assert.equal((await getAlerts(true)).alerts.find((a) => a.id === `updates:os-support:${id}`)?.silenced, true); await db.delete(maintenanceWindows).where(eq(maintenanceWindows.targetId, id)); }); }); describe("the agent scripts say it", () => { test("Linux reads /etc/os-release (and asks Proxmox by its own command) and reports it; Windows reports the product and the build; both are a new version", () => { const linux = readFileSync(join(repo, "agent", "linux", "report-tasks.sh"), "utf8"); assert.ok(linux.includes("collect_os()") && linux.includes("OS_RELEASE_FILE") && linux.includes("pveversion") && linux.includes("os: $os")); const windows = readFileSync(join(repo, "agent", "windows", "report-tasks.ps1"), "utf8"); assert.ok(windows.includes("Win32_OperatingSystem") && windows.includes("os = $osInfo") && windows.includes("BuildNumber")); const latest = latestAgentVersions(); assert.ok((latest.linux ?? 0) >= 7 && (latest.windows ?? 0) >= 6, "an agent that does not say it is behind"); }); });