Show what Duplicati's warnings said, and let a job alert on them
The first twenty warning messages of each run are kept with the run and shown on the job and in its history. A job can be set to raise a warning-level alert when its last run finished with warnings (off by default; a muted job raises nothing; maintenance on the machine's server silences it). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d78aba8bf9
commit
64961b1141
14 files changed
+9241
-24
No files matched your search
@@ -156,7 +156,7 @@ describe("what a report does", () => {
|
||||
describe("where a backup stands, and what is an alert", () => {
|
||||
const row = (extra: Partial<import("../src/services/duplicati.js").BackupRow> = {}): import("../src/services/duplicati.js").BackupRow => ({
|
||||
id: 1, sourceId: 1, name: "Documents", lastReportAt: null, lastResult: "Success", lastBegin: null, lastEnd: null, durationSeconds: null, examinedFiles: null, sizeBytes: null, addedFiles: null, modifiedFiles: null, deletedFiles: null,
|
||||
warnings: 0, errors: 0, message: null, lastSuccessAt: new Date(NOW - 5 * HOUR).toISOString(), consecutiveFailures: 0, expectedHours: 48, alert: true, createdAt: "2026-10-01 00:00:00", ...extra,
|
||||
warnings: 0, errors: 0, message: null, warningMessages: null, alertWarnings: false, lastSuccessAt: new Date(NOW - 5 * HOUR).toISOString(), consecutiveFailures: 0, expectedHours: 48, alert: true, createdAt: "2026-10-01 00:00:00", ...extra,
|
||||
});
|
||||
const NOW = Date.parse("2026-10-24T12:00:00Z");
|
||||
const iso = (hoursAgo: number) => new Date(NOW - hoursAgo * HOUR).toISOString();
|
||||
@@ -198,6 +198,91 @@ describe("where a backup stands, and what is an alert", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("what the warnings said", () => {
|
||||
test("a report's warnings are kept as one line each, the first twenty, each cut at 500 characters; the first is the message", () => {
|
||||
const r = d.parseReport(report({ ParsedResult: "Warning", WarningsActualLength: 3, Warnings: ["Failed to process path: C:\\locked.db\n access denied", " second ", { code: "x" }] }))!;
|
||||
assert.deepEqual(r.warningMessages, ["Failed to process path: C:\\locked.db access denied", "second", '{"code":"x"}']);
|
||||
assert.equal(r.message, "Failed to process path: C:\\locked.db access denied");
|
||||
const many = d.parseReport(report({ ParsedResult: "Warning", WarningsActualLength: 90, Warnings: Array.from({ length: 90 }, (_, i) => `w${i}`) }))!;
|
||||
assert.deepEqual([many.warnings, many.warningMessages.length, many.warningMessages[19]], [90, 20, "w19"]);
|
||||
assert.equal(d.parseReport(report({ ParsedResult: "Warning", Warnings: ["x".repeat(900)] }))!.warningMessages[0].length, 500);
|
||||
});
|
||||
|
||||
test("empty lines, things that are not a list and the plain text give none", () => {
|
||||
assert.deepEqual(d.messagesOf(["", " ", null, 5, "real"]), ["real"]);
|
||||
for (const nothing of [undefined, null, "text", {}, 7]) assert.deepEqual(d.messagesOf(nothing), []);
|
||||
assert.deepEqual(d.parseReport(["MainOperation: Backup", "ParsedResult: Warning", "WarningsActualLength: 2", "backup-name: Docs"].join("\n"))!.warningMessages, []);
|
||||
assert.deepEqual(d.parseReport(report())!.warningMessages, []);
|
||||
});
|
||||
|
||||
test("what is stored is a list read back as a list, whatever is in the column", () => {
|
||||
assert.equal(d.packMessages([]), null);
|
||||
assert.deepEqual(d.unpackMessages(d.packMessages(["a", "b"])), ["a", "b"]);
|
||||
for (const junk of [null, "", "not json", "{}", "5", '["ok", 5, null]']) assert.ok(Array.isArray(d.unpackMessages(junk)), String(junk));
|
||||
assert.deepEqual(d.unpackMessages('["ok", 5, null]'), ["ok"]);
|
||||
});
|
||||
|
||||
describe("with a report that arrives", () => {
|
||||
let token = "";
|
||||
before(async () => {
|
||||
token = (await d.createSource({ name: "Warny", serverId: null, createdBy: "Ada" })).token;
|
||||
});
|
||||
const job = async () => (await db.select().from(duplicatiBackups).where(eq(duplicatiBackups.name, "Warned")))[0];
|
||||
|
||||
test("the messages go with the run and with the job; a later clean run empties the job's, and the history keeps what was said then", async () => {
|
||||
await d.recordReport(token, report({ ParsedResult: "Warning", WarningsActualLength: 2, Warnings: ["file one locked", "file two locked"], EndTime: "2026-10-26T03:35:00Z" }, "Warned"), new Date("2026-10-26T03:36:00Z"));
|
||||
const b = await job();
|
||||
assert.deepEqual([b.warnings, d.unpackMessages(b.warningMessages)], [2, ["file one locked", "file two locked"]]);
|
||||
await d.recordReport(token, report({ EndTime: "2026-10-27T03:35:00Z" }, "Warned"), new Date("2026-10-27T03:36:00Z"));
|
||||
const after = await job();
|
||||
assert.deepEqual([after.warnings, after.warningMessages], [0, null]);
|
||||
const runs = await db.select().from(duplicatiRuns).where(eq(duplicatiRuns.backupId, b.id));
|
||||
assert.deepEqual(runs.map((r) => [r.result, d.unpackMessages(r.warningMessages)]).sort(), [["Success", []], ["Warning", ["file one locked", "file two locked"]]]);
|
||||
});
|
||||
|
||||
test("a job is set not to alert on warnings until somebody switches it on", async () => {
|
||||
assert.equal((await job()).alertWarnings, false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("a warning as an alert", () => {
|
||||
const NOW = Date.parse("2026-10-24T12:00:00Z");
|
||||
const HR = 3_600_000;
|
||||
const row = (extra: Partial<import("../src/services/duplicati.js").BackupRow> = {}): import("../src/services/duplicati.js").BackupRow => ({
|
||||
id: 1, sourceId: 1, name: "Documents", lastReportAt: null, lastResult: "Warning", lastBegin: null, lastEnd: null, durationSeconds: null, examinedFiles: null, sizeBytes: null, addedFiles: null, modifiedFiles: null, deletedFiles: null,
|
||||
warnings: 2, errors: 0, message: "file one locked", warningMessages: JSON.stringify(["file one locked", "file two locked"]), alertWarnings: true, lastSuccessAt: new Date(NOW - 5 * HR).toISOString(), consecutiveFailures: 0, expectedHours: 48, alert: true, createdAt: "2026-10-01 00:00:00", ...extra,
|
||||
});
|
||||
const p = (b: Partial<import("../src/services/duplicati.js").BackupRow>, serverId: number | null = null) => d.duplicatiProblems([{ id: 1, name: "Jackie", serverId }], [row(b)], NOW);
|
||||
|
||||
test("a run with warnings is an alert only for a job set to alert on them: a warning, with the count and the first thing it said", () => {
|
||||
assert.deepEqual(p({ alertWarnings: false }), []);
|
||||
const [a] = p({});
|
||||
assert.deepEqual([a.key, a.kind, a.severity], ["duplicati-warnings:1", "warnings", "warning"]);
|
||||
assert.equal(a.message, "The backup “Documents” on Jackie finished with 2 warnings: file one locked (and 1 more)");
|
||||
});
|
||||
|
||||
test("one warning says so in the singular and has no 'more'; with no text it still says how many", () => {
|
||||
assert.equal(p({ warnings: 1, warningMessages: JSON.stringify(["only one"]) })[0].message, "The backup “Documents” on Jackie finished with 1 warning: only one");
|
||||
assert.equal(p({ warningMessages: null, message: null })[0].message, "The backup “Documents” on Jackie finished with 2 warnings");
|
||||
assert.ok(p({ warningMessages: JSON.stringify(["y".repeat(400)]) })[0].message.length < 330, "a long message is cut");
|
||||
});
|
||||
|
||||
test("a clean run, a failed one, an overdue one and a muted job do not raise it; the failure or the lateness is the alert", () => {
|
||||
assert.deepEqual(p({ lastResult: "Success", warnings: 0, warningMessages: null }), []);
|
||||
assert.deepEqual(p({ lastResult: "Error", consecutiveFailures: 1 }).map((x) => x.kind), ["failed"]);
|
||||
assert.deepEqual(p({ lastSuccessAt: new Date(NOW - 80 * HR).toISOString() }).map((x) => x.kind), ["stale"]);
|
||||
assert.deepEqual(p({ alert: false }), [], "a job muted entirely says nothing, warnings included");
|
||||
});
|
||||
|
||||
test("the server a machine runs on is in the key, so its maintenance silences it, like the others", () => {
|
||||
const [a] = p({}, 9);
|
||||
assert.equal(a.key, "duplicati-warnings:1:server:9");
|
||||
assert.equal(subjectOfConditionKey(a.key), "server:9");
|
||||
assert.equal(subjectOfConditionKey("duplicati-warnings:1"), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the address Duplicati posts to, the pages, the alerts and /metrics", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
@@ -315,6 +400,42 @@ describe("the address Duplicati posts to, the pages, the alerts and /metrics", (
|
||||
who = "admin";
|
||||
});
|
||||
|
||||
test("warnings: the page can read what they said, per job and per run; an operator can switch the alert on, and a viewer cannot", async () => {
|
||||
const docs = (await db.select().from(duplicatiBackups).where(eq(duplicatiBackups.name, "Documents")))[0];
|
||||
await d.recordReport(token, report({ ParsedResult: "Warning", WarningsActualLength: 2, Warnings: ["file one locked", "file two locked"], EndTime: new Date().toISOString() }, "Documents"));
|
||||
who = "viewer";
|
||||
const body = (await (await call("GET", "/duplicati")).json()) as any;
|
||||
const b = body.backups.find((x: any) => x.name === "Documents");
|
||||
assert.deepEqual([b.state, b.warnings, b.warningMessages, b.alertWarnings, b.problem], ["warning", 2, ["file one locked", "file two locked"], false, null]);
|
||||
const runs = (await (await call("GET", `/duplicati/backups/${docs.id}/runs`)).json()) as any;
|
||||
assert.deepEqual(runs.runs[0].warningMessages, ["file one locked", "file two locked"]);
|
||||
assert.deepEqual(runs.runs.at(-1).warningMessages, [], "an older run that had none");
|
||||
assert.equal((await call("PATCH", `/duplicati/backups/${docs.id}`, { alertWarnings: true })).status, 403);
|
||||
who = "operator";
|
||||
assert.equal((await call("PATCH", `/duplicati/backups/${docs.id}`, { alertWarnings: "yes" })).status, 400);
|
||||
assert.equal((await call("PATCH", `/duplicati/backups/${docs.id}`, { alertWarnings: true })).status, 200);
|
||||
assert.equal((await db.select().from(duplicatiBackups).where(eq(duplicatiBackups.id, docs.id)))[0].alertWarnings, true);
|
||||
who = "admin";
|
||||
});
|
||||
|
||||
test("a job set to alert on warnings raises one in the Backups kind, with the first message, and the page says what is wrong; switched off, it stops", async () => {
|
||||
invalidateAlertsCache();
|
||||
const a = (await getAlerts(true)).alerts.find((x) => /^backup:duplicati-warnings:\d+:server:/.test(x.id))!;
|
||||
assert.ok(a, "the alert is there");
|
||||
assert.deepEqual([a.severity, a.category, a.source, a.link], ["warning", "backup", "Duplicati", "/duplicati"]);
|
||||
assert.match(a.message, /^The backup “Documents” on Jackie finished with 2 warnings: file one locked \(and 1 more\)/);
|
||||
const body = (await (await call("GET", "/duplicati")).json()) as any;
|
||||
assert.match(body.backups.find((x: any) => x.name === "Documents").problem, /finished with 2 warnings/);
|
||||
const docs = (await db.select().from(duplicatiBackups).where(eq(duplicatiBackups.name, "Documents")))[0];
|
||||
who = "operator";
|
||||
await call("PATCH", `/duplicati/backups/${docs.id}`, { alertWarnings: false });
|
||||
who = "admin";
|
||||
invalidateAlertsCache();
|
||||
assert.ok(!(await getAlerts(true)).alerts.some((x) => x.id.includes("duplicati-warnings")), "no alert with it switched off");
|
||||
assert.equal((await db.select().from(auditLog).where(eq(auditLog.action, "update_backup"))).some((e) => JSON.stringify(e.detail ?? "").includes("alertWarnings")), true, "the change is in the audit log");
|
||||
await d.recordReport(token, report({ EndTime: new Date().toISOString() }, "Documents"));
|
||||
});
|
||||
|
||||
test("/metrics has each backup: in order or not, how long since it last worked, and how big it is", async () => {
|
||||
const text = await buildMetrics();
|
||||
assert.match(text, /homelab_duplicati_backup_ok\{backup="Documents",source="Jackie"\} 1/);
|
||||
@@ -338,6 +459,6 @@ describe("the address Duplicati posts to, the pages, the alerts and /metrics", (
|
||||
assert.equal((await db.select().from(duplicatiBackups)).length, 0, "its backups go with it");
|
||||
assert.equal((await call("POST", `/duplicati-report/${token}`, report(), { "content-type": "text/plain" })).status, 404, "and its address stops working");
|
||||
const actions = (await db.select().from(auditLog).where(eq(auditLog.category, "duplicati"))).map((a) => a.action);
|
||||
assert.deepEqual(actions.sort(), ["create_source", "delete_source", "forget_backup", "update_backup", "update_backup", "update_source"].sort());
|
||||
assert.deepEqual(actions.sort(), ["create_source", "delete_source", "forget_backup", "update_backup", "update_backup", "update_backup", "update_backup", "update_source"].sort());
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user