346 lines
8.3 KiB
Python
346 lines
8.3 KiB
Python
import copy
|
|
import json
|
|
import unittest
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
import test_migration_recovery
|
|
|
|
from core.approval_store import ApprovalStoreError
|
|
from core.compatibility import canonical_hash
|
|
from core.locking import DeploymentLock, LockError
|
|
from core.operation_guard import OperationGuard, OperationBlocked
|
|
|
|
from core.migration_archive import (
|
|
MigrationArchive,
|
|
MigrationArchiveError,
|
|
)
|
|
|
|
|
|
class MigrationArchiveTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.fixture = (
|
|
test_migration_recovery.MigrationRecoveryTests()
|
|
)
|
|
|
|
self.addCleanup(self.fixture.doCleanups)
|
|
self.fixture.setUp()
|
|
|
|
self.fixture.execute()
|
|
|
|
self.store = self.fixture.store
|
|
self.identifier = self.fixture.identifier
|
|
|
|
self.archive = MigrationArchive(
|
|
site=self.fixture.recovery.site,
|
|
store=self.store,
|
|
lock_path=self.fixture.recovery.lock_path,
|
|
identity_probe=self.fixture.identity,
|
|
history_probe=self.fixture.history,
|
|
provenance_probe=self.fixture.provenance,
|
|
backup_probe=self.fixture.backup,
|
|
current_probe=self.fixture.current,
|
|
health_probe=self.fixture.health,
|
|
writes_enabled_probe=self.fixture.writes_enabled,
|
|
)
|
|
|
|
self.guard = OperationGuard(self.store)
|
|
|
|
def plan(self):
|
|
|
|
return self.archive.plan(self.identifier)
|
|
|
|
def execute(self):
|
|
|
|
plan = self.plan()
|
|
|
|
return self.archive.execute(
|
|
self.identifier,
|
|
confirmation=plan["confirmation"],
|
|
fingerprint=plan["fingerprint"],
|
|
)
|
|
|
|
def rewrite(self, suffix, **changes):
|
|
|
|
path = self.store.path(
|
|
self.identifier, suffix
|
|
)
|
|
|
|
record = self.store.read(path)
|
|
record.update(changes)
|
|
record.pop("checksum", None)
|
|
record["checksum"] = canonical_hash(record)
|
|
|
|
path.write_text(json.dumps(record))
|
|
|
|
def test_valid_archive_plan(self):
|
|
|
|
self.assertEqual(
|
|
self.plan()["status"],
|
|
"ARCHIVE_READY",
|
|
)
|
|
|
|
def test_successful_archive(self):
|
|
|
|
result = self.execute()
|
|
|
|
self.assertEqual(
|
|
result["status"],
|
|
"ARCHIVED",
|
|
)
|
|
|
|
def test_original_records_preserved(self):
|
|
|
|
before = {
|
|
path.name: path.read_bytes()
|
|
for path in self.store.directory.glob("*.json")
|
|
}
|
|
|
|
self.execute()
|
|
|
|
for name, content in before.items():
|
|
|
|
self.assertEqual(
|
|
(self.store.directory / name).read_bytes(),
|
|
content,
|
|
)
|
|
|
|
def test_guard_blocks_before_archive(self):
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_guard_clears_after_archive(self):
|
|
|
|
self.execute()
|
|
|
|
self.assertEqual(
|
|
self.guard.require_clear()["status"],
|
|
"CLEAR",
|
|
)
|
|
|
|
def test_consumed_approval_preserved(self):
|
|
|
|
self.execute()
|
|
|
|
self.assertTrue(
|
|
self.store.consumed_path(
|
|
self.identifier
|
|
).exists()
|
|
)
|
|
|
|
def test_archived_approval_cannot_be_reused(self):
|
|
|
|
self.execute()
|
|
|
|
with self.assertRaises(ApprovalStoreError):
|
|
|
|
self.fixture.fixture.fixture.consume()
|
|
|
|
def test_missing_activation_result_rejected(self):
|
|
|
|
self.fixture.recovery.result_path(
|
|
self.identifier
|
|
).unlink()
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_failed_activation_rejected(self):
|
|
|
|
self.rewrite(
|
|
".activation-result.json",
|
|
status="FAILED",
|
|
)
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_changed_database_identity_rejected(self):
|
|
|
|
self.fixture.identity.return_value = "0" * 64
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_changed_history_rejected(self):
|
|
|
|
self.fixture.history.return_value = ["unexpected"]
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_changed_provenance_rejected(self):
|
|
|
|
self.fixture.provenance.return_value[
|
|
"fingerprint"
|
|
] = "0" * 64
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_wrong_active_release_rejected(self):
|
|
|
|
self.fixture.active = "1"
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_backup_verification_required(self):
|
|
|
|
self.fixture.backup.return_value = False
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_health_verification_required(self):
|
|
|
|
self.fixture.health.side_effect = None
|
|
self.fixture.health.return_value = {
|
|
"healthy": False,
|
|
"release": "2",
|
|
}
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_writes_enabled_verification_required(self):
|
|
|
|
self.fixture.writes_enabled.return_value = False
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.plan()
|
|
|
|
def test_wrong_confirmation_rejected(self):
|
|
|
|
plan = self.plan()
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
|
|
self.archive.execute(
|
|
self.identifier,
|
|
confirmation="INVALID",
|
|
fingerprint=plan["fingerprint"],
|
|
)
|
|
|
|
def test_wrong_fingerprint_rejected(self):
|
|
|
|
plan = self.plan()
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
|
|
self.archive.execute(
|
|
self.identifier,
|
|
confirmation=plan["confirmation"],
|
|
fingerprint="0" * 64,
|
|
)
|
|
|
|
def test_duplicate_archival_rejected(self):
|
|
|
|
self.execute()
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
self.execute()
|
|
|
|
def test_corrupted_archive_blocks_operations(self):
|
|
|
|
self.execute()
|
|
|
|
self.archive.path(
|
|
self.identifier
|
|
).write_text("{invalid")
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_missing_consumption_blocks_operations(self):
|
|
|
|
self.execute()
|
|
|
|
self.store.consumed_path(
|
|
self.identifier
|
|
).unlink()
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_changed_archived_evidence_blocks_operations(self):
|
|
|
|
self.execute()
|
|
|
|
self.rewrite(
|
|
".activation-result.json",
|
|
active_release="99",
|
|
)
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_unrelated_intent_still_blocks_operations(self):
|
|
|
|
self.execute()
|
|
|
|
self.store.path(
|
|
"0" * 32,
|
|
".migration-intent.json",
|
|
).touch(mode=0o600)
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_archive_publication_failure_blocks_operations(self):
|
|
|
|
plan = self.plan()
|
|
|
|
with patch(
|
|
"core.approval_store.os.link",
|
|
side_effect=OSError("publication failure"),
|
|
):
|
|
|
|
with self.assertRaises(MigrationArchiveError):
|
|
|
|
self.archive.execute(
|
|
self.identifier,
|
|
confirmation=plan["confirmation"],
|
|
fingerprint=plan["fingerprint"],
|
|
)
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_concurrent_operation_rejected(self):
|
|
|
|
with DeploymentLock(
|
|
self.fixture.recovery.lock_path
|
|
):
|
|
|
|
with self.assertRaises(LockError):
|
|
self.plan()
|
|
|
|
def test_archive_marker_alone_does_not_clear_guard(self):
|
|
|
|
self.archive.path(
|
|
self.identifier
|
|
).touch(mode=0o600)
|
|
|
|
with self.assertRaises(OperationBlocked):
|
|
self.guard.require_clear()
|
|
|
|
def test_archival_never_reexecutes_migration(self):
|
|
|
|
self.execute()
|
|
|
|
self.fixture.fixture.migrate.assert_called_once()
|
|
|
|
def test_no_database_restore(self):
|
|
|
|
result = self.execute()
|
|
|
|
self.assertFalse(result["database_restored"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|