import json import unittest from unittest.mock import Mock, patch import test_rollback import test_recovery from core.rollback_archive import RollbackArchive, ArchiveError from core.compatibility import canonical_hash from core.locking import DeploymentLock, LockError class RollbackArchiveTests(unittest.TestCase): def setUp(self): self.fixture = test_rollback.RollbackTests() self.addCleanup(self.fixture.doCleanups) self.fixture.setUp() self.fixture.execute() self.recovery = self.fixture.fixture self.planner = self.recovery.planner self.journal = self.recovery.journal self.transaction = self.journal.load() self.identifier = self.transaction["transaction_id"] self.directory = self.recovery.root / "archives" self.directory.mkdir(mode=0o700) self.archive = RollbackArchive( planner=self.planner, directory=self.directory, health_probe=self.fixture.health.verify, ) self.marker = ( self.recovery.releases.release_path("2") / "BAD_RELEASE" ) def execute(self): plan = self.archive.plan() return self.archive.execute( confirmation=plan["confirmation"], fingerprint=plan["fingerprint"], ) def rewrite(self, path, **changes): record = json.loads(path.read_text()) record.update(changes) record.pop("checksum", None) record["checksum"] = canonical_hash(record) path.write_text(json.dumps(record)) def test_valid_plan(self): self.assertEqual( self.archive.plan()["status"], "ARCHIVE_READY", ) def test_plan_is_read_only(self): self.archive.plan() self.assertFalse(self.marker.exists()) self.assertEqual( self.journal.load(), self.transaction, ) def test_successful_archive(self): result = self.execute() self.assertEqual(result["status"], "ARCHIVED") self.assertIsNone(self.journal.load()) def test_failed_candidate_marked(self): self.execute() self.assertTrue(self.marker.is_file()) def test_original_transaction_preserved(self): result = self.execute() self.assertEqual( result["transaction"], self.transaction, ) def test_original_rollback_evidence_preserved(self): intent = self.fixture.executor.intent_path( self.identifier ) result = self.fixture.executor.result_path( self.identifier ) before = (intent.read_bytes(), result.read_bytes()) self.execute() self.assertEqual( (intent.read_bytes(), result.read_bytes()), before, ) def test_missing_result_rejected(self): self.fixture.executor.result_path( self.identifier ).unlink() with self.assertRaises(ArchiveError): self.archive.plan() def test_missing_intent_rejected(self): self.fixture.executor.intent_path( self.identifier ).unlink() with self.assertRaises(ArchiveError): self.archive.plan() def test_wrong_active_release_rejected(self): self.recovery.releases.activate("2") with self.assertRaises(ArchiveError): self.archive.plan() def test_changed_provenance_rejected(self): self.recovery.old["fingerprint"] = "0" * 64 with self.assertRaises(ArchiveError): self.archive.plan() def test_changed_backup_rejected(self): self.recovery.backup_verify.return_value[ "sha256" ] = "0" * 64 with self.assertRaises(ArchiveError): self.archive.plan() def test_runtime_verification_required(self): self.recovery.runtime_check.return_value = False with self.assertRaises(ArchiveError): self.archive.plan() def test_health_verification_required(self): self.fixture.health.verify.side_effect = None self.fixture.health.verify.return_value = { "healthy": False, "release": "1", } with self.assertRaises(ArchiveError): self.archive.plan() def test_wrong_confirmation_rejected(self): plan = self.archive.plan() with self.assertRaises(ArchiveError): self.archive.execute( confirmation="INVALID", fingerprint=plan["fingerprint"], ) self.assertFalse(self.marker.exists()) def test_wrong_fingerprint_rejected(self): plan = self.archive.plan() with self.assertRaises(ArchiveError): self.archive.execute( confirmation=plan["confirmation"], fingerprint="0" * 64, ) def test_modified_rollback_result_rejected(self): self.rewrite( self.fixture.executor.result_path( self.identifier ), active_release="99", ) with self.assertRaises(ArchiveError): self.archive.plan() def test_publication_failure_preserves_journal(self): plan = self.archive.plan() with patch( "core.approval_store.os.link", side_effect=OSError("publication failed"), ): with self.assertRaises(ArchiveError): self.archive.execute( confirmation=plan["confirmation"], fingerprint=plan["fingerprint"], ) self.assertEqual( self.journal.load(), self.transaction, ) self.assertFalse(self.marker.exists()) def test_concurrent_operation_rejected(self): with DeploymentLock(self.planner.lock_path): with self.assertRaises(LockError): self.archive.plan() def test_duplicate_archival_rejected(self): self.execute() with self.assertRaises(ArchiveError): self.archive.plan() def test_no_database_restoration(self): self.assertFalse( self.execute()["database_restored"] ) def test_both_releases_preserved(self): self.execute() for release in ("1", "2"): self.assertTrue( self.recovery.releases.release_path( release ).is_dir() ) def test_failed_release_cannot_be_validated(self): self.execute() with self.assertRaises(Exception): self.recovery.releases.validate("2") def test_archive_readback(self): result = self.execute() self.assertEqual( self.archive.load(self.identifier), result, ) if __name__ == "__main__": unittest.main() class InterruptedRollbackArchiveTests(unittest.TestCase): def test_confirmed_rollback_can_archive_interrupted_activation(self): prepare = test_recovery.RecoveryTests.prepare def interrupted(recovery): return prepare(recovery, interrupted=True) fixture = RollbackArchiveTests() self.addCleanup(fixture.doCleanups) with patch.object(test_recovery.RecoveryTests, "prepare", interrupted): fixture.setUp() self.assertEqual(fixture.transaction["phase"], "ACTIVATING") result = fixture.execute() self.assertEqual(result["transaction"]["phase"], "ACTIVATING") self.assertIsNone(fixture.journal.load()) self.assertTrue(fixture.marker.is_file()) self.assertEqual(fixture.archive.load(fixture.identifier), result) def test_unfinished_migration_blocks_rollback_journal_retirement(self): from core.approval_store import ApprovalStore fixture = RollbackArchiveTests() self.addCleanup(fixture.doCleanups) fixture.setUp() approvals = fixture.recovery.root / "approvals" approvals.mkdir(mode=0o700, exist_ok=True) fixture.journal.bind_migration_guard(ApprovalStore(approvals)) blocker = approvals / ("b" * 32 + ".migration-intent.json") blocker.write_text("{}") before = fixture.journal.path.read_bytes() with self.assertRaises(ArchiveError): fixture.execute() self.assertEqual(fixture.journal.path.read_bytes(), before) self.assertFalse(fixture.marker.exists()) self.assertFalse(fixture.archive.path(fixture.identifier).exists())