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_http.py · 5.3 KB · 125 lines
| 1 | """Scheduled backups on the admin surface: status, alerts, and the triggers. |
| 2 | |
| 3 | A backup or restore test that fails must never look healthy, so the |
| 4 | failure has to reach the places an admin actually looks -- /admin/status |
| 5 | `alerts` first of all -- and it must reach nobody else. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import json |
| 10 | |
| 11 | import pytest |
| 12 | from test_object_server import auth_headers, enable_admin_token, request |
| 13 | |
| 14 | import object_backup_schedule as sched |
| 15 | import object_records |
| 16 | import object_server |
| 17 | |
| 18 | |
| 19 | @pytest.fixture |
| 20 | def box(tmp_path, monkeypatch): |
| 21 | objects_dir = tmp_path / "objects" |
| 22 | data_dir = tmp_path / "data" |
| 23 | dest = tmp_path / "offsite" |
| 24 | dest.mkdir() |
| 25 | (objects_dir / "site").mkdir(parents=True) |
| 26 | (objects_dir / "site" / "home.py").write_text("def GET(request):\n return {}\n") |
| 27 | (data_dir / "schemas").mkdir(parents=True) |
| 28 | (data_dir / "schemas" / "notes.json").write_text(json.dumps( |
| 29 | {"name": "notes", "fields": [{"name": "id", "type": "string"}, |
| 30 | {"name": "title", "type": "string"}]})) |
| 31 | object_records.create_collection_record("notes", {"id": "n1", "title": "a"}, |
| 32 | base_dir=data_dir) |
| 33 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data_dir)) |
| 34 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_dir)) |
| 35 | enable_admin_token(monkeypatch) |
| 36 | return {"objects": objects_dir, "data": data_dir, "dest": dest} |
| 37 | |
| 38 | |
| 39 | def _join(): |
| 40 | thread = sched._BACKGROUND.get("run") |
| 41 | if thread is not None: |
| 42 | thread.join(timeout=120) |
| 43 | |
| 44 | |
| 45 | def test_backups_off_raise_no_alert_and_say_so(box, monkeypatch): |
| 46 | monkeypatch.delenv(sched.DESTINATION_ENV, raising=False) |
| 47 | status, _, payload = request("/admin/status", headers=auth_headers()) |
| 48 | assert status == 200 |
| 49 | assert payload["alerts"] == [] |
| 50 | assert payload["scheduled_backups"]["enabled"] is False |
| 51 | assert payload["capabilities"]["backups"]["destination_configured"] is False |
| 52 | status, _, payload = request("/admin/backups/scheduled/run", method="POST", |
| 53 | headers=auth_headers()) |
| 54 | assert status == 409 and sched.DESTINATION_ENV in payload["error"] |
| 55 | |
| 56 | |
| 57 | def test_back_up_now_then_status_is_healthy(box, monkeypatch): |
| 58 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 59 | status, _, payload = request("/admin/status", headers=auth_headers()) |
| 60 | assert any("BACKUPS NEED ATTENTION" in alert for alert in payload["alerts"]) |
| 61 | |
| 62 | status, _, payload = request("/admin/backups/scheduled/run", method="POST", |
| 63 | headers=auth_headers()) |
| 64 | assert status == 202 and payload["started"] == "backup" |
| 65 | _join() |
| 66 | status, _, payload = request("/admin/backups/scheduled", headers=auth_headers()) |
| 67 | assert status == 200 |
| 68 | backups = payload["scheduled_backups"] |
| 69 | assert backups["healthy"] is True, backups["problems"] |
| 70 | assert backups["last_good_backup"]["name"].startswith("dbbasic-snapshot-") |
| 71 | assert backups["last_restore_test"]["ok"] is True |
| 72 | status, _, payload = request("/admin/status", headers=auth_headers()) |
| 73 | assert payload["alerts"] == [] |
| 74 | |
| 75 | status, _, payload = request("/admin/backups/scheduled/test", method="POST", |
| 76 | headers=auth_headers()) |
| 77 | assert status == 202 and payload["started"] == "restore_test" |
| 78 | _join() |
| 79 | |
| 80 | |
| 81 | def test_a_failed_restore_test_is_an_admin_alert(box, monkeypatch): |
| 82 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 83 | sched.run_backup(box["data"], objects_dir=box["objects"]) |
| 84 | snapshot = sched.list_snapshots(box["dest"])[0] |
| 85 | target = snapshot / "objects" / "site" / "home.py" |
| 86 | target.write_text("def GET(request):\n return {'tampered': True}\n") |
| 87 | request("/admin/backups/scheduled/test", method="POST", headers=auth_headers()) |
| 88 | _join() |
| 89 | status, _, payload = request("/admin/status", headers=auth_headers()) |
| 90 | assert payload["scheduled_backups"]["healthy"] is False |
| 91 | assert any("restore test" in alert for alert in payload["alerts"]) |
| 92 | |
| 93 | |
| 94 | def test_testing_with_nothing_to_test_is_refused(box, monkeypatch): |
| 95 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 96 | status, _, payload = request("/admin/backups/scheduled/test", method="POST", |
| 97 | headers=auth_headers()) |
| 98 | assert status == 409 and "no snapshot" in payload["error"] |
| 99 | |
| 100 | |
| 101 | def test_the_scheduled_routes_are_admin_only(box, monkeypatch): |
| 102 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 103 | for path, method in (("/admin/backups/scheduled", "GET"), |
| 104 | ("/admin/backups/scheduled/run", "POST"), |
| 105 | ("/admin/backups/scheduled/test", "POST")): |
| 106 | status, _, _ = request(path, method=method) |
| 107 | assert status == 401, path |
| 108 | assert not sched.list_snapshots(box["dest"]) |
| 109 | |
| 110 | |
| 111 | def test_a_second_trigger_while_one_runs_is_refused(box, monkeypatch): |
| 112 | monkeypatch.setenv(sched.DESTINATION_ENV, str(box["dest"])) |
| 113 | import threading |
| 114 | |
| 115 | release = threading.Event() |
| 116 | blocker = threading.Thread(target=release.wait) |
| 117 | blocker.start() |
| 118 | sched._BACKGROUND["run"] = blocker |
| 119 | try: |
| 120 | status, _, payload = request("/admin/backups/scheduled/run", method="POST", |
| 121 | headers=auth_headers()) |
| 122 | assert status == 409 and "already running" in payload["error"] |
| 123 | finally: |
| 124 | release.set() |
| 125 | blocker.join() |