test_apply_pack_oserror_integrity.py
python
sha256:8860dea10c653956b613a814cc752a6d34cb3986cdf16749a49172affdabf045
fix tests
Human
minor
⚠ breaking
15 days ago
| 1 | """Tests for Bug 13: apply_mpack propagates unhandled OSError from write_commit. |
| 2 | |
| 3 | Root cause: apply_mpack's commit loop catches (KeyError, ValueError, TypeError) |
| 4 | but NOT OSError. When write_commit raises OSError, apply_mpack must log CRITICAL |
| 5 | and continue rather than crashing the entire pull/push/clone call stack. |
| 6 | |
| 7 | These tests use mock injection to force the OSError path, which is the correct |
| 8 | approach now that commits live in the unified object store (not msgpack files). |
| 9 | """ |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import datetime |
| 13 | import logging |
| 14 | import pathlib |
| 15 | from unittest.mock import patch |
| 16 | |
| 17 | import pytest |
| 18 | |
| 19 | from muse.core.types import fake_id |
| 20 | from muse.core.ids import hash_commit, hash_snapshot |
| 21 | from muse.core.mpack import MPack, apply_mpack |
| 22 | from muse.core.commits import ( |
| 23 | CommitDict, |
| 24 | CommitRecord, |
| 25 | read_commit, |
| 26 | ) |
| 27 | from muse.core.paths import muse_dir |
| 28 | |
| 29 | _TS = datetime.datetime(2024, 6, 15, 10, 0, 0, tzinfo=datetime.timezone.utc) |
| 30 | |
| 31 | |
| 32 | def _make_repo(tmp_path: pathlib.Path) -> pathlib.Path: |
| 33 | repo = tmp_path / "repo" |
| 34 | repo.mkdir() |
| 35 | dot = muse_dir(repo) |
| 36 | dot.mkdir() |
| 37 | (dot / "objects").mkdir() |
| 38 | (dot / "refs" / "heads").mkdir(parents=True) |
| 39 | (dot / "HEAD").write_text("ref: refs/heads/main\n") |
| 40 | return repo |
| 41 | |
| 42 | |
| 43 | def _make_commit_record(message: str, parent: str | None = None) -> CommitRecord: |
| 44 | manifest = {f"{message}.py": fake_id("obj")} |
| 45 | snap_id = hash_snapshot(manifest) |
| 46 | parent_ids = [parent] if parent else [] |
| 47 | cid = hash_commit( |
| 48 | parent_ids=parent_ids, |
| 49 | snapshot_id=snap_id, |
| 50 | message=message, |
| 51 | committed_at_iso=_TS.isoformat(), |
| 52 | author="tester", |
| 53 | ) |
| 54 | return CommitRecord( |
| 55 | commit_id=cid, |
| 56 | branch="main", |
| 57 | snapshot_id=snap_id, |
| 58 | message=message, |
| 59 | committed_at=_TS, |
| 60 | author="tester", |
| 61 | parent_commit_id=parent, |
| 62 | parent2_commit_id=None, |
| 63 | ) |
| 64 | |
| 65 | |
| 66 | def _mpack_with_commits(commits: list[CommitRecord]) -> MPack: |
| 67 | return MPack(blobs=[], snapshots=[], commits=[c.to_dict() for c in commits], tags=[]) |
| 68 | |
| 69 | |
| 70 | # ────────────────────────────────────────────────────────────────────────────── |
| 71 | # Bug 13: unhandled OSError from write_commit |
| 72 | # ────────────────────────────────────────────────────────────────────────────── |
| 73 | |
| 74 | class TestApplyMPackOsErrorIntegrity: |
| 75 | |
| 76 | def test_apply_mpack_does_not_crash_on_store_integrity_violation(self, tmp_path: pathlib.Path) -> None: |
| 77 | """Bug 13: apply_mpack must not propagate OSError from write_commit.""" |
| 78 | repo = _make_repo(tmp_path) |
| 79 | c = _make_commit_record("good-commit") |
| 80 | mpack = _mpack_with_commits([c]) |
| 81 | |
| 82 | with patch("muse.core.mpack.write_commit", side_effect=OSError("Store integrity violation")): |
| 83 | result = apply_mpack(repo, mpack) # must not raise |
| 84 | assert result is not None |
| 85 | |
| 86 | def test_apply_mpack_logs_critical_on_store_integrity_violation( |
| 87 | self, tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture |
| 88 | ) -> None: |
| 89 | """apply_mpack must log CRITICAL (not swallow silently) when OSError is raised.""" |
| 90 | repo = _make_repo(tmp_path) |
| 91 | c = _make_commit_record("critical-log-commit") |
| 92 | mpack = _mpack_with_commits([c]) |
| 93 | |
| 94 | with caplog.at_level(logging.CRITICAL, logger="muse.core.mpack"): |
| 95 | with patch("muse.core.mpack.write_commit", side_effect=OSError("Store integrity violation")): |
| 96 | apply_mpack(repo, mpack) |
| 97 | |
| 98 | crits = [r for r in caplog.records if r.levelno >= logging.CRITICAL] |
| 99 | assert crits, "apply_mpack must log CRITICAL when write_commit raises OSError" |
| 100 | assert any("integrity" in r.message.lower() or "violation" in r.message.lower() |
| 101 | for r in crits) |
| 102 | |
| 103 | def test_apply_mpack_continues_after_integrity_violation(self, tmp_path: pathlib.Path) -> None: |
| 104 | """Remaining commits must be processed after an integrity-violation skip.""" |
| 105 | repo = _make_repo(tmp_path) |
| 106 | bad = _make_commit_record("bad-commit") |
| 107 | good = _make_commit_record("good-commit-after") |
| 108 | mpack = _mpack_with_commits([bad, good]) |
| 109 | |
| 110 | call_count = 0 |
| 111 | original_write_commit = None |
| 112 | |
| 113 | def _write_commit_side_effect(repo_root: pathlib.Path, commit: CommitRecord, **kwargs: str) -> None: |
| 114 | nonlocal call_count |
| 115 | call_count += 1 |
| 116 | if commit.commit_id == bad.commit_id: |
| 117 | raise OSError("Store integrity violation") |
| 118 | from muse.core.commits import write_commit as _wc |
| 119 | _wc(repo_root, commit, **kwargs) |
| 120 | |
| 121 | with patch("muse.core.mpack.write_commit", side_effect=_write_commit_side_effect): |
| 122 | apply_mpack(repo, mpack) |
| 123 | |
| 124 | assert read_commit(repo, good.commit_id) is not None, ( |
| 125 | "apply_mpack must continue processing commits after an integrity violation" |
| 126 | ) |
| 127 | |
| 128 | def test_apply_mpack_commits_written_excludes_integrity_violation(self, tmp_path: pathlib.Path) -> None: |
| 129 | """commits_written must not include commits that triggered OSError.""" |
| 130 | repo = _make_repo(tmp_path) |
| 131 | bad = _make_commit_record("integrity-violation") |
| 132 | good = _make_commit_record("normal-commit") |
| 133 | mpack = _mpack_with_commits([bad, good]) |
| 134 | |
| 135 | def _write_commit_side_effect(repo_root: pathlib.Path, commit: CommitRecord, **kwargs: str) -> None: |
| 136 | if commit.commit_id == bad.commit_id: |
| 137 | raise OSError("Store integrity violation") |
| 138 | from muse.core.commits import write_commit as _wc |
| 139 | _wc(repo_root, commit, **kwargs) |
| 140 | |
| 141 | with patch("muse.core.mpack.write_commit", side_effect=_write_commit_side_effect): |
| 142 | result = apply_mpack(repo, mpack) |
| 143 | |
| 144 | assert result["commits_written"] == 1, ( |
| 145 | f"commits_written should be 1 (only good), got {result['commits_written']}" |
| 146 | ) |
| 147 | |
| 148 | def test_apply_mpack_oserror_commit_not_written(self, tmp_path: pathlib.Path) -> None: |
| 149 | """A commit that raised OSError during write must not appear in the store.""" |
| 150 | repo = _make_repo(tmp_path) |
| 151 | c = _make_commit_record("legit-commit") |
| 152 | mpack = _mpack_with_commits([c]) |
| 153 | |
| 154 | with patch("muse.core.mpack.write_commit", side_effect=OSError("injected")): |
| 155 | apply_mpack(repo, mpack) |
| 156 | |
| 157 | assert read_commit(repo, c.commit_id) is None, ( |
| 158 | "A commit whose write_commit raised OSError must not appear in the store" |
| 159 | ) |
| 160 | |
| 161 | def test_apply_mpack_valid_commit_no_integrity_violation(self, tmp_path: pathlib.Path) -> None: |
| 162 | """Regression: valid commits must still be written normally.""" |
| 163 | repo = _make_repo(tmp_path) |
| 164 | c = _make_commit_record("clean-commit") |
| 165 | mpack = _mpack_with_commits([c]) |
| 166 | result = apply_mpack(repo, mpack) |
| 167 | |
| 168 | assert result["commits_written"] == 1 |
| 169 | assert read_commit(repo, c.commit_id) is not None |
| 170 | |
| 171 | def test_apply_mpack_wrong_hash_raises_valuerror_not_oserror(self, tmp_path: pathlib.Path) -> None: |
| 172 | """Commits with mismatched commit_id hash are skipped (not raised).""" |
| 173 | repo = _make_repo(tmp_path) |
| 174 | bad_dict = CommitDict( |
| 175 | commit_id="sha256:" + "f" * 64, # wrong hash |
| 176 | branch="main", |
| 177 | snapshot_id="sha256:" + "a" * 64, |
| 178 | message="bad", |
| 179 | committed_at=_TS.isoformat(), |
| 180 | parent_commit_id=None, |
| 181 | parent2_commit_id=None, |
| 182 | author="tester", |
| 183 | ) |
| 184 | mpack = MPack(blobs=[], snapshots=[], commits=[bad_dict], tags=[]) |
| 185 | result = apply_mpack(repo, mpack) # must not raise |
| 186 | assert result["commits_written"] == 0 |
File History
1 commit
sha256:8860dea10c653956b613a814cc752a6d34cb3986cdf16749a49172affdabf045
fix tests
Human
minor
⚠
15 days ago