"""Unit tests for scripts/common/universal-db-restore and generic restore engine.""" import hashlib import json import os from pathlib import Path import subprocess import sys import tempfile import unittest from unittest.mock import MagicMock, patch ROOT_DIR = Path(__file__).resolve().parents[1] SCRIPT_PATH = ROOT_DIR / 'scripts' / 'common' / 'universal-db-restore' class UniversalDbRestoreTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) self.targets_file = self.root / 'targets.json' self.targets_data = { 'schemaVersion': 1, 'targets': { 'test-pg-prod': { 'application': 'testpg', 'environment': 'production', 'host': 'test-host', 'arch': 'x86_64', 'role': 'production_target', 'require_confirm': 'testpg@test-host', 'db_engine': 'postgresql', 'db_container': 'test-pg-container', 'db_name': 'testpgdb', 'db_user': 'testpguser', 'services_to_stop': ['backend'], 'verify_tables_not_empty': ['users'], 'truncate_tables': ['sessions'], 'ready_url': 'http://127.0.0.1:8999/healthz', }, 'test-maria-prod': { 'application': 'testmaria', 'environment': 'production', 'host': 'test-host', 'arch': 'x86_64', 'role': 'production_target', 'require_confirm': 'testmaria@test-host', 'db_engine': 'mariadb', 'db_container': 'test-maria-container', 'db_name': 'testmariadb', 'db_user': 'testmariauser', 'services_to_stop': ['web'], 'verify_tables_not_empty': ['posts'], 'truncate_tables': ['sessions'], }, }, } self.targets_file.write_text(json.dumps(self.targets_data, indent=2), encoding='utf-8') def test_missing_target_fails_precondition(self): result_file = self.root / 'result.json' cmd = [ sys.executable, str(SCRIPT_PATH), '--target', 'unknown-target', '--result', str(result_file), '--targets-file', str(self.targets_file), ] res = subprocess.run(cmd, capture_output=True, text=True) self.assertEqual(res.returncode, 2) # EXIT_PRECONDITION data = json.loads(result_file.read_text(encoding='utf-8')) self.assertEqual(data['status'], 'error') self.assertIn('not found', data['detail']) def test_confirmation_guard(self): dump_path = self.root / 'test.sql' dump_path.write_text('SELECT 1;', encoding='utf-8') result_file = self.root / 'result.json' env = os.environ.copy() env['SKIP_HOST_GUARD'] = '1' # Without confirmation cmd = [ sys.executable, str(SCRIPT_PATH), '--target', 'test-pg-prod', '--dump', str(dump_path), '--result', str(result_file), '--targets-file', str(self.targets_file), ] res = subprocess.run(cmd, env=env, capture_output=True, text=True) self.assertEqual(res.returncode, 2) data = json.loads(result_file.read_text(encoding='utf-8')) self.assertIn('requires confirmation', data['detail']) # With wrong confirmation cmd_wrong = cmd + ['--confirm', 'wrong-token'] res_wrong = subprocess.run(cmd_wrong, env=env, capture_output=True, text=True) self.assertEqual(res_wrong.returncode, 2) def test_sha256_verification(self): dump_path = self.root / 'test.sql' dump_path.write_text('SELECT 1;', encoding='utf-8') sha_path = self.root / 'test.sql.sha256' sha_path.write_text('0' * 64 + ' test.sql\n', encoding='utf-8') result_file = self.root / 'result.json' env = os.environ.copy() env['SKIP_HOST_GUARD'] = '1' cmd = [ sys.executable, str(SCRIPT_PATH), '--target', 'test-pg-prod', '--dump', str(dump_path), '--confirm', 'testpg@test-host', '--result', str(result_file), '--targets-file', str(self.targets_file), ] res = subprocess.run(cmd, env=env, capture_output=True, text=True) self.assertEqual(res.returncode, 4) # EXIT_CHECKSUM_MISMATCH data = json.loads(result_file.read_text(encoding='utf-8')) self.assertEqual(data['status'], 'error') self.assertIn('SHA256 mismatch', data['detail']) if __name__ == '__main__': unittest.main()