Fold alerts under their host, recurring maintenance, run playbooks from alerts, and an Updates page

- Servers can say what they run on (or it is worked out from the Proxmox node); when the host is
  down the guests' offline alerts fold under it: held back, not counted, not notified, still recorded.
- Recurring maintenance schedules made into ordinary windows a week ahead (maintenance_schedules).
- Run a Semaphore template limited to a server from its page or an alert, and watch the run.
- Agents report pending updates (apt, dnf, zypper, Windows Update); an Updates page lists them.
- Migrations 0024 and 0025.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-17 01:35:15 +02:00
1 parent 44e393f172
commit 171511c6db
45 files changed
+6772 -44

No files matched your search

+202
View File
@@ -0,0 +1,202 @@
/**
* Recurring maintenance: when a schedule happens (across summer and winter time), and the windows made from it.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
const dir = mkdtempSync(join(tmpdir(), "hm-sched-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers, maintenanceWindows, maintenanceSchedules, auditLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { maintenanceRouter } = await import("../src/routes/maintenance.js");
const { occurrencesBetween, zonedTime, materializeSchedules, isValidTimeZone } = await import("../src/services/maintenanceSchedules.js");
const { listActiveWindows } = await import("../src/services/maintenance.js");
const { eq, like } = await import("drizzle-orm");
await runMigrations();
const sched = (days: number[], startTime: string, durationMinutes: number, timezone: string) => ({ days: JSON.stringify(days), startTime, durationMinutes, timezone });
const iso = (d: Date) => d.toISOString();
describe("when a schedule happens", () => {
test("a wall-clock time follows summer and winter time", () => {
// Sundays in Stockholm: 18 October is still summer time (UTC+2), 1 November is winter time (UTC+1).
const s = sched([0], "02:00", 120, "Europe/Stockholm");
const occ = occurrencesBetween(s, new Date("2026-10-15T00:00:00Z"), new Date("2026-11-03T00:00:00Z"));
const byDate = Object.fromEntries(occ.map((o) => [o.date, [iso(o.start), iso(o.end)]]));
assert.deepEqual(byDate["2026-10-18"], ["2026-10-18T00:00:00.000Z", "2026-10-18T02:00:00.000Z"]);
assert.deepEqual(byDate["2026-11-01"], ["2026-11-01T01:00:00.000Z", "2026-11-01T03:00:00.000Z"]);
assert.deepEqual(Object.keys(byDate).sort(), ["2026-10-18", "2026-10-25", "2026-11-01"]);
});
test("it is the same wall-clock time in another zone, and a time that doesn't exist that night still happens", () => {
assert.equal(iso(zonedTime(2026, 7, 1, 2, 0, "America/New_York")), "2026-07-01T06:00:00.000Z");
assert.equal(iso(zonedTime(2026, 1, 1, 2, 0, "America/New_York")), "2026-01-01T07:00:00.000Z");
// 29 March 2026: clocks in Stockholm jump from 02:00 to 03:00, so 02:30 isn't a time that night.
const gap = zonedTime(2026, 3, 29, 2, 30, "Europe/Stockholm");
assert.ok(Math.abs(gap.getTime() - Date.parse("2026-03-29T01:00:00Z")) <= 3600_000);
assert.equal(zonedTime(2026, 10, 18, 0, 0, "UTC").toISOString(), "2026-10-18T00:00:00.000Z");
});
test("one that runs past midnight belongs to the day it starts, and one under way counts", () => {
const s = sched([6], "23:00", 180, "UTC"); // Saturday 23:00 for three hours
const [occ] = occurrencesBetween(s, new Date("2026-10-18T00:30:00Z"), new Date("2026-10-18T06:00:00Z"));
assert.equal(occ.date, "2026-10-17");
assert.equal(iso(occ.start), "2026-10-17T23:00:00.000Z");
assert.equal(iso(occ.end), "2026-10-18T02:00:00.000Z");
assert.equal(occurrencesBetween(s, new Date("2026-10-18T02:00:00Z"), new Date("2026-10-18T06:00:00Z")).length, 0, "already over");
});
test("every day, and only the days chosen", () => {
const week = occurrencesBetween(sched([0, 1, 2, 3, 4, 5, 6], "04:00", 60, "UTC"), new Date("2026-10-12T00:00:00Z"), new Date("2026-10-19T00:00:00Z"));
assert.equal(week.length, 7);
const weekdays = occurrencesBetween(sched([1, 3], "04:00", 60, "UTC"), new Date("2026-10-12T00:00:00Z"), new Date("2026-10-19T00:00:00Z"));
assert.deepEqual(weekdays.map((o) => o.date), ["2026-10-12", "2026-10-14"]);
});
test("time zone names are checked", () => {
assert.equal(isValidTimeZone("Europe/Stockholm"), true);
assert.equal(isValidTimeZone("Mars/Olympus"), false);
});
});
describe("the schedules and their windows", () => {
let app: Server;
let base = "";
let who = "operator";
let a = 0;
let b = 0;
before(async () => {
await db.insert(users).values([
{ oidcSub: "operator", name: "Olga", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
const made = await db
.insert(servers)
.values([
{ name: "elsa", apiTokenHash: "a", apiTokenPrefix: "a" },
{ name: "anna", apiTokenHash: "b", apiTokenPrefix: "b" },
])
.returning({ id: servers.id });
a = made[0].id;
b = made[1].id;
const app0 = express();
app0.use(express.json());
app0.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
app0.use("/api/maintenance", maintenanceRouter);
app = await new Promise<Server>((resolve) => {
const s = app0.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/maintenance`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const call = (method: string, path: string, body?: unknown) =>
fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
const good = { name: "Patch night", reason: "OS updates", targets: [{ type: "server", id: 0 }], days: [0, 1, 2, 3, 4, 5, 6], startTime: "03:00", durationMinutes: 60, timezone: "UTC" };
const windowsOf = async (id: number) => db.select().from(maintenanceWindows).where(like(maintenanceWindows.source, `schedule:${id}:%`));
let scheduleId = 0;
test("making a schedule makes the coming week's windows, once each, for every server on it", async () => {
const res = await call("POST", "/schedules", { ...good, targets: [{ type: "server", id: a }, { type: "server", id: b }, { type: "server", id: a }] });
assert.equal(res.status, 201);
const body = await res.json();
scheduleId = body.id;
const windows = await windowsOf(scheduleId);
assert.ok(body.windowsMade === windows.length && (windows.length === 14 || windows.length === 16), `made ${windows.length}`);
assert.equal(new Set(windows.map((w) => `${w.targetId}|${w.source}`)).size, windows.length, "no duplicates");
assert.ok(windows.every((w) => w.reason === "Patch night: OS updates" && w.createdBy === "Recurring: Patch night"));
assert.ok(windows.every((w) => w.endsAt > w.startedAt));
// running again makes nothing more
assert.equal(await materializeSchedules(), 0);
assert.equal((await windowsOf(scheduleId)).length, windows.length);
});
test("a window cancelled by hand isn't made again, and the list says what is next", async () => {
const before = await windowsOf(scheduleId);
const one = before.find((w) => w.startedAt > new Date().toISOString())!;
await db.update(maintenanceWindows).set({ endsAt: one.startedAt }).where(eq(maintenanceWindows.id, one.id));
assert.equal(await materializeSchedules(), 0);
assert.equal((await windowsOf(scheduleId)).length, before.length);
const list = await (await call("GET", "/schedules")).json();
assert.equal(list.schedules.length, 1);
assert.equal(list.schedules[0].targets.length, 2);
assert.deepEqual(list.schedules[0].days, [0, 1, 2, 3, 4, 5, 6]);
assert.ok(Date.parse(list.schedules[0].nextStart) > 0);
});
test("a window made from a schedule silences while it runs", async () => {
const now = Date.now();
await db.insert(maintenanceWindows).values({ targetType: "server", targetId: a, reason: "x", startedAt: new Date(now - 60_000).toISOString(), endsAt: new Date(now + 600_000).toISOString(), source: `schedule:${scheduleId}:2000-01-01` });
assert.ok((await listActiveWindows()).some((w) => w.source === `schedule:${scheduleId}:2000-01-01`));
await db.delete(maintenanceWindows).where(eq(maintenanceWindows.source, `schedule:${scheduleId}:2000-01-01`));
});
test("switching it off takes back what hasn't begun, and on makes it again", async () => {
assert.equal((await call("PATCH", `/schedules/${scheduleId}`, { enabled: false })).status, 200);
assert.equal((await windowsOf(scheduleId)).filter((w) => w.startedAt > new Date().toISOString()).length, 0);
assert.equal(await materializeSchedules(), 0, "off means nothing is made");
assert.equal((await call("PATCH", `/schedules/${scheduleId}`, { enabled: true })).status, 200);
assert.ok((await windowsOf(scheduleId)).length >= 12);
});
test("only operators change schedules; a bad one is refused", async () => {
who = "viewer";
assert.equal((await call("POST", "/schedules", { ...good, targets: [{ type: "server", id: a }] })).status, 403);
assert.equal((await call("PATCH", `/schedules/${scheduleId}`, { enabled: false })).status, 403);
assert.equal((await call("DELETE", `/schedules/${scheduleId}`)).status, 403);
assert.equal((await call("GET", "/schedules")).status, 200, "anyone signed in can see them");
who = "operator";
const target = [{ type: "server", id: a }];
for (const bad of [
{ ...good, targets: target, name: "" },
{ ...good, targets: [] },
{ ...good, targets: target, days: [] },
{ ...good, targets: target, days: [7] },
{ ...good, targets: target, startTime: "25:00" },
{ ...good, targets: target, startTime: "3am" },
{ ...good, targets: target, durationMinutes: 5 },
{ ...good, targets: target, timezone: "Mars/Olympus" },
{ ...good, targets: [{ type: "server", id: 99999 }] },
]) {
assert.equal((await call("POST", "/schedules", bad)).status, 400, JSON.stringify(bad).slice(0, 80));
}
});
test("removing a schedule takes back what hasn't begun and is in the audit log", async () => {
assert.equal((await call("DELETE", `/schedules/${scheduleId}`)).status, 204);
assert.equal((await db.select().from(maintenanceSchedules)).length, 0);
assert.equal((await windowsOf(scheduleId)).filter((w) => w.startedAt > new Date().toISOString()).length, 0);
assert.equal((await call("DELETE", `/schedules/${scheduleId}`)).status, 404);
const actions = (await db.select().from(auditLog)).filter((e) => e.category === "maintenance").map((e) => e.action);
for (const wanted of ["create_schedule", "disable_schedule", "enable_schedule", "delete_schedule"]) assert.ok(actions.includes(wanted), wanted);
});
});
+306
View File
@@ -0,0 +1,306 @@
/**
* Folding a guest's "offline" alert under its host's, and what servers say is waiting to be installed.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
const dir = mkdtempSync(join(tmpdir(), "hm-parents-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
// No waiting after a restart before servers are judged, or nothing here could be "offline".
process.env.HEALTH_STARTUP_GRACE_MS = "0";
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
const { diffConditions, alsoList } = await import("../src/services/healthMonitor.js");
const { resolveParents, coveredByParent, wouldLoop } = await import("../src/services/serverParents.js");
const { nextUpdateState, parsePendingUpdates } = await import("../src/services/pendingUpdates.js");
const { serversRouter } = await import("../src/routes/servers.js");
const { agentReportRouter } = await import("../src/routes/agentReport.js");
const { hashToken } = await import("../src/services/tokens.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
const row = (id: number, name: string, extra: Partial<{ hostname: string | null; parentServerId: number | null; proxmoxNode: string | null }> = {}) => ({
id,
name,
hostname: null,
parentServerId: null,
proxmoxNode: null,
...extra,
});
describe("resolveParents", () => {
test("a link set by hand wins, and a Proxmox node that is exactly one server is found", () => {
const rows = [row(1, "pve1", { hostname: "pve1.lab.example" }), row(2, "web", { proxmoxNode: "PVE1" }), row(3, "db", { parentServerId: 2, proxmoxNode: "pve1" }), row(4, "lone")];
const parents = resolveParents(rows);
assert.deepEqual(parents.get(2), { parentId: 1, auto: true });
assert.deepEqual(parents.get(3), { parentId: 2, auto: false }, "by hand beats the node");
assert.equal(parents.get(4), undefined);
assert.equal(parents.get(1), undefined);
});
test("two servers that could be the node, or none, give no parent; a dead link is ignored", () => {
const rows = [row(1, "pve1"), row(2, "pve1", { hostname: "other" }), row(3, "web", { proxmoxNode: "pve1" }), row(4, "x", { proxmoxNode: "nothing" }), row(5, "y", { parentServerId: 99 }), row(6, "z", { parentServerId: 6 })];
const parents = resolveParents(rows);
assert.equal(parents.get(3), undefined, "ambiguous");
assert.equal(parents.get(4), undefined);
assert.equal(parents.get(5), undefined);
assert.equal(parents.get(6), undefined, "not its own parent");
});
test("a server is not its own node", () => {
assert.equal(resolveParents([row(1, "pve1", { proxmoxNode: "pve1" })]).size, 0);
});
});
describe("coveredByParent", () => {
const parents = resolveParents([row(1, "host"), row(2, "vm", { parentServerId: 1 }), row(3, "ct", { parentServerId: 2 }), row(4, "other", { parentServerId: 1 })]);
test("a guest is covered only while something above it is down, and by the topmost one", () => {
assert.deepEqual([...coveredByParent(new Set([1, 2, 3]), parents)].sort(), [[2, 1], [3, 1]]);
assert.deepEqual([...coveredByParent(new Set([2, 3]), parents)], [[3, 2]], "the host is up: the VM is the cause");
assert.deepEqual([...coveredByParent(new Set([3]), parents)], [], "nothing above it is down");
assert.deepEqual([...coveredByParent(new Set([1]), parents)], []);
assert.deepEqual([...coveredByParent(new Set([1, 3]), parents)], [[3, 1]], "an up VM in between doesn't hide the cause");
});
test("a loop covers nothing, so nothing is swallowed", () => {
const loop = resolveParents([row(1, "a", { parentServerId: 2 }), row(2, "b", { parentServerId: 1 }), row(3, "c", { parentServerId: 1 })]);
assert.equal(coveredByParent(new Set([1, 2, 3]), loop).size, 0);
});
test("wouldLoop spots a link that would close a loop", () => {
const rows = [row(1, "host"), row(2, "vm", { parentServerId: 1 })];
assert.equal(wouldLoop(rows, 1, 2), true);
assert.equal(wouldLoop(rows, 1, 1), true);
assert.equal(wouldLoop(rows, 2, 1), false);
});
});
describe("diffConditions with covered problems", () => {
const c = (key: string) => ({ key, source: "server", message: key });
const names = (list: { key: string }[]) => list.map((x) => x.key).sort();
test("a problem explained by another is recorded but not announced, and neither is its end", () => {
const first = diffConditions({}, [c("offline:server:1"), c("offline:server:2")], new Set(), new Set(), new Set(["offline:server:2"]));
assert.deepEqual(names(first.added), ["offline:server:1"]);
assert.deepEqual(names(first.newlyDown), ["offline:server:1", "offline:server:2"], "both are down, for the record");
assert.equal(first.next["offline:server:2"].covered, true);
const later = diffConditions(first.next, [c("offline:server:1")], new Set(), new Set(), new Set());
assert.deepEqual(names(later.resolved), [], "the guest's end isn't announced either");
assert.deepEqual(names(later.ended), ["offline:server:2"], "but it did end");
});
test("when the explanation goes away and the problem is still there, it is announced as new", () => {
const first = diffConditions({}, [c("offline:server:1"), c("offline:server:2")], new Set(), new Set(), new Set(["offline:server:2"]));
const hostBack = diffConditions(first.next, [c("offline:server:2")], new Set(), new Set(), new Set());
assert.deepEqual(names(hostBack.added), ["offline:server:2"]);
assert.deepEqual(names(hostBack.resolved), ["offline:server:1"]);
assert.deepEqual(names(hostBack.newlyDown), [], "it was already down; nothing began");
// and only once
const again = diffConditions(hostBack.next, [c("offline:server:2")], new Set(), new Set(), new Set());
assert.deepEqual(again.added, []);
});
test("with nothing covered it works as before", () => {
const run = diffConditions({}, [c("a"), c("b")]);
assert.deepEqual(names(run.added), ["a", "b"]);
assert.deepEqual(names(run.newlyDown), ["a", "b"]);
const gone = diffConditions(run.next, [c("a")]);
assert.deepEqual(names(gone.resolved), ["b"]);
assert.deepEqual(names(gone.ended), ["b"]);
});
test("a list of names reads as words", () => {
assert.equal(alsoList(["a"]), "a");
assert.equal(alsoList(["a", "b", "c"]), "a, b and c");
assert.equal(alsoList(["a", "b", "c", "d", "e", "f", "g"]), "a, b, c, d, e and 2 more");
});
});
describe("the alerts, folded", () => {
const hoursAgo = (n: number) => new Date(Date.now() - n * 3600_000).toISOString();
let hostId = 0;
before(async () => {
const made = await db
.insert(servers)
.values([
{ name: "pve1", apiTokenHash: "h", apiTokenPrefix: "h", lastSeenAt: hoursAgo(2) },
{ name: "web", apiTokenHash: "w", apiTokenPrefix: "w", lastSeenAt: hoursAgo(2), proxmoxNode: "pve1" },
{ name: "db", apiTokenHash: "d", apiTokenPrefix: "d", lastSeenAt: hoursAgo(2), proxmoxNode: "pve1" },
{ name: "elsewhere", apiTokenHash: "e", apiTokenPrefix: "e", lastSeenAt: hoursAgo(2) },
])
.returning({ id: servers.id });
hostId = made[0].id;
});
test("the host's alert says what it takes with it, and the guests' are held back, not counted", async () => {
const report = await getAlerts(true);
const offline = report.alerts.filter((a) => a.category === "offline");
assert.equal(offline.length, 4);
const host = offline.find((a) => a.message.startsWith("pve1"))!;
assert.deepEqual(host.affects, ["db", "web"]);
assert.equal(host.silenced, false);
const web = offline.find((a) => a.message.startsWith("web"))!;
assert.deepEqual(web.causedBy, { name: "pve1" });
assert.equal(web.silenced, true, "held back like a silenced alert");
assert.equal(offline.find((a) => a.message.startsWith("elsewhere"))!.causedBy, undefined);
assert.equal(report.counts.critical, 2, "pve1 and the unrelated one");
assert.equal(report.counts.grouped, 2);
assert.equal(report.counts.silenced, 0, "none of this is maintenance");
});
test("when the host reports again the guests that are still down stand alone", async () => {
await db.update(servers).set({ lastSeenAt: new Date().toISOString() }).where(eq(servers.id, hostId));
invalidateAlertsCache(); // a look under ten seconds old is reused otherwise
const report = await getAlerts(true);
assert.equal(report.counts.critical, 3);
assert.equal(report.counts.grouped, 0);
assert.ok(report.alerts.every((a) => !a.causedBy));
});
});
describe("what servers say is waiting to be installed", () => {
test("how long updates have been waiting carries from report to report", () => {
const t0 = "2026-10-01T00:00:00.000Z";
const t1 = "2026-10-05T00:00:00.000Z";
const t2 = "2026-10-09T00:00:00.000Z";
const t3 = "2026-10-12T00:00:00.000Z";
const first = nextUpdateState({ pendingUpdates: null, updatesPendingSince: null }, { manager: "apt", count: 4, security: 1 }, t0);
assert.equal(first.updatesPendingSince, t0);
const second = nextUpdateState(first, { manager: "apt", count: 6, security: 2, reboot_required: true }, t1);
assert.equal(second.updatesPendingSince, t0, "still waiting: the clock keeps running");
assert.deepEqual(parsePendingUpdates(second.pendingUpdates), { manager: "apt", count: 6, security: 2, rebootRequired: true, reportedAt: t1 });
const cleared = nextUpdateState(second, { manager: "apt", count: 0, security: 0 }, t2);
assert.equal(cleared.updatesPendingSince, null);
assert.equal(nextUpdateState(cleared, { manager: "apt", count: 2 }, t3).updatesPendingSince, t3, "and starts again when some appear");
assert.equal(parsePendingUpdates(nextUpdateState(cleared, { count: 2 }, t3).pendingUpdates)?.security, null, "unknown security is unknown, not zero");
});
test("garbage is no updates, not an error", () => {
assert.equal(parsePendingUpdates(null), null);
assert.equal(parsePendingUpdates("{"), null);
assert.equal(parsePendingUpdates(JSON.stringify({ count: "many" })), null);
});
});
describe("the routes", () => {
let app: Server;
let base = "";
let who = "admin";
let hostId = 0;
let guestId = 0;
before(async () => {
await db.insert(users).values([
{ oidcSub: "admin", name: "Ada", role: "admin" },
{ oidcSub: "operator", name: "Olga", role: "operator" },
]);
const [host] = await db.insert(servers).values({ name: "rack", apiTokenHash: hashToken("host-token"), apiTokenPrefix: "ht", lastSeenAt: new Date().toISOString() }).returning();
const [guest] = await db.insert(servers).values({ name: "vm1", apiTokenHash: hashToken("guest-token"), apiTokenPrefix: "gt", lastSeenAt: new Date().toISOString() }).returning();
hostId = host.id;
guestId = guest.id;
const a = express();
a.use("/api/agent/report", agentReportRouter);
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/servers", serversRouter);
app = await new Promise<Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const call = (method: string, path: string, body?: unknown) =>
fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
const report = (token: string, system: unknown) =>
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, body: JSON.stringify({ hostname: "h", os_type: "linux", system, tasks: [] }) });
test("an agent's update counts are stored, a report without them keeps what was said, and a garbled answer costs nothing", async () => {
assert.equal((await report("guest-token", { updates: { manager: "apt", count: 7, security: 2, reboot_required: true } })).status, 202);
let [s] = await db.select().from(servers).where(eq(servers.id, guestId));
assert.deepEqual(parsePendingUpdates(s.pendingUpdates), { manager: "apt", count: 7, security: 2, rebootRequired: true, reportedAt: parsePendingUpdates(s.pendingUpdates)!.reportedAt });
const since = s.updatesPendingSince;
assert.ok(since);
assert.equal((await report("guest-token", { cpu: { cores: 2 } })).status, 202);
[s] = await db.select().from(servers).where(eq(servers.id, guestId));
assert.equal(parsePendingUpdates(s.pendingUpdates)?.count, 7, "an agent that said nothing doesn't wipe it");
assert.equal((await report("guest-token", { updates: { count: "lots" }, cpu: { cores: 4 } })).status, 202, "the report itself still goes through");
[s] = await db.select().from(servers).where(eq(servers.id, guestId));
assert.equal(s.cpuCores, 4);
assert.equal(parsePendingUpdates(s.pendingUpdates)?.count, 7);
assert.equal(s.updatesPendingSince, since);
assert.equal((await report("host-token", { updates: { manager: "dnf", count: 0 } })).status, 202);
});
test("the updates list shows every server that reports, with or without figures", async () => {
await db.insert(servers).values({ name: "silent", apiTokenHash: "s", apiTokenPrefix: "s" }); // never reported
const body = await (await call("GET", "/servers/updates")).json();
const byName = new Map(body.servers.map((x: { name: string }) => [x.name, x]));
assert.ok(!byName.has("silent"));
const vm = byName.get("vm1") as { updates: { count: number; security: number; rebootRequired: boolean }; pendingSince: string };
assert.equal(vm.updates.count, 7);
assert.equal(vm.updates.rebootRequired, true);
assert.ok(vm.pendingSince);
assert.equal((byName.get("rack") as { pendingSince: string | null }).pendingSince, null, "nothing waiting");
});
test("an admin says what a server runs on; a loop or a stranger is refused; it shows on both pages", async () => {
assert.equal((await call("PATCH", `/servers/${guestId}`, { parentServerId: hostId })).status, 200);
const detail = await (await call("GET", `/servers/${guestId}/detail`)).json();
assert.deepEqual(detail.relations.parent, { id: hostId, name: "rack", auto: false });
const hostDetail = await (await call("GET", `/servers/${hostId}/detail`)).json();
assert.deepEqual(hostDetail.relations.children, [{ id: guestId, name: "vm1", auto: false }]);
assert.equal(detail.server.pendingUpdates.count, 7, "the detail shows what is waiting, as a figure rather than stored text");
const loop = await call("PATCH", `/servers/${hostId}`, { parentServerId: guestId });
assert.equal(loop.status, 400);
assert.equal((await loop.json()).error, "parent_loop");
assert.equal((await call("PATCH", `/servers/${guestId}`, { parentServerId: guestId })).status, 400);
assert.equal((await call("PATCH", `/servers/${guestId}`, { parentServerId: 99999 })).status, 400);
who = "operator";
assert.equal((await call("PATCH", `/servers/${guestId}`, { parentServerId: null })).status, 403);
who = "admin";
assert.equal((await call("PATCH", `/servers/${guestId}`, { parentServerId: null })).status, 200);
assert.equal((await (await call("GET", `/servers/${guestId}/detail`)).json()).relations.parent, null, "back to working it out");
});
test("removing the host sets the servers on it free", async () => {
await call("PATCH", `/servers/${guestId}`, { parentServerId: hostId });
assert.equal((await call("DELETE", `/servers/${hostId}`)).status, 204);
const [s] = await db.select().from(servers).where(eq(servers.id, guestId));
assert.equal(s.parentServerId, null);
});
});
+143
View File
@@ -0,0 +1,143 @@
/**
* Running a Semaphore template from the app, limited to one host, and watching how the run is getting on.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
const dir = mkdtempSync(join(tmpdir(), "hm-sem-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, integrations, auditLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { integrationsRouter } = await import("../src/routes/integrations.js");
await runMigrations();
describe("running a Semaphore template", () => {
let semaphore: http.Server;
let app: http.Server;
let base = "";
let who = "operator";
let integrationId = 0;
const received: { method: string; url: string; body: Record<string, unknown> | null; auth: string | undefined }[] = [];
before(async () => {
semaphore = await new Promise<http.Server>((resolve) => {
const s = http.createServer((req, res) => {
let raw = "";
req.on("data", (c) => (raw += c));
req.on("end", () => {
received.push({ method: req.method ?? "", url: req.url ?? "", body: raw ? JSON.parse(raw) : null, auth: req.headers.authorization });
res.setHeader("content-type", "application/json");
if (req.method === "POST" && req.url === "/api/project/3/tasks") return res.end(JSON.stringify({ id: 77, status: "waiting", created: "2026-10-17T10:00:00Z" }));
if (req.method === "GET" && req.url === "/api/project/3/tasks/77") return res.end(JSON.stringify({ id: 77, status: "running", start: "2026-10-17T10:00:05Z" }));
res.statusCode = 404;
res.end(JSON.stringify({ error: "not here" }));
});
});
s.listen(0, "127.0.0.1", () => resolve(s));
});
const url = `http://127.0.0.1:${(semaphore.address() as AddressInfo).port}`;
await db.insert(users).values([
{ oidcSub: "operator", name: "Olga", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
]);
const [row] = await db.insert(integrations).values({ type: "semaphore", name: "Semaphore", baseUrl: url, config: JSON.stringify({ url, token: "secret-token" }) }).returning();
integrationId = row.id;
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/integrations", integrationsRouter);
app = await new Promise<http.Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/integrations/${integrationId}/semaphore`;
});
after(() => {
semaphore.close();
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows keeps the database file locked until the process exits.
}
});
const run = (body?: unknown) =>
fetch(`${base}/projects/3/templates/9/run`, { method: "POST", headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
test("a run limited to a host tells Semaphore which, and who started it", async () => {
const res = await run({ limit: "elsa" });
assert.equal(res.status, 201);
assert.equal((await res.json()).task.id, 77);
const sent = received.find((r) => r.method === "POST")!;
assert.equal(sent.url, "/api/project/3/tasks");
assert.equal(sent.auth, "Bearer secret-token");
assert.equal(sent.body?.template_id, 9);
assert.equal(sent.body?.limit, "elsa");
assert.match(String(sent.body?.message), /Olga/);
assert.match(String(sent.body?.message), /limited to elsa/);
});
test("a run with no limit sends none, and the audit log records what ran and where", async () => {
received.length = 0;
assert.equal((await run()).status, 201);
assert.equal((await run({})).status, 201);
for (const r of received) {
assert.equal(r.body?.limit, undefined);
assert.doesNotMatch(String(r.body?.message), /limited/);
}
const entries = (await db.select().from(auditLog)).filter((e) => e.action === "run_template");
assert.equal(entries.length, 3);
assert.match(entries[0].detail ?? "", /"limit":"elsa"/);
assert.doesNotMatch(entries[1].detail ?? "", /limit/);
assert.ok(entries.every((e) => !(e.detail ?? "").includes("secret-token")));
});
test("anything that isn't a host or a pattern is refused before it reaches Semaphore", async () => {
received.length = 0;
for (const limit of ["--extra-vars x=1", "-e foo", "elsa; reboot", "$(id)", "a b", "", "x".repeat(201), "../etc"]) {
const res = await run({ limit });
assert.equal(res.status, limit === "" ? 201 : 400, JSON.stringify(limit).slice(0, 40));
}
// the empty one is "no limit"; nothing else was sent on
assert.equal(received.filter((r) => r.method === "POST").length, 1);
for (const ok of ["web01,db*", "elsa.lab.example", "10.0.0.5", "group:&web"]) {
if (ok.includes("&")) continue; // not a pattern this accepts
assert.equal((await run({ limit: ok })).status, 201, ok);
}
});
test("a viewer can't run anything", async () => {
who = "viewer";
received.length = 0;
assert.equal((await run({ limit: "elsa" })).status, 403);
assert.equal(received.length, 0);
who = "operator";
});
test("how a run is getting on can be asked for, and a missing run is reported", async () => {
const res = await fetch(`${base}/projects/3/tasks/77`);
assert.equal(res.status, 200);
assert.deepEqual((await res.json()).task, { id: 77, status: "running", created: null, start: "2026-10-17T10:00:05Z", end: null });
assert.equal((await fetch(`${base}/projects/3/tasks/999`)).status, 502);
assert.equal((await fetch(`${base}/projects/x/tasks/77`)).status, 400);
});
});