forked from mtusi2026/mtusi2026lab7
107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
import { test, expect } from "bun:test";
|
|
import {
|
|
createFailoverReader,
|
|
failoverReceiptPath,
|
|
type FailoverProof,
|
|
type ReplicaBaseline,
|
|
} from "../scripts/failover-proof";
|
|
const operation = "11111111-1111-4111-8111-111111111111",
|
|
attempt = "22222222-2222-4222-8222-222222222222";
|
|
const baseline: ReplicaBaseline = {
|
|
api1: {
|
|
container: "a".repeat(64),
|
|
alias: "33333333-3333-4333-8333-333333333333",
|
|
},
|
|
api2: {
|
|
container: "b".repeat(64),
|
|
alias: "44444444-4444-4444-8444-444444444444",
|
|
},
|
|
};
|
|
function valid(): FailoverProof {
|
|
return {
|
|
operation_id: operation,
|
|
attempt_id: attempt,
|
|
service: "api1",
|
|
target_alias: baseline.api1.alias,
|
|
container: baseline.api1.container,
|
|
before_running: true,
|
|
running: false,
|
|
volumes_removed: false,
|
|
};
|
|
}
|
|
test("read current receipt contents and bind its stopped replica to the current attempt baseline", async () => {
|
|
const calls: string[] = [];
|
|
const r = await createFailoverReader({
|
|
read: async (path) => {
|
|
calls.push(path);
|
|
return valid();
|
|
},
|
|
})(operation, attempt, baseline);
|
|
expect(r.proof.service).toBe("api1");
|
|
expect(calls).toEqual([failoverReceiptPath(operation, attempt)]);
|
|
});
|
|
test.each([
|
|
{
|
|
name: "old operation",
|
|
patch: { operation_id: attempt },
|
|
code: "FAILOVER_WRONG_OPERATION",
|
|
},
|
|
{
|
|
name: "foreign attempt",
|
|
patch: { attempt_id: operation },
|
|
code: "FAILOVER_WRONG_OPERATION",
|
|
},
|
|
{
|
|
name: "wrong alias",
|
|
patch: { target_alias: baseline.api2.alias },
|
|
code: "FAILOVER_TARGET_MISMATCH",
|
|
},
|
|
{
|
|
name: "wrong container",
|
|
patch: { container: baseline.api2.container },
|
|
code: "FAILOVER_TARGET_MISMATCH",
|
|
},
|
|
{
|
|
name: "still running",
|
|
patch: { running: true },
|
|
code: "FAILOVER_STOP_NOT_PROVED",
|
|
},
|
|
{
|
|
name: "not initially running",
|
|
patch: { before_running: false },
|
|
code: "FAILOVER_STOP_NOT_PROVED",
|
|
},
|
|
{
|
|
name: "foreign service",
|
|
patch: { service: "worker" },
|
|
code: "FAILOVER_SERVICE_INVALID",
|
|
},
|
|
])("reject $name receipt even when a file exists", async ({ patch, code }) => {
|
|
await expect(
|
|
createFailoverReader({ read: async () => ({ ...valid(), ...patch }) })(
|
|
operation,
|
|
attempt,
|
|
baseline,
|
|
),
|
|
).rejects.toThrow(code);
|
|
});
|
|
test("missing receipt preserves reader error instead of accepting older stable files", async () => {
|
|
const error = new Error("FIXTURE_ENOENT");
|
|
let actual: unknown;
|
|
try {
|
|
await createFailoverReader({
|
|
read: async () => {
|
|
throw error;
|
|
},
|
|
})(operation, attempt, baseline);
|
|
} catch (e) {
|
|
actual = e;
|
|
}
|
|
expect(actual).toBe(error);
|
|
});
|
|
test("required read port has no live filesystem fallback", () => {
|
|
expect(() => createFailoverReader({} as never)).toThrow(
|
|
"FAILOVER_READ_PORT_REQUIRED",
|
|
);
|
|
});
|