"""Offline safety-contract tests. DB execution evidence is in docs/auth-schema-rehearsal.json.""" import hashlib import json import os import pathlib import subprocess import tempfile import unittest # Exercise the actual shell workflow without Docker, host backups, or a database. FAKE_DOCKER = r'''#!/usr/bin/env python3 import json, os, pathlib, sys args = sys.argv[1:] state_path = pathlib.Path(os.environ['FAKE_STATE']) state = json.loads(state_path.read_text()) if state_path.exists() else {'polls': 0, 'tcp_ready': False, 'calls': []} state['calls'].append(args) rc = 0 if args[0] == 'exec': command = ' '.join(args) tcp = '-h 127.0.0.1' in command if 'pg_isready' in args: state['polls'] += 1 # Entrypoint's temporary server accepts sockets immediately, not TCP. if tcp: state['tcp_ready'] = state['polls'] >= 3 and os.environ.get('FAKE_MODE') != 'never_ready' rc = 0 if state['tcp_ready'] else 1 elif 'pg_dump' in command: print('fake full archive') elif 'pg_restore' in args and '--list' in args: sys.stdin.read() print('fake archive TOC') elif 'pg_restore' in args: sys.stdin.read() if not tcp or not state['tcp_ready']: print('pg_restore: transient Unix socket missing', file=sys.stderr) rc = 1 elif os.environ.get('FAKE_MODE') == 'restore_fails': rc = 1 elif 'psql' in command: sys.stdin.read() if not tcp: print('SQL command did not select stable TCP', file=sys.stderr) rc = 1 else: print('{}') state_path.write_text(json.dumps(state)) sys.exit(rc) ''' class RestoreReadiness(unittest.TestCase): def run_workflow(self, mode='transient'): with tempfile.TemporaryDirectory(prefix='auth-readiness-') as directory: work = pathlib.Path(directory) scripts = work/'scripts' scripts.mkdir() for name in ['auth-schema-reconcile.sql', 'auth-schema-verify.sql']: (scripts/name).write_bytes((ROOT/'scripts'/name).read_bytes()) script = (ROOT/'scripts/ci-auth-schema-reconcile.sh').read_text() script = script.replace('backup_root=/opt/backups/canina-auth-reconcile', 'backup_root="$FAKE_BACKUP_ROOT"') (scripts/'ci-auth-schema-reconcile.sh').write_text(script) binaries = work/'bin' binaries.mkdir() fakes = { 'docker': FAKE_DOCKER, 'git': '#!/bin/sh\ncase "$*" in\n "rev-parse --show-toplevel") printf "%s\\n" "$FAKE_ROOT";;\n "rev-parse HEAD") printf "%s\\n" "$FAKE_SHA";;\n "status --porcelain") exit 0;;\n *) exit 2;;\nesac\n', 'sleep': '#!/bin/sh\nexit 0\n', } for name, content in fakes.items(): (binaries/name).write_text(content) (binaries/name).chmod(0o755) sha = 'a'*40 digest = hashlib.sha256((scripts/'auth-schema-reconcile.sql').read_bytes()).hexdigest() backups = work/'backups' backups.mkdir() (backups/f'stage-{digest}.receipt').write_text(sha+'\n') env = dict(os.environ, PATH=str(binaries)+os.pathsep+os.environ['PATH'], GITHUB_ACTIONS='true', FAKE_ROOT=str(work), FAKE_SHA=sha, FAKE_BACKUP_ROOT=str(backups), FAKE_STATE=str(work/'state.json'), FAKE_MODE=mode, STAGE_AUTH_ACCEPTANCE_APPROVED=f'{sha}:{digest}', PRODUCTION_SCHEMA_APPROVED=f'{sha}:{digest}') result = subprocess.run(['bash', str(scripts/'ci-auth-schema-reconcile.sh'), 'production', sha, digest], env=env, text=True, capture_output=True, timeout=60) return result, json.loads((work/'state.json').read_text()) def test_waits_for_final_tcp_server_despite_ready_transient_socket(self): result, state = self.run_workflow() self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(state['polls'], 3) self.assertTrue(state['tcp_ready']) calls = state['calls'] restore = next(i for i, call in enumerate(calls) if '--exit-on-error' in call) self.assertEqual(sum('pg_isready' in c for c in calls[:restore]), 3) for call in calls: command = ' '.join(call) if any(tool in command for tool in ['pg_isready', 'pg_dump', 'psql']) or '--exit-on-error' in call: self.assertIn('-h 127.0.0.1', command) self.assertEqual(calls[-1][0:2], ['rm', '-f']) def test_readiness_timeout_never_restores_or_executes_sql(self): result, state = self.run_workflow('never_ready') self.assertNotEqual(result.returncode, 0) self.assertEqual(state['polls'], 60) self.assertFalse(any('--exit-on-error' in c or 'psql' in ' '.join(c) for c in state['calls'])) self.assertEqual(state['calls'][-1][0:2], ['rm', '-f']) def test_restore_failure_never_executes_sql_or_retries_restore(self): result, state = self.run_workflow('restore_fails') self.assertNotEqual(result.returncode, 0) self.assertEqual(sum('--exit-on-error' in c for c in state['calls']), 1) self.assertFalse(any('psql' in ' '.join(c) for c in state['calls'])) self.assertEqual(state['calls'][-1][0:2], ['rm', '-f']) ROOT = pathlib.Path(__file__).resolve().parents[1] class Safety(unittest.TestCase): def test_shell_syntax(self): subprocess.run(['bash', '-n', str(ROOT/'scripts/ci-auth-schema-reconcile.sh')], check=True) def test_narrow_sql(self): sql=(ROOT/'scripts/auth-schema-reconcile.sql').read_text() for forbidden in ['DROP TABLE', 'DELETE FROM', 'UPDATE public.', 'INSERT INTO', 'migrate deploy', 'db push']: self.assertNotIn(forbidden, '\n'.join(x for x in sql.splitlines() if not x.startswith('--'))) self.assertIn("lock_timeout = '3s'", sql) self.assertIn("statement_timeout = '30s'", sql) self.assertIn('COMMIT;', sql) def test_fail_closed_approvals(self): w=(ROOT/'.gitea/workflows/auth-schema-reconcile.yml').read_text() for expected in ['REVIEWED_AUTH_SCHEMA_COMMIT','PRODUCTION_AUTH_SCHEMA_APPROVAL','STAGE_AUTH_SCHEMA_ACCEPTANCE','StrictHostKeyChecking=yes','IdentityAgent=none']: self.assertIn(expected,w) s=(ROOT/'scripts/ci-auth-schema-reconcile.sh').read_text() self.assertLess(s.index('pg_restore --exit-on-error'),s.index('if [[ $environment == production ]]; then',s.index('pg_restore --exit-on-error'))) self.assertIn('cmp "$backup.expected.json" "$backup.actual.json"',s) def test_real_rehearsal(self): import json r=json.loads((ROOT/'docs/auth-schema-rehearsal.json').read_text()) self.assertTrue(r['pre_patch_missing_columns_failed']) self.assertFalse(r['production_writes']) self.assertGreater(r['schema_backup_bytes'],90000) if __name__=='__main__':unittest.main()