Nextcloud, NetBox and n8n integrations
Three read-only integrations, each with a monitoring page, alerts, maintenance silencing and held-when-unreadable handling. Nextcloud: database needing upgrade (critical), maintenance mode, and the data disk running low; users, files and app updates with an admin app password. NetBox: no background worker running, counts, and a list of the servers this app manages that have no address in NetBox (a list, not an alert). n8n: an active workflow whose last run failed, clearing when it next succeeds. Disk messages for Immich, Paperless and Nextcloud now use the same units as the pages that show them. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a16996d975
commit
4ff0b44943
46 files changed
+10137
-10
No files matched your search
@@ -159,7 +159,7 @@ describe("what is wrong", () => {
|
||||
assert.deepEqual(keys(row({ diskPercent: 89.9 })), []);
|
||||
assert.deepEqual(keys(row({ diskPercent: 90 })), ["immich-disk:1=warning"]);
|
||||
assert.deepEqual(keys(row({ diskPercent: 95 })), ["immich-disk:1=critical"]);
|
||||
assert.match(im.immichProblems(1, "Photos", row({ diskPercent: 96, diskUsed: 3.84e12 }))[0].message, /96 % full \(3840 GB of 4000 GB\)/);
|
||||
assert.match(im.immichProblems(1, "Photos", row({ diskPercent: 96, diskUsed: 3.84e12 }))[0].message, /96 % full \(3576 GB of 3725 GB\)/);
|
||||
});
|
||||
|
||||
test("failed jobs are one alert when there are ten or more together, naming the worst queues", () => {
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
/**
|
||||
* n8n: reading what its API says (workflows in pages, the latest runs), working out each workflow's last outcome, what is stored, what is wrong, the health pass, the alerts, the
|
||||
* page's route and /metrics.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer as createHttpServer, 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-n8n-"));
|
||||
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, integrations, n8nState } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const adapter = await import("../src/integrations/n8n/adapter.js");
|
||||
const n8 = await import("../src/services/n8nSync.js");
|
||||
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
|
||||
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { n8nRouter } = await import("../src/routes/n8n.js");
|
||||
const { buildMetrics } = await import("../src/services/metrics.js");
|
||||
const { setInternalFlag } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const MIN = 60_000;
|
||||
const DAY = 86_400_000;
|
||||
const NOW = new Date("2026-10-20T12:00:00Z");
|
||||
const ago = (min: number, from: Date = NOW) => new Date(from.getTime() - min * MIN).toISOString();
|
||||
|
||||
type Read = import("../src/integrations/n8n/adapter.js").N8nRead;
|
||||
type Workflow = import("../src/integrations/n8n/adapter.js").N8nWorkflow;
|
||||
const wf = (id: string, name: string, extra: Partial<Workflow> = {}): Workflow => ({ id, name, active: true, lastStatus: "success", lastAt: ago(30), failed24h: 0, ...extra });
|
||||
const read = (workflows: Workflow[] = [wf("1", "Backup notifier")], extra: Partial<Read> = {}): Read => ({ workflows, executionsKnown: true, ...extra });
|
||||
|
||||
describe("what n8n's API says", () => {
|
||||
test("workflows: id, name and whether it is switched on; ones without an id are left out", () => {
|
||||
const w = adapter.parseWorkflows({ data: [{ id: "a1", name: "Notify", active: true }, { id: 7, name: "Old", active: false }, { name: "no id" }] });
|
||||
assert.deepEqual(w, [{ id: "a1", name: "Notify", active: true }, { id: "7", name: "Old", active: false }]);
|
||||
assert.deepEqual(adapter.parseWorkflows(null), []);
|
||||
});
|
||||
|
||||
test("each workflow's last outcome is the newest finished run; a run still going is not an outcome; failures are counted for the last day", () => {
|
||||
const workflows = [{ id: "1", name: "A", active: true }, { id: "2", name: "B", active: true }, { id: "3", name: "C", active: false }];
|
||||
const runs = {
|
||||
data: [
|
||||
{ workflowId: "1", status: "running", startedAt: ago(1) }, // still going: the one before it counts
|
||||
{ workflowId: "1", status: "error", stoppedAt: ago(20) },
|
||||
{ workflowId: "1", status: "success", stoppedAt: ago(200) },
|
||||
{ workflowId: "1", status: "crashed", stoppedAt: ago(300) },
|
||||
{ workflowId: "1", status: "error", stoppedAt: new Date(NOW.getTime() - 2 * DAY).toISOString() }, // older than a day
|
||||
{ workflowId: "2", status: "success", stoppedAt: ago(50) },
|
||||
{ workflowId: "9", status: "error", stoppedAt: ago(10) }, // a workflow that is not in the list
|
||||
],
|
||||
};
|
||||
const s = adapter.summariseRuns(workflows, runs, NOW);
|
||||
assert.deepEqual(s.map((x) => [x.id, x.lastStatus, x.failed24h]), [["1", "error", 2], ["2", "success", 0], ["3", null, 0]]);
|
||||
assert.equal(s[0].lastAt, ago(20));
|
||||
assert.equal(s[2].lastAt, null);
|
||||
});
|
||||
|
||||
test("order does not matter: the newest wins whichever way the list comes", () => {
|
||||
const w = [{ id: "1", name: "A", active: true }];
|
||||
const runs = [{ workflowId: "1", status: "success", stoppedAt: ago(5) }, { workflowId: "1", status: "error", stoppedAt: ago(60) }];
|
||||
assert.equal(adapter.summariseRuns(w, { data: runs }, NOW)[0].lastStatus, "success");
|
||||
assert.equal(adapter.summariseRuns(w, { data: [...runs].reverse() }, NOW)[0].lastStatus, "success");
|
||||
});
|
||||
|
||||
describe("the adapter against an n8n that answers", () => {
|
||||
let server: Server;
|
||||
let url = "";
|
||||
let executionsStatus = 200;
|
||||
const seen: { path: string; key: string | undefined; cursor: string | null }[] = [];
|
||||
before(async () => {
|
||||
server = createHttpServer((req, res) => {
|
||||
const u = new URL(req.url ?? "", "http://x");
|
||||
seen.push({ path: u.pathname, key: req.headers["x-n8n-api-key"] as string | undefined, cursor: u.searchParams.get("cursor") });
|
||||
const send = (code: number, body: unknown) => {
|
||||
res.writeHead(code, { "content-type": "application/json" });
|
||||
res.end(JSON.stringify(body));
|
||||
};
|
||||
if (req.headers["x-n8n-api-key"] !== "key") return send(401, {});
|
||||
if (u.pathname === "/api/v1/workflows") {
|
||||
return u.searchParams.get("cursor") === "page2"
|
||||
? send(200, { data: [{ id: "3", name: "Third", active: true }], nextCursor: null })
|
||||
: send(200, { data: [{ id: "1", name: "First", active: true }, { id: "2", name: "Second", active: false }], nextCursor: "page2" });
|
||||
}
|
||||
if (u.pathname === "/api/v1/executions") {
|
||||
if (executionsStatus !== 200) return send(executionsStatus, {});
|
||||
return send(200, { data: [{ workflowId: "1", status: "error", stoppedAt: new Date(Date.now() - 5 * MIN).toISOString() }, { workflowId: "3", status: "success", stoppedAt: new Date(Date.now() - 9 * MIN).toISOString() }] });
|
||||
}
|
||||
send(404, {});
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
});
|
||||
after(() => server.close());
|
||||
|
||||
test("it follows the cursor through every workflow, sends the key, and works out each one's last run", async () => {
|
||||
const r = await adapter.createN8nAdapter({ url: `${url}/`, apiKey: "key" }).read();
|
||||
assert.deepEqual(r.workflows.map((w) => [w.id, w.active, w.lastStatus, w.failed24h]), [["1", true, "error", 1], ["2", false, null, 0], ["3", true, "success", 0]]);
|
||||
assert.equal(r.executionsKnown, true);
|
||||
assert.deepEqual(seen.filter((s) => s.path === "/api/v1/workflows").map((s) => s.cursor), [null, "page2"]);
|
||||
assert.ok(seen.every((s) => s.key === "key"));
|
||||
});
|
||||
|
||||
test("when the runs cannot be read the workflows are still there, and their last runs are unknown, not fine", async () => {
|
||||
executionsStatus = 500;
|
||||
const r = await adapter.createN8nAdapter({ url, apiKey: "key" }).read();
|
||||
assert.equal(r.executionsKnown, false);
|
||||
assert.equal(r.workflows.length, 3);
|
||||
assert.ok(r.workflows.every((w) => w.lastStatus === null));
|
||||
executionsStatus = 200;
|
||||
});
|
||||
|
||||
test("a refused key and a dead address are said plainly", async () => {
|
||||
const bad = await adapter.createN8nAdapter({ url, apiKey: "nope" }).ping();
|
||||
assert.equal(bad.ok, false);
|
||||
assert.match(bad.error ?? "", /did not accept the API key/);
|
||||
assert.equal((await adapter.createN8nAdapter({ url, apiKey: "key" }).ping()).ok, true);
|
||||
assert.equal((await adapter.createN8nAdapter({ url: "http://127.0.0.1:1", apiKey: "key" }).ping()).ok, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is stored", () => {
|
||||
let id = 0;
|
||||
before(async () => {
|
||||
id = (await db.insert(integrations).values({ type: "n8n", name: "Flows", baseUrl: "http://10.9.6.1:5678", config: JSON.stringify({ url: "http://10.9.6.1:5678", apiKey: "key" }) }).returning({ id: integrations.id }))[0].id;
|
||||
});
|
||||
const state = async () => (await db.select().from(n8nState).where(eq(n8nState.integrationId, id)))[0];
|
||||
|
||||
test("the state is replaced at each read", async () => {
|
||||
await n8.storeRead(id, read([wf("1", "A"), wf("2", "B")]), new Date());
|
||||
assert.deepEqual(n8.workflowsOf(await state()).map((w) => w.name), ["A", "B"]);
|
||||
await n8.storeRead(id, read([wf("3", "C")], { executionsKnown: false }), new Date());
|
||||
const s = await state();
|
||||
assert.deepEqual([n8.workflowsOf(s).map((w) => w.name), s.executionsKnown], [["C"], false]);
|
||||
});
|
||||
|
||||
test("a read that failed is said, and what was stored is left alone", async () => {
|
||||
const ok = await n8.syncN8n(id, new Date(), async () => read([wf("1", "Kept")]));
|
||||
assert.equal(ok.ok, true);
|
||||
const failed = await n8.syncN8n(id, new Date(), async () => {
|
||||
throw new Error("connection refused");
|
||||
});
|
||||
assert.deepEqual([failed.ok, failed.error], [false, "connection refused"]);
|
||||
assert.equal(failed.at, ok.at);
|
||||
assert.deepEqual(n8.workflowsOf(await state()).map((w) => w.name), ["Kept"]);
|
||||
assert.equal(n8.isN8nFresh((await n8.n8nSyncStatuses())[id], Date.now()), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is wrong", () => {
|
||||
const row = (workflows: Workflow[], executionsKnown = true) => ({ workflows: JSON.stringify(workflows), executionsKnown });
|
||||
const keys = (r: ReturnType<typeof row>) => n8.n8nProblems(1, "Flows", r, NOW).map((p) => `${p.key}=${p.severity}`);
|
||||
|
||||
test("an active workflow whose last run failed is a warning, one alert for each; it says when, and if it crashed or failed more than once", () => {
|
||||
const p = n8.n8nProblems(1, "Flows", row([wf("a", "Backup notifier", { lastStatus: "error", lastAt: ago(20), failed24h: 3 }), wf("b", "Mailer", { lastStatus: "crashed", lastAt: ago(300), failed24h: 1 })]), NOW);
|
||||
assert.deepEqual(p.map((x) => [x.key, x.severity]), [["n8n-failed:1:a", "warning"], ["n8n-failed:1:b", "warning"]]);
|
||||
assert.equal(p[0].message, "Flows: the workflow “Backup notifier” failed on its last run (20 min ago), and 3 runs failed in the last day");
|
||||
assert.match(p[1].message, /“Mailer” failed on its last run \(5 h ago, crashed\)$/);
|
||||
});
|
||||
|
||||
test("one that succeeded since, one that is switched off, one whose failure is over a week old and one never seen run say nothing", () => {
|
||||
assert.deepEqual(
|
||||
keys(row([wf("a", "A", { lastStatus: "success" }), wf("b", "B", { active: false, lastStatus: "error", lastAt: ago(10) }), wf("c", "C", { lastStatus: "error", lastAt: new Date(NOW.getTime() - 8 * DAY).toISOString() }), wf("d", "D", { lastStatus: null, lastAt: null })])),
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
test("when the runs could not be read nothing is claimed", () => {
|
||||
assert.deepEqual(keys(row([wf("a", "A", { lastStatus: "error", lastAt: ago(5) })], false)), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the health pass, the page and /metrics", () => {
|
||||
let nId = 0;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
const fresh = () => setInternalFlag("n8nSync", JSON.stringify({ [nId]: { at: new Date().toISOString(), ok: true, error: null } }));
|
||||
const stale = () => setInternalFlag("n8nSync", JSON.stringify({ [nId]: { at: new Date(Date.now() - 30 * MIN).toISOString(), ok: true, error: null } }));
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }]);
|
||||
nId = (await db.insert(integrations).values({ type: "n8n", name: "Automations", baseUrl: "http://10.9.6.2:5678", config: JSON.stringify({ url: "http://10.9.6.2:5678", apiKey: "key" }) }).returning({ id: integrations.id }))[0].id;
|
||||
const t = new Date();
|
||||
await n8.storeRead(nId, read([wf("a", "Backup notifier", { lastStatus: "error", lastAt: ago(20, t), failed24h: 2 }), wf("b", "Fine", { lastAt: ago(5, t) }), wf("c", "Off", { active: false })]), t);
|
||||
await fresh();
|
||||
const a = express();
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/n8n", n8nRouter);
|
||||
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/n8n`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("the health pass judges it from what is stored, and holds it when the last read is old", async () => {
|
||||
let { snapshot, held } = await collectSnapshot();
|
||||
assert.deepEqual(snapshot.n8n?.map((n) => n.integrationId), [nId]);
|
||||
const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true });
|
||||
const c = list.find((x) => x.key === `n8n-failed:${nId}:a`)!;
|
||||
assert.deepEqual([c.severity, c.source], ["warning", `n8n:${nId}`]);
|
||||
assert.equal(subjectOfConditionKey(c.key), `integration:${nId}`, "maintenance on it silences its alerts");
|
||||
assert.equal(list.some((x) => x.key.startsWith(`n8n-failed:${nId}:b`) || x.key.startsWith(`n8n-failed:${nId}:c`)), false);
|
||||
await stale();
|
||||
({ snapshot, held } = await collectSnapshot());
|
||||
assert.deepEqual([snapshot.n8n ?? [], held.has(`n8n:${nId}`)], [[], true]);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("the Alerts page shows it in the Automation kind, with where to look", async () => {
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const a = report.alerts.find((x) => x.id === `automation:n8n-failed:${nId}:a`)!;
|
||||
assert.deepEqual([a.severity, a.category, a.source, a.link, a.target?.type, a.target?.id], ["warning", "automation", "n8n", "/n8n", "integration", nId]);
|
||||
});
|
||||
|
||||
test("anyone signed in sees each n8n and its workflows; an old reading says nothing is wrong", async () => {
|
||||
const body = (await (await fetch(base)).json()) as { n8ns: { id: number; fresh: boolean; state: { workflows: { name: string; lastStatus: string | null }[]; executionsKnown: boolean } | null; problems: unknown[] }[] };
|
||||
const n = body.n8ns.find((x) => x.id === nId)!;
|
||||
assert.deepEqual([n.fresh, n.state?.executionsKnown, n.state?.workflows.map((w) => [w.name, w.lastStatus])], [true, true, [["Backup notifier", "error"], ["Fine", "success"], ["Off", "success"]]]);
|
||||
assert.equal(n.problems.length, 1);
|
||||
await stale();
|
||||
const old = (await (await fetch(base)).json()) as typeof body;
|
||||
assert.deepEqual(old.n8ns.find((x) => x.id === nId)!.problems, []);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("/metrics has the workflows, those switched on and those failing — only while the reading is recent", async () => {
|
||||
let text = await buildMetrics(new Date());
|
||||
assert.match(text, /homelab_n8n_workflows\{name="Automations"\} 3/);
|
||||
assert.match(text, /homelab_n8n_workflows_active\{name="Automations"\} 2/);
|
||||
assert.match(text, /homelab_n8n_workflows_failing\{name="Automations"\} 1/);
|
||||
await stale();
|
||||
text = await buildMetrics(new Date());
|
||||
assert.doesNotMatch(text, /homelab_n8n/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,257 @@
|
||||
/**
|
||||
* NetBox: reading what its API says (status, counts, the addresses in pages), which servers are not in it, what is stored, what is wrong, the health pass, the alerts, the page's
|
||||
* route and /metrics.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer as createHttpServer, 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-netbox-"));
|
||||
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, netboxState } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const adapter = await import("../src/integrations/netbox/adapter.js");
|
||||
const nb = await import("../src/services/netboxSync.js");
|
||||
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
|
||||
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { netboxRouter } = await import("../src/routes/netbox.js");
|
||||
const { buildMetrics } = await import("../src/services/metrics.js");
|
||||
const { setInternalFlag } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const MIN = 60_000;
|
||||
type Read = import("../src/integrations/netbox/adapter.js").NetBoxRead;
|
||||
const read = (extra: Partial<Read> = {}): Read => ({ version: "4.2.5", workersRunning: 1, devices: 12, virtualMachines: 30, prefixes: 8, ipAddresses: 4, ips: ["10.0.0.5", "10.0.0.6", "10.0.0.7", "fd00::1"], ...extra });
|
||||
|
||||
describe("what NetBox's API says", () => {
|
||||
test("the status: version and workers; the workers are null when it does not say", () => {
|
||||
assert.deepEqual(adapter.parseStatus({ "netbox-version": "4.2.5", "rq-workers-running": 2 }), { version: "4.2.5", workersRunning: 2 });
|
||||
assert.deepEqual(adapter.parseStatus({ "netbox-version": "4.2.5", "rq-workers-running": 0 }), { version: "4.2.5", workersRunning: 0 });
|
||||
assert.deepEqual(adapter.parseStatus({}), { version: null, workersRunning: null });
|
||||
});
|
||||
|
||||
test("counts, and addresses without their prefix length", () => {
|
||||
assert.equal(adapter.parseCount({ count: 12, results: [] }), 12);
|
||||
assert.equal(adapter.parseCount({}), null);
|
||||
assert.deepEqual(adapter.parseAddresses({ results: [{ address: "10.0.0.5/24" }, { address: "FD00::1/64" }, { address: "" }, {}] }), ["10.0.0.5", "fd00::1"]);
|
||||
});
|
||||
|
||||
test("a v2 token (nbt_…) goes as a Bearer token, an older one as Token", () => {
|
||||
assert.equal(adapter.authorization("abc123"), "Token abc123");
|
||||
assert.equal(adapter.authorization("nbt_key.secret"), "Bearer nbt_key.secret");
|
||||
});
|
||||
|
||||
describe("the adapter against a NetBox that answers", () => {
|
||||
let server: Server;
|
||||
let url = "";
|
||||
const seen: string[] = [];
|
||||
const TOTAL = 1500;
|
||||
before(async () => {
|
||||
server = createHttpServer((req, res) => {
|
||||
const u = new URL(req.url ?? "", "http://x");
|
||||
seen.push(`${req.headers.authorization} ${u.pathname}${u.search}`);
|
||||
const send = (code: number, body: unknown) => {
|
||||
res.writeHead(code, { "content-type": "application/json" });
|
||||
res.end(JSON.stringify(body));
|
||||
};
|
||||
if (req.headers.authorization !== "Token tok") return send(403, {});
|
||||
if (u.pathname === "/api/status/") return send(200, { "netbox-version": "4.2.5", "rq-workers-running": 0 });
|
||||
if (u.pathname === "/api/dcim/devices/") return send(200, { count: 12, results: [] });
|
||||
if (u.pathname === "/api/virtualization/virtual-machines/") return send(500, {}); // the rest is worth having, not worth failing for
|
||||
if (u.pathname === "/api/ipam/prefixes/") return send(200, { count: 8, results: [] });
|
||||
if (u.pathname === "/api/ipam/ip-addresses/") {
|
||||
if (u.searchParams.get("limit") === "1") return send(200, { count: TOTAL, results: [] });
|
||||
const offset = Number(u.searchParams.get("offset") ?? 0);
|
||||
const limit = Number(u.searchParams.get("limit") ?? 50);
|
||||
const results = Array.from({ length: Math.max(0, Math.min(limit, TOTAL - offset)) }, (_, i) => ({ address: `10.1.${Math.floor((offset + i) / 250)}.${((offset + i) % 250) + 1}/24` }));
|
||||
return send(200, { count: TOTAL, results });
|
||||
}
|
||||
send(404, {});
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
});
|
||||
after(() => server.close());
|
||||
|
||||
test("it reads the counts, the workers and every address in pages; a count that cannot be read is null", async () => {
|
||||
const r = await adapter.createNetBoxAdapter({ url: `${url}/`, token: "tok" }).read();
|
||||
assert.deepEqual([r.version, r.workersRunning, r.devices, r.virtualMachines, r.prefixes, r.ipAddresses], ["4.2.5", 0, 12, null, 8, 1500]);
|
||||
assert.equal(r.ips?.length, 1500, "both pages");
|
||||
assert.ok(r.ips?.includes("10.1.0.1") && r.ips?.includes("10.1.5.250"));
|
||||
const pages = seen.filter((s) => s.includes("brief=1")).map((s) => /offset=(\d+)/.exec(s)![1]);
|
||||
assert.deepEqual(pages, ["0", "1000"]);
|
||||
});
|
||||
|
||||
test("a refused token and a dead address are said plainly", async () => {
|
||||
const bad = await adapter.createNetBoxAdapter({ url, token: "nope" }).ping();
|
||||
assert.equal(bad.ok, false);
|
||||
assert.match(bad.error ?? "", /did not accept the token/);
|
||||
assert.equal((await adapter.createNetBoxAdapter({ url, token: "tok" }).ping()).ok, true);
|
||||
assert.equal((await adapter.createNetBoxAdapter({ url: "http://127.0.0.1:1", token: "tok" }).ping()).ok, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("which servers are not in NetBox", () => {
|
||||
const s = (id: number, name: string, ips: string[]) => ({ id, name, ips });
|
||||
|
||||
test("a server none of whose addresses are in NetBox is listed; one with any address in it is not", () => {
|
||||
const out = nb.serversMissingFromNetBox([s(1, "web", ["10.0.0.5", "172.17.0.1"]), s(2, "nas", ["10.0.0.9"]), s(3, "pi", ["10.0.0.7"])], ["10.0.0.5", "10.0.0.7"]);
|
||||
assert.deepEqual(out.map((x) => x.name), ["nas"]);
|
||||
});
|
||||
|
||||
test("a server that reports no usable address is not judged: loopback, link-local and nothing are nothing to look for", () => {
|
||||
const out = nb.serversMissingFromNetBox([s(1, "a", []), s(2, "b", ["127.0.0.1", "169.254.3.4", "fe80::1", "::1"]), s(3, "c", ["10.0.0.99"])], ["10.0.0.5"]);
|
||||
assert.deepEqual(out.map((x) => x.name), ["c"]);
|
||||
});
|
||||
|
||||
test("an address that is the same on every host (Docker's bridge) is taken out before it is weighed, and the order is by name", () => {
|
||||
const excluded = (ip: string) => ip.startsWith("172.17.");
|
||||
const out = nb.serversMissingFromNetBox([s(1, "zeta", ["172.17.0.1"]), s(2, "beta", ["172.17.0.1", "10.0.0.2"]), s(3, "alpha", ["10.0.0.1"])], ["10.0.0.2"], excluded);
|
||||
assert.deepEqual(out.map((x) => [x.name, x.ips]), [["alpha", ["10.0.0.1"]]], "zeta has only the bridge address, beta is in NetBox");
|
||||
});
|
||||
|
||||
test("case and spaces do not matter", () => {
|
||||
assert.deepEqual(nb.serversMissingFromNetBox([s(1, "v6", [" FD00::1 "])], ["fd00::1"]), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is stored", () => {
|
||||
let id = 0;
|
||||
before(async () => {
|
||||
id = (await db.insert(integrations).values({ type: "netbox", name: "IPAM", baseUrl: "http://10.9.7.1", config: JSON.stringify({ url: "http://10.9.7.1", token: "tok" }) }).returning({ id: integrations.id }))[0].id;
|
||||
});
|
||||
const state = async () => (await db.select().from(netboxState).where(eq(netboxState.integrationId, id)))[0];
|
||||
|
||||
test("the state is replaced at each read; addresses that could not be listed stay null, not empty", async () => {
|
||||
await nb.storeRead(id, read(), new Date());
|
||||
assert.deepEqual(nb.ipsOf(await state()), ["10.0.0.5", "10.0.0.6", "10.0.0.7", "fd00::1"]);
|
||||
await nb.storeRead(id, read({ ips: null, workersRunning: null }), new Date());
|
||||
const s = await state();
|
||||
assert.deepEqual([nb.ipsOf(s), s.workersRunning], [null, null]);
|
||||
});
|
||||
|
||||
test("a read that failed is said, and what was stored is left alone", async () => {
|
||||
const ok = await nb.syncNetBox(id, new Date(), async () => read({ version: "4.2.6" }));
|
||||
assert.equal(ok.ok, true);
|
||||
const failed = await nb.syncNetBox(id, new Date(), async () => {
|
||||
throw new Error("connection refused");
|
||||
});
|
||||
assert.deepEqual([failed.ok, failed.error], [false, "connection refused"]);
|
||||
assert.equal(failed.at, ok.at);
|
||||
assert.equal((await state()).version, "4.2.6");
|
||||
assert.equal(nb.isNetBoxFresh((await nb.netboxSyncStatuses())[id], Date.now()), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is wrong", () => {
|
||||
test("no worker running is a warning; one running, or not said, is nothing", () => {
|
||||
assert.deepEqual(nb.netboxProblems(1, "IPAM", { workersRunning: 0 }).map((p) => [p.key, p.severity]), [["netbox-workers:1", "warning"]]);
|
||||
assert.match(nb.netboxProblems(1, "IPAM", { workersRunning: 0 })[0].message, /no background worker is running/);
|
||||
assert.deepEqual(nb.netboxProblems(1, "IPAM", { workersRunning: 1 }), []);
|
||||
assert.deepEqual(nb.netboxProblems(1, "IPAM", { workersRunning: null }), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the health pass, the page and /metrics", () => {
|
||||
let nbId = 0;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
const ids: Record<string, number> = {};
|
||||
const fresh = () => setInternalFlag("netboxSync", JSON.stringify({ [nbId]: { at: new Date().toISOString(), ok: true, error: null } }));
|
||||
const stale = () => setInternalFlag("netboxSync", JSON.stringify({ [nbId]: { at: new Date(Date.now() - 30 * MIN).toISOString(), ok: true, error: null } }));
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }]);
|
||||
nbId = (await db.insert(integrations).values({ type: "netbox", name: "NetBox", baseUrl: "http://10.9.7.2", config: JSON.stringify({ url: "http://10.9.7.2", token: "tok" }) }).returning({ id: integrations.id }))[0].id;
|
||||
const mk = async (name: string, ips: string[]) => (await db.insert(servers).values({ name, apiTokenHash: name, apiTokenPrefix: name, ipAddresses: JSON.stringify(ips) }).returning({ id: servers.id }))[0].id;
|
||||
ids.web = await mk("web", ["10.0.0.5"]);
|
||||
ids.nas = await mk("nas", ["10.0.0.99"]);
|
||||
await nb.storeRead(nbId, read({ workersRunning: 0, ipAddresses: 4 }), new Date());
|
||||
await fresh();
|
||||
const a = express();
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/netbox", netboxRouter);
|
||||
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/netbox`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("the health pass judges it from what is stored, and holds it when the last read is old", async () => {
|
||||
let { snapshot, held } = await collectSnapshot();
|
||||
assert.deepEqual(snapshot.netbox?.map((n) => n.integrationId), [nbId]);
|
||||
const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true });
|
||||
const c = list.find((x) => x.key === `netbox-workers:${nbId}`)!;
|
||||
assert.deepEqual([c.severity, c.source], ["warning", `netbox:${nbId}`]);
|
||||
assert.equal(subjectOfConditionKey(c.key), `integration:${nbId}`, "maintenance on it silences its alerts");
|
||||
await stale();
|
||||
({ snapshot, held } = await collectSnapshot());
|
||||
assert.deepEqual([snapshot.netbox ?? [], held.has(`netbox:${nbId}`)], [[], true]);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("the Alerts page shows it under Services, with where to look", async () => {
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const a = report.alerts.find((x) => x.id === `services:netbox-workers:${nbId}`)!;
|
||||
assert.deepEqual([a.severity, a.category, a.source, a.link, a.target?.type, a.target?.id], ["warning", "services", "NetBox", "/netbox", "integration", nbId]);
|
||||
});
|
||||
|
||||
test("anyone signed in sees each NetBox, the counts and which servers are not in it; an old reading says nothing is wrong", async () => {
|
||||
const body = (await (await fetch(base)).json()) as { netboxes: { id: number; fresh: boolean; state: { workersRunning: number; missing: { name: string }[] | null; truncated: boolean } | null; problems: unknown[] }[] };
|
||||
const n = body.netboxes.find((x) => x.id === nbId)!;
|
||||
assert.deepEqual([n.fresh, n.state?.workersRunning, n.state?.missing?.map((m) => m.name), n.state?.truncated], [true, 0, ["nas"], false]);
|
||||
assert.equal(n.problems.length, 1);
|
||||
await stale();
|
||||
const old = (await (await fetch(base)).json()) as typeof body;
|
||||
assert.deepEqual(old.netboxes.find((x) => x.id === nbId)!.problems, []);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("when NetBox has more addresses than were read, the page says what is missing may be in the rest; when they could not be listed, nothing is claimed", async () => {
|
||||
await nb.storeRead(nbId, read({ ipAddresses: 9000 }), new Date());
|
||||
const body = (await (await fetch(base)).json()) as { netboxes: { id: number; state: { truncated: boolean; missing: unknown } }[] };
|
||||
assert.equal(body.netboxes.find((x) => x.id === nbId)!.state.truncated, true);
|
||||
await nb.storeRead(nbId, read({ ips: null }), new Date());
|
||||
const none = (await (await fetch(base)).json()) as typeof body;
|
||||
assert.equal(none.netboxes.find((x) => x.id === nbId)!.state.missing, null);
|
||||
await nb.storeRead(nbId, read({ workersRunning: 0, ipAddresses: 4 }), new Date());
|
||||
});
|
||||
|
||||
test("/metrics has the workers and the counts — only while the reading is recent", async () => {
|
||||
let text = await buildMetrics(new Date());
|
||||
assert.match(text, /homelab_netbox_workers\{name="NetBox"\} 0/);
|
||||
assert.match(text, /homelab_netbox_devices\{name="NetBox"\} 12/);
|
||||
assert.match(text, /homelab_netbox_ip_addresses\{name="NetBox"\} 4/);
|
||||
await stale();
|
||||
text = await buildMetrics(new Date());
|
||||
assert.doesNotMatch(text, /homelab_netbox/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,244 @@
|
||||
/**
|
||||
* Nextcloud: reading what its API says (status.php, and the monitoring app with an administrator's app password), what is stored, what is wrong, the health pass, the alerts, the
|
||||
* page's route and /metrics.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer as createHttpServer, 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-nextcloud-"));
|
||||
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, integrations, nextcloudState } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const adapter = await import("../src/integrations/nextcloud/adapter.js");
|
||||
const nc = await import("../src/services/nextcloudSync.js");
|
||||
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
|
||||
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { nextcloudRouter } = await import("../src/routes/nextcloud.js");
|
||||
const { buildMetrics } = await import("../src/services/metrics.js");
|
||||
const { setInternalFlag } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const MIN = 60_000;
|
||||
type Read = import("../src/integrations/nextcloud/adapter.js").NextcloudRead;
|
||||
const read = (extra: Partial<Read> = {}): Read => ({ version: "31.0.2", maintenance: false, needsUpgrade: false, detail: true, freeBytes: 500e9, users: 4, files: 120000, active24h: 2, appUpdates: 1, ...extra });
|
||||
|
||||
const INFO = { ocs: { meta: { status: "ok" }, data: { nextcloud: { system: { version: "31.0.2.1", freespace: 412e9, apps: { num_installed: 40, num_updates_available: 3, app_updates: { calendar: "5.1.0" } } }, storage: { num_users: 4, num_files: 120000 } }, activeUsers: { last5minutes: 1, last24hours: 2 } } } };
|
||||
|
||||
describe("what Nextcloud's API says", () => {
|
||||
test("status.php: the version, maintenance mode and a database that wants upgrading", () => {
|
||||
assert.deepEqual(adapter.parseStatus({ installed: true, maintenance: false, needsDbUpgrade: false, version: "31.0.2.1", versionstring: "31.0.2" }), { version: "31.0.2", maintenance: false, needsUpgrade: false, installed: true });
|
||||
const m = adapter.parseStatus({ installed: true, maintenance: 1, needsDbUpgrade: "1", version: "31.0.2.1" });
|
||||
assert.deepEqual([m.maintenance, m.needsUpgrade, m.version], [true, true, "31.0.2.1"]);
|
||||
assert.equal(adapter.parseStatus({ installed: false }).installed, false);
|
||||
assert.deepEqual(adapter.parseStatus(null), { version: null, maintenance: false, needsUpgrade: false, installed: true });
|
||||
});
|
||||
|
||||
test("the monitoring app: room left, users, files, who was active and the app updates", () => {
|
||||
assert.deepEqual(adapter.parseServerInfo(INFO), { freeBytes: 412e9, users: 4, files: 120000, active24h: 2, appUpdates: 3 });
|
||||
assert.equal(adapter.parseServerInfo({ ocs: { meta: {} } }), null, "no data is not an empty answer");
|
||||
assert.equal(adapter.parseServerInfo({ ocs: { data: { nextcloud: { system: { apps: { app_updates: { a: "1", b: "2" } } } } } } })?.appUpdates, 2, "counted from the list when no count is given");
|
||||
assert.equal(adapter.parseServerInfo({ ocs: { data: { nextcloud: { system: {} } } } })?.appUpdates, null);
|
||||
});
|
||||
|
||||
describe("the adapter against a Nextcloud that answers", () => {
|
||||
let server: Server;
|
||||
let url = "";
|
||||
let infoStatus = 200;
|
||||
let installed = true;
|
||||
const seen: { path: string; auth: string | undefined; ocs: string | undefined }[] = [];
|
||||
before(async () => {
|
||||
server = createHttpServer((req, res) => {
|
||||
const path = (req.url ?? "").split("?")[0];
|
||||
seen.push({ path, auth: req.headers.authorization, ocs: req.headers["ocs-apirequest"] as string | undefined });
|
||||
const send = (code: number, body: unknown) => {
|
||||
res.writeHead(code, { "content-type": "application/json" });
|
||||
res.end(JSON.stringify(body));
|
||||
};
|
||||
if (path === "/status.php") return send(200, { installed, maintenance: true, needsDbUpgrade: false, versionstring: "31.0.2" });
|
||||
if (path === "/ocs/v2.php/apps/serverinfo/api/v1/info") return infoStatus === 200 ? send(200, INFO) : send(infoStatus, {});
|
||||
send(404, {});
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
||||
});
|
||||
after(() => server.close());
|
||||
|
||||
test("without a login only the status is read, and nothing is sent to the monitoring app", async () => {
|
||||
seen.length = 0;
|
||||
const r = await adapter.createNextcloudAdapter({ url: `${url}/` }).read();
|
||||
assert.deepEqual([r.version, r.maintenance, r.detail, r.freeBytes], ["31.0.2", true, false, null]);
|
||||
assert.deepEqual(seen.map((s) => s.path), ["/status.php"]);
|
||||
});
|
||||
|
||||
test("with an administrator's app password the monitoring app is asked, the way Nextcloud wants it", async () => {
|
||||
seen.length = 0;
|
||||
const r = await adapter.createNextcloudAdapter({ url, username: "admin", appPassword: "pw" }).read();
|
||||
assert.deepEqual([r.detail, r.freeBytes, r.users, r.active24h, r.appUpdates], [true, 412e9, 4, 2, 3]);
|
||||
const info = seen.find((s) => s.path.includes("serverinfo"))!;
|
||||
assert.equal(info.auth, `Basic ${Buffer.from("admin:pw").toString("base64")}`);
|
||||
assert.equal(info.ocs, "true");
|
||||
});
|
||||
|
||||
test("a login that is refused (or an app that is off) costs only the detail, not the status", async () => {
|
||||
infoStatus = 401;
|
||||
const r = await adapter.createNextcloudAdapter({ url, username: "admin", appPassword: "wrong" }).read();
|
||||
assert.deepEqual([r.version, r.detail, r.freeBytes], ["31.0.2", false, null]);
|
||||
const ping = await adapter.createNextcloudAdapter({ url, username: "admin", appPassword: "wrong" }).ping();
|
||||
assert.equal(ping.ok, false);
|
||||
assert.match(ping.error ?? "", /did not accept the login/);
|
||||
infoStatus = 200;
|
||||
});
|
||||
|
||||
test("one that says it is not installed, and a dead address, are said", async () => {
|
||||
installed = false;
|
||||
await assert.rejects(() => adapter.createNextcloudAdapter({ url }).read(), /not installed/);
|
||||
installed = true;
|
||||
assert.equal((await adapter.createNextcloudAdapter({ url: "http://127.0.0.1:1" }).ping()).ok, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is stored", () => {
|
||||
let id = 0;
|
||||
before(async () => {
|
||||
id = (await db.insert(integrations).values({ type: "nextcloud", name: "Cloud", baseUrl: "http://10.9.8.1", config: JSON.stringify({ url: "http://10.9.8.1" }) }).returning({ id: integrations.id }))[0].id;
|
||||
});
|
||||
const state = async () => (await db.select().from(nextcloudState).where(eq(nextcloudState.integrationId, id)))[0];
|
||||
|
||||
test("the state is replaced at each read; what was not asked stays null", async () => {
|
||||
await nc.storeRead(id, read(), new Date());
|
||||
assert.deepEqual([(await state()).freeBytes, (await state()).appUpdates], [500e9, 1]);
|
||||
await nc.storeRead(id, read({ detail: false, freeBytes: null, users: null, files: null, active24h: null, appUpdates: null }), new Date());
|
||||
const s = await state();
|
||||
assert.deepEqual([s.detail, s.freeBytes, s.appUpdates], [false, null, null]);
|
||||
});
|
||||
|
||||
test("a read that failed is said, and what was stored is left alone", async () => {
|
||||
const ok = await nc.syncNextcloud(id, new Date(), async () => read({ version: "31.0.3" }));
|
||||
assert.equal(ok.ok, true);
|
||||
const failed = await nc.syncNextcloud(id, new Date(), async () => {
|
||||
throw new Error("connection refused");
|
||||
});
|
||||
assert.deepEqual([failed.ok, failed.error], [false, "connection refused"]);
|
||||
assert.equal(failed.at, ok.at);
|
||||
assert.equal((await state()).version, "31.0.3");
|
||||
assert.equal(nc.isNextcloudFresh((await nc.nextcloudSyncStatuses())[id], Date.now()), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is wrong", () => {
|
||||
const row = (extra: Partial<Parameters<typeof nc.nextcloudProblems>[2]> = {}) => ({ maintenance: false, needsUpgrade: false, freeBytes: 500e9 as number | null, ...extra });
|
||||
const keys = (r: ReturnType<typeof row>) => nc.nextcloudProblems(1, "Cloud", r).map((p) => `${p.key}=${p.severity}`);
|
||||
|
||||
test("nothing wrong says nothing; what was not asked is not a problem", () => {
|
||||
assert.deepEqual(keys(row()), []);
|
||||
assert.deepEqual(keys(row({ freeBytes: null })), []);
|
||||
});
|
||||
|
||||
test("a database that wants upgrading is critical, maintenance mode a warning, each saying what to run", () => {
|
||||
assert.deepEqual(keys(row({ needsUpgrade: true, maintenance: true })), ["nextcloud-upgrade:1=critical", "nextcloud-maintenance:1=warning"]);
|
||||
const p = nc.nextcloudProblems(1, "Cloud", row({ needsUpgrade: true, maintenance: true }));
|
||||
assert.match(p[0].message, /occ upgrade/);
|
||||
assert.match(p[1].message, /occ maintenance:mode --off/);
|
||||
});
|
||||
|
||||
test("the room for the data: a warning under 20 GB, critical under 5 GB", () => {
|
||||
assert.deepEqual(keys(row({ freeBytes: 20e9 })), []);
|
||||
assert.deepEqual(keys(row({ freeBytes: 19e9 })), ["nextcloud-disk:1=warning"]);
|
||||
assert.deepEqual(keys(row({ freeBytes: 4e9 })), ["nextcloud-disk:1=critical"]);
|
||||
assert.match(nc.nextcloudProblems(1, "Cloud", row({ freeBytes: 4.2e9 }))[0].message, /only 3\.9 GB is left/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the health pass, the page and /metrics", () => {
|
||||
let ncId = 0;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
const fresh = () => setInternalFlag("nextcloudSync", JSON.stringify({ [ncId]: { at: new Date().toISOString(), ok: true, error: null } }));
|
||||
const stale = () => setInternalFlag("nextcloudSync", JSON.stringify({ [ncId]: { at: new Date(Date.now() - 30 * MIN).toISOString(), ok: true, error: null } }));
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }]);
|
||||
ncId = (await db.insert(integrations).values({ type: "nextcloud", name: "Family cloud", baseUrl: "http://10.9.8.2", config: JSON.stringify({ url: "http://10.9.8.2" }) }).returning({ id: integrations.id }))[0].id;
|
||||
await nc.storeRead(ncId, read({ maintenance: true, needsUpgrade: true, freeBytes: 3e9 }), new Date());
|
||||
await fresh();
|
||||
const a = express();
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/nextcloud", nextcloudRouter);
|
||||
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/nextcloud`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("the health pass judges it from what is stored, and holds it when the last read is old", async () => {
|
||||
let { snapshot, held } = await collectSnapshot();
|
||||
assert.deepEqual(snapshot.nextcloud?.map((n) => n.integrationId), [ncId]);
|
||||
const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true });
|
||||
const c = list.find((x) => x.key === `nextcloud-upgrade:${ncId}`)!;
|
||||
assert.deepEqual([c.severity, c.source], ["critical", `nextcloud:${ncId}`]);
|
||||
for (const k of ["nextcloud-upgrade", "nextcloud-maintenance", "nextcloud-disk"]) assert.equal(subjectOfConditionKey(`${k}:${ncId}`), `integration:${ncId}`, "maintenance on it silences its alerts");
|
||||
await stale();
|
||||
({ snapshot, held } = await collectSnapshot());
|
||||
assert.deepEqual([snapshot.nextcloud ?? [], held.has(`nextcloud:${ncId}`)], [[], true]);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("the Alerts page shows them: the sign-in problems under Services, the disk under Disk, with where to look", async () => {
|
||||
invalidateAlertsCache();
|
||||
const report = await getAlerts(true);
|
||||
const up = report.alerts.find((x) => x.id === `services:nextcloud-upgrade:${ncId}`)!;
|
||||
assert.deepEqual([up.severity, up.category, up.source, up.link, up.target?.type, up.target?.id], ["critical", "services", "Nextcloud", "/nextcloud", "integration", ncId]);
|
||||
assert.equal(report.alerts.find((x) => x.id === `disk:nextcloud-disk:${ncId}`)?.severity, "critical");
|
||||
assert.ok(report.alerts.some((x) => x.id === `services:nextcloud-maintenance:${ncId}`));
|
||||
});
|
||||
|
||||
test("anyone signed in sees each Nextcloud; an old reading says nothing is wrong", async () => {
|
||||
const body = (await (await fetch(base)).json()) as { nextclouds: { id: number; fresh: boolean; state: { maintenance: boolean; freeBytes: number; appUpdates: number } | null; problems: unknown[] }[] };
|
||||
const n = body.nextclouds.find((x) => x.id === ncId)!;
|
||||
assert.deepEqual([n.fresh, n.state?.maintenance, n.state?.freeBytes, n.state?.appUpdates], [true, true, 3e9, 1]);
|
||||
assert.equal(n.problems.length, 3);
|
||||
await stale();
|
||||
const old = (await (await fetch(base)).json()) as typeof body;
|
||||
assert.deepEqual(old.nextclouds.find((x) => x.id === ncId)!.problems, []);
|
||||
await fresh();
|
||||
});
|
||||
|
||||
test("/metrics has maintenance mode, the room left, who was active and the app updates — only while the reading is recent", async () => {
|
||||
let text = await buildMetrics(new Date());
|
||||
assert.match(text, /homelab_nextcloud_maintenance_mode\{name="Family cloud"\} 1/);
|
||||
assert.match(text, /homelab_nextcloud_free_bytes\{name="Family cloud"\} 3000000000|homelab_nextcloud_free_bytes\{name="Family cloud"\} 3e\+9/);
|
||||
assert.match(text, /homelab_nextcloud_active_users_24h\{name="Family cloud"\} 2/);
|
||||
assert.match(text, /homelab_nextcloud_app_updates\{name="Family cloud"\} 1/);
|
||||
await stale();
|
||||
text = await buildMetrics(new Date());
|
||||
assert.doesNotMatch(text, /homelab_nextcloud/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user