mirror of
https://github.com/Joulenap/joulenap.git
synced 2026-08-11 13:21:43 +02:00
The suite failed roughly one full run in five, differently each time. Three independent causes, not one: 1. The lifespan started `missed-backup-check` and `interrupted-run-alert` as daemon threads that nothing joined, and the missed-run check opens a database session. Every test that builds an app leaked one, so a thread routinely outlived its test and kept using the database while the next test tore the engine down and built its own. They are now joined on shutdown, which is also the right behaviour in production: a restart no longer abandons an alert halfway out. The join is bounded well under Docker's stop grace, so a black-holing notification channel still cannot hold the process open. The job service's queue worker stays unjoined on purpose - it may be mid-backup, and blocking shutdown on a running vzdump is worse. 2. `_ensure_ready()` initialised the database lazily when the session factory was missing, which built a schema at `paths.db_path()` - the real database - from whichever caller got there first. `create_all` reflects once and then issues CREATEs, so two of those at the same time collide with "table X already exists", the error the suite reported. It now raises instead, so a caller that outlived its setup fails loudly rather than racing. One test was relying on that lazy path and gained the fixture it always needed; it was the only one, i.e. the only test that had been writing to the real database. 3. The 409 came from a test helper, not from the database race. `_run_route` waited for the run row to leave RUNNING, but the row is finalised inside the cycle while the power-off, the notification, the single-run lock and the queue's current entry all still follow. A second run of the same route inside that window is correctly rejected as already queued. The helper now waits for the queue itself to go idle. Both races were reproduced before being fixed - the schema collision by racing threads through a probe with no initialisation, the 409 by widening the window with a temporary sleep, which made it fail every run - and both stop reproducing with the fixes in place. Two regression guards, each confirmed to fail on the pre-fix code: an autouse fixture that diffs the live threads around every test and fails the one that leaves any behind, and a test pinning that using the database before initialising it raises. 636 passed, 2 skipped; 10 consecutive full runs green, with the real database untouched.
86 lines
3.3 KiB
Python
86 lines
3.3 KiB
Python
"""Smoke tests for the SQLite layer."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from sqlalchemy import select, text
|
|
|
|
from app.db import session_scope
|
|
from app.db.models import LogEvent, LogLevel, Run, RunKind, RunStatus, RunTrigger
|
|
|
|
|
|
def test_using_the_db_before_init_raises_instead_of_initialising_it(monkeypatch):
|
|
"""No lazy init_db(): it used to build the schema at paths.db_path() — the *real*
|
|
database — whenever a caller (typically a thread outliving its setup) got there first,
|
|
and two at once collided with "table X already exists". That was the flaky suite (R2)."""
|
|
from app.db import base
|
|
|
|
monkeypatch.setattr(base, "_SessionLocal", None)
|
|
with pytest.raises(RuntimeError, match="not initialised"):
|
|
with session_scope():
|
|
pass
|
|
assert base._SessionLocal is None # and it did not quietly create one on the way out
|
|
|
|
|
|
def test_sqlite_pragmas_applied(temp_db):
|
|
# WAL + busy_timeout + foreign_keys keep concurrent access safe while a running cycle
|
|
# commits and the dashboard polls (BE-B3). Assert they're set on a fresh connection.
|
|
with session_scope() as s:
|
|
assert s.execute(text("PRAGMA journal_mode")).scalar() == "wal"
|
|
assert s.execute(text("PRAGMA foreign_keys")).scalar() == 1
|
|
assert s.execute(text("PRAGMA busy_timeout")).scalar() == 5000
|
|
|
|
|
|
def test_create_run_with_logs(temp_db):
|
|
with session_scope() as s:
|
|
run = Run(kind=RunKind.BACKUP, trigger=RunTrigger.MANUAL)
|
|
run.logs.append(LogEvent(level=LogLevel.INFO, message="started"))
|
|
run.logs.append(LogEvent(level=LogLevel.OK, message="done"))
|
|
s.add(run)
|
|
|
|
with session_scope() as s:
|
|
run = s.scalars(select(Run)).one()
|
|
assert run.status == RunStatus.RUNNING
|
|
assert run.started_at is not None
|
|
assert len(run.logs) == 2
|
|
assert run.logs[0].message == "started"
|
|
|
|
|
|
def test_timestamps_read_back_utc_aware(temp_db):
|
|
# SQLite drops tzinfo on read; UtcDateTime must re-attach UTC so timestamps serialize
|
|
# with an offset (otherwise the frontend misreads them as local time).
|
|
with session_scope() as s:
|
|
run = Run(kind=RunKind.BACKUP, trigger=RunTrigger.MANUAL)
|
|
run.logs.append(LogEvent(level=LogLevel.INFO, message="started"))
|
|
s.add(run)
|
|
|
|
with session_scope() as s:
|
|
run = s.scalars(select(Run)).one()
|
|
assert run.started_at.tzinfo is not None
|
|
assert run.started_at.utcoffset().total_seconds() == 0 # UTC
|
|
assert run.logs[0].ts.tzinfo is not None
|
|
|
|
# And the API schema serializes it with a trailing offset/Z, not a bare local-looking time.
|
|
from app.api.schemas import RunSummary
|
|
|
|
with session_scope() as s:
|
|
run = s.scalars(select(Run)).one()
|
|
payload = RunSummary.of(run).model_dump_json()
|
|
assert '"started_at":"' in payload
|
|
started = payload.split('"started_at":"', 1)[1].split('"', 1)[0]
|
|
assert started.endswith("Z") or "+" in started
|
|
|
|
|
|
def test_cascade_delete_logs(temp_db):
|
|
with session_scope() as s:
|
|
run = Run(kind=RunKind.GC, trigger=RunTrigger.SCHEDULED)
|
|
run.logs.append(LogEvent(level=LogLevel.WARN, message="orphan chunks"))
|
|
s.add(run)
|
|
|
|
with session_scope() as s:
|
|
run = s.scalars(select(Run)).one()
|
|
s.delete(run)
|
|
|
|
with session_scope() as s:
|
|
assert s.scalars(select(LogEvent)).all() == []
|