diff --git a/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts b/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts index 24d1cf363..295d094c7 100644 --- a/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts +++ b/server/tests/unit/db/engine/postgres/schema-bootstrap.test.ts @@ -11,9 +11,10 @@ import { } from '../../../../../src/db/engine/postgres/schema-bootstrap'; import type { MigrationSafetyNet } from '../../../../../src/db/legacy-baseline'; import type { AnyOrm } from '../../../../../src/db/orm'; +import { answersSchema, failsWith, installFakeTool, writesFile } from '../../../../helpers/fake-pg-tools'; import { Logger } from '@nestjs/common'; -import { chmodSync, mkdtempSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs'; +import { mkdtempSync, readdirSync, rmSync, utimesSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; @@ -102,8 +103,11 @@ function deps( afterEach(() => vi.restoreAllMocks()); describe('bootstrapPostgresSchema', () => { - it('PGBOOT-001: an empty database gets the baseline and its migrations recorded, all in one transaction', async () => { + beforeEach(() => { vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); + }); + + it('PGBOOT-001: an empty database gets the baseline and its migrations recorded, all in one transaction', async () => { const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [] }); const net = deps(); await bootstrapPostgresSchema(orm, net); @@ -115,7 +119,6 @@ describe('bootstrapPostgresSchema', () => { }); it('PGBOOT-002: a fresh database runs the migrations after the cut without the safety net (nothing to lose)', async () => { - vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2, M3], tables: [] }); const net = deps(); await bootstrapPostgresSchema(orm, net); @@ -135,7 +138,6 @@ describe('bootstrapPostgresSchema', () => { }); it('PGBOOT-008: an empty history table beside no other table is a fresh database (a first boot cut short)', async () => { - vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); const { orm, db } = fakeOrm({ executed: [], pending: [M1, M2], tables: [], history: true }); await bootstrapPostgresSchema(orm, deps()); expect(db.logged).toEqual([M1, M2]); @@ -148,7 +150,6 @@ describe('bootstrapPostgresSchema', () => { }); it('PGBOOT-005: pending migrations on a database in use run after the safety net, labelled by the last one', async () => { - vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); const { orm, db } = fakeOrm({ executed: [M2, M1], pending: [M3], tables: ['users'] }); const net = deps(); await bootstrapPostgresSchema(orm, net); @@ -186,6 +187,7 @@ describe('postgresSafetyNet', () => { beforeEach(() => { dir = mkdtempSync(path.join(tmpdir(), 'trek-pg-net-')); bin = mkdtempSync(path.join(tmpdir(), 'trek-pg-net-bin-')); + vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); }); afterEach(() => { rmSync(dir, { recursive: true, force: true }); @@ -197,13 +199,8 @@ describe('postgresSafetyNet', () => { * connect, beside the psql that names the schema the dump is limited to. */ const fakePgDump = (ok: boolean) => { - const body = ok - ? 'for a in "$@"; do case "$a" in --file=*) printf dump > "${a#--file=}";; esac; done' - : 'echo "could not connect" >&2; exit 1'; - writeFileSync(path.join(bin, 'pg_dump'), `#!/bin/sh\n${body}\n`); - writeFileSync(path.join(bin, 'psql'), "#!/bin/sh\nprintf 'public\\tpublic\\n'\n"); - chmodSync(path.join(bin, 'pg_dump'), 0o755); - chmodSync(path.join(bin, 'psql'), 0o755); + installFakeTool(bin, 'pg_dump', { snippets: [ok ? writesFile('dump') : failsWith('could not connect')] }); + installFakeTool(bin, 'psql', { snippets: [answersSchema()] }); }; const envWith = (preMigrateSnapshot: boolean | undefined, isTest: boolean, keep = 3) => @@ -223,7 +220,6 @@ describe('postgresSafetyNet', () => { it.each([undefined, true])( 'PGBOOT-010: dumps the database before migrating it (setting %s), keeping the newest copies and no half-written one', async (setting) => { - vi.spyOn(Logger.prototype, 'log').mockImplementation(() => undefined); fakePgDump(true); for (const old of ['20200101000001', '20200101000002']) { writeFileSync(path.join(dir, `pre-migrate-${old}-20250101T000000Z.pgdump`), 'old'); diff --git a/server/tests/unit/nest/boot-restore.test.ts b/server/tests/unit/nest/boot-restore.test.ts index 7416bd7a2..f4232e665 100644 --- a/server/tests/unit/nest/boot-restore.test.ts +++ b/server/tests/unit/nest/boot-restore.test.ts @@ -8,6 +8,14 @@ */ import { knownMigrationNames } from '../../../src/db/known-migrations'; import { BootRestoreError, restoreOnFirstBoot } from '../../../src/nest/backup/boot-restore'; +import { + answersSchema, + answersTables, + installFakeTool, + listsMigrations, + touches, + writesRestrictedScript, +} from '../../helpers/fake-pg-tools'; import archiver from 'archiver'; import Database from 'better-sqlite3'; @@ -128,39 +136,19 @@ describe('restoreOnFirstBoot', () => { describe('on Postgres', () => { /** - * Stand-in client tools: psql lists the tables written to `tables` and - * marks `loaded` when it runs a restore; pg_restore lists a known migration - * as the dump's history, or writes a restricted script. Their arguments are - * pinned in pg-tools.test.ts. + * Stand-in client tools (tests/helpers/fake-pg-tools.ts): psql lists + * `tables` and marks `loaded` when it runs a restore; pg_restore lists a + * known migration as the dump's history, or writes a restricted script. + * Their arguments are pinned in pg-tools.test.ts. */ - const postgresPlan = (archivePath: string) => { + const postgresPlan = (archivePath: string, tables: string[] = []) => { const bin = path.join(root, 'bin'); fs.mkdirSync(bin, { recursive: true }); - const write = (name: string, body: string) => { - const version = `[ "$1" = --version ] && { echo '${name} (PostgreSQL) 17.11'; exit 0; }`; - fs.writeFileSync(path.join(bin, name), `#!/bin/sh\n${version}\n${body}\n`); - fs.chmodSync(path.join(bin, name), 0o755); - }; - write( - 'psql', - [ - 'case "$*" in', - " *quote_ident*) printf 'public\\tpublic\\n';;", - ` *--command=*) [ -f '${root}/tables' ] && cat '${root}/tables'; exit 0;;`, - ` *) touch '${root}/loaded';;`, - 'esac', - ].join('\n'), - ); + installFakeTool(bin, 'psql', { + snippets: [answersSchema(), answersTables(tables), touches(path.join(root, 'loaded'))], + }); const migration = [...knownMigrationNames()][0]; - write( - 'pg_restore', - [ - 'case "$*" in', - ` *--data-only*) printf 'COPY public.mikro_orm_migrations (id, name) FROM stdin;\\n1\\t${migration}\\n\\\\.\\n';;`, - ` *) for a in "$@"; do case "$a" in --file=*) printf '%s\\n' '\\restrict k1' 'CREATE TABLE public.mikro_orm_migrations (' > "\${a#--file=}";; esac; done;;`, - 'esac', - ].join('\n'), - ); + installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations([migration]), writesRestrictedScript()] }); return { archive: archivePath, dbFile, @@ -172,8 +160,7 @@ describe('restoreOnFirstBoot', () => { const loaded = () => fs.existsSync(path.join(root, 'loaded')); it('BOOT-RESTORE-004b: a database holding tables is left alone, so a restart never rolls back', async () => { - const plan = postgresPlan(await archive({ 'travel.pgdump': 'dump' })); - fs.writeFileSync(path.join(root, 'tables'), 'users\ntrips\n'); + const plan = postgresPlan(await archive({ 'travel.pgdump': 'dump' }), ['users', 'trips']); const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); expect(await restoreOnFirstBoot(plan)).toEqual({ restored: false, reason: 'database-exists' }); expect(warn).toHaveBeenCalledWith(expect.stringContaining('the Postgres database (2 table(s))')); diff --git a/server/tests/unit/nest/postgres-database-backup.test.ts b/server/tests/unit/nest/postgres-database-backup.test.ts index 18da987a1..207463803 100644 --- a/server/tests/unit/nest/postgres-database-backup.test.ts +++ b/server/tests/unit/nest/postgres-database-backup.test.ts @@ -1,8 +1,8 @@ /** * PostgresDatabaseBackup: the backup port on a Postgres database, over - * stand-in pg_dump, pg_restore and psql scripts (TREK_DB_PG_BIN_DIR) and a - * stand-in DatabaseLifecycle. The tools' own arguments are pinned in - * tests/unit/db/engine/postgres/pg-tools.test.ts. + * stand-in pg_dump, pg_restore and psql tools (TREK_DB_PG_BIN_DIR, built by + * tests/helpers/fake-pg-tools.ts) and a stand-in DatabaseLifecycle. The tools' + * own arguments are pinned in tests/unit/db/engine/postgres/pg-tools.test.ts. */ import { knownMigrationNames } from '../../../src/db/known-migrations'; import type { DataPathsService } from '../../../src/nest/app-config/data-paths.service'; @@ -10,8 +10,16 @@ import { PostgresDatabaseBackup } from '../../../src/nest/backup/postgres-databa import { DomainError } from '../../../src/nest/common/domain-error'; import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface'; import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service'; +import { + answersSchema, + failsWith, + installFakeTool, + listsMigrations, + writesFile, + writesRestrictedScript, +} from '../../helpers/fake-pg-tools'; -import { chmodSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; @@ -23,33 +31,13 @@ let extract: string; let reopen: ReturnType; let backup: PostgresDatabaseBackup; -/** A stand-in tool: `body` is the shell it runs. */ -function tool(name: 'pg_dump' | 'pg_restore' | 'psql', body: string): void { - const version = `[ "$1" = --version ] && { echo '${name} (PostgreSQL) 17.11'; exit 0; }`; - writeFileSync(path.join(bin, name), `#!/bin/sh\n${version}\n${body}\n`); - chmodSync(path.join(bin, name), 0o755); -} -const WRITES_ITS_FILE = 'for a in "$@"; do case "$a" in --file=*) printf dump > "${a#--file=}";; esac; done'; -/** A psql that answers the current-schema query a dump and a restore start with, and otherwise succeeds. */ -const ANSWERS_SCHEMA = 'case "$*" in *quote_ident*) printf \'public\\tpublic\\n\';; esac'; -const FAILS = 'echo "connection refused" >&2; exit 1'; -/** A pg_restore whose --data-only listing shows `names` as the dump's migrations. */ -const LISTS = (names: string[]) => - [ - 'case "$*" in *--data-only*)', - " printf 'COPY public.mikro_orm_migrations (id, name, executed_at) FROM stdin;\\n'", - ...names.map((name, i) => ` printf '${i + 1}\\t${name}\\tx\\n'`), - " printf '\\\\.\\n';;", - 'esac', - ].join('\n'); - beforeEach(() => { root = mkdtempSync(path.join(tmpdir(), 'trek-pg-backup-')); bin = path.join(root, 'bin'); extract = path.join(root, 'extract'); mkdirSync(bin); mkdirSync(extract); - tool('psql', ANSWERS_SCHEMA); + installFakeTool(bin, 'psql', { snippets: [answersSchema()] }); vi.stubEnv('TREK_DB_URL', 'postgresql://trek:pw@db/trek'); vi.stubEnv('TREK_DB_PG_BIN_DIR', bin); reopen = vi.fn(async () => undefined); @@ -78,50 +66,81 @@ describe('PostgresDatabaseBackup', () => { expect(backup.archiveEntry).toBe('travel.pgdump'); expect(backup.canSnapshot()).toBe(true); await expect(backup.checkpoint()).resolves.toBeUndefined(); - tool('pg_dump', WRITES_ITS_FILE); + installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] }); await backup.snapshot(path.join(root, 'a.pgdump')); expect(existsSync(path.join(root, 'a.pgdump'))).toBe(true); - tool('pg_dump', `printf partial > "$(echo "$4" | sed 's/--file=//')"; ${FAILS}`); + installFakeTool(bin, 'pg_dump', { snippets: [writesFile('partial'), failsWith('connection refused')] }); await expect(backup.snapshot(path.join(root, 'b.pgdump'))).rejects.toThrow('pg_dump failed: connection refused'); expect(existsSync(path.join(root, 'b.pgdump'))).toBe(false); expect(existsSync(path.join(root, 'b.pgdump.partial'))).toBe(false); }); - it('PGBACKUP-002: verify takes a TREK dump this build knows, and refuses a missing, SQLite, foreign or newer one', async () => { + it('PGBACKUP-002: verify accepts a TREK dump this build knows', async () => { + writeFileSync(path.join(extract, 'travel.pgdump'), 'dump'); + installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(KNOWN.slice(0, 3))] }); + await expect(backup.verify(extract)).resolves.toBeNull(); + }); + + it('PGBACKUP-002a: verify refuses an archive without a TREK dump in it', async () => { expect(await refusal(backup.verify(extract))).toEqual({ status: 400, message: 'Invalid backup: travel.pgdump not found', }); - writeFileSync(path.join(extract, 'travel.db'), 'sqlite'); - expect((await refusal(backup.verify(extract))).message).toMatch(/holds a SQLite database/); - writeFileSync(path.join(extract, 'travel.pgdump'), 'dump'); - tool('pg_restore', LISTS(KNOWN.slice(0, 3))); - await expect(backup.verify(extract)).resolves.toBeNull(); - tool('pg_restore', LISTS([...KNOWN, 'Migration29990101000000_from_the_future'])); - expect((await refusal(backup.verify(extract))).message).toMatch(/made by a newer TREK version/); - tool('pg_restore', LISTS([])); - expect((await refusal(backup.verify(extract))).message).toMatch(/holds no TREK migration history/); - vi.spyOn(console, 'warn').mockImplementation(() => undefined); - tool('pg_restore', FAILS); - expect((await refusal(backup.verify(extract))).message).toBe('Uploaded file is not a valid Postgres dump'); + }); + + it.each([ + { + id: 'PGBACKUP-002b', + setup: () => writeFileSync(path.join(extract, 'travel.db'), 'sqlite'), + message: /holds a SQLite database/, + }, + { + id: 'PGBACKUP-002c', + setup: () => { + writeFileSync(path.join(extract, 'travel.pgdump'), 'dump'); + installFakeTool(bin, 'pg_restore', { + snippets: [listsMigrations([...KNOWN, 'Migration29990101000000_from_the_future'])], + }); + }, + message: /made by a newer TREK version/, + }, + { + id: 'PGBACKUP-002d', + setup: () => { + writeFileSync(path.join(extract, 'travel.pgdump'), 'dump'); + installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations([])] }); + }, + message: /holds no TREK migration history/, + }, + { + id: 'PGBACKUP-002e', + setup: () => { + writeFileSync(path.join(extract, 'travel.pgdump'), 'dump'); + installFakeTool(bin, 'pg_restore', { snippets: [failsWith('pg_restore: error: not a valid archive')] }); + vi.spyOn(console, 'warn').mockImplementation(() => undefined); + }, + message: 'Uploaded file is not a valid Postgres dump', + }, + ])('$id: verify refuses a dump TREK cannot restore', async ({ setup, message }) => { + setup(); + const refused = await refusal(backup.verify(extract)); + if (typeof message === 'string') expect(refused.message).toBe(message); + else expect(refused.message).toMatch(message); }); it('PGBACKUP-003: keeps a dump of the database a restore replaces, or says it could not', async () => { vi.spyOn(console, 'log').mockImplementation(() => undefined); vi.spyOn(console, 'warn').mockImplementation(() => undefined); - tool('pg_dump', WRITES_ITS_FILE); + installFakeTool(bin, 'pg_dump', { snippets: [writesFile('dump')] }); const kept = await backup.keepCopyBeforeRestore(); expect(kept).toMatch(/pre-restore-\d+\.pgdump$/); expect(existsSync(kept as string)).toBe(true); - tool('pg_dump', FAILS); + installFakeTool(bin, 'pg_dump', { snippets: [failsWith('connection refused')] }); await expect(backup.keepCopyBeforeRestore()).resolves.toBeNull(); }); it('PGBACKUP-004: replace restores, then reopens; a reopen failure is reported, not thrown', async () => { - tool( - 'pg_restore', - "printf '%s\\n' '\\restrict k1' 'CREATE TABLE public.mikro_orm_migrations (' > \"${4#--file=}\"", - ); + installFakeTool(bin, 'pg_restore', { snippets: [writesRestrictedScript()] }); await expect(backup.replace(path.join(extract, 'travel.pgdump'))).resolves.toEqual({ reopenError: null }); expect(reopen).toHaveBeenCalledTimes(1); const lost = new Error('reconnect failed'); @@ -130,7 +149,7 @@ describe('PostgresDatabaseBackup', () => { }); it('PGBACKUP-005: a failed restore still reopens, and both failures surface when the reopen fails too', async () => { - tool('pg_restore', FAILS); + installFakeTool(bin, 'pg_restore', { snippets: [failsWith('connection refused')] }); const source = path.join(extract, 'travel.pgdump'); await expect(backup.replace(source)).rejects.toThrow('pg_restore failed: connection refused'); expect(reopen).toHaveBeenCalledTimes(1);