gitza-mcp/tests/deployment.ts

134 lines
3.9 KiB
TypeScript

// Rehearse the actual deployment definition, including database privileges and restore.
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { parseEnv } from 'node:util';
import { generateSecrets } from '../src/secrets.js';
import { Vault, randomSecret } from '../src/crypto.js';
const root = resolve(import.meta.dirname, '..');
const project = 'gitza-mcp-deploy-test-' + randomSecret().slice(0, 10).toLowerCase();
const directory = await mkdtemp(join(tmpdir(), project));
const secretFile = join(directory, '.env.secrets');
await generateSecrets(secretFile);
const secrets = parseEnv(await readFile(secretFile, 'utf8'));
const env = {
...process.env,
...secrets,
MCP_IMAGE_TAG: project,
MCP_ORIGIN: 'https://mcp.example.test',
GITZA_CLIENT_ID: 'deployment-fixture',
GITZA_CLIENT_SECRET: randomSecret(),
};
function compose(args: string[], input?: string | Buffer, inherited = false) {
const result = spawnSync('docker', ['compose', '-p', project, '-f', 'compose.yml', ...args], {
cwd: root,
env,
input,
encoding: 'utf8',
timeout: 300000,
maxBuffer: 32 * 1024 * 1024,
stdio: inherited ? 'inherit' : 'pipe',
});
assert.equal(result.status, 0, `Deployment check failed: ${args[0]} (output withheld)`);
return result.stdout ?? '';
}
function sql(query: string, database = 'mcp') {
return compose(
[
'exec',
'-T',
'postgres',
'psql',
'-v',
'ON_ERROR_STOP=1',
'-U',
'postgres',
'-d',
database,
'-At',
],
query,
).trim();
}
try {
compose(['config', '--quiet']);
compose(['up', '--build', '-d', '--wait', '--wait-timeout', '120'], undefined, true);
assert.equal(
sql("SELECT rolsuper OR rolcreatedb OR rolcreaterole FROM pg_roles WHERE rolname='mcp'"),
'f',
);
assert.equal(sql('SELECT version FROM schema_migrations'), '1');
compose(['exec', '-T', 'mcp', 'node', 'dist/healthcheck.js']);
const vault = new Vault(secrets.MCP_ENCRYPTION_KEYS!);
const canary = { access_token: randomSecret(), expires_at: Date.now() + 3600000 };
const encrypted = vault.seal(canary, 'account:deployment-test');
sql(
`INSERT INTO upstream_accounts (account_id, login, credential) VALUES ('deployment-test','fixture','${encrypted}')`,
);
// Stream a real custom-format dump through the subprocess pipe into a disposable restore.
const dumped = spawnSync(
'docker',
[
'compose',
'-p',
project,
'-f',
'compose.yml',
'exec',
'-T',
'postgres',
'pg_dump',
'-U',
'postgres',
'-d',
'mcp',
'-Fc',
],
{
cwd: root,
env,
maxBuffer: 32 * 1024 * 1024,
timeout: 30000,
},
);
assert.equal(dumped.status, 0);
sql('CREATE DATABASE mcp_restore OWNER mcp', 'postgres');
compose(
[
'exec',
'-T',
'postgres',
'pg_restore',
'-U',
'postgres',
'-d',
'mcp_restore',
'--exit-on-error',
],
dumped.stdout,
);
const restored = sql(
"SELECT credential FROM upstream_accounts WHERE account_id='deployment-test'",
'mcp_restore',
);
assert.deepEqual(vault.open(restored, 'account:deployment-test'), canary);
compose(['restart', 'postgres', 'mcp']);
compose(['up', '-d', '--wait', '--wait-timeout', '120']);
assert.equal(
sql("SELECT credential FROM upstream_accounts WHERE account_id='deployment-test'"),
encrypted,
);
compose(['exec', '-T', 'mcp', 'node', 'dist/healthcheck.js']);
process.stdout.write(
'Deployment passed: migration ordering, least-privilege DB, readiness, persistent restart and decryptable database restore.\n',
);
} finally {
try {
compose(['down', '-v', '--remove-orphans']);
} finally {
await rm(directory, { recursive: true, force: true });
}
}