import copy import unittest from datetime import timedelta from unittest.mock import Mock import test_approval_store from core.dev3.operations import SiteOperations from core.migration import MigrationCoordinator, MigrationError from core.locking import DeploymentLock, LockError class MigrationTests(unittest.TestCase): def setUp(self): self.fixture = test_approval_store.ApprovalStoreTests() self.addCleanup(self.fixture.doCleanups) self.fixture.setUp() self.ops = SiteOperations(self.fixture.root) self.fixture.store = self.ops.approvals self.fixture.save() self.root = self.fixture.root self.context = Mock( side_effect=lambda: copy.deepcopy(self.fixture.base) ) self.contain = Mock(return_value=True) self.contained = Mock(return_value=True) self.backup = Mock(return_value=True) self.before = ["2026_01_01_000000_create_users"] self.after = ( self.before + self.fixture.base["pending_migrations"] ) self.history = Mock( side_effect=[ list(self.before), list(self.before), list(self.after), ] ) self.migrate = Mock(return_value=0) self.application = Mock(return_value=True) self.clock = Mock(return_value=self.fixture.now) self.coordinator = MigrationCoordinator( store=self.fixture.store, lock_path=self.ops.lock_path, journal=self.ops.journal, original_release_probe=lambda: { "release":"1", "revision":self.fixture.base["revision"], "fingerprint":self.fixture.base["provenance"], }, context_probe=self.context, contain=self.contain, containment_probe=self.contained, backup_probe=self.backup, history_probe=self.history, migration_runner=self.migrate, application_probe=self.application, clock=self.clock, ) def execute(self): return self.coordinator.execute( self.fixture.identifier, self.fixture.rehearsal, confirmation=self.fixture.approval["confirmation"], ) def test_successful_migration(self): self.assertEqual(self.execute()["status"], "MIGRATED") def test_runner_called_once(self): self.execute() self.migrate.assert_called_once() def test_exact_migration_inventory(self): self.execute() self.migrate.assert_called_once_with( tuple(self.fixture.base["pending_migrations"]) ) def test_approval_consumed_before_execution(self): def check(pending): self.assertTrue( self.fixture.store.consumed_path( self.fixture.identifier ).exists() ) return 0 self.migrate.side_effect = check self.execute() def test_intent_persisted_before_execution(self): def check(pending): record = self.fixture.store.read( self.coordinator.intent_path(self.fixture.identifier) ) self.assertEqual(record["status"], "MIGRATION_INTENT") return 0 self.migrate.side_effect = check self.execute() def test_wrong_confirmation_rejected(self): with self.assertRaises(MigrationError): self.coordinator.execute( self.fixture.identifier, self.fixture.rehearsal, confirmation="INVALID", ) self.contain.assert_not_called() self.migrate.assert_not_called() def test_expired_approval_rejected(self): self.clock.return_value += timedelta(hours=25) with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_context_change_rejected(self): self.fixture.base["database_identity"] = "0" * 64 with self.assertRaises(MigrationError): self.execute() self.contain.assert_not_called() def test_backup_verification_required(self): self.backup.return_value = False with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_containment_required(self): self.contain.return_value = False with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_containment_verification_required(self): self.contained.return_value = False with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_context_rechecked_after_containment(self): original = copy.deepcopy(self.fixture.base) changed = copy.deepcopy(original) changed["database_identity"] = "0" * 64 self.context.side_effect = [original, changed] with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_backup_rechecked_after_containment(self): self.backup.side_effect = [True, False] with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_history_change_before_execution(self): self.history.side_effect = [ self.before, self.before + ["unexpected"], ] with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_previously_applied_migration_rejected(self): self.history.side_effect = [self.after] with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_runner_failure_rejected(self): self.migrate.return_value = 1 with self.assertRaises(MigrationError): self.execute() self.application.assert_not_called() def test_runner_exception_rejected(self): self.migrate.side_effect = RuntimeError() with self.assertRaises(MigrationError): self.execute() def test_missing_applied_migration_rejected(self): self.history.side_effect = [ self.before, self.before, self.before ] with self.assertRaises(MigrationError): self.execute() def test_unexpected_migration_rejected(self): self.history.side_effect = [ self.before, self.before, self.after + ["unexpected"], ] with self.assertRaises(MigrationError): self.execute() def test_removed_history_rejected(self): self.history.side_effect = [ self.before, self.before, self.fixture.base["pending_migrations"], ] with self.assertRaises(MigrationError): self.execute() def test_application_verification_required(self): self.application.return_value = False with self.assertRaises(MigrationError): self.execute() def test_success_evidence_persisted(self): result = self.execute() self.assertEqual( result, self.fixture.store.read( self.coordinator.result_path(self.fixture.identifier) ), ) def test_failure_evidence_persisted(self): self.migrate.return_value = 1 with self.assertRaises(MigrationError): self.execute() result = self.fixture.store.read( self.coordinator.result_path(self.fixture.identifier) ) self.assertEqual(result["status"], "FAILED") self.assertTrue(result["manual_recovery_required"]) def test_failed_execution_cannot_repeat(self): self.migrate.return_value = 1 with self.assertRaises(MigrationError): self.execute() self.migrate.return_value = 0 with self.assertRaises(MigrationError): self.execute() self.migrate.assert_called_once() def test_successful_execution_cannot_repeat(self): self.execute() with self.assertRaises(MigrationError): self.execute() self.migrate.assert_called_once() def test_existing_intent_blocks_execution(self): self.coordinator.intent_path( self.fixture.identifier ).touch(mode=0o600) with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_concurrent_deployment_rejected(self): with DeploymentLock(self.ops.lock_path): with self.assertRaises(LockError): self.execute() def test_database_rollback_prohibited(self): self.assertFalse( self.execute()["automatic_database_rollback"] ) def test_application_activation_not_performed(self): self.assertFalse(self.execute()["application_activated"]) def test_containment_remains_required(self): self.assertTrue(self.execute()["containment_required"]) def test_boolean_runner_result_rejected(self): self.migrate.return_value = False with self.assertRaises(MigrationError): self.execute() def test_approval_expiry_rechecked(self): self.clock.side_effect = [ self.fixture.now, self.fixture.now + timedelta(hours=25), ] with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() def test_different_approval_intent_blocks_migration(self): identifier = "0" * 32 self.fixture.store.path( identifier, ".migration-intent.json", ).touch(mode=0o600) with self.assertRaises(MigrationError): self.execute() self.contain.assert_not_called() self.migrate.assert_not_called() def test_other_consumed_approval_blocks_migration(self): self.fixture.store.consumed_path( "0" * 32 ).touch(mode=0o600) with self.assertRaises(MigrationError): self.execute() self.migrate.assert_not_called() if __name__ == "__main__": unittest.main()