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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_package_schedules.py · 12.6 KB · 295 lines
| 1 | """Schedules ship with the app that needs them. |
| 2 | |
| 3 | Before this, every recurring pass on a running server had been typed into |
| 4 | the scheduler's state by hand and appeared nowhere in the repository: a |
| 5 | rebuilt box came back with no nightly work and said nothing about it. The |
| 6 | properties worth holding are therefore about SURVIVING an install, not |
| 7 | just performing one -- run history is kept, a pause is honoured, and a |
| 8 | schedule pointing at nothing is refused rather than written. |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | import pathlib |
| 13 | |
| 14 | import pytest |
| 15 | |
| 16 | import object_packages |
| 17 | import object_state |
| 18 | |
| 19 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 20 | PACKAGES = REPO_ROOT / "packages" |
| 21 | |
| 22 | MANIFEST = { |
| 23 | "id": "app-demo", |
| 24 | "name": "Demo", |
| 25 | "version": "1.0.0", |
| 26 | "objects": [{"id": "system_nightly", "path": "objects/system/nightly.py"}], |
| 27 | "schedules": [{"id": "nightly_pass", "object_id": "system_nightly", |
| 28 | "schedule": "10 6 * * *", |
| 29 | "description": "The nightly pass."}], |
| 30 | } |
| 31 | |
| 32 | SOURCE = "def POST(request):\n return {'ok': True}\n" |
| 33 | |
| 34 | |
| 35 | @pytest.fixture |
| 36 | def package(tmp_path): |
| 37 | """A one-object, one-schedule package plus empty install roots.""" |
| 38 | root = tmp_path / "packages" |
| 39 | pkg = root / "app-demo" |
| 40 | (pkg / "objects" / "system").mkdir(parents=True) |
| 41 | (pkg / "objects" / "system" / "nightly.py").write_text(SOURCE) |
| 42 | (pkg / "dbbasic-package.json").write_text(json.dumps(MANIFEST)) |
| 43 | objects = tmp_path / "objects" |
| 44 | objects.mkdir() |
| 45 | data = tmp_path / "data" |
| 46 | data.mkdir() |
| 47 | return {"root": root, "dir": pkg, "objects": objects, "data": data} |
| 48 | |
| 49 | |
| 50 | def write_manifest(package, **overrides): |
| 51 | manifest = {**MANIFEST, **overrides} |
| 52 | (package["dir"] / "dbbasic-package.json").write_text(json.dumps(manifest)) |
| 53 | |
| 54 | |
| 55 | def install(package, **kwargs): |
| 56 | return object_packages.install_package( |
| 57 | "app-demo", root=package["root"], base_dir=package["data"], |
| 58 | object_roots=[package["objects"]], **kwargs) |
| 59 | |
| 60 | |
| 61 | def tasks(package): |
| 62 | state = object_state.get_object_state("scheduler", base_dir=package["data"]) |
| 63 | return {k[len("task_"):]: json.loads(v) |
| 64 | for k, v in state.items() if k.startswith("task_")} |
| 65 | |
| 66 | |
| 67 | def set_task(package, schedule_id, task): |
| 68 | manager = object_state.ObjectStateManager("scheduler", base_dir=package["data"]) |
| 69 | manager.set(f"task_{schedule_id}", json.dumps(task)) |
| 70 | |
| 71 | |
| 72 | # --- installing ------------------------------------------------------------ |
| 73 | |
| 74 | def test_installing_a_package_puts_its_schedule_on_the_board(package): |
| 75 | result = install(package) |
| 76 | assert [s["status"] for s in result["schedules"]] == ["written"] |
| 77 | |
| 78 | task = tasks(package)["nightly_pass"] |
| 79 | assert task["object_id"] == "system_nightly" |
| 80 | assert task["schedule"] == "10 6 * * *" |
| 81 | assert task["method"] == "POST" |
| 82 | assert task["status"] == "active" |
| 83 | # next_run is the daemon's to compute; the package does not guess it. |
| 84 | assert "next_run" not in task |
| 85 | |
| 86 | |
| 87 | def test_the_plan_says_what_it_would_schedule_before_it_does(package): |
| 88 | plan = object_packages.dry_run_package( |
| 89 | "app-demo", root=package["root"], base_dir=package["data"], |
| 90 | object_roots=[package["objects"]]) |
| 91 | assert plan["schedules"] == [{"id": "nightly_pass", "object_id": "system_nightly", |
| 92 | "schedule": "10 6 * * *", "type": "cron", |
| 93 | "exists": False, "action": "create"}] |
| 94 | assert plan["package"]["schedule_count"] == 1 |
| 95 | assert tasks(package) == {} # a dry run writes nothing |
| 96 | |
| 97 | |
| 98 | def test_a_package_with_no_schedules_touches_no_state(package): |
| 99 | write_manifest(package, schedules=[]) |
| 100 | result = install(package) |
| 101 | assert result["schedules"] == [] |
| 102 | assert not (package["data"] / "state" / "scheduler").exists() |
| 103 | |
| 104 | |
| 105 | # --- surviving an upgrade ---------------------------------------------------- |
| 106 | |
| 107 | def test_reinstalling_keeps_the_run_history(package): |
| 108 | """An upgrade that forgets when a pass last worked makes 'is this |
| 109 | running?' unanswerable.""" |
| 110 | install(package) |
| 111 | set_task(package, "nightly_pass", {**tasks(package)["nightly_pass"], |
| 112 | "last_run": 1784959800, "run_count": 12, |
| 113 | "next_run": 1785046200}) |
| 114 | install(package, allow_replace=True) |
| 115 | |
| 116 | task = tasks(package)["nightly_pass"] |
| 117 | assert task["run_count"] == 12 and task["last_run"] == 1784959800 |
| 118 | assert task["next_run"] == 1785046200 # unchanged cron keeps tonight's firing |
| 119 | |
| 120 | |
| 121 | def test_a_paused_task_stays_paused(package): |
| 122 | """The package declares what SHOULD run; an operator decides what does |
| 123 | right now. A reinstall that restarts a deliberately-stopped pass is how |
| 124 | an upgrade becomes an incident.""" |
| 125 | install(package) |
| 126 | set_task(package, "nightly_pass", {**tasks(package)["nightly_pass"], |
| 127 | "status": "paused"}) |
| 128 | result = install(package, allow_replace=True) |
| 129 | |
| 130 | assert tasks(package)["nightly_pass"]["status"] == "paused" |
| 131 | assert result["schedules"][0]["task_status"] == "paused" |
| 132 | |
| 133 | |
| 134 | def test_changing_the_cron_reschedules_instead_of_leaving_a_stale_firing(package): |
| 135 | install(package) |
| 136 | set_task(package, "nightly_pass", {**tasks(package)["nightly_pass"], |
| 137 | "next_run": 1785046200, "run_count": 3}) |
| 138 | write_manifest(package, version="1.1.0", |
| 139 | schedules=[{**MANIFEST["schedules"][0], "schedule": "30 2 * * *"}]) |
| 140 | result = install(package, allow_replace=True) |
| 141 | |
| 142 | task = tasks(package)["nightly_pass"] |
| 143 | assert task["schedule"] == "30 2 * * *" |
| 144 | assert task["next_run"] is None # the daemon recomputes it |
| 145 | assert task["run_count"] == 3 # but history is not reset |
| 146 | assert result["schedules"][0]["action"] == "update" |
| 147 | |
| 148 | |
| 149 | def test_an_unchanged_schedule_reports_unchanged(package): |
| 150 | install(package) |
| 151 | result = install(package, allow_replace=True) |
| 152 | assert result["schedules"][0]["status"] == "unchanged" |
| 153 | assert result["schedules"][0]["action"] == "unchanged" |
| 154 | |
| 155 | |
| 156 | def test_a_plan_that_says_unchanged_writes_nothing_at_all(package): |
| 157 | """Including the description in the comparison: a dry run that |
| 158 | under-reports is worse than no dry run.""" |
| 159 | install(package) |
| 160 | write_manifest(package, schedules=[{**MANIFEST["schedules"][0], |
| 161 | "description": "Reworded."}]) |
| 162 | plan = object_packages.dry_run_package( |
| 163 | "app-demo", root=package["root"], base_dir=package["data"], |
| 164 | object_roots=[package["objects"]]) |
| 165 | assert plan["schedules"][0]["action"] == "update" |
| 166 | |
| 167 | result = install(package, allow_replace=True) |
| 168 | assert result["schedules"][0]["status"] == "updated" |
| 169 | assert tasks(package)["nightly_pass"]["description"] == "Reworded." |
| 170 | |
| 171 | |
| 172 | def test_a_hand_made_task_is_adopted_not_duplicated(package): |
| 173 | """Every schedule on the demo box was hand-entered before packages |
| 174 | could declare them; installing must take ownership of that row rather |
| 175 | than run the same pass twice.""" |
| 176 | set_task(package, "nightly_pass", |
| 177 | {"id": "nightly_pass", "object_id": "system_nightly", "method": "POST", |
| 178 | "payload": {}, "schedule": "10 6 * * *", "type": "cron", |
| 179 | "status": "active", "run_count": 40}) |
| 180 | install(package) |
| 181 | assert len(tasks(package)) == 1 |
| 182 | assert tasks(package)["nightly_pass"]["run_count"] == 40 |
| 183 | |
| 184 | |
| 185 | # --- refusing what could not run ---------------------------------------------- |
| 186 | |
| 187 | def test_a_schedule_aimed_at_a_missing_object_blocks_the_install(package): |
| 188 | """The silent failure this whole feature exists to end: a task that |
| 189 | fires nightly into nothing.""" |
| 190 | write_manifest(package, schedules=[{"id": "nightly_pass", |
| 191 | "object_id": "system_absent", |
| 192 | "schedule": "10 6 * * *"}]) |
| 193 | with pytest.raises(object_packages.PackageInstallError) as exc: |
| 194 | install(package) |
| 195 | assert "system_absent" in str(exc.value) |
| 196 | assert tasks(package) == {} |
| 197 | |
| 198 | |
| 199 | def test_a_schedule_may_target_an_object_already_on_the_server(package): |
| 200 | (package["objects"] / "system").mkdir() |
| 201 | (package["objects"] / "system" / "other.py").write_text(SOURCE) |
| 202 | write_manifest(package, schedules=[{"id": "nightly_pass", |
| 203 | "object_id": "system_other", |
| 204 | "schedule": "10 6 * * *"}]) |
| 205 | assert install(package)["schedules"][0]["status"] == "written" |
| 206 | |
| 207 | |
| 208 | @pytest.mark.parametrize("bad, reason", [ |
| 209 | ({"id": "nightly pass", "object_id": "system_nightly", "schedule": "10 6 * * *"}, |
| 210 | "schedule id"), |
| 211 | ({"id": "nightly_pass", "object_id": "system_nightly", "schedule": "not a cron"}, |
| 212 | "cron"), |
| 213 | ({"id": "nightly_pass", "object_id": "system_nightly", "schedule": "10 6 * * *", |
| 214 | "type": "hourly"}, "cron|onetime"), |
| 215 | ({"id": "nightly_pass", "object_id": "system_nightly", "schedule": "10 6 * * *", |
| 216 | "method": "TRACE"}, "method"), |
| 217 | ({"id": "nightly_pass", "object_id": "system_nightly", "schedule": "10 6 * * *", |
| 218 | "payload": "everything"}, "payload"), |
| 219 | ]) |
| 220 | def test_a_schedule_that_could_never_fire_is_refused_at_install(package, bad, reason): |
| 221 | """The daemon treats an unparseable expression as 'no next run' and |
| 222 | moves on in silence, so this has to be caught here or not at all.""" |
| 223 | write_manifest(package, schedules=[bad]) |
| 224 | with pytest.raises(object_packages.InvalidPackageManifestError) as exc: |
| 225 | install(package) |
| 226 | assert reason in str(exc.value) |
| 227 | |
| 228 | |
| 229 | def test_two_schedules_cannot_share_an_id(package): |
| 230 | write_manifest(package, schedules=[MANIFEST["schedules"][0], |
| 231 | {**MANIFEST["schedules"][0], |
| 232 | "schedule": "0 3 * * *"}]) |
| 233 | with pytest.raises(object_packages.InvalidPackageManifestError): |
| 234 | install(package) |
| 235 | |
| 236 | |
| 237 | def test_a_onetime_schedule_takes_a_timestamp(package): |
| 238 | write_manifest(package, schedules=[{"id": "one_off", "object_id": "system_nightly", |
| 239 | "type": "onetime", |
| 240 | "schedule": "2026-08-01T06:00:00Z"}]) |
| 241 | assert install(package)["schedules"][0]["status"] == "written" |
| 242 | write_manifest(package, schedules=[{"id": "one_off", "object_id": "system_nightly", |
| 243 | "type": "onetime", "schedule": "soon"}]) |
| 244 | with pytest.raises(object_packages.InvalidPackageManifestError): |
| 245 | install(package) |
| 246 | |
| 247 | |
| 248 | # --- the real packages ---------------------------------------------------------- |
| 249 | |
| 250 | def test_every_shipped_schedule_names_an_object_its_package_installs(): |
| 251 | """A schedule and the object it calls must travel together, or a |
| 252 | rebuilt box gets one without the other.""" |
| 253 | found = 0 |
| 254 | for manifest_path in sorted(PACKAGES.glob("*/dbbasic-package.json")): |
| 255 | manifest = json.loads(manifest_path.read_text()) |
| 256 | schedules = manifest.get("schedules") or [] |
| 257 | if not schedules: |
| 258 | continue |
| 259 | provided = {entry["id"] for entry in manifest.get("objects", [])} |
| 260 | for schedule in schedules: |
| 261 | assert schedule["object_id"] in provided, ( |
| 262 | f"{manifest_path.parent.name}: {schedule['id']} targets " |
| 263 | f"{schedule['object_id']}, which it does not install") |
| 264 | found += 1 |
| 265 | assert found >= 7, "the demo box's recurring passes should all be declared" |
| 266 | |
| 267 | |
| 268 | def test_shipped_schedules_do_not_collide_on_an_id_or_a_minute(): |
| 269 | """Two packages writing the same task_ key would silently overwrite |
| 270 | each other; two heavy passes on the same minute would merely be rude.""" |
| 271 | seen_ids: dict[str, str] = {} |
| 272 | seen_slots: dict[str, str] = {} |
| 273 | for manifest_path in sorted(PACKAGES.glob("*/dbbasic-package.json")): |
| 274 | manifest = json.loads(manifest_path.read_text()) |
| 275 | for schedule in manifest.get("schedules") or []: |
| 276 | package_id = manifest["id"] |
| 277 | assert schedule["id"] not in seen_ids, ( |
| 278 | f"{schedule['id']} declared by both {seen_ids.get(schedule['id'])} " |
| 279 | f"and {package_id}") |
| 280 | seen_ids[schedule["id"]] = package_id |
| 281 | if schedule.get("type", "cron") != "cron": |
| 282 | continue |
| 283 | assert schedule["schedule"] not in seen_slots, ( |
| 284 | f"{schedule['schedule']} used by both " |
| 285 | f"{seen_slots[schedule['schedule']]} and {schedule['id']}") |
| 286 | seen_slots[schedule["schedule"]] = schedule["id"] |
| 287 | |
| 288 | |
| 289 | def test_billing_folds_usage_before_it_rates_the_period(): |
| 290 | """Ordering is load-bearing: a period closing today must be rated |
| 291 | against usage that has already been folded into summaries.""" |
| 292 | manifest = json.loads((PACKAGES / "app-billing" / "dbbasic-package.json").read_text()) |
| 293 | minute_of = {s["object_id"]: (lambda p: int(p[1]) * 60 + int(p[0]))( |
| 294 | s["schedule"].split()) for s in manifest["schedules"]} |
| 295 | assert minute_of["system_usage_rollup"] < minute_of["system_billing_runner"] |