202 lines
15 KiB
TypeScript
202 lines
15 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import Fastify from "fastify"
|
|
import { test } from "node:test"
|
|
import { readFile } from "node:fs/promises"
|
|
import { EventEmitter } from "node:events"
|
|
import { AsyncLocalStorage } from "node:async_hooks"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
import { runInNewContext } from "node:vm"
|
|
import ts from "typescript"
|
|
import { DatabaseRequestGate } from "./database-request-gate.js"
|
|
import * as operation from "./system-database-operation.js"
|
|
import * as restore from "./system-database-restore-execution.js"
|
|
import * as partitionRestore from "./system-database-partition-restore.js"
|
|
import * as maintenance from "../modules/mcp/database-maintenance.js"
|
|
import * as archiveBoundary from "./database-control-archive-boundary.js"
|
|
|
|
// Execute the actual restore/export entry points with inert I/O providers. No
|
|
// configured pool, native subprocess, filesystem journal or network is opened.
|
|
async function fixture() {
|
|
const calls: string[] = []
|
|
// Execute the actual shared singleton wiring in a fresh inert module.
|
|
const shared = {} as { sharedDatabaseGate: DatabaseRequestGate; databaseSchemaReadiness: ReturnType<typeof operation.createDatabaseSchemaReadiness> }
|
|
const sharedSource = await readFile(new URL("./shared-database-gate.ts", import.meta.url), "utf8")
|
|
const sharedCompiled = ts.transpileModule(sharedSource, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
|
|
runInNewContext(sharedCompiled, { exports: shared, require: (name: string) => {
|
|
if (name === "./database-request-gate.js") return { DatabaseRequestGate }
|
|
if (name === "./system-database-operation.js") return operation
|
|
throw Error(`Unapproved shared import ${name}`)
|
|
} })
|
|
const { sharedDatabaseGate: gate, databaseSchemaReadiness } = shared
|
|
let failure: "decode" | "psql" | "migration" | "cleanup" | "journal-applied" | "journal-close" | "revoke" | undefined
|
|
const step = (name: string) => async () => { calls.push(name) }
|
|
const spawn = (command: string, args: string[]) => {
|
|
const child = new EventEmitter() as EventEmitter & { stdout: EventEmitter; stderr: EventEmitter; stdin: { end(): void } }
|
|
child.stdout = new EventEmitter(); child.stderr = new EventEmitter()
|
|
child.stdin = { end() { queueMicrotask(() => {
|
|
const phase = args.includes("--version") ? "version" : args.includes("--list") ? "list" : command === "psql" ? "psql" : args.includes("--clean") ? "decode" : "dump"
|
|
calls.push(phase)
|
|
if (phase === "dump") assert.deepEqual(Array.from(args), ["-Fc", archiveBoundary.DATABASE_CONTROL_DUMP_EXCLUDE_SCHEMA_ARG, "inert-owned-database"])
|
|
if (failure === phase) { child.stderr.emit("data", Buffer.from(`synthetic ${phase} failure`)); child.emit("close", 1); return }
|
|
if (phase === "list") child.stdout.emit("data", Buffer.from("1; 1259 1 TABLE public servers owner\n2; 1259 2 TABLE public schema_migrations owner\n"))
|
|
child.emit("close", 0)
|
|
}) } }
|
|
return child
|
|
}
|
|
const files = {
|
|
open: async () => { let state = ""; return { writeFile: async (raw: string) => { state = JSON.parse(raw).state }, sync: async () => { if (failure === "journal-applied" && state === "applied") throw Error("synthetic applied fsync failure") }, close: async () => { if (failure === "journal-close") throw Error("synthetic close failure") } } },
|
|
chmod: async () => {}, mkdir: async () => {}, writeFile: async () => {},
|
|
rm: async () => { if (failure === "cleanup") throw Error("synthetic cleanup failure") },
|
|
}
|
|
const modules: Record<string, unknown> = {
|
|
"node:crypto": { randomUUID: () => "owned-inert-fixture" }, "node:async_hooks": { AsyncLocalStorage },
|
|
pg: { Pool: class { constructor() { throw Error("Real SQL forbidden") } } },
|
|
"node:fs/promises": files, "node:os": os, "node:path": path, "node:child_process": { spawn },
|
|
"./bounded-database-dump.js": {
|
|
boundedDatabaseCommand: (command: string, args: string[]) => new Promise<Buffer>((resolve, reject) => {
|
|
const child = spawn(command, args), chunks: Buffer[] = []
|
|
child.stdout.on("data", chunk => chunks.push(chunk))
|
|
child.once("close", code => code === 0 ? resolve(Buffer.concat(chunks)) : reject(Error("Synthetic command failure")))
|
|
child.stdin.end()
|
|
}),
|
|
},
|
|
"../modules/events/service/events-service.js": { appendEvent: async () => {} },
|
|
"./database-dump-connection.js": {
|
|
runPrivateDatabaseDump: async () => { throw Error("Unused private dump") },
|
|
restoreDatabaseToolEnvironment: () => ({}),
|
|
validatePrivateRestoreConnection: () => {},
|
|
withPrivateRestoreService: (_connection: string, work: (service: { dsn: string; env: object }) => Promise<unknown>) => work({ dsn: "inert-owned-database", env: {} }),
|
|
},
|
|
"./database-control-archive-boundary.js": archiveBoundary,
|
|
"./shared-database-gate.js": { sharedDatabaseGate: gate, databaseSchemaReadiness },
|
|
"./config-mutation-policy.js": { assertDatabaseRestoreAllowed: async () => {} },
|
|
"../modules/mcp/security.js": { accessGeneration: async () => "fixture", invalidateMcpAccess: async () => { if (failure === "revoke") throw Error("synthetic revoke failure") }, mcpStateDirectory: () => "inert-state" },
|
|
"../modules/mcp/database-maintenance.js": maintenance,
|
|
"../db/migrate.js": { applySqlMigrations: async () => { calls.push("migration"); if (failure === "migration") throw Error("synthetic migration failure") } },
|
|
"./system-database-restore-execution.js": restore,
|
|
"../config.js": { env: { DATABASE_URL: "inert-owned-database" } },
|
|
"../db/index.js": { pool: { connect: () => { throw Error("Real SQL forbidden") } } },
|
|
"../db/operation-lock.js": { withSystemDatabaseOperationLock: (_connect: unknown, work: (owner: object, discard: () => void) => unknown) => work({}, () => {}) },
|
|
"../db/database-restore-quarantine.js": { databaseRestoreQuarantineEnabled: () => false },
|
|
"./database-restore-target.js": { withDatabaseRestoreTarget: (_owner: unknown, _discard: unknown, work: (sql: string) => unknown) => work("SELECT 1") },
|
|
"./system-database-operation.js": operation,
|
|
"./scheduler.js": { stopSchedulerForDatabaseOperation: step("stop scheduler"), restartSchedulerAfterDatabaseOperation: step("start scheduler") },
|
|
"./traffic-flow-ripe.js": { pauseRipeBackgroundWork: step("pause ripe"), resumeRipeBackgroundWork: () => { calls.push("resume ripe") } },
|
|
"./system-database-cache.js": { invalidateSystemDatabaseCaches: () => { calls.push("invalidate") } },
|
|
"./system-database-partition-restore.js": partitionRestore,
|
|
"./traffic-flow-ingest.js": { stopTrafficFlowListenerForDatabaseOperation: step("stop listener"), restartTrafficFlowListenerAfterDatabaseOperation: step("start listener") },
|
|
}
|
|
const exports = {} as { restoreSystemDatabaseBackup(buffer: Buffer): Promise<Record<string, unknown>>; exportSystemDatabaseBackup(): Promise<unknown> }
|
|
const source = await readFile(new URL("./system-database-backup.ts", import.meta.url), "utf8")
|
|
const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, esModuleInterop: true } }).outputText
|
|
runInNewContext(compiled, { exports, require: (name: string) => { assert(name in modules, `Unapproved import ${name}`); return modules[name] }, Buffer, process: { env: {} }, Error, Date, Promise }, { filename: "system-database-backup.ts" })
|
|
return { calls, exports, gate, fail: (phase?: typeof failure) => { failure = phase } }
|
|
}
|
|
const archive = Buffer.from("PGDMPinert")
|
|
const restartCalls = (calls: string[]) => calls.filter(c => ["start listener", "start scheduler", "resume ripe"].includes(c))
|
|
|
|
test("actual restore leaves consumers stopped after migration failure, including subsequent export", async () => {
|
|
const f = await fixture(); f.fail("migration")
|
|
const result = await f.exports.restoreSystemDatabaseBackup(archive)
|
|
assert.match(String(result.migrationError), /миграции/); assert.match(String(result.schedulerError), /не подтверждена/)
|
|
assert.equal(f.calls.filter(c => c === "psql").length, 1); assert.deepEqual(restartCalls(f.calls), [])
|
|
let dispatched = 0
|
|
await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/)
|
|
assert.equal(dispatched, 0); assert.equal(f.gate.isPaused(), true)
|
|
let resumed = false
|
|
const waiter = f.gate.whenOpen().then(() => { resumed = true })
|
|
f.fail(); await f.exports.exportSystemDatabaseBackup(); assert.deepEqual(restartCalls(f.calls), [])
|
|
await Promise.resolve(); assert.equal(resumed, false)
|
|
void waiter
|
|
await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/)
|
|
assert.equal(dispatched, 0)
|
|
})
|
|
test("actual restore preserves unknown replacement failure without restarting or retrying", async () => {
|
|
const f = await fixture(); f.fail("psql")
|
|
await assert.rejects(f.exports.restoreSystemDatabaseBackup(archive), /Команда восстановления PostgreSQL psql не выполнена/)
|
|
assert.equal(f.calls.filter(c => c === "psql").length, 1); assert(!f.calls.includes("migration")); assert.deepEqual(restartCalls(f.calls), [])
|
|
assert.equal(f.gate.isPaused(), true)
|
|
let dispatched = 0
|
|
await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/)
|
|
assert.equal(dispatched, 0)
|
|
// Repair uses the existing exclusive service boundary, outside request admission.
|
|
let resumed = false
|
|
const waiter = f.gate.whenOpen().then(() => { resumed = true })
|
|
await Promise.resolve(); assert.equal(resumed, false)
|
|
f.fail("revoke"); await assert.rejects(f.exports.restoreSystemDatabaseBackup(archive), /synthetic revoke failure/)
|
|
await Promise.resolve(); assert.equal(resumed, false); assert.equal(f.calls.filter(c => c === "psql").length, 1)
|
|
await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/)
|
|
f.fail(); await f.exports.restoreSystemDatabaseBackup(archive)
|
|
await waiter; assert.equal(resumed, true); assert.equal(f.gate.isPaused(), false)
|
|
await f.gate.request(async () => { dispatched++ }); assert.equal(dispatched, 1)
|
|
})
|
|
test("archive decoding failure before target replacement permits normal restart", async () => {
|
|
const f = await fixture(); f.fail("decode")
|
|
await assert.rejects(f.exports.restoreSystemDatabaseBackup(archive), /Команда восстановления PostgreSQL pg_restore не выполнена/)
|
|
assert(!f.calls.includes("psql")); assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"])
|
|
await f.gate.request(async () => {})
|
|
})
|
|
test("acknowledged schema restarts consumers even when temporary cleanup fails", async () => {
|
|
const f = await fixture(); f.fail("cleanup")
|
|
const result = await f.exports.restoreSystemDatabaseBackup(archive)
|
|
assert.match(String(result.cleanupError), /временный файл/); assert.equal(result.migrationError, undefined)
|
|
assert(f.calls.indexOf("migration") < f.calls.indexOf("start listener"))
|
|
await f.gate.request(async () => {})
|
|
assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"])
|
|
})
|
|
test("ordinary backup retains its restart behavior", async () => {
|
|
const f = await fixture(); await f.exports.exportSystemDatabaseBackup()
|
|
assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"])
|
|
})
|
|
test("a separate explicit restore clears the latch only after acknowledged migrations", async () => {
|
|
const f = await fixture(); f.fail("migration")
|
|
await f.exports.restoreSystemDatabaseBackup(archive); assert.deepEqual(restartCalls(f.calls), [])
|
|
f.calls.length = 0; f.fail()
|
|
const result = await f.exports.restoreSystemDatabaseBackup(archive)
|
|
assert.equal(result.migrationError, undefined); assert.equal(result.schedulerError, undefined)
|
|
assert(f.calls.indexOf("migration") < f.calls.indexOf("start listener"))
|
|
await f.gate.request(async () => {})
|
|
assert.deepEqual(restartCalls(f.calls), ["start listener", "resume ripe", "start scheduler"])
|
|
})
|
|
|
|
for (const phase of ["journal-applied", "journal-close"] as const) test(`acknowledged replacement with ${phase} stays closed until explicit repair`, async () => {
|
|
const f = await fixture(); f.fail(phase)
|
|
const result = await f.exports.restoreSystemDatabaseBackup(archive)
|
|
assert.equal(typeof result.maintenanceError, "string"); assert.deepEqual(restartCalls(f.calls), [])
|
|
let dispatched = 0, resumed = false
|
|
await assert.rejects(f.gate.request(async () => { dispatched++ }), /не подтверждена/)
|
|
const waiting = f.gate.whenOpen().then(() => { resumed = true })
|
|
await Promise.resolve(); assert.equal(resumed, false); assert.equal(dispatched, 0)
|
|
f.fail(); await f.exports.restoreSystemDatabaseBackup(archive); await waiting
|
|
await f.gate.request(async () => { dispatched++ }); assert.equal(dispatched, 1)
|
|
})
|
|
|
|
test("actual HTTP gate preserves only the existing backup/restore repair exclusions", async () => {
|
|
const f = await fixture(), app = Fastify()
|
|
const plugin = {} as { installDatabaseRequestGate(app: ReturnType<typeof Fastify>): void }
|
|
const source = await readFile(new URL("../plugins/database-request-gate.ts", import.meta.url), "utf8")
|
|
const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
|
|
runInNewContext(compiled, { exports: plugin, require: (name: string) => {
|
|
if (name === "../services/shared-database-gate.js") return { sharedDatabaseGate: f.gate }
|
|
if (name === "../services/config-mutation-policy.js") return { withConfigurationActor: (_actor: string, work: () => unknown) => work() }
|
|
if (name === "../modules/mcp/database-request-lease.js") return { withDatabaseApiRequest: (work: () => unknown) => work() }
|
|
throw Error(`Unapproved HTTP adapter import ${name}`)
|
|
} })
|
|
plugin.installDatabaseRequestGate(app)
|
|
let ordinarySql = 0
|
|
app.get("/api/system/database/status", async () => { ordinarySql++; return { ok: true } })
|
|
app.post("/api/system/database/restore", async () => f.exports.restoreSystemDatabaseBackup(archive))
|
|
app.get("/api/system/database/backup", async () => f.exports.exportSystemDatabaseBackup())
|
|
try {
|
|
f.fail("psql")
|
|
assert.equal((await app.inject({ method: "POST", url: "/api/system/database/restore" })).statusCode, 500)
|
|
assert.equal((await app.inject("/api/system/database/status")).statusCode, 503); assert.equal(ordinarySql, 0)
|
|
f.fail()
|
|
assert.equal((await app.inject("/api/system/database/backup")).statusCode, 200)
|
|
assert.equal((await app.inject("/api/system/database/status")).statusCode, 503); assert.equal(ordinarySql, 0)
|
|
assert.equal((await app.inject({ method: "POST", url: "/api/system/database/restore" })).statusCode, 200)
|
|
assert.equal((await app.inject("/api/system/database/status")).statusCode, 200); assert.equal(ordinarySql, 1)
|
|
} finally { await app.close() }
|
|
})
|