27 lines
2.6 KiB
JavaScript
27 lines
2.6 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { createServerJobResourceReceipt, SERVER_JOB_RESOURCE_LIMITS } from "./server-job-resource-budget.mjs";
|
|
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
|
const reportPath = path.join(root, "tests/golden/M13-04C/server-job-resource-budget-report.json");
|
|
const manifestPath = path.join(root, "tests/golden/M13-04C/manifest.json");
|
|
const hashFile = async (file) => crypto.createHash("sha256").update(await fs.readFile(file)).digest("hex");
|
|
const budget = { schemaVersion: 1, ...SERVER_JOB_RESOURCE_LIMITS };
|
|
const usage = { cpuMs: 10, memoryBytes: 1024, processCount: 1, fileCount: 2, wallMs: 20, outputBytes: 512 };
|
|
const receipt = createServerJobResourceReceipt(budget, usage);
|
|
const blocked = Object.fromEntries(Object.keys(SERVER_JOB_RESOURCE_LIMITS).map((field) => {
|
|
try { createServerJobResourceReceipt(budget, { ...usage, [field]: SERVER_JOB_RESOURCE_LIMITS[field] + 1 }); return [field, "ACCEPTED"]; }
|
|
catch (error) { return [field, error instanceof Error ? error.message.split(":", 1)[0] : String(error)]; }
|
|
}));
|
|
assert.deepEqual(Object.values(blocked), Object.keys(SERVER_JOB_RESOURCE_LIMITS).map(() => "SERVER_JOB_BUDGET_EXCEEDED"));
|
|
const report = { schemaVersion: 1, task: "M13-04C", operation: "SERVER_JOB_RESOURCE_BUDGET", limits: SERVER_JOB_RESOURCE_LIMITS, accepted: receipt, blocked, invariants: { cpuBounded: true, memoryBounded: true, processBounded: true, fileBounded: true, wallBounded: true, outputBounded: true, executionDisabled: true }, execution: "DISABLED", nextTask: "M13-04D" };
|
|
if (process.env.UPDATE_M13_04C_REPORT === "1") { await fs.mkdir(path.dirname(reportPath), { recursive: true }); await fs.writeFile(reportPath, `${JSON.stringify(report, null, 2)}\n`); }
|
|
assert.deepEqual(JSON.parse(await fs.readFile(reportPath, "utf8")), report);
|
|
const manifest = JSON.parse(await fs.readFile(manifestPath, "utf8"));
|
|
assert.deepEqual({ schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-04C", parentTask: "M13-04B", nextTask: "M13-04D" });
|
|
for (const artifact of Object.values(manifest.artifacts)) assert.equal(await hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`);
|
|
process.stdout.write(`server-job-budget-ok cpu=bounded memory=bounded process=bounded files=bounded wall=bounded output=bounded overages=6 execution=DISABLED next=${manifest.nextTask}\n`);
|