|
| 1 | +"""Tests for backup retention pruning (mempalace.backups.prune_backups). |
| 2 | +
|
| 3 | +These guard the fix for unbounded backup growth: ``mempalace migrate`` and |
| 4 | +``mempalace repair max-seq-id`` each drop a fresh full-size, timestamped copy |
| 5 | +every run, and used to never delete the old ones — a palace was found with |
| 6 | +hundreds of GB of stale backups beside a few hundred MB of live data. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from mempalace.backups import prune_backups |
| 14 | + |
| 15 | + |
| 16 | +def _make_backup_dir(parent, name, mtime): |
| 17 | + """Create a directory backup with a fixed mtime.""" |
| 18 | + path = parent / name |
| 19 | + path.mkdir() |
| 20 | + (path / "chroma.sqlite3").write_text("db") |
| 21 | + os.utime(path, (mtime, mtime)) |
| 22 | + return path |
| 23 | + |
| 24 | + |
| 25 | +def _make_backup_file(parent, name, mtime): |
| 26 | + """Create a file backup with a fixed mtime.""" |
| 27 | + path = parent / name |
| 28 | + path.write_text("db") |
| 29 | + os.utime(path, (mtime, mtime)) |
| 30 | + return path |
| 31 | + |
| 32 | + |
| 33 | +def test_prune_keeps_newest_and_removes_oldest(tmp_path): |
| 34 | + # 5 backups, mtimes 100..500; keep 2 newest (400, 500). |
| 35 | + paths = [_make_backup_file(tmp_path, f"b.{i}", mtime=i * 100) for i in range(1, 6)] |
| 36 | + |
| 37 | + removed = prune_backups(str(tmp_path / "b.*"), max_backups=2) |
| 38 | + |
| 39 | + surviving = {p.name for p in tmp_path.iterdir()} |
| 40 | + assert surviving == {"b.4", "b.5"} |
| 41 | + assert set(removed) == {str(paths[0]), str(paths[1]), str(paths[2])} |
| 42 | + |
| 43 | + |
| 44 | +def test_prune_removes_directory_backups(tmp_path): |
| 45 | + """migrate writes directory backups (full copytree) — must rmtree them.""" |
| 46 | + _make_backup_dir(tmp_path, "palace.pre-migrate.1", mtime=100) |
| 47 | + _make_backup_dir(tmp_path, "palace.pre-migrate.2", mtime=200) |
| 48 | + keep = _make_backup_dir(tmp_path, "palace.pre-migrate.3", mtime=300) |
| 49 | + |
| 50 | + removed = prune_backups(str(tmp_path / "palace.pre-migrate.*"), max_backups=1) |
| 51 | + |
| 52 | + assert keep.is_dir() |
| 53 | + assert len(removed) == 2 |
| 54 | + assert not (tmp_path / "palace.pre-migrate.1").exists() |
| 55 | + assert not (tmp_path / "palace.pre-migrate.2").exists() |
| 56 | + |
| 57 | + |
| 58 | +def test_prune_noop_when_under_limit(tmp_path): |
| 59 | + _make_backup_file(tmp_path, "b.1", mtime=100) |
| 60 | + _make_backup_file(tmp_path, "b.2", mtime=200) |
| 61 | + |
| 62 | + removed = prune_backups(str(tmp_path / "b.*"), max_backups=10) |
| 63 | + |
| 64 | + assert removed == [] |
| 65 | + assert len(list(tmp_path.iterdir())) == 2 |
| 66 | + |
| 67 | + |
| 68 | +def test_prune_noop_when_exactly_at_limit(tmp_path): |
| 69 | + _make_backup_file(tmp_path, "b.1", mtime=100) |
| 70 | + _make_backup_file(tmp_path, "b.2", mtime=200) |
| 71 | + |
| 72 | + removed = prune_backups(str(tmp_path / "b.*"), max_backups=2) |
| 73 | + |
| 74 | + assert removed == [] |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("disabled", [0, -1, None]) |
| 78 | +def test_prune_disabled_keeps_everything(tmp_path, disabled): |
| 79 | + for i in range(1, 6): |
| 80 | + _make_backup_file(tmp_path, f"b.{i}", mtime=i * 100) |
| 81 | + |
| 82 | + removed = prune_backups(str(tmp_path / "b.*"), max_backups=disabled) |
| 83 | + |
| 84 | + assert removed == [] |
| 85 | + assert len(list(tmp_path.iterdir())) == 5 |
| 86 | + |
| 87 | + |
| 88 | +def test_prune_no_matches(tmp_path): |
| 89 | + assert prune_backups(str(tmp_path / "nope.*"), max_backups=3) == [] |
| 90 | + |
| 91 | + |
| 92 | +def test_prune_only_touches_matching_pattern(tmp_path): |
| 93 | + """Live data and unrelated files must never be swept up by a backup glob.""" |
| 94 | + _make_backup_file(tmp_path, "chroma.sqlite3.max-seq-id-backup-1", mtime=100) |
| 95 | + _make_backup_file(tmp_path, "chroma.sqlite3.max-seq-id-backup-2", mtime=200) |
| 96 | + _make_backup_file(tmp_path, "chroma.sqlite3.max-seq-id-backup-3", mtime=300) |
| 97 | + # The live database and an unrelated file — must survive. |
| 98 | + live = _make_backup_file(tmp_path, "chroma.sqlite3", mtime=400) |
| 99 | + other = _make_backup_file(tmp_path, "tunnels.json", mtime=400) |
| 100 | + |
| 101 | + prune_backups( |
| 102 | + str(tmp_path / "chroma.sqlite3.max-seq-id-backup-*"), |
| 103 | + max_backups=1, |
| 104 | + ) |
| 105 | + |
| 106 | + assert live.exists() |
| 107 | + assert other.exists() |
| 108 | + assert (tmp_path / "chroma.sqlite3.max-seq-id-backup-3").exists() |
| 109 | + assert not (tmp_path / "chroma.sqlite3.max-seq-id-backup-1").exists() |
| 110 | + assert not (tmp_path / "chroma.sqlite3.max-seq-id-backup-2").exists() |
| 111 | + |
| 112 | + |
| 113 | +def test_prune_respects_glob_escape_for_metacharacter_paths(tmp_path): |
| 114 | + """Palace paths can contain glob metacharacters like ``[``. |
| 115 | +
|
| 116 | + Without ``glob.escape`` the pattern would silently match nothing (the |
| 117 | + bracket is read as a character class), leaving backups unpruned. Callers |
| 118 | + escape the literal prefix; this confirms the helper prunes correctly once |
| 119 | + they do. |
| 120 | + """ |
| 121 | + import glob |
| 122 | + |
| 123 | + weird = tmp_path / "weird[name]" |
| 124 | + weird.mkdir() |
| 125 | + for i in range(1, 4): |
| 126 | + _make_backup_file(weird, f"chroma.sqlite3.max-seq-id-backup-{i}", mtime=i * 100) |
| 127 | + |
| 128 | + pattern = os.path.join(glob.escape(str(weird)), "chroma.sqlite3.max-seq-id-backup-*") |
| 129 | + removed = prune_backups(pattern, max_backups=1) |
| 130 | + |
| 131 | + assert len(removed) == 2 |
| 132 | + assert (weird / "chroma.sqlite3.max-seq-id-backup-3").exists() |
| 133 | + |
| 134 | + |
| 135 | +def test_prune_is_best_effort_on_delete_failure(tmp_path, monkeypatch): |
| 136 | + """A failed deletion is logged and skipped, never raised — pruning must |
| 137 | + not undo a migrate/repair that already succeeded.""" |
| 138 | + for i in range(1, 5): |
| 139 | + _make_backup_file(tmp_path, f"b.{i}", mtime=i * 100) |
| 140 | + |
| 141 | + real_remove = os.remove |
| 142 | + |
| 143 | + def flaky_remove(path): |
| 144 | + if path.endswith("b.1"): |
| 145 | + raise OSError("permission denied") |
| 146 | + return real_remove(path) |
| 147 | + |
| 148 | + monkeypatch.setattr(os, "remove", flaky_remove) |
| 149 | + |
| 150 | + logs = [] |
| 151 | + removed = prune_backups(str(tmp_path / "b.*"), max_backups=2, log=logs.append) |
| 152 | + |
| 153 | + # b.1 and b.2 were over the limit; b.1 failed, b.2 succeeded. |
| 154 | + assert str(tmp_path / "b.2") in removed |
| 155 | + assert str(tmp_path / "b.1") not in removed |
| 156 | + assert (tmp_path / "b.1").exists() |
| 157 | + assert any("could not remove" in line for line in logs) |
0 commit comments