Dependencies beyond servers, web push, inventory auto-fill with QR labels, Windows agent health
- Dependencies: servers and integrations can depend on each other; alerts are folded under the root cause, and a failure explained by something down isn't announced - Web push: hand-written VAPID and RFC 8291 encryption, per-device subscriptions, a fifth notification channel, service worker handlers - Inventory: maker/model/serial from the agents fill blank fields, "Refresh from agent", QR label printing and ?asset= deep links - Windows agent (v4) reports failed services, disk health, Storage Spaces pools and hardware identity; Linux agent (v5) reports hardware identity Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5cc51c57de
commit
ec9c2f73f0
55 files changed
+12553
-65
No files matched your search
@@ -0,0 +1,212 @@
|
||||
/**
|
||||
* Dependencies between servers and services: what is folded under what when something is down, and the routes that say what needs what.
|
||||
*
|
||||
* 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-deps-"));
|
||||
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 express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, integrations, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { buildEdges, coveredNodes, wouldLoop, integrationCausedBy, HEALTH_STATE_FLAG } = await import("../src/services/dependencies.js");
|
||||
const { setInternalFlag } = await import("../src/services/settingsStore.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { trackIntegrationHealth } = await import("../src/services/integrationHealthMonitor.js");
|
||||
const { dependenciesRouter } = await import("../src/routes/dependencies.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const everything = () => true;
|
||||
const edge = (fromType: "server" | "integration", fromId: number, toType: "server" | "integration", toId: number) => ({ fromType, fromId, toType, toId });
|
||||
|
||||
describe("who is down because of whom", () => {
|
||||
test("a node is explained by the root of what it needs, however far up; things that are up in between don't matter", () => {
|
||||
const edges = buildEdges(new Map(), [edge("integration", 1, "server", 1), edge("integration", 2, "integration", 1), edge("server", 2, "integration", 2)], everything);
|
||||
const covered = coveredNodes(new Set(["server:1", "integration:1", "integration:2", "server:2"]), edges);
|
||||
assert.deepEqual([...covered.entries()].sort(), [["integration:1", "server:1"], ["integration:2", "server:1"], ["server:2", "server:1"]]);
|
||||
// the middle one is up: the one above it still explains the one below
|
||||
const skip = coveredNodes(new Set(["server:1", "server:2"]), edges);
|
||||
assert.deepEqual([...skip.entries()], [["server:2", "server:1"]]);
|
||||
});
|
||||
|
||||
test("the 'runs on' links of servers are part of the graph", () => {
|
||||
const edges = buildEdges(new Map([[2, { parentId: 1, auto: false }]]), [edge("integration", 1, "server", 2)], everything);
|
||||
const covered = coveredNodes(new Set(["server:1", "integration:1"]), edges);
|
||||
assert.equal(covered.get("integration:1"), "server:1");
|
||||
});
|
||||
|
||||
test("something that needs two things is explained by whichever is down; with none down it stands alone", () => {
|
||||
const edges = buildEdges(new Map(), [edge("server", 1, "server", 2), edge("server", 1, "integration", 1)], everything);
|
||||
assert.equal(coveredNodes(new Set(["server:1", "integration:1"]), edges).get("server:1"), "integration:1");
|
||||
assert.equal(coveredNodes(new Set(["server:1"]), edges).size, 0);
|
||||
});
|
||||
|
||||
test("a loop explains nothing, and edges to things that are gone are ignored", () => {
|
||||
const loop = buildEdges(new Map(), [edge("server", 1, "server", 2), edge("server", 2, "server", 1)], everything);
|
||||
assert.equal(coveredNodes(new Set(["server:1", "server:2"]), loop).size, 0);
|
||||
const gone = buildEdges(new Map(), [edge("server", 1, "server", 99)], (k) => k !== "server:99");
|
||||
assert.equal(gone.size, 0);
|
||||
});
|
||||
|
||||
test("a new dependency that would close a loop is seen, including through 'runs on'", () => {
|
||||
const edges = buildEdges(new Map([[2, { parentId: 1, auto: true }]]), [edge("integration", 1, "server", 2)], everything);
|
||||
assert.equal(wouldLoop(edges, "server:1", "integration:1"), true, "the integration needs server 2, which runs on server 1");
|
||||
assert.equal(wouldLoop(edges, "server:1", "server:1"), true);
|
||||
assert.equal(wouldLoop(edges, "integration:1", "server:1"), false, "already needs it, indirectly: not a loop");
|
||||
});
|
||||
});
|
||||
|
||||
describe("through the routes, the alerts and the notifications", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "operator";
|
||||
let box = 0;
|
||||
let other = 0;
|
||||
let gitea = 0;
|
||||
let semaphore = 0;
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([
|
||||
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
||||
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
||||
]);
|
||||
const hoursAgo = (n: number) => new Date(Date.now() - n * 3600_000).toISOString();
|
||||
const made = await db
|
||||
.insert(servers)
|
||||
.values([
|
||||
{ name: "box", apiTokenHash: "b", apiTokenPrefix: "b", lastSeenAt: hoursAgo(3) },
|
||||
{ name: "other", apiTokenHash: "o", apiTokenPrefix: "o", lastSeenAt: new Date().toISOString() },
|
||||
])
|
||||
.returning({ id: servers.id });
|
||||
box = made[0].id;
|
||||
other = made[1].id;
|
||||
const ints = await db
|
||||
.insert(integrations)
|
||||
.values([
|
||||
{ type: "gitea", name: "Gitea", baseUrl: "https://git.lab", config: "{}" },
|
||||
{ type: "semaphore", name: "Semaphore", baseUrl: "https://sem.lab", config: "{}" },
|
||||
])
|
||||
.returning({ id: integrations.id });
|
||||
gitea = ints[0].id;
|
||||
semaphore = ints[1].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/dependencies", dependenciesRouter);
|
||||
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/dependencies`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
const send = (method: string, path: string, body?: unknown) => fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
let firstId = 0;
|
||||
|
||||
test("an operator says what needs what; it is listed with names", async () => {
|
||||
const res = await send("POST", "/", edge("integration", gitea, "server", box));
|
||||
assert.equal(res.status, 201);
|
||||
firstId = (await res.json()).id;
|
||||
assert.equal((await send("POST", "/", edge("integration", semaphore, "integration", gitea))).status, 201);
|
||||
const list = await (await send("GET", "/")).json();
|
||||
assert.deepEqual(list.dependencies.map((d: { from: { name: string }; to: { name: string } }) => `${d.from.name} > ${d.to.name}`).sort(), ["Gitea > box", "Semaphore > Gitea"]);
|
||||
assert.ok(list.nodes.some((n: { key: string; name: string }) => n.key === `server:${box}` && n.name === "box"));
|
||||
});
|
||||
|
||||
test("it can't be nonsense: not itself, not twice, not a loop, not something that isn't there, not by a viewer", async () => {
|
||||
assert.equal((await send("POST", "/", edge("server", box, "server", box))).status, 400);
|
||||
assert.equal((await send("POST", "/", edge("integration", gitea, "server", box))).status, 409, "twice");
|
||||
const loop = await send("POST", "/", edge("server", box, "integration", semaphore));
|
||||
assert.equal(loop.status, 409, "the server would wait for the thing that waits for it");
|
||||
assert.match((await loop.json()).message, /wait for each other/);
|
||||
assert.equal((await send("POST", "/", edge("server", 99999, "server", box))).status, 400);
|
||||
assert.equal((await send("POST", "/", { fromType: "dns_provider", fromId: 1, toType: "server", toId: box })).status, 400);
|
||||
who = "viewer";
|
||||
assert.equal((await send("POST", "/", edge("server", other, "server", box))).status, 403);
|
||||
assert.equal((await send("GET", "/")).status, 200, "everyone signed in can look");
|
||||
who = "operator";
|
||||
});
|
||||
|
||||
test("when the server is down and both services are failing, only the server is announced; the services are held back under it", async () => {
|
||||
await trackIntegrationHealth("gitea", false);
|
||||
await trackIntegrationHealth("semaphore", false);
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const offline = report.alerts.find((a) => a.category === "offline" && a.message.startsWith("box"))!;
|
||||
assert.equal(offline.silenced, false);
|
||||
assert.deepEqual(offline.affects, ["Gitea", "Semaphore"]);
|
||||
const failing = report.alerts.filter((a) => a.category === "integration");
|
||||
assert.equal(failing.length, 2);
|
||||
for (const f of failing) {
|
||||
assert.deepEqual(f.causedBy, { name: "box" });
|
||||
assert.equal(f.silenced, true);
|
||||
}
|
||||
assert.equal(failing.find((f) => f.id.includes("gitea"))!.affects, undefined, "only the root says what it takes with it");
|
||||
assert.equal(report.counts.critical + report.counts.warning, 1);
|
||||
assert.equal(report.counts.grouped, 2);
|
||||
});
|
||||
|
||||
test("when the server is back, Gitea stands alone and Semaphore is held back under Gitea", async () => {
|
||||
await db.update(servers).set({ lastSeenAt: new Date().toISOString() }).where(eq(servers.id, box));
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const failing = report.alerts.filter((a) => a.category === "integration");
|
||||
assert.equal(failing.find((f) => f.id.includes("gitea"))!.causedBy, undefined);
|
||||
assert.deepEqual(failing.find((f) => f.id.includes("semaphore"))!.causedBy, { name: "Gitea" });
|
||||
});
|
||||
|
||||
test("a kind of service is folded only if every enabled instance of it is explained", async () => {
|
||||
const [second] = await db.insert(integrations).values({ type: "gitea", name: "Gitea 2", baseUrl: "https://git2.lab", config: "{}" }).returning({ id: integrations.id });
|
||||
await db.update(servers).set({ lastSeenAt: new Date(Date.now() - 3 * 3600_000).toISOString() }).where(eq(servers.id, box));
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
assert.equal(report.alerts.find((a) => a.category === "integration" && a.id.includes("gitea"))!.causedBy, undefined, "the second one doesn't need the box");
|
||||
await db.delete(integrations).where(eq(integrations.id, second.id));
|
||||
});
|
||||
|
||||
test("a failure that crosses its threshold while the server it needs is down isn't announced; one that doesn't need it is", async () => {
|
||||
assert.equal(await integrationCausedBy("gitea", ["gitea"]), null, "nothing is known to be down yet");
|
||||
await setInternalFlag(HEALTH_STATE_FLAG, JSON.stringify({ [`offline:server:${box}`]: { message: "box is offline", source: "server" } }));
|
||||
assert.deepEqual(await integrationCausedBy("gitea", ["gitea"]), { key: `server:${box}`, name: "box" });
|
||||
assert.deepEqual(await integrationCausedBy("semaphore", ["gitea", "semaphore"]), { key: `server:${box}`, name: "box" }, "through Gitea");
|
||||
assert.equal(await integrationCausedBy("tailscale", ["tailscale"]), null, "an unrelated kind");
|
||||
await setInternalFlag(HEALTH_STATE_FLAG, JSON.stringify({ [`offline:server:${other}`]: { message: "other is offline", source: "server" } }));
|
||||
assert.equal(await integrationCausedBy("gitea", ["gitea"]), null, "a different server being down explains nothing");
|
||||
});
|
||||
|
||||
test("removing one works, is audited, and the services stand alone again", async () => {
|
||||
assert.equal((await send("DELETE", `/${firstId}`)).status, 204);
|
||||
assert.equal((await send("DELETE", `/${firstId}`)).status, 404);
|
||||
const actions = (await db.select().from(auditLog)).filter((e) => e.category === "dependency").map((e) => e.action);
|
||||
assert.deepEqual(actions.sort(), ["create", "create", "delete"]);
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
assert.equal(report.alerts.find((a) => a.category === "integration" && a.id.includes("gitea"))!.causedBy, undefined);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* Filling in the inventory from what an agent says the machine is: only blanks are filled, a refresh overwrites on purpose, and an agent's
|
||||
* report fills the entries of the machine it came from.
|
||||
*
|
||||
* 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-invfill-"));
|
||||
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 express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, assets, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { inventoryRouter } = await import("../src/routes/inventory.js");
|
||||
const { agentReportRouter } = await import("../src/routes/agentReport.js");
|
||||
const { hashToken } = await import("../src/services/tokens.js");
|
||||
const { blanksToFill, differences, hardwareOf } = await import("../src/services/inventoryAutofill.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
describe("what would be filled, and what would change", () => {
|
||||
const hw = { manufacturer: "Dell Inc.", model: "PowerEdge R730", serial: "ABC123" };
|
||||
|
||||
test("only blank fields are filled; what a person wrote stays", () => {
|
||||
assert.deepEqual(blanksToFill({ manufacturer: null, model: "R730 (mine)", serial: " " }, hw), { manufacturer: "Dell Inc.", serial: "ABC123" });
|
||||
assert.deepEqual(blanksToFill({ manufacturer: "x", model: "y", serial: "z" }, hw), {});
|
||||
assert.deepEqual(blanksToFill({ manufacturer: null, model: null, serial: null }, null), {});
|
||||
assert.deepEqual(blanksToFill({ manufacturer: null, model: null, serial: null }, { manufacturer: "Dell", model: null, serial: null }), { manufacturer: "Dell" });
|
||||
});
|
||||
|
||||
test("a refresh would change the fields where the agent says something else, and nothing that already matches", () => {
|
||||
assert.deepEqual(differences({ manufacturer: "Dell Inc.", model: "R730", serial: null }, hw), { model: "PowerEdge R730", serial: "ABC123" });
|
||||
assert.deepEqual(differences(hw, hw), {});
|
||||
assert.deepEqual(differences({ manufacturer: "Dell Inc. ", model: "PowerEdge R730", serial: "ABC123" }, hw), {}, "stray spaces aren't a difference");
|
||||
assert.deepEqual(differences({ manufacturer: "x", model: "y", serial: "z" }, { manufacturer: null, model: null, serial: null }), {}, "an agent that says nothing changes nothing");
|
||||
});
|
||||
|
||||
test("a server that has said nothing has no hardware", () => {
|
||||
assert.equal(hardwareOf(null), null);
|
||||
assert.equal(hardwareOf({ hwManufacturer: null, hwModel: null, hwSerial: null }), null);
|
||||
assert.deepEqual(hardwareOf({ hwManufacturer: "A", hwModel: null, hwSerial: null }), { manufacturer: "A", model: null, serial: null });
|
||||
});
|
||||
});
|
||||
|
||||
describe("through the routes and the agent's report", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let id = 0;
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "operator", name: "Olle", role: "operator" }]);
|
||||
const [s] = await db.insert(servers).values({ name: "elsa", apiTokenHash: hashToken("tok"), apiTokenPrefix: "t" }).returning();
|
||||
id = s.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: "operator" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/inventory", inventoryRouter);
|
||||
app = await new Promise<Server>((resolve) => {
|
||||
const x = a.listen(0, "127.0.0.1", () => resolve(x));
|
||||
});
|
||||
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 report = (hardware: unknown) =>
|
||||
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer tok" }, body: JSON.stringify({ hostname: "h", os_type: "linux", tasks: [], system: { hardware } }) });
|
||||
const send = (method: string, path: string, body?: unknown) =>
|
||||
fetch(`${base}/inventory${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
const entry = async (assetId: number) => (await db.select().from(assets).where(eq(assets.id, assetId)))[0];
|
||||
|
||||
let linked = 0;
|
||||
let unlinked = 0;
|
||||
|
||||
test("an entry made before the agent has said anything stays blank, then an agent's report fills it", async () => {
|
||||
linked = (await (await send("POST", "/", { name: "elsa's box", kind: "server", serverId: id, model: "my own words" })).json()).asset.id;
|
||||
unlinked = (await (await send("POST", "/", { name: "a switch", kind: "network" })).json()).asset.id;
|
||||
assert.deepEqual([(await entry(linked)).manufacturer, (await entry(linked)).serial], [null, null]);
|
||||
|
||||
assert.equal((await report({ manufacturer: "Dell Inc.", model: "PowerEdge R730", serial: "ABC123" })).status, 202);
|
||||
const row = await entry(linked);
|
||||
assert.deepEqual([row.manufacturer, row.model, row.serial], ["Dell Inc.", "my own words", "ABC123"], "blanks filled, the model a person wrote is kept");
|
||||
const [s] = await db.select().from(servers).where(eq(servers.id, id));
|
||||
assert.deepEqual([s.hwManufacturer, s.hwModel, s.hwSerial], ["Dell Inc.", "PowerEdge R730", "ABC123"]);
|
||||
assert.equal((await entry(unlinked)).manufacturer, null, "an entry that isn't this machine is left alone");
|
||||
});
|
||||
|
||||
test("the list shows what the agent says and what a refresh would change", async () => {
|
||||
const list = (await (await send("GET", "/")).json()).assets;
|
||||
const a = list.find((x: { id: number }) => x.id === linked);
|
||||
assert.deepEqual(a.agent, { manufacturer: "Dell Inc.", model: "PowerEdge R730", serial: "ABC123", differs: { model: "PowerEdge R730" } });
|
||||
assert.equal(list.find((x: { id: number }) => x.id === unlinked).agent, null);
|
||||
});
|
||||
|
||||
test("a new entry linked to a machine that has already reported starts out filled in", async () => {
|
||||
const made = (await (await send("POST", "/", { name: "elsa again", kind: "server", serverId: id })).json()).asset;
|
||||
assert.deepEqual([made.manufacturer, made.model, made.serial], ["Dell Inc.", "PowerEdge R730", "ABC123"]);
|
||||
const edited = (await (await send("PUT", `/${unlinked}`, { name: "a switch", kind: "network", serverId: id })).json()).asset;
|
||||
assert.equal(edited.serial, "ABC123", "linking an existing entry fills its blanks too");
|
||||
});
|
||||
|
||||
test("a report with nothing in it, or garbage, changes nothing", async () => {
|
||||
assert.equal((await report({ manufacturer: " " })).status, 202);
|
||||
assert.equal((await report("nonsense")).status, 202);
|
||||
const [s] = await db.select().from(servers).where(eq(servers.id, id));
|
||||
assert.equal(s.hwSerial, "ABC123");
|
||||
});
|
||||
|
||||
test("refreshing from the agent overwrites on purpose, says what changed, and is in the audit log", async () => {
|
||||
const res = await send("POST", `/${linked}/refresh-from-agent`);
|
||||
assert.equal(res.status, 200);
|
||||
const body = await res.json();
|
||||
assert.deepEqual(body.changed, ["model"]);
|
||||
assert.equal(body.asset.model, "PowerEdge R730");
|
||||
const again = await (await send("POST", `/${linked}/refresh-from-agent`)).json();
|
||||
assert.deepEqual(again.changed, [], "nothing left to change");
|
||||
const log = (await db.select().from(auditLog)).filter((e) => e.action === "refresh_from_agent");
|
||||
assert.equal(log.length, 1, "only a refresh that changed something is recorded");
|
||||
});
|
||||
|
||||
test("an entry without a server, or one that isn't there, can't be refreshed", async () => {
|
||||
const lone = (await (await send("POST", "/", { name: "a shelf", kind: "other" })).json()).asset.id;
|
||||
assert.equal((await send("POST", `/${lone}/refresh-from-agent`)).status, 400);
|
||||
assert.equal((await send("POST", "/99999/refresh-from-agent")).status, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,292 @@
|
||||
/**
|
||||
* Web Push: the encryption against the standard's own worked example, the signed token, the routes, and a message all the way to a stand-in push
|
||||
* service and back out through a stand-in browser.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createDecipheriv, createECDH, createPublicKey, hkdfSync, verify } from "node:crypto";
|
||||
import { createServer, type IncomingMessage, type Server } 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-push-"));
|
||||
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 express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, pushSubscriptions, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { encryptPayload, vapidAuthorization, getVapidKeys, checkEndpoint, allowLocalPushEndpointsForTests, sendPush } = await import("../src/services/webPush.js");
|
||||
const { updateSettings } = await import("../src/services/settingsStore.js");
|
||||
const { notifyNow, enabledChannelCount } = await import("../src/services/notify.js");
|
||||
const { pushRouter } = await import("../src/routes/push.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const b64u = (s: string) => Buffer.from(s, "base64url");
|
||||
|
||||
/** What a browser does with a push it receives (RFC 8291 §3.4), to read what we wrote. */
|
||||
function decrypt(body: Buffer, uaPrivate: Buffer, uaPublic: Buffer, authSecret: Buffer): Buffer {
|
||||
const salt = body.subarray(0, 16);
|
||||
const idlen = body[20];
|
||||
const asPublic = body.subarray(21, 21 + idlen);
|
||||
const ciphertext = body.subarray(21 + idlen);
|
||||
const ecdh = createECDH("prime256v1");
|
||||
ecdh.setPrivateKey(uaPrivate);
|
||||
const secret = ecdh.computeSecret(asPublic);
|
||||
const ikm = Buffer.from(hkdfSync("sha256", secret, authSecret, Buffer.concat([Buffer.from("WebPush: info\0"), uaPublic, asPublic]), 32));
|
||||
const cek = Buffer.from(hkdfSync("sha256", ikm, salt, Buffer.from("Content-Encoding: aes128gcm\0"), 16));
|
||||
const nonce = Buffer.from(hkdfSync("sha256", ikm, salt, Buffer.from("Content-Encoding: nonce\0"), 12));
|
||||
const d = createDecipheriv("aes-128-gcm", cek, nonce);
|
||||
d.setAuthTag(ciphertext.subarray(ciphertext.length - 16));
|
||||
const plain = Buffer.concat([d.update(ciphertext.subarray(0, ciphertext.length - 16)), d.final()]);
|
||||
assert.equal(plain[plain.length - 1], 2, "ends with the last-record marker");
|
||||
return plain.subarray(0, plain.length - 1);
|
||||
}
|
||||
|
||||
/** A stand-in browser: its keys, in the shape a subscription carries them. */
|
||||
function browser() {
|
||||
const ecdh = createECDH("prime256v1");
|
||||
ecdh.generateKeys();
|
||||
const auth = Buffer.from(Array.from({ length: 16 }, (_, i) => i * 7 + 3));
|
||||
return { ecdh, priv: ecdh.getPrivateKey(), pub: ecdh.getPublicKey(), auth, keys: { p256dh: ecdh.getPublicKey().toString("base64url"), auth: auth.toString("base64url") } };
|
||||
}
|
||||
|
||||
describe("the encryption", () => {
|
||||
test("it reproduces the worked example in RFC 8291, byte for byte", () => {
|
||||
const asPrivate = b64u("yfWPiYE-n46HLnH0KqZOF1fJJU3MYrct3AELtAQ-oRw");
|
||||
const ephemeral = createECDH("prime256v1");
|
||||
ephemeral.setPrivateKey(asPrivate);
|
||||
assert.equal(ephemeral.getPublicKey().toString("base64url"), "BP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A8");
|
||||
const body = encryptPayload(
|
||||
Buffer.from("When I grow up, I want to be a watermelon"),
|
||||
{ p256dh: "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4", auth: "BTBZMqHH6r4Tts7J_aSIgg" },
|
||||
{ ephemeral, salt: b64u("DGv6ra1nlYgDCS1FRnbzlw") },
|
||||
);
|
||||
assert.equal(
|
||||
body.toString("base64url"),
|
||||
"DGv6ra1nlYgDCS1FRnbzlwAAEABBBP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A_yl95bQpu6cVPTpK4Mqgkf1CXztLVBSt2Ks3oZwbuwXPXLWyouBWLVWGNWQexSgSxsj_Qulcy4a-fN",
|
||||
);
|
||||
});
|
||||
|
||||
test("the browser it is for can read it, and it is different every time", () => {
|
||||
const b = browser();
|
||||
const one = encryptPayload(Buffer.from("hello"), b.keys);
|
||||
const two = encryptPayload(Buffer.from("hello"), b.keys);
|
||||
assert.notDeepEqual(one, two, "a fresh key and salt each time");
|
||||
assert.equal(decrypt(one, b.priv, b.pub, b.auth).toString(), "hello");
|
||||
const other = browser();
|
||||
assert.throws(() => decrypt(one, other.priv, other.pub, other.auth), "someone else's browser can't");
|
||||
});
|
||||
|
||||
test("keys that aren't a browser's, or a message too long for one push, are refused", () => {
|
||||
const b = browser();
|
||||
assert.throws(() => encryptPayload(Buffer.from("x"), { p256dh: "AAAA", auth: b.keys.auth }), /P-256/);
|
||||
assert.throws(() => encryptPayload(Buffer.from("x"), { p256dh: b.keys.p256dh, auth: "AAAA" }), /16 bytes/);
|
||||
assert.throws(() => encryptPayload(Buffer.alloc(4100), b.keys), /too long/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the signed token", () => {
|
||||
test("it names who it is for, who sent it and when it ends, and the signature checks out against the public key", async () => {
|
||||
const keys = await getVapidKeys();
|
||||
const header = vapidAuthorization("https://push.example", "mailto:me@example.com", keys, 1_700_000_000_000);
|
||||
const m = /^vapid t=([\w-]+)\.([\w-]+)\.([\w-]+), k=([\w-]+)$/.exec(header)!;
|
||||
assert.ok(m, header);
|
||||
assert.equal(m[4], keys.publicKey);
|
||||
assert.deepEqual(JSON.parse(Buffer.from(m[1], "base64url").toString()), { typ: "JWT", alg: "ES256" });
|
||||
assert.deepEqual(JSON.parse(Buffer.from(m[2], "base64url").toString()), { aud: "https://push.example", exp: 1_700_000_000 + 12 * 3600, sub: "mailto:me@example.com" });
|
||||
const pub = b64u(keys.publicKey);
|
||||
const key = createPublicKey({ key: { kty: "EC", crv: "P-256", x: pub.subarray(1, 33).toString("base64url"), y: pub.subarray(33, 65).toString("base64url") }, format: "jwk" });
|
||||
assert.equal(verify("sha256", Buffer.from(`${m[1]}.${m[2]}`), { key, dsaEncoding: "ieee-p1363" }, b64u(m[3])), true);
|
||||
assert.equal(b64u(m[3]).length, 64, "the signature is the plain r and s, as JWT wants");
|
||||
});
|
||||
|
||||
test("the server's key pair is kept: the same one comes back", async () => {
|
||||
const a = await getVapidKeys();
|
||||
const b = await getVapidKeys();
|
||||
assert.deepEqual(a, b);
|
||||
assert.equal(b64u(a.publicKey).length, 65);
|
||||
assert.equal(b64u(a.privateKey).length, 32);
|
||||
});
|
||||
});
|
||||
|
||||
describe("which addresses are push services", () => {
|
||||
test("https only, no login, and never an address on a private network", async () => {
|
||||
await assert.rejects(checkEndpoint("http://fcm.googleapis.com/x"), /https/);
|
||||
await assert.rejects(checkEndpoint("https://user:pw@fcm.googleapis.com/x"), /login/);
|
||||
await assert.rejects(checkEndpoint("not a url"), /web address/);
|
||||
await assert.rejects(checkEndpoint("https://127.0.0.1/x"));
|
||||
await assert.rejects(checkEndpoint("https://192.168.1.10/x"));
|
||||
await assert.rejects(checkEndpoint("https://169.254.169.254/latest"));
|
||||
await assert.rejects(checkEndpoint("https://localhost/x"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("through the routes, the channel and a push service", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "ada";
|
||||
let pushService: Server;
|
||||
let pushBase = "";
|
||||
const received: { path: string; headers: IncomingMessage["headers"]; body: Buffer }[] = [];
|
||||
let respondWith = 201;
|
||||
let ada = 0;
|
||||
|
||||
before(async () => {
|
||||
allowLocalPushEndpointsForTests(true);
|
||||
const made = await db
|
||||
.insert(users)
|
||||
.values([
|
||||
{ oidcSub: "ada", name: "Ada", role: "admin" },
|
||||
{ oidcSub: "vic", name: "Vic", role: "viewer" },
|
||||
])
|
||||
.returning({ id: users.id });
|
||||
ada = made[0].id;
|
||||
pushService = createServer((req, res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (c) => chunks.push(c));
|
||||
req.on("end", () => {
|
||||
received.push({ path: req.url ?? "", headers: req.headers, body: Buffer.concat(chunks) });
|
||||
res.statusCode = respondWith;
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
await new Promise<void>((r) => pushService.listen(0, "127.0.0.1", r));
|
||||
pushBase = `http://127.0.0.1:${(pushService.address() as AddressInfo).port}`;
|
||||
|
||||
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/push", pushRouter);
|
||||
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/push`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
allowLocalPushEndpointsForTests(false);
|
||||
app.close();
|
||||
pushService.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
const send = (method: string, path: string, body?: unknown) => fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
const mine = browser();
|
||||
const vics = browser();
|
||||
let adaSubId = 0;
|
||||
|
||||
test("while push is off nobody can subscribe, and there is no key to subscribe with", async () => {
|
||||
assert.equal((await send("GET", "/key")).status, 409);
|
||||
assert.equal((await send("POST", "/subscribe", { endpoint: `${pushBase}/a`, keys: mine.keys })).status, 409);
|
||||
assert.equal(await enabledChannelCount(), 0);
|
||||
});
|
||||
|
||||
test("with it on, a browser gets the key and subscribes; the same browser again is the same subscription", async () => {
|
||||
await updateSettings({ webPush: { enabled: true, contact: "me@example.com" } });
|
||||
const key = await (await send("GET", "/key")).json();
|
||||
assert.equal(key.publicKey, (await getVapidKeys()).publicKey);
|
||||
const res = await send("POST", "/subscribe", { endpoint: `${pushBase}/ada-phone`, keys: mine.keys, label: "Ada's phone" });
|
||||
assert.equal(res.status, 201);
|
||||
adaSubId = (await res.json()).id;
|
||||
assert.equal((await send("POST", "/subscribe", { endpoint: `${pushBase}/ada-phone`, keys: mine.keys, label: "Ada's phone" })).status, 201);
|
||||
assert.equal((await db.select().from(pushSubscriptions)).length, 1);
|
||||
const list = await (await send("GET", "/mine")).json();
|
||||
assert.equal(list.subscriptions.length, 1);
|
||||
assert.equal(list.subscriptions[0].label, "Ada's phone");
|
||||
assert.equal((await db.select().from(auditLog)).filter((e) => e.category === "push" && e.action === "subscribe").length, 1, "only the first time");
|
||||
});
|
||||
|
||||
test("a subscription that isn't one is refused", async () => {
|
||||
assert.equal((await send("POST", "/subscribe", { endpoint: "nonsense", keys: mine.keys })).status, 400);
|
||||
assert.equal((await send("POST", "/subscribe", { endpoint: `${pushBase}/x`, keys: { p256dh: "short", auth: "short" } })).status, 400);
|
||||
allowLocalPushEndpointsForTests(false);
|
||||
const refused = await send("POST", "/subscribe", { endpoint: "https://10.0.0.5/push", keys: mine.keys });
|
||||
allowLocalPushEndpointsForTests(true);
|
||||
assert.equal(refused.status, 400, "a private address is never a push service");
|
||||
});
|
||||
|
||||
test("a message through the channels is encrypted for the browser, signed, and addressed to the push service", async () => {
|
||||
received.length = 0;
|
||||
assert.equal(await enabledChannelCount(), 1, "push counts as a channel once someone is subscribed");
|
||||
await notifyNow("Server down", "elsa has not reported for 3 hours");
|
||||
assert.equal(received.length, 1);
|
||||
const r = received[0];
|
||||
assert.equal(r.path, "/ada-phone");
|
||||
assert.equal(r.headers["content-encoding"], "aes128gcm");
|
||||
assert.equal(r.headers.ttl, "86400");
|
||||
assert.match(String(r.headers.authorization), /^vapid t=[\w-]+\.[\w-]+\.[\w-]+, k=[\w-]+$/);
|
||||
const claims = JSON.parse(Buffer.from(String(r.headers.authorization).split(".")[1], "base64url").toString());
|
||||
assert.equal(claims.aud, pushBase);
|
||||
assert.equal(claims.sub, "mailto:me@example.com");
|
||||
const message = JSON.parse(decrypt(r.body, mine.priv, mine.pub, mine.auth).toString());
|
||||
assert.deepEqual(message, { title: "Server down", body: "elsa has not reported for 3 hours", url: "/alerts" });
|
||||
assert.notEqual((await db.select().from(pushSubscriptions))[0].lastSuccessAt, null);
|
||||
});
|
||||
|
||||
test("with push switched off nothing is sent", async () => {
|
||||
await updateSettings({ webPush: { enabled: false } });
|
||||
received.length = 0;
|
||||
await notifyNow("Server down", "x");
|
||||
assert.equal(received.length, 0);
|
||||
await updateSettings({ webPush: { enabled: true } });
|
||||
});
|
||||
|
||||
test("a push service that says the browser is gone has that subscription removed; one that is merely failing keeps it", async () => {
|
||||
const rows = await db.select().from(pushSubscriptions);
|
||||
respondWith = 500;
|
||||
assert.deepEqual(await sendPush({ title: "t", body: "b" }, rows), { sent: 0, removed: 0, failed: 1 });
|
||||
assert.equal((await db.select().from(pushSubscriptions)).length, 1);
|
||||
respondWith = 410;
|
||||
assert.deepEqual(await sendPush({ title: "t", body: "b" }, rows), { sent: 0, removed: 1, failed: 0 });
|
||||
assert.equal((await db.select().from(pushSubscriptions)).length, 0);
|
||||
respondWith = 201;
|
||||
});
|
||||
|
||||
test("the test button writes to this person's own browsers only", async () => {
|
||||
assert.equal((await send("POST", "/test")).status, 400, "nothing subscribed now");
|
||||
await send("POST", "/subscribe", { endpoint: `${pushBase}/ada-phone`, keys: mine.keys });
|
||||
who = "vic";
|
||||
await send("POST", "/subscribe", { endpoint: `${pushBase}/vic-laptop`, keys: vics.keys });
|
||||
received.length = 0;
|
||||
const res = await send("POST", "/test");
|
||||
assert.deepEqual(await res.json(), { sent: 1, removed: 0, failed: 0 });
|
||||
assert.deepEqual(received.map((r) => r.path), ["/vic-laptop"]);
|
||||
assert.equal(JSON.parse(decrypt(received[0].body, vics.priv, vics.pub, vics.auth).toString()).title, "Homelab Manager — Test");
|
||||
});
|
||||
|
||||
test("a person removes their own browsers, not someone else's; an administrator can remove anyone's", async () => {
|
||||
const all = await db.select().from(pushSubscriptions);
|
||||
const adas = all.find((s) => s.userId === ada)!;
|
||||
const vicsRow = all.find((s) => s.userId !== ada)!;
|
||||
who = "vic";
|
||||
assert.equal((await send("DELETE", `/${adas.id}`)).status, 404, "not found, rather than 'not yours'");
|
||||
assert.equal((await send("DELETE", `/${vicsRow.id}`)).status, 204);
|
||||
who = "ada";
|
||||
await send("POST", "/subscribe", { endpoint: `${pushBase}/vic-laptop`, keys: vics.keys });
|
||||
who = "vic";
|
||||
assert.equal((await send("POST", "/subscribe", { endpoint: `${pushBase}/vic-laptop`, keys: vics.keys })).status, 201, "taking a browser over makes it theirs");
|
||||
who = "ada";
|
||||
const taken = (await db.select().from(pushSubscriptions)).find((s) => s.endpoint.endsWith("/vic-laptop"))!;
|
||||
assert.equal((await send("DELETE", `/${taken.id}`)).status, 204, "an administrator may");
|
||||
assert.ok(adaSubId > 0);
|
||||
assert.equal((await db.select().from(auditLog)).filter((e) => e.category === "push" && e.action === "unsubscribe").length, 2);
|
||||
assert.equal((await db.select().from(pushSubscriptions).where(eq(pushSubscriptions.userId, ada))).length, 1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* What the Windows agent says about the machine: services that have failed, each disk's health, Storage Spaces pools and what the machine
|
||||
* is. The mapping from what Windows returns is tested with stand-in objects, and the real collectors are run once as a smoke test.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
const script = join(repoRoot, "agent", "windows", "report-tasks.ps1");
|
||||
const probe = process.platform === "win32" ? spawnSync("pwsh", ["-NoProfile", "-Command", "$PSVersionTable.PSVersion.Major"], { encoding: "utf8" }) : null;
|
||||
const havePwsh = probe?.status === 0;
|
||||
|
||||
/** Runs PowerShell with the agent script loaded (without running it) and returns what the snippet printed as JSON. */
|
||||
function run(snippet: string): unknown {
|
||||
const work = mkdtempSync(join(tmpdir(), "hm-winhealth-"));
|
||||
try {
|
||||
const file = join(work, "t.ps1");
|
||||
writeFileSync(file, `$ErrorActionPreference = 'Stop'\n. '${script.replace(/'/g, "''")}'\n${snippet}\n`);
|
||||
const r = spawnSync("pwsh", ["-NoProfile", "-NonInteractive", "-File", file], { encoding: "utf8" });
|
||||
assert.equal(r.stderr.trim(), "", `PowerShell said:\n${r.stderr}\n${r.stdout}`);
|
||||
return JSON.parse(r.stdout.trim());
|
||||
} finally {
|
||||
rmSync(work, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
describe("the Windows agent's health parts", { skip: !havePwsh }, () => {
|
||||
test("it says it is version 4, and stays plain ASCII", () => {
|
||||
const text = readFileSync(script, "utf8");
|
||||
assert.match(text, /^\$script:AgentVersion = 4\s*$/m);
|
||||
assert.ok(!/[^\x00-\x7f]/.test(text));
|
||||
});
|
||||
|
||||
test("a service has failed when it starts by itself, isn't running, and stopped with an error — not for exit 0, never-started, or on request", () => {
|
||||
const out = run(`
|
||||
function svc($n, $d, $mode, $state, $code) { [pscustomobject]@{ Name = $n; DisplayName = $d; StartMode = $mode; State = $state; ExitCode = $code } }
|
||||
$list = @(
|
||||
(svc 'Crashed' 'A crashed service' 'Auto' 'Stopped' 1066),
|
||||
(svc 'Crashed2' 'Another' 'Auto' 'Stopped' 2),
|
||||
(svc 'Finished' 'Did its job' 'Auto' 'Stopped' 0),
|
||||
(svc 'Triggered' 'Starts on a trigger' 'Auto' 'Stopped' 1077),
|
||||
(svc 'OnRequest' 'Manual one' 'Manual' 'Stopped' 1066),
|
||||
(svc 'Disabled' 'Disabled one' 'Disabled' 'Stopped' 1066),
|
||||
(svc 'Fine' 'Running' 'Auto' 'Running' 0),
|
||||
(svc 'Starting' 'On its way' 'Auto' 'Start Pending' 1066),
|
||||
(svc 'gupdate' 'Google Update' 'Auto' 'Stopped' 1),
|
||||
(svc 'NoCode' 'No code at all' 'Auto' 'Stopped' $null)
|
||||
)
|
||||
ConvertTo-Json -InputObject @{ all = @(ConvertTo-FailedUnits $list); ignoring = @(ConvertTo-FailedUnits $list @('gupdate*', 'Crashed2')); none = @(ConvertTo-FailedUnits @()) } -Compress
|
||||
`) as { all: { unit: string; description: string }[]; ignoring: { unit: string }[]; none: unknown[] };
|
||||
assert.deepEqual(out.all.map((u) => u.unit), ["Crashed", "Crashed2", "gupdate"]);
|
||||
assert.equal(out.all[0].description, "A crashed service");
|
||||
assert.deepEqual(out.ignoring.map((u) => u.unit), ["Crashed"], "names and patterns in the ignore list are left out");
|
||||
assert.deepEqual(out.none, []);
|
||||
});
|
||||
|
||||
test("a disk passes when Windows says Healthy; Warning and Unhealthy don't; Unknown says nothing; temperature and wear come from the counter where sane", () => {
|
||||
const out = run(`
|
||||
function disk($id, $name, $health, $bus = 'SATA') { [pscustomobject]@{ DeviceId = $id; FriendlyName = $name; HealthStatus = $health; BusType = $bus } }
|
||||
$disks = @(
|
||||
(disk '0' ' Samsung SSD 850 ' 'Healthy'),
|
||||
(disk '1' 'Old HDD' 'Warning'),
|
||||
(disk '2' 'Dead HDD' 'Unhealthy'),
|
||||
(disk '3' 'Mystery' 'Unknown'),
|
||||
(disk '4' 'NVMe' 'Healthy' 'NVMe'),
|
||||
(disk '5' 'Virtual' 'Healthy' 'File Backed Virtual'),
|
||||
(disk '6' 'Odd readings' 'Healthy')
|
||||
)
|
||||
$counters = @{
|
||||
'0' = [pscustomobject]@{ Temperature = 36; Wear = 7 }
|
||||
'4' = [pscustomobject]@{ Temperature = 0; Wear = 0 }
|
||||
'6' = [pscustomobject]@{ Temperature = 4000; Wear = 900 }
|
||||
}
|
||||
ConvertTo-Json -InputObject @(ConvertTo-DiskHealth $disks $counters) -Compress
|
||||
`) as { device: string; model: string; healthy: boolean | null; temperature_c: number | null; wear_percent: number | null }[];
|
||||
const byDevice = Object.fromEntries(out.map((d) => [d.device, d]));
|
||||
assert.deepEqual(Object.keys(byDevice), ["PhysicalDisk0", "PhysicalDisk1", "PhysicalDisk2", "PhysicalDisk3", "PhysicalDisk4", "PhysicalDisk6"], "the virtual disk is left out");
|
||||
assert.deepEqual([byDevice.PhysicalDisk0.healthy, byDevice.PhysicalDisk1.healthy, byDevice.PhysicalDisk2.healthy, byDevice.PhysicalDisk3.healthy], [true, false, false, null]);
|
||||
assert.equal(byDevice.PhysicalDisk0.model, "Samsung SSD 850", "names are trimmed");
|
||||
assert.deepEqual([byDevice.PhysicalDisk0.temperature_c, byDevice.PhysicalDisk0.wear_percent], [36, 7]);
|
||||
assert.deepEqual([byDevice.PhysicalDisk4.temperature_c, byDevice.PhysicalDisk4.wear_percent], [null, 0], "0 degrees isn't a reading, 0% wear is");
|
||||
assert.deepEqual([byDevice.PhysicalDisk6.temperature_c, byDevice.PhysicalDisk6.wear_percent], [null, null], "nonsense is no reading");
|
||||
assert.deepEqual([byDevice.PhysicalDisk1.temperature_c, byDevice.PhysicalDisk1.wear_percent], [null, null], "a disk with no counter has none");
|
||||
});
|
||||
|
||||
test("Storage Spaces pools are reported in ZFS's words, with how full they are; one Windows can't judge is left out", () => {
|
||||
const out = run(`
|
||||
function pool($n, $h, $size, $alloc) { [pscustomobject]@{ FriendlyName = $n; HealthStatus = $h; Size = $size; AllocatedSize = $alloc } }
|
||||
ConvertTo-Json -InputObject @(ConvertTo-PoolHealth @(
|
||||
(pool 'Media' 'Healthy' 1000 250),
|
||||
(pool 'Backups' 'Warning' 1000 900),
|
||||
(pool 'Broken' 'Unhealthy' 0 0),
|
||||
(pool 'Strange' 'Unknown' 1000 1)
|
||||
)) -Compress
|
||||
`) as { name: string; health: string; capacity_percent: number | null }[];
|
||||
assert.deepEqual(out.map((p) => [p.name, p.health, p.capacity_percent]), [["Media", "ONLINE", 25], ["Backups", "DEGRADED", 90], ["Broken", "FAULTED", null]]);
|
||||
});
|
||||
|
||||
test("a maker's placeholders aren't a model or a serial number", () => {
|
||||
const out = run(`
|
||||
$t = @('To Be Filled By O.E.M.', 'Default string', 'System Serial Number', 'System Product Name', '0000000', ' ', 'None', 'Unknown', 'ASUS', 'ROG STRIX B560-F', 'PF2ABCDE', '0A1B')
|
||||
ConvertTo-Json -InputObject @($t | ForEach-Object { Test-PlaceholderText $_ }) -Compress
|
||||
`) as boolean[];
|
||||
assert.deepEqual(out, [true, true, true, true, true, true, true, true, false, false, false, false]);
|
||||
});
|
||||
|
||||
test("on this machine the real collectors answer, in a shape the server reads, and one that can't be read is left out rather than reported well", () => {
|
||||
const out = run(`
|
||||
$health = Get-HostHealth @{ IgnoreServices = @() }
|
||||
$hardware = Get-HardwareInfo
|
||||
ConvertTo-Json -InputObject @{ health = $health; hardware = $hardware } -Depth 6 -Compress
|
||||
`) as { health: { failed_units?: unknown[]; smart?: { device: string; healthy: boolean | null }[]; zfs?: unknown[] } | null; hardware: { manufacturer?: string; model?: string; serial?: string } | null };
|
||||
if (out.health) {
|
||||
for (const part of ["failed_units", "smart", "zfs"] as const) if (out.health[part] !== undefined) assert.ok(Array.isArray(out.health[part]), `${part} is a list`);
|
||||
for (const d of out.health.smart ?? []) assert.match(d.device, /^PhysicalDisk\d+$/);
|
||||
}
|
||||
if (out.hardware) for (const v of Object.values(out.hardware)) assert.ok(typeof v === "string" && v.length > 0 && v.length <= 150);
|
||||
});
|
||||
});
|
||||
@@ -100,11 +100,12 @@ describe("the Windows agent updating itself", { skip: !havePwsh }, () => {
|
||||
|
||||
test("a newer script that matches the checksum replaces the old one, and the token is sent only to the question", async () => {
|
||||
served = real();
|
||||
reply = { update: true, version: 3, sha256: sha(served) };
|
||||
const current = Number(/AgentVersion = (\d+)/.exec(served.toString())![1]);
|
||||
reply = { update: true, version: current, sha256: sha(served) };
|
||||
const out = await attempt();
|
||||
assert.deepEqual(out.content, served, "the installed file is now the served script, byte for byte");
|
||||
assert.deepEqual(out.leftovers, []);
|
||||
assert.match(out.stderr, /Updated the agent from version 1 to version 3/);
|
||||
assert.match(out.stderr, new RegExp(`Updated the agent from version 1 to version ${current}`));
|
||||
const question = out.asked.find((l) => l.includes("/api/agent/report/update"))!;
|
||||
assert.match(question, /version=1/);
|
||||
assert.match(question, /auth=Bearer secret-token/);
|
||||
|
||||
Reference in new issue
Block a user