askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_object_backup.py · 21.4 KB · 547 lines
| 1 | import gzip |
| 2 | import io |
| 3 | import json |
| 4 | import tarfile |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | import object_backup |
| 10 | import object_logs |
| 11 | import object_state |
| 12 | |
| 13 | |
| 14 | def write_file(path: Path, content: str | bytes): |
| 15 | path.parent.mkdir(parents=True, exist_ok=True) |
| 16 | if isinstance(content, bytes): |
| 17 | path.write_bytes(content) |
| 18 | else: |
| 19 | path.write_text(content) |
| 20 | return path |
| 21 | |
| 22 | |
| 23 | def make_runtime_tree(tmp_path): |
| 24 | objects_dir = tmp_path / "objects" |
| 25 | data_dir = tmp_path / "data" |
| 26 | |
| 27 | write_file( |
| 28 | objects_dir / "site" / "home.py", |
| 29 | "def GET(request):\n return {'status': 'ok'}\n", |
| 30 | ) |
| 31 | write_file(objects_dir / "__pycache__" / "cached.pyc", "cache") |
| 32 | write_file(objects_dir / ".secret.py", "secret") |
| 33 | |
| 34 | write_file(data_dir / "state" / "site_home" / "state.tsv", "count\t3\t1.0\n") |
| 35 | write_file(data_dir / "state" / "site_home" / ".state.tsv.tmp", "partial") |
| 36 | |
| 37 | write_file( |
| 38 | data_dir / "logs" / "site_home" / "log.tsv", |
| 39 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 40 | "cur\t2026-01-01T00:00:01Z\tINFO\tcurrent served\n", |
| 41 | ) |
| 42 | with gzip.open(data_dir / "logs" / "site_home" / "log-20260101-000000.tsv.gz", "wt") as f: |
| 43 | f.write( |
| 44 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 45 | "old\t2026-01-01T00:00:00Z\tINFO\trotated served\n" |
| 46 | ) |
| 47 | write_file(data_dir / "logs" / "site_home" / ".log.tsv.lock", "") |
| 48 | |
| 49 | write_file( |
| 50 | data_dir / "versions" / "site_home" / "metadata.tsv", |
| 51 | "version_id\ttimestamp\tauthor\tmessage\thash\n" |
| 52 | "1\t2026-01-01T00:00:00Z\ttest\tinitial\tabc\n", |
| 53 | ) |
| 54 | write_file(data_dir / "versions" / "site_home" / "v1.txt", "def GET(request):\n return {}\n") |
| 55 | write_file( |
| 56 | data_dir / "source_changes" / "site_home" / "changes.jsonl", |
| 57 | '{"action":"source_update","object_id":"site_home","version_id":1}\n', |
| 58 | ) |
| 59 | write_file( |
| 60 | data_dir / "schema_versions" / "contacts" / "metadata.tsv", |
| 61 | "version_id\ttimestamp\tauthor\tmessage\thash\n" |
| 62 | "1\t2026-01-01T00:00:00Z\ttest\tinitial\tabc\n", |
| 63 | ) |
| 64 | write_file(data_dir / "schema_versions" / "contacts" / "v1.json", '{"fields": []}\n') |
| 65 | write_file( |
| 66 | data_dir / "record_changes" / "contacts" / "changes.jsonl", |
| 67 | '{"action":"create","collection":"contacts","record_id":"c1"}\n', |
| 68 | ) |
| 69 | write_file( |
| 70 | data_dir / "package_changes" / "hello-world" / "changes.jsonl", |
| 71 | '{"action":"dry_run","package_id":"hello-world"}\n', |
| 72 | ) |
| 73 | write_file( |
| 74 | data_dir / "file_changes" / "site_home" / "changes.jsonl", |
| 75 | '{"action":"file_create","object_id":"site_home","file_name":"upload.txt"}\n', |
| 76 | ) |
| 77 | write_file(data_dir / "files" / "site_home" / "upload.txt", "file payload\n") |
| 78 | write_file(data_dir / "schemas" / "contacts.json", '{"fields": [{"name": "id"}]}\n') |
| 79 | write_file(data_dir / "collections" / "contacts" / "records.tsv", "id\tname\nc1\tAda\n") |
| 80 | write_file(data_dir / "ratelimit" / "site_home.txt", "123\n") |
| 81 | |
| 82 | return objects_dir, data_dir |
| 83 | |
| 84 | |
| 85 | def archive_names(path: Path) -> list[str]: |
| 86 | with tarfile.open(path, "r:*") as archive: |
| 87 | return archive.getnames() |
| 88 | |
| 89 | |
| 90 | def read_manifest(path: Path) -> dict: |
| 91 | with tarfile.open(path, "r:*") as archive: |
| 92 | manifest = archive.extractfile(object_backup.MANIFEST_NAME) |
| 93 | assert manifest is not None |
| 94 | return json.loads(manifest.read().decode("utf-8")) |
| 95 | |
| 96 | |
| 97 | def test_create_runtime_backup_includes_runtime_files_and_manifest(tmp_path): |
| 98 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 99 | backup = tmp_path / "backup" / "runtime.tar.gz" |
| 100 | |
| 101 | summary = object_backup.create_runtime_backup( |
| 102 | backup, |
| 103 | objects_dir=objects_dir, |
| 104 | data_dir=data_dir, |
| 105 | created_at="2026-01-01T00:00:00Z", |
| 106 | ) |
| 107 | |
| 108 | names = archive_names(backup) |
| 109 | assert object_backup.MANIFEST_NAME in names |
| 110 | assert "objects/site/home.py" in names |
| 111 | assert "data/state/site_home/state.tsv" in names |
| 112 | assert "data/logs/site_home/log.tsv" in names |
| 113 | assert "data/logs/site_home/log-20260101-000000.tsv.gz" in names |
| 114 | assert "data/versions/site_home/metadata.tsv" in names |
| 115 | assert "data/versions/site_home/v1.txt" in names |
| 116 | assert "data/source_changes/site_home/changes.jsonl" in names |
| 117 | assert "data/schema_versions/contacts/metadata.tsv" in names |
| 118 | assert "data/schema_versions/contacts/v1.json" in names |
| 119 | assert "data/record_changes/contacts/changes.jsonl" in names |
| 120 | assert "data/package_changes/hello-world/changes.jsonl" in names |
| 121 | assert "data/file_changes/site_home/changes.jsonl" in names |
| 122 | assert "data/files/site_home/upload.txt" in names |
| 123 | assert "data/schemas/contacts.json" in names |
| 124 | assert "data/collections/contacts/records.tsv" in names |
| 125 | |
| 126 | assert "objects/.secret.py" not in names |
| 127 | assert "objects/__pycache__/cached.pyc" not in names |
| 128 | assert "data/state/site_home/.state.tsv.tmp" not in names |
| 129 | assert "data/logs/site_home/.log.tsv.lock" not in names |
| 130 | assert "data/ratelimit/site_home.txt" not in names |
| 131 | |
| 132 | manifest = read_manifest(backup) |
| 133 | assert manifest["format_version"] == object_backup.BACKUP_FORMAT_VERSION |
| 134 | assert manifest["created_at"] == "2026-01-01T00:00:00Z" |
| 135 | assert manifest["files"] == summary.files == 15 |
| 136 | assert "deployment secrets are not included" in manifest["notes"] |
| 137 | |
| 138 | verification = object_backup.verify_runtime_backup(backup) |
| 139 | assert verification.ok |
| 140 | assert verification.files == 15 |
| 141 | assert verification.entries == [ |
| 142 | "data/collections", |
| 143 | "data/file_changes", |
| 144 | "data/files", |
| 145 | "data/logs", |
| 146 | "data/package_changes", |
| 147 | "data/record_changes", |
| 148 | "data/schema_versions", |
| 149 | "data/schemas", |
| 150 | "data/source_changes", |
| 151 | "data/state", |
| 152 | "data/versions", |
| 153 | "objects", |
| 154 | ] |
| 155 | |
| 156 | |
| 157 | def test_create_runtime_restore_point_uses_data_backups_and_excludes_existing_backups(tmp_path): |
| 158 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 159 | write_file(data_dir / "backups" / "old-runtime.tar.gz", "old backup") |
| 160 | |
| 161 | summary = object_backup.create_runtime_restore_point( |
| 162 | "package-hello-world", |
| 163 | objects_dir=objects_dir, |
| 164 | data_dir=data_dir, |
| 165 | created_at="2026-01-01T00:00:00Z", |
| 166 | ) |
| 167 | |
| 168 | backup = Path(summary.path) |
| 169 | assert backup.parent == data_dir / "backups" |
| 170 | assert backup.name == "20260101T000000Z-package-hello-world.tar.gz" |
| 171 | names = archive_names(backup) |
| 172 | assert "objects/site/home.py" in names |
| 173 | assert "data/backups/old-runtime.tar.gz" not in names |
| 174 | |
| 175 | manifest = read_manifest(backup) |
| 176 | assert manifest["created_at"] == "2026-01-01T00:00:00Z" |
| 177 | |
| 178 | |
| 179 | def test_create_runtime_restore_point_rejects_unsafe_labels(tmp_path): |
| 180 | with pytest.raises(object_backup.BackupRestoreError, match="invalid restore point label"): |
| 181 | object_backup.create_runtime_restore_point( |
| 182 | "../bad", |
| 183 | objects_dir=tmp_path / "objects", |
| 184 | data_dir=tmp_path / "data", |
| 185 | ) |
| 186 | |
| 187 | |
| 188 | def test_restore_runtime_backup_restores_objects_state_logs_versions_and_files(tmp_path): |
| 189 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 190 | backup = tmp_path / "runtime.tar.gz" |
| 191 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 192 | |
| 193 | restored_objects = tmp_path / "restored" / "objects" |
| 194 | restored_data = tmp_path / "restored" / "data" |
| 195 | summary = object_backup.restore_runtime_backup( |
| 196 | backup, |
| 197 | objects_dir=restored_objects, |
| 198 | data_dir=restored_data, |
| 199 | ) |
| 200 | |
| 201 | assert summary.files == 15 |
| 202 | assert (restored_objects / "site" / "home.py").read_text().startswith("def GET") |
| 203 | assert object_state.get_object_state("site_home", base_dir=restored_data) == {"count": 3} |
| 204 | assert (restored_data / "schema_versions" / "contacts" / "v1.json").exists() |
| 205 | |
| 206 | logs = object_logs.get_object_logs("site_home", base_dir=restored_data) |
| 207 | assert [entry["message"] for entry in logs] == ["current served", "rotated served"] |
| 208 | |
| 209 | assert (restored_data / "versions" / "site_home" / "metadata.tsv").exists() |
| 210 | assert (restored_data / "source_changes" / "site_home" / "changes.jsonl").exists() |
| 211 | assert (restored_data / "record_changes" / "contacts" / "changes.jsonl").exists() |
| 212 | assert (restored_data / "package_changes" / "hello-world" / "changes.jsonl").exists() |
| 213 | assert (restored_data / "file_changes" / "site_home" / "changes.jsonl").exists() |
| 214 | assert (restored_data / "files" / "site_home" / "upload.txt").read_text() == "file payload\n" |
| 215 | assert (restored_data / "schemas" / "contacts.json").exists() |
| 216 | assert (restored_data / "collections" / "contacts" / "records.tsv").read_text() == "id\tname\nc1\tAda\n" |
| 217 | |
| 218 | |
| 219 | def test_restore_refuses_to_overwrite_existing_runtime_files(tmp_path): |
| 220 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 221 | backup = tmp_path / "runtime.tar.gz" |
| 222 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 223 | |
| 224 | restored_objects = tmp_path / "restored" / "objects" |
| 225 | restored_data = tmp_path / "restored" / "data" |
| 226 | object_backup.restore_runtime_backup(backup, objects_dir=restored_objects, data_dir=restored_data) |
| 227 | |
| 228 | with pytest.raises(object_backup.BackupRestoreError, match="already exists"): |
| 229 | object_backup.restore_runtime_backup(backup, objects_dir=restored_objects, data_dir=restored_data) |
| 230 | |
| 231 | result = object_backup.restore_runtime_backup( |
| 232 | backup, |
| 233 | objects_dir=restored_objects, |
| 234 | data_dir=restored_data, |
| 235 | overwrite=True, |
| 236 | ) |
| 237 | assert result.overwritten is True |
| 238 | |
| 239 | |
| 240 | def test_restore_runtime_backup_can_prune_extra_runtime_files(tmp_path): |
| 241 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 242 | backup = tmp_path / "runtime.tar.gz" |
| 243 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 244 | |
| 245 | restored_objects = tmp_path / "restored" / "objects" |
| 246 | restored_data = tmp_path / "restored" / "data" |
| 247 | object_backup.restore_runtime_backup(backup, objects_dir=restored_objects, data_dir=restored_data) |
| 248 | |
| 249 | write_file(restored_objects / "new" / "package_object.py", "def GET(request): return {}\n") |
| 250 | write_file(restored_data / "collections" / "new" / "records.tsv", "id\tname\nn1\tnew\n") |
| 251 | write_file(restored_data / "backups" / "keep-this.tar.gz", "backup payload\n") |
| 252 | |
| 253 | result = object_backup.restore_runtime_backup( |
| 254 | backup, |
| 255 | objects_dir=restored_objects, |
| 256 | data_dir=restored_data, |
| 257 | overwrite=True, |
| 258 | prune_extra=True, |
| 259 | ) |
| 260 | |
| 261 | assert result.pruned_files == 2 |
| 262 | assert result.pruned_dirs >= 2 |
| 263 | assert not (restored_objects / "new" / "package_object.py").exists() |
| 264 | assert not (restored_data / "collections" / "new" / "records.tsv").exists() |
| 265 | assert (restored_data / "backups" / "keep-this.tar.gz").exists() |
| 266 | |
| 267 | |
| 268 | def test_restore_runtime_backup_reports_what_it_changes_and_keeps_histories(tmp_path): |
| 269 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 270 | backup = tmp_path / "runtime.tar.gz" |
| 271 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 272 | |
| 273 | write_file(objects_dir / "site" / "home.py", "def GET(request): return {'edited': 1}\n") |
| 274 | write_file(objects_dir / "new" / "thing.py", "def GET(request): return {}\n") |
| 275 | changes_file = data_dir / "source_changes" / "site_home" / "changes.jsonl" |
| 276 | changes_file.write_text(changes_file.read_text() + '{"action":"source_update","version_id":2}\n') |
| 277 | write_file(data_dir / "record_changes" / "new" / "archive" / "changes-1.jsonl.gz", b"gz") |
| 278 | |
| 279 | seen = [] |
| 280 | object_backup.restore_runtime_backup( |
| 281 | backup, |
| 282 | objects_dir=objects_dir, |
| 283 | data_dir=data_dir, |
| 284 | overwrite=True, |
| 285 | prune_extra=True, |
| 286 | preserve=object_backup.is_package_restore_preserved, |
| 287 | on_change=lambda name, before, after: seen.append((name, before, after)), |
| 288 | ) |
| 289 | |
| 290 | # Only files whose bytes changed (or that are pruned: these two, and the |
| 291 | # tree's cache/temp files the archive leaves out) are reported. |
| 292 | by_name = {name: (before, after) for name, before, after in seen} |
| 293 | assert set(by_name) == { |
| 294 | "objects/new/thing.py", |
| 295 | "objects/site/home.py", |
| 296 | "objects/__pycache__/cached.pyc", |
| 297 | "objects/.secret.py", |
| 298 | "data/state/site_home/.state.tsv.tmp", |
| 299 | } |
| 300 | assert by_name["objects/new/thing.py"][1] is None |
| 301 | assert by_name["objects/site/home.py"][0] == b"def GET(request): return {'edited': 1}\n" |
| 302 | # The histories are kept as they are on disk: not rewound, not pruned. |
| 303 | assert "version_id\":2" in changes_file.read_text() |
| 304 | assert (data_dir / "record_changes" / "new" / "archive" / "changes-1.jsonl.gz").exists() |
| 305 | |
| 306 | |
| 307 | def test_history_members_are_exactly_the_history_roots(): |
| 308 | assert object_backup.is_history_member("data/versions/site_home/v1.txt") |
| 309 | assert object_backup.is_history_member("data/record_changes/notes/archive/changes-1.jsonl.gz") |
| 310 | assert object_backup.is_history_member("data/logs/site_home/log.tsv") |
| 311 | assert not object_backup.is_history_member("data/versions") |
| 312 | assert not object_backup.is_history_member("data/collections/notes/records.tsv") |
| 313 | assert not object_backup.is_history_member("data/permissions/policy.json") |
| 314 | assert not object_backup.is_history_member("objects/versions/x.py") |
| 315 | |
| 316 | |
| 317 | def test_restore_runtime_backup_prune_requires_overwrite(tmp_path): |
| 318 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 319 | backup = tmp_path / "runtime.tar.gz" |
| 320 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 321 | |
| 322 | with pytest.raises(object_backup.BackupRestoreError, match="prune_extra requires overwrite"): |
| 323 | object_backup.restore_runtime_backup( |
| 324 | backup, |
| 325 | objects_dir=tmp_path / "restored" / "objects", |
| 326 | data_dir=tmp_path / "restored" / "data", |
| 327 | prune_extra=True, |
| 328 | ) |
| 329 | |
| 330 | |
| 331 | def test_verify_and_restore_reject_path_traversal_member(tmp_path): |
| 332 | backup = tmp_path / "bad.tar.gz" |
| 333 | with tarfile.open(backup, "w:gz") as archive: |
| 334 | _add_manifest(archive, files=1, bytes_count=4) |
| 335 | info = tarfile.TarInfo("../outside.py") |
| 336 | payload = b"evil" |
| 337 | info.size = len(payload) |
| 338 | archive.addfile(info, io.BytesIO(payload)) |
| 339 | |
| 340 | verification = object_backup.verify_runtime_backup(backup) |
| 341 | assert not verification.ok |
| 342 | assert any("unsafe archive member path" in error for error in verification.errors) |
| 343 | |
| 344 | with pytest.raises(object_backup.BackupRestoreError, match="unsafe archive member path"): |
| 345 | object_backup.restore_runtime_backup( |
| 346 | backup, |
| 347 | objects_dir=tmp_path / "objects", |
| 348 | data_dir=tmp_path / "data", |
| 349 | ) |
| 350 | |
| 351 | |
| 352 | def test_verify_and_restore_reject_link_members(tmp_path): |
| 353 | backup = tmp_path / "link.tar.gz" |
| 354 | with tarfile.open(backup, "w:gz") as archive: |
| 355 | _add_manifest(archive, files=0, bytes_count=0) |
| 356 | info = tarfile.TarInfo("objects/link.py") |
| 357 | info.type = tarfile.SYMTYPE |
| 358 | info.linkname = "/etc/passwd" |
| 359 | archive.addfile(info) |
| 360 | |
| 361 | verification = object_backup.verify_runtime_backup(backup) |
| 362 | assert not verification.ok |
| 363 | assert any("uses links" in error for error in verification.errors) |
| 364 | |
| 365 | with pytest.raises(object_backup.BackupRestoreError, match="uses links"): |
| 366 | object_backup.restore_runtime_backup( |
| 367 | backup, |
| 368 | objects_dir=tmp_path / "objects", |
| 369 | data_dir=tmp_path / "data", |
| 370 | ) |
| 371 | |
| 372 | |
| 373 | def test_cli_create_verify_and_restore_json(tmp_path, capsys): |
| 374 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 375 | backup = tmp_path / "runtime.tar.gz" |
| 376 | |
| 377 | create_exit = object_backup.main( |
| 378 | [ |
| 379 | "create", |
| 380 | str(backup), |
| 381 | "--objects-dir", |
| 382 | str(objects_dir), |
| 383 | "--data-dir", |
| 384 | str(data_dir), |
| 385 | "--json", |
| 386 | ] |
| 387 | ) |
| 388 | create_payload = json.loads(capsys.readouterr().out) |
| 389 | assert create_exit == 0 |
| 390 | assert create_payload["files"] == 15 |
| 391 | |
| 392 | verify_exit = object_backup.main(["verify", str(backup), "--json"]) |
| 393 | verify_payload = json.loads(capsys.readouterr().out) |
| 394 | assert verify_exit == 0 |
| 395 | assert verify_payload["ok"] is True |
| 396 | |
| 397 | restore_exit = object_backup.main( |
| 398 | [ |
| 399 | "restore", |
| 400 | str(backup), |
| 401 | "--objects-dir", |
| 402 | str(tmp_path / "restored" / "objects"), |
| 403 | "--data-dir", |
| 404 | str(tmp_path / "restored" / "data"), |
| 405 | "--json", |
| 406 | ] |
| 407 | ) |
| 408 | restore_payload = json.loads(capsys.readouterr().out) |
| 409 | assert restore_exit == 0 |
| 410 | assert restore_payload["files"] == 15 |
| 411 | |
| 412 | |
| 413 | def test_read_backup_member_returns_bytes_or_none(tmp_path): |
| 414 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 415 | backup = tmp_path / "runtime.tar.gz" |
| 416 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 417 | |
| 418 | payload = object_backup.read_backup_member(backup, "data/collections/contacts/records.tsv") |
| 419 | assert payload == b"id\tname\nc1\tAda\n" |
| 420 | |
| 421 | assert object_backup.read_backup_member(backup, "data/collections/missing/records.tsv") is None |
| 422 | |
| 423 | |
| 424 | def test_restore_runtime_backup_scoped_to_one_collection(tmp_path): |
| 425 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 426 | write_file(data_dir / "collections" / "notes" / "records.tsv", "id\ttext\nn1\thello\n") |
| 427 | backup = tmp_path / "runtime.tar.gz" |
| 428 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 429 | |
| 430 | restored_objects = tmp_path / "restored" / "objects" |
| 431 | restored_data = tmp_path / "restored" / "data" |
| 432 | summary = object_backup.restore_runtime_backup( |
| 433 | backup, |
| 434 | objects_dir=restored_objects, |
| 435 | data_dir=restored_data, |
| 436 | select=["data/collections/notes"], |
| 437 | ) |
| 438 | |
| 439 | assert summary.scoped is True |
| 440 | assert summary.selected == 2 # the "notes" directory member plus its records.tsv |
| 441 | assert ( |
| 442 | restored_data / "collections" / "notes" / "records.tsv" |
| 443 | ).read_text() == "id\ttext\nn1\thello\n" |
| 444 | assert not (restored_data / "collections" / "contacts").exists() |
| 445 | assert not (restored_data / "state").exists() |
| 446 | assert not restored_objects.exists() |
| 447 | |
| 448 | |
| 449 | def test_restore_runtime_backup_unscoped_summary_defaults(tmp_path): |
| 450 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 451 | backup = tmp_path / "runtime.tar.gz" |
| 452 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 453 | |
| 454 | summary = object_backup.restore_runtime_backup( |
| 455 | backup, |
| 456 | objects_dir=tmp_path / "restored" / "objects", |
| 457 | data_dir=tmp_path / "restored" / "data", |
| 458 | ) |
| 459 | |
| 460 | assert summary.scoped is False |
| 461 | assert summary.selected == 0 |
| 462 | |
| 463 | |
| 464 | def test_restore_runtime_backup_select_and_prune_extra_raises(tmp_path): |
| 465 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 466 | backup = tmp_path / "runtime.tar.gz" |
| 467 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 468 | |
| 469 | with pytest.raises( |
| 470 | object_backup.BackupRestoreError, match="select cannot be combined with prune_extra" |
| 471 | ): |
| 472 | object_backup.restore_runtime_backup( |
| 473 | backup, |
| 474 | objects_dir=tmp_path / "restored" / "objects", |
| 475 | data_dir=tmp_path / "restored" / "data", |
| 476 | overwrite=True, |
| 477 | prune_extra=True, |
| 478 | select=["data/collections/contacts"], |
| 479 | ) |
| 480 | |
| 481 | |
| 482 | def test_restore_runtime_backup_select_no_match_raises(tmp_path): |
| 483 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 484 | backup = tmp_path / "runtime.tar.gz" |
| 485 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 486 | |
| 487 | with pytest.raises( |
| 488 | object_backup.BackupRestoreError, match="no files in the backup match the selection" |
| 489 | ): |
| 490 | object_backup.restore_runtime_backup( |
| 491 | backup, |
| 492 | objects_dir=tmp_path / "restored" / "objects", |
| 493 | data_dir=tmp_path / "restored" / "data", |
| 494 | select=["data/collections/does-not-exist"], |
| 495 | ) |
| 496 | |
| 497 | |
| 498 | def test_restore_runtime_backup_select_rejects_bad_root(tmp_path): |
| 499 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 500 | backup = tmp_path / "runtime.tar.gz" |
| 501 | object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir) |
| 502 | |
| 503 | with pytest.raises(object_backup.BackupRestoreError, match="invalid restore selector"): |
| 504 | object_backup.restore_runtime_backup( |
| 505 | backup, |
| 506 | objects_dir=tmp_path / "restored" / "objects", |
| 507 | data_dir=tmp_path / "restored" / "data", |
| 508 | select=["etc/passwd"], |
| 509 | ) |
| 510 | |
| 511 | |
| 512 | def _add_manifest(archive: tarfile.TarFile, *, files: int, bytes_count: int): |
| 513 | manifest = { |
| 514 | "format_version": object_backup.BACKUP_FORMAT_VERSION, |
| 515 | "created_at": "2026-01-01T00:00:00Z", |
| 516 | "type": "dbbasic-runtime", |
| 517 | "entries": [], |
| 518 | "files": files, |
| 519 | "bytes": bytes_count, |
| 520 | "warnings": [], |
| 521 | } |
| 522 | payload = json.dumps(manifest).encode("utf-8") |
| 523 | info = tarfile.TarInfo(object_backup.MANIFEST_NAME) |
| 524 | info.size = len(payload) |
| 525 | archive.addfile(info, io.BytesIO(payload)) |
| 526 | |
| 527 | |
| 528 | def test_restore_does_not_follow_a_symlink_planted_at_its_temp_path(tmp_path): |
| 529 | """The restore writes each file through `.<name>.restore.tmp` beside it. A |
| 530 | symlink planted there used to be followed, writing the archive's bytes to |
| 531 | wherever it pointed -- outside the runtime roots.""" |
| 532 | objects_dir, data_dir = make_runtime_tree(tmp_path) |
| 533 | backup_path = tmp_path / "backup.tar.gz" |
| 534 | object_backup.create_runtime_backup(backup_path, objects_dir=objects_dir, data_dir=data_dir) |
| 535 | |
| 536 | outside = tmp_path / "outside.txt" |
| 537 | outside.write_text("untouched") |
| 538 | planted = objects_dir / "site" / ".home.py.restore.tmp" |
| 539 | planted.symlink_to(outside) |
| 540 | |
| 541 | object_backup.restore_runtime_backup( |
| 542 | backup_path, objects_dir=objects_dir, data_dir=data_dir, overwrite=True, |
| 543 | ) |
| 544 | |
| 545 | assert outside.read_text() == "untouched" |
| 546 | assert not planted.exists() and not planted.is_symlink() |
| 547 | assert "status" in (objects_dir / "site" / "home.py").read_text() |