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_backup_schedule_daemon.py · 4.1 KB · 97 lines
| 1 | """The daemon runs scheduled backups: off by default, on a thread, loudly. |
| 2 | |
| 3 | A backup nobody starts is the failure this exists to end, and a backup |
| 4 | that stops the scheduler while it copies a large box would trade one |
| 5 | silent failure for another -- so the pass starts a thread and returns. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import json |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | import object_backup_schedule as sched |
| 14 | import object_daemon |
| 15 | import object_records |
| 16 | |
| 17 | |
| 18 | @pytest.fixture |
| 19 | def box(tmp_path, monkeypatch): |
| 20 | objects_dir = tmp_path / "objects" |
| 21 | data_dir = tmp_path / "data" |
| 22 | dest = tmp_path / "offsite" |
| 23 | dest.mkdir() |
| 24 | (objects_dir / "site").mkdir(parents=True) |
| 25 | (objects_dir / "site" / "home.py").write_text("def GET(request):\n return {}\n") |
| 26 | (data_dir / "schemas").mkdir(parents=True) |
| 27 | (data_dir / "schemas" / "notes.json").write_text(json.dumps( |
| 28 | {"name": "notes", "fields": [{"name": "id", "type": "string"}, |
| 29 | {"name": "title", "type": "string"}]})) |
| 30 | object_records.create_collection_record("notes", {"id": "n1", "title": "a"}, |
| 31 | base_dir=data_dir) |
| 32 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_dir)) |
| 33 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 34 | return {"objects": objects_dir, "data": data_dir, "dest": dest} |
| 35 | |
| 36 | |
| 37 | def test_the_pass_does_nothing_when_no_destination_is_set(box, monkeypatch): |
| 38 | monkeypatch.delenv(sched.DESTINATION_ENV, raising=False) |
| 39 | assert object_daemon.process_scheduled_backup(base_dir=box["data"]) is None |
| 40 | assert not sched.status_path(box["data"]).exists() |
| 41 | |
| 42 | |
| 43 | def test_the_pass_runs_a_backup_on_a_thread_and_logs_the_outcome(box, monkeypatch, capsys): |
| 44 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 45 | thread = object_daemon.process_scheduled_backup(base_dir=box["data"], |
| 46 | objects_dir=box["objects"]) |
| 47 | assert thread is not None |
| 48 | thread.join(timeout=120) |
| 49 | out = capsys.readouterr().out |
| 50 | assert "[INFO] Backup: dbbasic-snapshot-" in out and "restore test ok" in out |
| 51 | assert len(sched.list_snapshots(box["dest"])) == 1 |
| 52 | # Not due again until the interval has passed. |
| 53 | assert object_daemon.process_scheduled_backup(base_dir=box["data"]) is None |
| 54 | |
| 55 | |
| 56 | def test_a_failed_backup_is_an_error_line(box, monkeypatch, capsys): |
| 57 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["data"] / "inside")) |
| 58 | (box["data"] / "inside").mkdir() |
| 59 | thread = object_daemon.process_scheduled_backup(base_dir=box["data"], |
| 60 | objects_dir=box["objects"]) |
| 61 | thread.join(timeout=60) |
| 62 | out = capsys.readouterr().out |
| 63 | assert "[ERROR] Backup: FAILED:" in out and "overlaps the data directory" in out |
| 64 | |
| 65 | |
| 66 | def test_a_failed_restore_test_is_an_error_line_not_an_info_line(capsys): |
| 67 | object_daemon._log_backup_outcome("backup", { |
| 68 | "ok": True, "name": "dbbasic-snapshot-x", "files": 3, "bytes": 10, "linked": 0, |
| 69 | "copied": 3, "copied_bytes": 10, "total_seconds": 1, |
| 70 | "restore_test": {"ok": False, "errors": ["content does not match the manifest: x"]}}) |
| 71 | out = capsys.readouterr().out |
| 72 | assert "[ERROR]" in out and "restore test FAILED" in out |
| 73 | assert "[INFO]" not in out |
| 74 | |
| 75 | |
| 76 | def test_the_daemon_loop_calls_the_backup_pass(): |
| 77 | """Wired into main(), not just defined: a pass the loop never calls |
| 78 | is the silent failure this whole file is about.""" |
| 79 | import inspect |
| 80 | |
| 81 | assert "process_scheduled_backup(base_dir=base_dir)" in inspect.getsource(object_daemon.main) |
| 82 | assert "Backups:" in inspect.getsource(object_daemon.main) |
| 83 | |
| 84 | |
| 85 | def test_status_file_records_a_run_in_progress_and_clears_it(box, monkeypatch): |
| 86 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 87 | seen = [] |
| 88 | real = sched.create_snapshot |
| 89 | |
| 90 | def spying(*args, **kwargs): |
| 91 | seen.append(json.loads(sched.status_path(box["data"]).read_text()).get("running")) |
| 92 | return real(*args, **kwargs) |
| 93 | |
| 94 | monkeypatch.setattr(sched, "create_snapshot", spying) |
| 95 | sched.run_backup(box["data"], objects_dir=box["objects"]) |
| 96 | assert seen and seen[0]["kind"] == "backup" |
| 97 | assert sched.read_status(box["data"]).get("running") is None |