From 07a28752015fc5ed43bdbf8e33b5df07363243c9 Mon Sep 17 00:00:00 2001 From: codeasier Date: Sat, 3 Oct 2026 10:45:47 +0800 Subject: [PATCH] fix(import): protect rollback failures and gate runtime coverage --- .github/workflows/test.yml | 7 + pyproject.toml | 3 +- .../core/snapshot_import_backend.py | 17 ++- tests/core/test_import_backend_failures.py | 122 ++++++++++++++++++ tests/snapshot/test_replay_boundaries.py | 86 ++++++++++++ 5 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 tests/core/test_import_backend_failures.py create mode 100644 tests/snapshot/test_replay_boundaries.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aa59a40..5cdd27b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -100,6 +100,13 @@ jobs: - name: Run tests with coverage run: pytest tests/test_fixture_provenance.py && pytest --cov --cov-report=xml --cov-report=term-missing + - name: Guard critical runtime coverage + # Separate floors prevent strong modules from masking a weak boundary. + run: | + python -m coverage report --include='*/core/snapshot_import_backend.py' --fail-under=90 + python -m coverage report --include='*/simulate/simulated_caching_allocator.py' --fail-under=88 + python -m coverage report --include='*/simulate/snapshot_mutator.py' --fail-under=84 + - name: Upload coverage report uses: actions/upload-artifact@v4 if: matrix.python-version == '3.13' && always() diff --git a/pyproject.toml b/pyproject.toml index a95509a..3c05a93 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -116,7 +116,8 @@ exclude_lines = [ "if __name__ == .__main__.", "if TYPE_CHECKING:", ] -fail_under = 0 +# Line + branch coverage baseline; CI also guards critical runtime files separately. +fail_under = 90 [tool.coverage.xml] output = "coverage.xml" diff --git a/src/pt_snap_cli/core/snapshot_import_backend.py b/src/pt_snap_cli/core/snapshot_import_backend.py index 0f671ec..b24ea66 100644 --- a/src/pt_snap_cli/core/snapshot_import_backend.py +++ b/src/pt_snap_cli/core/snapshot_import_backend.py @@ -5,6 +5,7 @@ import sqlite3 import tempfile from collections.abc import Callable +from contextlib import ExitStack, suppress from pathlib import Path from pt_snap_cli.core.errors import ImportExecutionError, ImportToolMissingError @@ -26,7 +27,7 @@ def dump_to_db( output_dir.mkdir(parents=True, exist_ok=True) run_dump_to_db = self._load_run_dump_to_db() - with tempfile.TemporaryDirectory(dir=output_dir) as tmp_dir_name: + with tempfile.TemporaryDirectory(dir=output_dir) as tmp_dir_name, ExitStack() as cleanup: tmp_dir = Path(tmp_dir_name) tmp_db_path = tmp_dir / db_path.name try: @@ -51,6 +52,13 @@ def dump_to_db( had_destination = db_path.exists() backup_path: Path | None = None backup_fd: int | None = None + + def close_backup() -> None: + if backup_fd is not None: + with suppress(OSError): + os.close(backup_fd) + + cleanup.callback(close_backup) try: if had_destination and post_publish is not None: backup_fd, backup_name = tempfile.mkstemp( @@ -69,7 +77,12 @@ def dump_to_db( chunk = src.read(1024 * 1024) if not chunk: break - os.write(backup_fd, chunk) + remaining = memoryview(chunk) + while remaining: + written = os.write(backup_fd, remaining) + if written <= 0: + raise OSError("Could not write rollback backup") + remaining = remaining[written:] os.fchmod(backup_fd, source_mode or 0o600) except BaseException: try: diff --git a/tests/core/test_import_backend_failures.py b/tests/core/test_import_backend_failures.py new file mode 100644 index 0000000..e41cdbd --- /dev/null +++ b/tests/core/test_import_backend_failures.py @@ -0,0 +1,122 @@ +import os +from pathlib import Path + +import pytest + +from pt_snap_cli.core.errors import ImportExecutionError +from pt_snap_cli.core.snapshot_import_backend import SnapshotImportBackend + + +@pytest.fixture +def staged_backend(tmp_path, monkeypatch): + backend = SnapshotImportBackend() + source = tmp_path / "snapshot.pkl" + output = tmp_path / "output" + output.mkdir() + destination = backend.target_db_path(source, output) + destination.write_bytes(b"previous database") + descriptors = [] + real_mkstemp = __import__("tempfile").mkstemp + + def tracked_mkstemp(*args, **kwargs): + fd, name = real_mkstemp(*args, **kwargs) + descriptors.append(fd) + return fd, name + + def dump(snapshot_file, dump_dir, device): + (Path(dump_dir) / destination.name).write_bytes(b"new database") + return True + + monkeypatch.setattr(backend, "_load_run_dump_to_db", lambda: dump) + monkeypatch.setattr( + "pt_snap_cli.core.snapshot_import_backend.tempfile.mkstemp", tracked_mkstemp + ) + yield backend, source, output, destination, descriptors + # Leak detection is deterministic even on Python versions without ResourceWarning. + for fd in descriptors: + try: + os.fstat(fd) + except OSError: + continue + os.close(fd) + pytest.fail("rollback descriptor was left open") + + +@pytest.mark.parametrize("failure", ["backup", "publish", "focus", "rollback"]) +def test_failure_closes_rollback_descriptor(staged_backend, monkeypatch, failure): + backend, source, output, destination, _ = staged_backend + original = destination.read_bytes() + + def fail(*args, **kwargs): + raise OSError(f"{failure} failed") + + def post_publish(path): + assert path.read_bytes() == b"new database" + if failure == "rollback": + monkeypatch.setattr("pt_snap_cli.core.snapshot_import_backend.os.read", fail) + if failure in ("focus", "rollback"): + raise RuntimeError("focus failed") + + if failure == "backup": + monkeypatch.setattr("pt_snap_cli.core.snapshot_import_backend.os.write", fail) + elif failure == "publish": + monkeypatch.setattr("pt_snap_cli.core.snapshot_import_backend.os.replace", fail) + with pytest.raises((ImportExecutionError, RuntimeError), match="failed") as error: + backend.dump_to_db(source, output, post_publish=post_publish) + backups = list(output.glob("*.rollback")) + if failure == "rollback": + assert "Recovery backup:" in str(error.value) + assert len(backups) == 1 + assert backups[0].read_bytes() == original + else: + assert destination.read_bytes() == original + assert not backups + assert not [p for p in output.iterdir() if p.is_dir()] + + +def test_short_backup_writes_preserve_complete_rollback(staged_backend, monkeypatch): + backend, source, output, destination, _ = staged_backend + original = destination.read_bytes() + real_write = os.write + monkeypatch.setattr( + "pt_snap_cli.core.snapshot_import_backend.os.write", + lambda fd, data: real_write(fd, data[:3]), + ) + + def fail_focus(path): + raise RuntimeError("focus failed") + + with pytest.raises(RuntimeError, match="focus failed"): + backend.dump_to_db(source, output, post_publish=fail_focus) + assert destination.read_bytes() == original + assert list(output.iterdir()) == [destination] + + +def test_zero_backup_write_fails_without_publishing(staged_backend, monkeypatch): + backend, source, output, destination, _ = staged_backend + monkeypatch.setattr("pt_snap_cli.core.snapshot_import_backend.os.write", lambda *args: 0) + with pytest.raises(ImportExecutionError, match="backup"): + backend.dump_to_db(source, output, post_publish=lambda path: None) + assert destination.read_bytes() == b"previous database" + + +def test_missing_staged_database_does_not_publish(staged_backend, monkeypatch): + backend, source, output, destination, _ = staged_backend + monkeypatch.setattr(backend, "_load_run_dump_to_db", lambda: lambda **kwargs: True) + with pytest.raises(ImportExecutionError, match="Expected database not produced"): + backend.dump_to_db(source, output) + assert list(output.iterdir()) == [destination] + assert destination.read_bytes() == b"previous database" + + +def test_finalization_failure_does_not_publish(staged_backend): + backend, source, output, destination, _ = staged_backend + + def fail_finalize(path): + assert path.read_bytes() == b"new database" + raise ValueError("metadata failed") + + with pytest.raises(ValueError, match="metadata failed"): + backend.dump_to_db(source, output, finalize_temp_db=fail_finalize) + assert list(output.iterdir()) == [destination] + assert destination.read_bytes() == b"previous database" diff --git a/tests/snapshot/test_replay_boundaries.py b/tests/snapshot/test_replay_boundaries.py new file mode 100644 index 0000000..d157f6a --- /dev/null +++ b/tests/snapshot/test_replay_boundaries.py @@ -0,0 +1,86 @@ +import pytest + +from pt_snap_cli.snapshot.simulate import snapshot_mutator + +from .helpers import assert_valid_snapshot +from .test_snapshot_mutator import ( + make_allocator, + make_block, + make_event, + make_segment, + make_snapshot, +) + + +@pytest.mark.parametrize( + ("operation", "address", "size", "stream", "workspace", "expected"), + [ + ("free_block", 0x3000, 0x100, 0, False, False), + ("free_block", 0x1400, 0x100, 0, False, False), + ("free_block", 0x1000, 0x200, 0, False, False), + ("active_block", 0x3000, 0x100, 0, False, False), + ("active_block", 0x1400, 0x100, 0, False, False), + ("active_block", 0x1000, 0x100, 0, False, False), + ("active_block", 0x1000, 0x100, 0, True, True), + ("free_segment", 0x3000, 0x1000, 0, False, False), + ("free_segment", 0x1000, 0x800, 0, False, False), + ("free_segment", 0x1000, 0x1000, 0, False, False), + ("unmap_segment", 0x3000, 0x100, 0, False, False), + ("unmap_segment", 0x1800, 0x1000, 0, False, False), + ("unmap_segment", 0x1000, 0x100, 1, False, False), + ], +) +def test_invalid_replay_preserves_state(operation, address, size, stream, workspace, expected): + allocator = make_allocator([make_segment(blocks=[make_block()])]) + snapshot = allocator.ctx.device_snapshot + before = snapshot.to_dict() + allocator.ctx.workspace_flag = workspace + assert getattr(allocator, operation)(make_event("alloc", address, size, stream)) is expected + assert snapshot.to_dict() == before + assert_valid_snapshot(snapshot) + + +def test_overlapping_allocation_is_rejected_without_mutation(): + allocator = make_allocator([make_segment(blocks=[make_block()])]) + before = allocator.ctx.device_snapshot.to_dict() + assert allocator.alloc_block(make_block()) is False + assert allocator.ctx.device_snapshot.to_dict() == before + assert_valid_snapshot(allocator.ctx.device_snapshot) + + +@pytest.mark.parametrize(("target", "source"), [(-1, 1), (2, 1), (0, -1), (0, 2), (0, 0)]) +def test_invalid_merge_indices_preserve_segments(target, source): + snapshot = make_snapshot([make_segment(0x1000, 0x100), make_segment(0x1100, 0x100)]) + before = snapshot.to_dict() + assert snapshot_mutator._merge_segments(snapshot, target, source) is False + assert snapshot.to_dict() == before + assert_valid_snapshot(snapshot) + + +@pytest.mark.parametrize(("address", "stream"), [(0x1200, 0), (0x1100, 1)]) +def test_incompatible_merge_preserves_segments(address, stream): + snapshot = make_snapshot([make_segment(0x1000, 0x100), make_segment(address, 0x100, stream)]) + before = snapshot.to_dict() + assert snapshot_mutator._merge_segments(snapshot, 0, 1) is False + assert snapshot.to_dict() == before + assert_valid_snapshot(snapshot) + + +@pytest.mark.parametrize( + ("index", "address", "size", "direction"), + [ + (-1, 0x1000, 0x100, "left"), + (1, 0x1000, 0x100, "left"), + (0, 0x1000, 0x100, "invalid"), + (0, 0xF00, 0x100, "left"), + (0, 0x1F00, 0x200, "right"), + (0, 0x1000, 0x100, "left"), + (0, 0x1000, 0x1000, "right"), + ], +) +def test_invalid_shrink_preserves_live_block(index, address, size, direction): + snapshot = make_snapshot([make_segment(blocks=[make_block()])]) + before = snapshot.to_dict() + assert snapshot_mutator._shrink_segment(snapshot, index, address, size, direction) is False + assert snapshot.to_dict() == before + assert_valid_snapshot(snapshot)