gabriel / muse public
test_apply_pack_oserror_integrity.py python
202 lines 8.2 KB
Raw
sha256:8860dea10c653956b613a814cc752a6d34cb3986cdf16749a49172affdabf045 fix tests Human minor ⚠ breaking 3 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 Manifest, 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 _manifest_for(message: str) -> Manifest:
44 return {f"{message}.py": fake_id("obj")}
45
46
47 def _make_commit_record(message: str, parent: str | None = None) -> CommitRecord:
48 manifest = _manifest_for(message)
49 snap_id = hash_snapshot(manifest)
50 parent_ids = [parent] if parent else []
51 cid = hash_commit(
52 parent_ids=parent_ids,
53 snapshot_id=snap_id,
54 message=message,
55 committed_at_iso=_TS.isoformat(),
56 author="tester",
57 )
58 return CommitRecord(
59 commit_id=cid,
60 branch="main",
61 snapshot_id=snap_id,
62 message=message,
63 committed_at=_TS,
64 author="tester",
65 parent_commit_id=parent,
66 parent2_commit_id=None,
67 )
68
69
70 def _mpack_with_commits(commits: list[CommitRecord]) -> MPack:
71 # Include the snapshot for each commit so apply_mpack's missing-snapshot
72 # guard passes and commits reach the write_commit call (where OSError mocks fire).
73 snapshots = [
74 {
75 "snapshot_id": c.snapshot_id,
76 "parent_snapshot_id": None,
77 "delta_upsert": _manifest_for(c.message),
78 "delta_remove": [],
79 "directories": [],
80 }
81 for c in commits
82 ]
83 return MPack(blobs=[], snapshots=snapshots, commits=[c.to_dict() for c in commits], tags=[])
84
85
86 # ──────────────────────────────────────────────────────────────────────────────
87 # Bug 13: unhandled OSError from write_commit
88 # ──────────────────────────────────────────────────────────────────────────────
89
90 class TestApplyMPackOsErrorIntegrity:
91
92 def test_apply_mpack_does_not_crash_on_store_integrity_violation(self, tmp_path: pathlib.Path) -> None:
93 """Bug 13: apply_mpack must not propagate OSError from write_commit."""
94 repo = _make_repo(tmp_path)
95 c = _make_commit_record("good-commit")
96 mpack = _mpack_with_commits([c])
97
98 with patch("muse.core.mpack.write_commit", side_effect=OSError("Store integrity violation")):
99 result = apply_mpack(repo, mpack) # must not raise
100 assert result is not None
101
102 def test_apply_mpack_logs_critical_on_store_integrity_violation(
103 self, tmp_path: pathlib.Path, caplog: pytest.LogCaptureFixture
104 ) -> None:
105 """apply_mpack must log CRITICAL (not swallow silently) when OSError is raised."""
106 repo = _make_repo(tmp_path)
107 c = _make_commit_record("critical-log-commit")
108 mpack = _mpack_with_commits([c])
109
110 with caplog.at_level(logging.CRITICAL, logger="muse.core.mpack"):
111 with patch("muse.core.mpack.write_commit", side_effect=OSError("Store integrity violation")):
112 apply_mpack(repo, mpack)
113
114 crits = [r for r in caplog.records if r.levelno >= logging.CRITICAL]
115 assert crits, "apply_mpack must log CRITICAL when write_commit raises OSError"
116 assert any("integrity" in r.message.lower() or "violation" in r.message.lower()
117 for r in crits)
118
119 def test_apply_mpack_continues_after_integrity_violation(self, tmp_path: pathlib.Path) -> None:
120 """Remaining commits must be processed after an integrity-violation skip."""
121 repo = _make_repo(tmp_path)
122 bad = _make_commit_record("bad-commit")
123 good = _make_commit_record("good-commit-after")
124 mpack = _mpack_with_commits([bad, good])
125
126 call_count = 0
127 original_write_commit = None
128
129 def _write_commit_side_effect(repo_root: pathlib.Path, commit: CommitRecord, **kwargs: str) -> None:
130 nonlocal call_count
131 call_count += 1
132 if commit.commit_id == bad.commit_id:
133 raise OSError("Store integrity violation")
134 from muse.core.commits import write_commit as _wc
135 _wc(repo_root, commit, **kwargs)
136
137 with patch("muse.core.mpack.write_commit", side_effect=_write_commit_side_effect):
138 apply_mpack(repo, mpack)
139
140 assert read_commit(repo, good.commit_id) is not None, (
141 "apply_mpack must continue processing commits after an integrity violation"
142 )
143
144 def test_apply_mpack_commits_written_excludes_integrity_violation(self, tmp_path: pathlib.Path) -> None:
145 """commits_written must not include commits that triggered OSError."""
146 repo = _make_repo(tmp_path)
147 bad = _make_commit_record("integrity-violation")
148 good = _make_commit_record("normal-commit")
149 mpack = _mpack_with_commits([bad, good])
150
151 def _write_commit_side_effect(repo_root: pathlib.Path, commit: CommitRecord, **kwargs: str) -> None:
152 if commit.commit_id == bad.commit_id:
153 raise OSError("Store integrity violation")
154 from muse.core.commits import write_commit as _wc
155 _wc(repo_root, commit, **kwargs)
156
157 with patch("muse.core.mpack.write_commit", side_effect=_write_commit_side_effect):
158 result = apply_mpack(repo, mpack)
159
160 assert result["commits_written"] == 1, (
161 f"commits_written should be 1 (only good), got {result['commits_written']}"
162 )
163
164 def test_apply_mpack_oserror_commit_not_written(self, tmp_path: pathlib.Path) -> None:
165 """A commit that raised OSError during write must not appear in the store."""
166 repo = _make_repo(tmp_path)
167 c = _make_commit_record("legit-commit")
168 mpack = _mpack_with_commits([c])
169
170 with patch("muse.core.mpack.write_commit", side_effect=OSError("injected")):
171 apply_mpack(repo, mpack)
172
173 assert read_commit(repo, c.commit_id) is None, (
174 "A commit whose write_commit raised OSError must not appear in the store"
175 )
176
177 def test_apply_mpack_valid_commit_no_integrity_violation(self, tmp_path: pathlib.Path) -> None:
178 """Regression: valid commits must still be written normally."""
179 repo = _make_repo(tmp_path)
180 c = _make_commit_record("clean-commit")
181 mpack = _mpack_with_commits([c])
182 result = apply_mpack(repo, mpack)
183
184 assert result["commits_written"] == 1
185 assert read_commit(repo, c.commit_id) is not None
186
187 def test_apply_mpack_wrong_hash_raises_valuerror_not_oserror(self, tmp_path: pathlib.Path) -> None:
188 """Commits with mismatched commit_id hash are skipped (not raised)."""
189 repo = _make_repo(tmp_path)
190 bad_dict = CommitDict(
191 commit_id="sha256:" + "f" * 64, # wrong hash
192 branch="main",
193 snapshot_id="sha256:" + "a" * 64,
194 message="bad",
195 committed_at=_TS.isoformat(),
196 parent_commit_id=None,
197 parent2_commit_id=None,
198 author="tester",
199 )
200 mpack = MPack(blobs=[], snapshots=[], commits=[bad_dict], tags=[])
201 result = apply_mpack(repo, mpack) # must not raise
202 assert result["commits_written"] == 0
File History 1 commit