import hashlib import json import os import stat import tempfile import unittest from pathlib import Path from unittest.mock import patch from core.transactions import ( TransactionError, TransactionJournal, ) class TransactionTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) self.journal = TransactionJournal(self.root) def create(self): return self.journal.create( site="example-absdev-net", previous="1", candidate="2", revision="a" * 40, provenance="b" * 64, ) def advance(self, phase): current = self.journal.load() return self.journal.transition( current["transaction_id"], current["phase"], phase, ) def complete(self): self.create() for phase in ( "BACKED_UP", "QUIESCED", "ACTIVATING", "ACTIVATED", "VERIFIED", ): self.advance(phase) return self.journal.load() def test_empty_journal(self): self.assertIsNone(self.journal.load()) def test_create_transaction(self): self.assertEqual( self.create()["phase"], "PREPARED" ) def test_transaction_identifier(self): self.assertRegex( self.create()["transaction_id"], r"^[a-f0-9]{32}$", ) def test_persistent_transaction(self): original = self.create() self.assertEqual( TransactionJournal(self.root).load(), original, ) def test_duplicate_transaction_rejected(self): self.create() with self.assertRaises(TransactionError): self.create() def test_file_permissions(self): self.create() self.assertEqual( stat.S_IMODE( self.journal.path.stat().st_mode ), 0o600, ) def test_initial_history(self): result = self.create() self.assertEqual( len(result["history"]), 1 ) self.assertEqual( result["history"][0]["phase"], "PREPARED", ) def test_valid_transition(self): self.create() self.assertEqual( self.advance("BACKED_UP")["phase"], "BACKED_UP", ) def test_invalid_transition_rejected(self): self.create() with self.assertRaises(TransactionError): self.advance("ACTIVATED") def test_unknown_phase_rejected(self): self.create() with self.assertRaises(TransactionError): self.advance("UNKNOWN") def test_duplicate_phase_rejected(self): self.create() with self.assertRaises(TransactionError): self.advance("PREPARED") def test_wrong_transaction_rejected(self): self.create() with self.assertRaises(TransactionError): self.journal.transition( "0" * 32, "PREPARED", "BACKED_UP", ) def test_stale_phase_rejected(self): transaction = self.create() self.advance("BACKED_UP") with self.assertRaises(TransactionError): self.journal.transition( transaction["transaction_id"], "PREPARED", "BACKED_UP", ) def test_complete_transaction(self): self.assertEqual( self.complete()["phase"], "VERIFIED", ) def test_history_preserved(self): result = self.complete() self.assertEqual( [item["phase"] for item in result["history"]], [ "PREPARED", "BACKED_UP", "QUIESCED", "ACTIVATING", "ACTIVATED", "VERIFIED", ], ) def test_verified_is_terminal(self): self.complete() with self.assertRaises(TransactionError): self.advance("FAILED") def test_failure_containment(self): self.create() result = self.advance("FAILED") self.assertEqual(result["phase"], "FAILED") def test_failed_is_terminal(self): self.create() self.advance("FAILED") with self.assertRaises(TransactionError): self.advance("BACKED_UP") def test_corrupted_json_rejected(self): self.create() self.journal.path.write_text("{invalid") with self.assertRaises(TransactionError): self.journal.load() def test_modified_evidence_rejected(self): self.create() data = json.loads( self.journal.path.read_text() ) data["candidate"] = "99" self.journal.path.write_text( json.dumps(data) ) with self.assertRaises(TransactionError): self.journal.load() def test_duplicate_json_keys_rejected(self): self.create() self.journal.path.write_text( '{"phase":"FAILED","phase":"VERIFIED"}' ) with self.assertRaises(TransactionError): self.journal.load() def test_invalid_history_rejected(self): data = self.create() data["history"][0]["phase"] = "VERIFIED" del data["checksum"] data["checksum"] = hashlib.sha256( json.dumps( data, sort_keys=True, separators=(",", ":"), allow_nan=False, ).encode() ).hexdigest() self.journal.path.write_text( json.dumps(data) ) with self.assertRaises(TransactionError): self.journal.load() def test_symlink_rejected(self): target = self.root / "external" target.write_text("{}") self.journal.path.symlink_to(target) with self.assertRaises(TransactionError): self.journal.load() def test_hardlink_rejected(self): self.create() os.link( self.journal.path, self.root / "duplicate", ) with self.assertRaises(TransactionError): self.journal.load() def test_insecure_permissions_rejected(self): self.create() self.journal.path.chmod(0o644) with self.assertRaises(TransactionError): self.journal.load() def test_invalid_site_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "../outside", "1", "2", "a" * 40, "b" * 64, ) def test_invalid_revision_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "2", "invalid", "b" * 64, ) def test_invalid_candidate_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "../outside", "a" * 40, "b" * 64, ) def test_same_release_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "1", "a" * 40, "b" * 64, ) def test_atomic_write_failure(self): original = self.create() with patch( "core.transactions.os.replace", side_effect=OSError("Simulated failure"), ): with self.assertRaises(TransactionError): self.advance("BACKED_UP") self.assertEqual( self.journal.load(), original ) def test_no_temporary_files_remaining(self): self.complete() self.assertFalse( list(self.root.glob(".transaction-*")) ) def test_insecure_directory_rejected(self): self.root.chmod(0o777) with self.assertRaises(TransactionError): TransactionJournal(self.root) def test_concurrent_writer_rejected(self): from core.locking import DeploymentLock, LockError with DeploymentLock( self.root / ".journal.lock" ): with self.assertRaises(LockError): self.create() def test_invalid_provenance_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "2", "a" * 40, "invalid", ) def test_failed_transition_preserves_evidence(self): original = self.create() with self.assertRaises(TransactionError): self.advance("VERIFIED") self.assertEqual( self.journal.load(), original ) def test_previous_provenance_persisted(self): result = self.journal.create( "example", "1", "2", "a" * 40, "b" * 64, previous_revision="c" * 40, previous_provenance="d" * 64, ) self.assertEqual( result["previous_revision"], "c" * 40, ) self.assertEqual( self.journal.load()["previous_provenance"], "d" * 64, ) def test_partial_previous_evidence_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "2", "a" * 40, "b" * 64, previous_revision="c" * 40, ) def test_invalid_previous_revision_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "2", "a" * 40, "b" * 64, previous_revision="invalid", previous_provenance="d" * 64, ) def test_invalid_previous_provenance_rejected(self): with self.assertRaises(TransactionError): self.journal.create( "example", "1", "2", "a" * 40, "b" * 64, previous_revision="c" * 40, previous_provenance="invalid", ) def test_previous_evidence_survives_transition(self): transaction = self.journal.create( "example", "1", "2", "a" * 40, "b" * 64, previous_revision="c" * 40, previous_provenance="d" * 64, ) result = self.journal.transition( transaction["transaction_id"], "PREPARED", "BACKED_UP", ) self.assertEqual( result["previous_provenance"], "d" * 64, ) if __name__ == "__main__": unittest.main()