Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_backup.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_backup.py · 18.1 KB · 476 lines
1import gzip
2import io
3import json
4import tarfile
5from pathlib import Path
6
7import pytest
8
9import object_backup
10import object_logs
11import object_state
12
13
14def 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
23def 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
85def archive_names(path: Path) -> list[str]:
86 with tarfile.open(path, "r:*") as archive:
87 return archive.getnames()
88
89
90def 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
97def 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
157def 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
179def 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
188def 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
219def 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
240def 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
268def test_restore_runtime_backup_prune_requires_overwrite(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 with pytest.raises(object_backup.BackupRestoreError, match="prune_extra requires overwrite"):
274 object_backup.restore_runtime_backup(
275 backup,
276 objects_dir=tmp_path / "restored" / "objects",
277 data_dir=tmp_path / "restored" / "data",
278 prune_extra=True,
279 )
280
281
282def test_verify_and_restore_reject_path_traversal_member(tmp_path):
283 backup = tmp_path / "bad.tar.gz"
284 with tarfile.open(backup, "w:gz") as archive:
285 _add_manifest(archive, files=1, bytes_count=4)
286 info = tarfile.TarInfo("../outside.py")
287 payload = b"evil"
288 info.size = len(payload)
289 archive.addfile(info, io.BytesIO(payload))
290
291 verification = object_backup.verify_runtime_backup(backup)
292 assert not verification.ok
293 assert any("unsafe archive member path" in error for error in verification.errors)
294
295 with pytest.raises(object_backup.BackupRestoreError, match="unsafe archive member path"):
296 object_backup.restore_runtime_backup(
297 backup,
298 objects_dir=tmp_path / "objects",
299 data_dir=tmp_path / "data",
300 )
301
302
303def test_verify_and_restore_reject_link_members(tmp_path):
304 backup = tmp_path / "link.tar.gz"
305 with tarfile.open(backup, "w:gz") as archive:
306 _add_manifest(archive, files=0, bytes_count=0)
307 info = tarfile.TarInfo("objects/link.py")
308 info.type = tarfile.SYMTYPE
309 info.linkname = "/etc/passwd"
310 archive.addfile(info)
311
312 verification = object_backup.verify_runtime_backup(backup)
313 assert not verification.ok
314 assert any("uses links" in error for error in verification.errors)
315
316 with pytest.raises(object_backup.BackupRestoreError, match="uses links"):
317 object_backup.restore_runtime_backup(
318 backup,
319 objects_dir=tmp_path / "objects",
320 data_dir=tmp_path / "data",
321 )
322
323
324def test_cli_create_verify_and_restore_json(tmp_path, capsys):
325 objects_dir, data_dir = make_runtime_tree(tmp_path)
326 backup = tmp_path / "runtime.tar.gz"
327
328 create_exit = object_backup.main(
329 [
330 "create",
331 str(backup),
332 "--objects-dir",
333 str(objects_dir),
334 "--data-dir",
335 str(data_dir),
336 "--json",
337 ]
338 )
339 create_payload = json.loads(capsys.readouterr().out)
340 assert create_exit == 0
341 assert create_payload["files"] == 15
342
343 verify_exit = object_backup.main(["verify", str(backup), "--json"])
344 verify_payload = json.loads(capsys.readouterr().out)
345 assert verify_exit == 0
346 assert verify_payload["ok"] is True
347
348 restore_exit = object_backup.main(
349 [
350 "restore",
351 str(backup),
352 "--objects-dir",
353 str(tmp_path / "restored" / "objects"),
354 "--data-dir",
355 str(tmp_path / "restored" / "data"),
356 "--json",
357 ]
358 )
359 restore_payload = json.loads(capsys.readouterr().out)
360 assert restore_exit == 0
361 assert restore_payload["files"] == 15
362
363
364def test_read_backup_member_returns_bytes_or_none(tmp_path):
365 objects_dir, data_dir = make_runtime_tree(tmp_path)
366 backup = tmp_path / "runtime.tar.gz"
367 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
368
369 payload = object_backup.read_backup_member(backup, "data/collections/contacts/records.tsv")
370 assert payload == b"id\tname\nc1\tAda\n"
371
372 assert object_backup.read_backup_member(backup, "data/collections/missing/records.tsv") is None
373
374
375def test_restore_runtime_backup_scoped_to_one_collection(tmp_path):
376 objects_dir, data_dir = make_runtime_tree(tmp_path)
377 write_file(data_dir / "collections" / "notes" / "records.tsv", "id\ttext\nn1\thello\n")
378 backup = tmp_path / "runtime.tar.gz"
379 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
380
381 restored_objects = tmp_path / "restored" / "objects"
382 restored_data = tmp_path / "restored" / "data"
383 summary = object_backup.restore_runtime_backup(
384 backup,
385 objects_dir=restored_objects,
386 data_dir=restored_data,
387 select=["data/collections/notes"],
388 )
389
390 assert summary.scoped is True
391 assert summary.selected == 2 # the "notes" directory member plus its records.tsv
392 assert (
393 restored_data / "collections" / "notes" / "records.tsv"
394 ).read_text() == "id\ttext\nn1\thello\n"
395 assert not (restored_data / "collections" / "contacts").exists()
396 assert not (restored_data / "state").exists()
397 assert not restored_objects.exists()
398
399
400def test_restore_runtime_backup_unscoped_summary_defaults(tmp_path):
401 objects_dir, data_dir = make_runtime_tree(tmp_path)
402 backup = tmp_path / "runtime.tar.gz"
403 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
404
405 summary = object_backup.restore_runtime_backup(
406 backup,
407 objects_dir=tmp_path / "restored" / "objects",
408 data_dir=tmp_path / "restored" / "data",
409 )
410
411 assert summary.scoped is False
412 assert summary.selected == 0
413
414
415def test_restore_runtime_backup_select_and_prune_extra_raises(tmp_path):
416 objects_dir, data_dir = make_runtime_tree(tmp_path)
417 backup = tmp_path / "runtime.tar.gz"
418 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
419
420 with pytest.raises(
421 object_backup.BackupRestoreError, match="select cannot be combined with prune_extra"
422 ):
423 object_backup.restore_runtime_backup(
424 backup,
425 objects_dir=tmp_path / "restored" / "objects",
426 data_dir=tmp_path / "restored" / "data",
427 overwrite=True,
428 prune_extra=True,
429 select=["data/collections/contacts"],
430 )
431
432
433def test_restore_runtime_backup_select_no_match_raises(tmp_path):
434 objects_dir, data_dir = make_runtime_tree(tmp_path)
435 backup = tmp_path / "runtime.tar.gz"
436 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
437
438 with pytest.raises(
439 object_backup.BackupRestoreError, match="no files in the backup match the selection"
440 ):
441 object_backup.restore_runtime_backup(
442 backup,
443 objects_dir=tmp_path / "restored" / "objects",
444 data_dir=tmp_path / "restored" / "data",
445 select=["data/collections/does-not-exist"],
446 )
447
448
449def test_restore_runtime_backup_select_rejects_bad_root(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 with pytest.raises(object_backup.BackupRestoreError, match="invalid restore selector"):
455 object_backup.restore_runtime_backup(
456 backup,
457 objects_dir=tmp_path / "restored" / "objects",
458 data_dir=tmp_path / "restored" / "data",
459 select=["etc/passwd"],
460 )
461
462
463def _add_manifest(archive: tarfile.TarFile, *, files: int, bytes_count: int):
464 manifest = {
465 "format_version": object_backup.BACKUP_FORMAT_VERSION,
466 "created_at": "2026-01-01T00:00:00Z",
467 "type": "dbbasic-runtime",
468 "entries": [],
469 "files": files,
470 "bytes": bytes_count,
471 "warnings": [],
472 }
473 payload = json.dumps(manifest).encode("utf-8")
474 info = tarfile.TarInfo(object_backup.MANIFEST_NAME)
475 info.size = len(payload)
476 archive.addfile(info, io.BytesIO(payload))