import copy import json import unittest from unittest.mock import Mock import test_migration from core.compatibility import canonical_hash from core.locking import DeploymentLock, LockError from core.migration_recovery import ( MigrationRecovery, MigrationRecoveryError, ) class MigrationRecoveryTests(unittest.TestCase): def setUp(self): self.fixture = test_migration.MigrationTests() self.addCleanup(self.fixture.doCleanups) self.fixture.setUp() self.identifier = self.fixture.fixture.identifier self.store = self.fixture.fixture.store self.migration_result = self.fixture.execute() self.identity = Mock( return_value=self.migration_result[ "database_identity" ] ) self.history = Mock( return_value=list(self.fixture.after) ) self.provenance = Mock( return_value={ "revision": self.fixture.fixture.base["revision"], "fingerprint": self.fixture.fixture.base["provenance"], } ) self.backup = Mock(return_value=True) self.contain = Mock(return_value=True) self.contained = Mock(return_value=True) self.active = "1" self.current = Mock( side_effect=lambda: self.active ) def activate(release): self.active = release return True self.activate = Mock(side_effect=activate) self.refresh = Mock(return_value=True) self.ready = Mock(return_value=True) self.release_containment = Mock(return_value=True) self.writes_enabled = Mock(return_value=True) self.health = Mock( side_effect=lambda release: { "healthy": True, "release": release, } ) self.recovery = MigrationRecovery( site=self.fixture.fixture.base["site"], store=self.store, lock_path=self.fixture.ops.lock_path, identity_probe=self.identity, history_probe=self.history, provenance_probe=self.provenance, backup_probe=self.backup, contain=self.contain, containment_probe=self.contained, current_probe=self.current, activate=self.activate, refresh=self.refresh, readiness_probe=self.ready, release_containment=self.release_containment, writes_enabled_probe=self.writes_enabled, health_probe=self.health, ) def plan(self): return self.recovery.inspect(self.identifier) def execute(self): plan = self.plan() return self.recovery.execute( self.identifier, confirmation=plan["confirmation"], fingerprint=plan["plan_fingerprint"], ) def rewrite(self, path, record): data = copy.deepcopy(record) data.pop("checksum", None) data["checksum"] = canonical_hash(data) path.write_text(json.dumps(data)) def test_valid_recovery_plan(self): self.assertEqual( self.plan()["status"], "FORWARD_ACTIVATION_READY", ) def test_plan_is_read_only(self): before = { path.name: path.read_bytes() for path in self.store.directory.glob("*.json") } self.plan() after = { path.name: path.read_bytes() for path in self.store.directory.glob("*.json") } self.assertEqual(before, after) self.activate.assert_not_called() self.release_containment.assert_not_called() def test_successful_activation(self): result = self.execute() self.assertEqual(result["status"], "VERIFIED") self.assertEqual(self.active, "2") def test_migration_never_repeated(self): self.execute() self.fixture.migrate.assert_called_once() def test_wrong_confirmation(self): plan = self.plan() with self.assertRaises(MigrationRecoveryError): self.recovery.execute( self.identifier, confirmation="INVALID", fingerprint=plan["plan_fingerprint"], ) self.activate.assert_not_called() def test_wrong_fingerprint(self): plan = self.plan() with self.assertRaises(MigrationRecoveryError): self.recovery.execute( self.identifier, confirmation=plan["confirmation"], fingerprint="0" * 64, ) self.activate.assert_not_called() def test_changed_database_identity(self): self.identity.return_value = "0" * 64 with self.assertRaises(MigrationRecoveryError): self.plan() def test_changed_migration_history(self): self.history.return_value = ["unexpected"] with self.assertRaises(MigrationRecoveryError): self.plan() def test_changed_original_release_blocks_recovery(self): self.active = "3" with self.assertRaises(MigrationRecoveryError): self.plan() def test_changed_original_revision_blocks_recovery(self): candidate = self.provenance.return_value self.provenance.side_effect = lambda release: ( {**candidate, "revision": "c" * 40} if release == "1" else candidate ) with self.assertRaises(MigrationRecoveryError): self.plan() def test_legacy_unbound_intent_blocks_recovery(self): path = self.fixture.coordinator.intent_path(self.identifier) record = self.store.read(path) record.pop("original_revision") self.rewrite(path, record) result_path = self.fixture.coordinator.result_path(self.identifier) result = self.store.read(result_path) result["intent_checksum"] = self.store.read(path)["checksum"] self.rewrite(result_path, result) with self.assertRaises(MigrationRecoveryError): self.plan() def test_changed_candidate_revision(self): self.provenance.return_value["revision"] = "0" * 40 with self.assertRaises(MigrationRecoveryError): self.plan() def test_changed_candidate_fingerprint(self): self.provenance.return_value["fingerprint"] = "0" * 64 with self.assertRaises(MigrationRecoveryError): self.plan() def test_backup_verification_required(self): self.backup.return_value = False with self.assertRaises(MigrationRecoveryError): self.plan() def test_missing_result_rejected(self): self.fixture.coordinator.result_path( self.identifier ).unlink() with self.assertRaises(MigrationRecoveryError): self.plan() def test_corrupted_result_rejected(self): self.fixture.coordinator.result_path( self.identifier ).write_text("{invalid") with self.assertRaises(MigrationRecoveryError): self.plan() def test_failed_migration_rejected(self): path = self.fixture.coordinator.result_path( self.identifier ) record = self.store.read(path) record["status"] = "FAILED" self.rewrite(path, record) with self.assertRaises(MigrationRecoveryError): self.plan() def test_inconsistent_history_evidence_rejected(self): path = self.fixture.coordinator.result_path( self.identifier ) record = self.store.read(path) record["history_after"] = ["unexpected"] self.rewrite(path, record) with self.assertRaises(MigrationRecoveryError): self.plan() def test_wrong_site_rejected(self): self.recovery.site = "different-site" with self.assertRaises(MigrationRecoveryError): self.plan() def test_consumption_evidence_required(self): self.store.consumed_path( self.identifier ).unlink() with self.assertRaises(MigrationRecoveryError): self.plan() def test_containment_required(self): self.contained.return_value = False with self.assertRaises(MigrationRecoveryError): self.execute() self.activate.assert_not_called() def test_activation_intent_persisted_first(self): def check(release): path = self.recovery.intent_path( self.identifier ) self.assertTrue(path.exists()) self.active = release return True self.activate.side_effect = check self.execute() def test_activation_failure_preserves_containment(self): self.activate.return_value = False self.activate.side_effect = None with self.assertRaises(MigrationRecoveryError): self.execute() self.release_containment.assert_not_called() def test_refresh_failure_preserves_containment(self): self.refresh.return_value = False with self.assertRaises(MigrationRecoveryError): self.execute() self.release_containment.assert_not_called() def test_readiness_required_before_reopening(self): self.ready.return_value = False with self.assertRaises(MigrationRecoveryError): self.execute() self.release_containment.assert_not_called() def test_failed_health_reestablishes_containment(self): self.health.side_effect = RuntimeError() with self.assertRaises(MigrationRecoveryError): self.execute() self.assertGreaterEqual( self.contain.call_count, 2, ) def test_reopening_must_be_verified(self): self.writes_enabled.return_value = False with self.assertRaises(MigrationRecoveryError): self.execute() self.health.assert_not_called() def test_existing_activation_intent_blocks_retry(self): self.recovery.intent_path( self.identifier ).touch(mode=0o600) with self.assertRaises(MigrationRecoveryError): self.execute() self.activate.assert_not_called() def test_successful_execution_cannot_repeat(self): self.execute() with self.assertRaises(MigrationRecoveryError): self.execute() self.activate.assert_called_once() def test_concurrent_operation_rejected(self): with DeploymentLock( self.fixture.ops.lock_path ): with self.assertRaises(LockError): self.plan() def test_original_migration_evidence_preserved(self): path = self.fixture.coordinator.result_path( self.identifier ) before = path.read_bytes() self.execute() self.assertEqual(path.read_bytes(), before) def test_database_rollback_prohibited(self): result = self.execute() self.assertFalse( result["database_restored"] ) self.assertFalse( result["migration_reexecuted"] ) def test_final_result_persisted(self): result = self.execute() self.assertEqual( self.store.read( self.recovery.result_path(self.identifier) ), result, ) def test_failure_result_persisted(self): self.refresh.return_value = False with self.assertRaises(MigrationRecoveryError): self.execute() result = self.store.read( self.recovery.result_path(self.identifier) ) self.assertEqual(result["status"], "FAILED") if __name__ == "__main__": unittest.main()