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

9 downloads

dbbasic-object-server/tests/test_backup_schedule_http.py
Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_backup_schedule_http.py · 5.3 KB · 125 lines
1"""Scheduled backups on the admin surface: status, alerts, and the triggers.
2
3A backup or restore test that fails must never look healthy, so the
4failure has to reach the places an admin actually looks -- /admin/status
5`alerts` first of all -- and it must reach nobody else.
6"""
7from __future__ import annotations
8
9import json
10
11import pytest
12from test_object_server import auth_headers, enable_admin_token, request
13
14import object_backup_schedule as sched
15import object_records
16import object_server
17
18
19@pytest.fixture
20def 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
39def _join():
40 thread = sched._BACKGROUND.get("run")
41 if thread is not None:
42 thread.join(timeout=120)
43
44
45def 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
57def 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
81def 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
94def 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
101def 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
111def 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()