Add Back up now for Fragnet game servers
Operators can ask the game panel for a backup of a server whose plan has room for backups (Back up on the row, Back up now beside its backups). The panel makes it in the background; the page re-reads every 20 s until it is done. Refused here for a plan with no room, a suspended server, one already being backed up, or the same server within 30 s; the panel's own refusals (plan full, rate limit, permission) are shown in its words. Written to the audit log. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
31f4debb9a
commit
f9991a49af
9 files changed
+326
-11
No files matched your search
@@ -149,6 +149,8 @@ describe("what the panel's API says", () => {
|
||||
let databasesStatus = 200;
|
||||
let rejectIncludes = false;
|
||||
let powerStatus = 204;
|
||||
let backupStatus = 200;
|
||||
let backupDetail = "";
|
||||
const seen: { method: string | undefined; path: string; auth: string | undefined; body: string }[] = [];
|
||||
before(async () => {
|
||||
fake = createHttpServer((req, res) => {
|
||||
@@ -171,6 +173,7 @@ describe("what the panel's API says", () => {
|
||||
const m = /^\/api\/client\/servers\/([a-z0-9]{8})\/(resources|backups|schedules|databases|power)$/.exec(u.pathname);
|
||||
if (!m) return send(404, {});
|
||||
if (m[2] === "power") return req.method === "POST" ? send(powerStatus, {}) : send(405, {});
|
||||
if (m[2] === "backups" && req.method === "POST") return backupStatus === 200 ? send(200, apiBackup("new-backup", "2026-06-10T12:00:00Z", null, false)) : send(backupStatus, { errors: [{ code: "Refused", status: String(backupStatus), detail: backupDetail }] });
|
||||
if (m[2] === "resources") return send(200, apiResources(m[1] === "abcd1234" ? "running" : "offline"));
|
||||
if (m[2] === "backups") return send(200, { object: "list", data: [apiBackup("b1", "2026-06-01T00:00:00Z", "2026-06-01T00:03:00Z")], meta: { pagination: { current_page: 1, total_pages: 1 } } });
|
||||
if (m[2] === "schedules") return send(200, { object: "list", data: [{ attributes: { id: 1, name: "Restart", cron: { minute: "0", hour: "4", day_of_month: "*", month: "*", day_of_week: "*" }, is_active: true } }] });
|
||||
@@ -222,6 +225,51 @@ describe("what the panel's API says", () => {
|
||||
assert.equal((await adapter.createFragnetAdapter({ token: "t", url: "http://127.0.0.1:1" }).ping()).ok, false);
|
||||
});
|
||||
|
||||
test("a backup is a POST of a name to that server's backups; what comes back is the one being made, and what the panel says when it refuses is said", async () => {
|
||||
seen.length = 0;
|
||||
const made = await adapter.createFragnetAdapter({ token: "t", url }).backup("abcd1234", " Homelab Manager — test ");
|
||||
assert.deepEqual(seen.map((x) => [x.method, x.path, x.body]), [["POST", "/api/client/servers/abcd1234/backups", JSON.stringify({ name: "Homelab Manager — test" })]]);
|
||||
assert.deepEqual([made?.uuid, made?.completedAt, made?.successful], ["new-backup", null, false], "being made: no completion time yet");
|
||||
const refuse = async (status: number, detail = "") => {
|
||||
backupStatus = status;
|
||||
backupDetail = detail;
|
||||
try {
|
||||
await adapter.createFragnetAdapter({ token: "t", url }).backup("abcd1234", "x");
|
||||
return "";
|
||||
} catch (err) {
|
||||
return (err as Error).message;
|
||||
} finally {
|
||||
backupStatus = 200;
|
||||
}
|
||||
};
|
||||
assert.equal(await refuse(400, "Cannot create a new backup, this server has reached its limit of 3 backups."), "The panel refused to make a backup: Cannot create a new backup, this server has reached its limit of 3 backups.");
|
||||
assert.match(await refuse(400), /refused to make a backup: HTTP 400/);
|
||||
assert.match(await refuse(403), /may not make backups on this server \(HTTP 403\).*Backups → Create/);
|
||||
assert.match(await refuse(429), /only lets a few backups be made in a short while \(HTTP 429\)/);
|
||||
assert.match(await refuse(409), /busy \(HTTP 409\)/);
|
||||
assert.match(await refuse(404), /does not know that server/);
|
||||
assert.match(await refuse(500, "boom"), /HTTP 500 — boom/);
|
||||
keyOk = false;
|
||||
assert.match(await refuse(200), /did not accept the key \(HTTP 401\)/);
|
||||
keyOk = true;
|
||||
});
|
||||
|
||||
test("nothing but a server id of the panel and a name is accepted for a backup", async () => {
|
||||
seen.length = 0;
|
||||
await assert.rejects(() => adapter.createFragnetAdapter({ token: "t", url }).backup("../../x", "n"), /isn't a server id/);
|
||||
await assert.rejects(() => adapter.createFragnetAdapter({ token: "t", url }).backup("abcd1234", " "), /needs a name/);
|
||||
assert.deepEqual(seen, []);
|
||||
assert.equal((await adapter.createFragnetAdapter({ token: "t", url }).backup("abcd1234", "n".repeat(300)))?.uuid, "new-backup");
|
||||
assert.equal(JSON.parse(seen[0].body).name.length, 100, "cut to 100 characters");
|
||||
});
|
||||
|
||||
test("the panel's own words and a new backup are read from what it sends", () => {
|
||||
assert.equal(adapter.panelDetail({ errors: [{ code: "TooManyBackupsException", detail: "limit reached" }] }), "limit reached");
|
||||
assert.equal(adapter.panelDetail({ errors: [] }), null);
|
||||
assert.equal(adapter.panelDetail(null), null);
|
||||
assert.equal(adapter.parseNewBackup({ object: "backup", attributes: { name: "no uuid" } }), null);
|
||||
});
|
||||
|
||||
test("a power signal is a POST of exactly that signal to that server; a busy server and an unknown one are said; nothing else is accepted", async () => {
|
||||
seen.length = 0;
|
||||
await adapter.createFragnetAdapter({ token: "t", url }).power("abcd1234", "restart");
|
||||
@@ -503,3 +551,123 @@ describe("the health pass, the page, the power signals and /metrics", () => {
|
||||
assert.deepEqual(await db.select().from(fragnetState).where(eq(fragnetState.integrationId, fgId)), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("back up now", () => {
|
||||
let app: Server;
|
||||
let panel: Server;
|
||||
let base = "";
|
||||
let id = 0;
|
||||
let panelStatus = 200;
|
||||
let panelDetailText = "";
|
||||
const asked: string[] = [];
|
||||
const post = (sub: string, server: string, integration = id) => fetch(`${base}/${integration}/servers/${server}/backup`, { method: "POST", headers: { "x-sub": sub } });
|
||||
|
||||
before(async () => {
|
||||
panel = createHttpServer((req, res) => {
|
||||
let body = "";
|
||||
req.on("data", (c) => (body += c));
|
||||
req.on("end", () => {
|
||||
asked.push(`${req.method} ${req.url} ${body}`);
|
||||
res.writeHead(panelStatus, { "content-type": "application/json" });
|
||||
res.end(JSON.stringify(panelStatus === 200 ? apiBackup("made-1", new Date().toISOString(), null, false) : { errors: [{ code: "Refused", detail: panelDetailText }] }));
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve) => panel.listen(0, "127.0.0.1", resolve));
|
||||
const panelUrl = `http://127.0.0.1:${(panel.address() as AddressInfo).port}`;
|
||||
id = (await db.insert(integrations).values({ type: "fragnet", name: "Backups account", baseUrl: panelUrl, config: JSON.stringify({ token: "ptlc_x", url: panelUrl }) }).returning({ id: integrations.id }))[0].id;
|
||||
const now = Date.now();
|
||||
await fn.storeRead(
|
||||
id,
|
||||
read(
|
||||
server({ id: "aaaa1111", name: "fine", backups: [okBackup(DAY)] }),
|
||||
server({ id: "bbbb2222", name: "no-room", backupLimit: 0 }),
|
||||
server({ id: "cccc3333", name: "busy", backups: [okBackup(DAY), { uuid: "going", name: null, bytes: 0, createdAt: iso(now - 10 * MIN), completedAt: null, successful: false, locked: false }] }),
|
||||
server({ id: "dddd4444", name: "stale", backups: [{ uuid: "lost", name: null, bytes: 0, createdAt: iso(now - 3 * HOUR), completedAt: null, successful: false, locked: false }] }),
|
||||
server({ id: "eeee5555", name: "suspended", suspended: true }),
|
||||
server({ id: "ffff6666", name: "refused" }),
|
||||
server({ id: "gggg7777", name: "twice" }),
|
||||
),
|
||||
new Date(),
|
||||
);
|
||||
const a = express();
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: String(req.headers["x-sub"] ?? "viewer") } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/fragnet", fragnetRouter);
|
||||
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/fragnet`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
panel.close();
|
||||
});
|
||||
|
||||
test("an operator may, a viewer may not; the panel is asked for a backup with a name that says it was asked for here, and the audit log has it", async () => {
|
||||
assert.equal((await post("viewer", "aaaa1111")).status, 403);
|
||||
assert.deepEqual(asked, []);
|
||||
const res = await post("operator", "aaaa1111");
|
||||
assert.equal(res.status, 201);
|
||||
const body = (await res.json()) as { name: string; uuid: string | null };
|
||||
assert.match(body.name, /^Homelab Manager — \d{4}-\d{2}-\d{2} \d{2}:\d{2} UTC$/);
|
||||
assert.equal(body.uuid, "made-1");
|
||||
assert.deepEqual(asked, [`POST /api/client/servers/aaaa1111/backups ${JSON.stringify({ name: body.name })}`]);
|
||||
const entry = (await db.select().from(auditLog)).filter((e) => e.action === "backup_game_server").pop()!;
|
||||
assert.deepEqual([entry.category, entry.targetType, entry.targetId], ["integration", "fragnet_server", "aaaa1111"]);
|
||||
assert.match(entry.detail ?? "", /"name":"fine"/);
|
||||
assert.match(entry.detail ?? "", /"backup":"made-1"/);
|
||||
});
|
||||
|
||||
test("a server asked for a backup a moment ago is not asked again — and is, once the wait is over; another server is not held up", async () => {
|
||||
asked.length = 0;
|
||||
assert.equal((await post("operator", "gggg7777")).status, 201);
|
||||
const again = await post("operator", "gggg7777");
|
||||
assert.equal(again.status, 429);
|
||||
const body = (await again.json()) as { error: string; retryAfterSeconds: number };
|
||||
assert.equal(body.error, "too_soon");
|
||||
assert.ok(body.retryAfterSeconds >= 1 && body.retryAfterSeconds <= 30);
|
||||
assert.equal(again.headers.get("retry-after"), String(body.retryAfterSeconds));
|
||||
assert.equal(asked.length, 1, "the panel was not asked the second time");
|
||||
const { BACKUP_COOLDOWN_MS } = await import("../src/routes/fragnet.js");
|
||||
assert.equal(BACKUP_COOLDOWN_MS, 30_000);
|
||||
});
|
||||
|
||||
test("a plan with no room for backups, a backup already being made, and a server that is suspended are refused before the panel is asked — a backup lost long ago is not in the way", async () => {
|
||||
asked.length = 0;
|
||||
const noRoom = await post("operator", "bbbb2222");
|
||||
assert.deepEqual([noRoom.status, ((await noRoom.json()) as { error: string }).error], [400, "no_backups_on_plan"]);
|
||||
const busy = await post("operator", "cccc3333");
|
||||
assert.deepEqual([busy.status, ((await busy.json()) as { error: string }).error], [409, "already_being_made"]);
|
||||
const suspended = await post("operator", "eeee5555");
|
||||
assert.deepEqual([suspended.status, ((await suspended.json()) as { error: string }).error], [409, "server_busy"]);
|
||||
assert.deepEqual(asked, []);
|
||||
assert.equal((await post("operator", "dddd4444")).status, 201, "one 'being made' for three hours is taken to be lost");
|
||||
});
|
||||
|
||||
test("what the panel says when it refuses is the answer, and a refusal is not held against the next try", async () => {
|
||||
asked.length = 0;
|
||||
panelStatus = 400;
|
||||
panelDetailText = "Cannot create a new backup, this server has reached its limit of 3 backups.";
|
||||
const refused = await post("operator", "ffff6666");
|
||||
assert.equal(refused.status, 502);
|
||||
assert.match(((await refused.json()) as { error: string }).error, /reached its limit of 3 backups/);
|
||||
panelStatus = 200;
|
||||
assert.equal((await post("operator", "ffff6666")).status, 201, "nothing was made, so there is no wait");
|
||||
assert.equal(asked.length, 2);
|
||||
const refusalsLogged = (await db.select().from(auditLog)).filter((e) => e.action === "backup_game_server" && /ffff6666/.test(e.targetId ?? ""));
|
||||
assert.equal(refusalsLogged.length, 1, "only the backup that was asked for is in the audit log");
|
||||
});
|
||||
|
||||
test("only a server the last read found, and only a well-formed id, an integration that is there and one of this kind are accepted", async () => {
|
||||
asked.length = 0;
|
||||
assert.equal((await post("operator", "zzzz9999")).status, 404);
|
||||
assert.equal((await post("operator", "..%2F..%2Fx")).status, 400);
|
||||
assert.equal((await post("operator", "aaaa1111", 999999)).status, 404);
|
||||
const other = (await db.insert(integrations).values({ type: "hetzner", name: "Other", baseUrl: "https://api.hetzner.cloud", config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id;
|
||||
assert.equal((await post("operator", "aaaa1111", other)).status, 400);
|
||||
await db.delete(integrations).where(eq(integrations.id, other));
|
||||
assert.deepEqual(asked, []);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user