180 lines
6.3 KiB
Python
180 lines
6.3 KiB
Python
"""Ownership is bound to the protected store composition (R4 binding).
|
|
|
|
A held lock on an unrelated directory must not authorize mutation of a
|
|
store composition it does not cover. The guard validates the held guard
|
|
against the actual schedule/run store roots (canonicalized for platform
|
|
aliases) instead of trusting the caller to have passed the right lock.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from tests.scheduling.controlled import (
|
|
DictDeployments,
|
|
ScriptedDispatcher,
|
|
fixture_environment,
|
|
)
|
|
from wf_artifacts.runs.store import FileRunStore
|
|
from wf_scheduling.calendar import OneShotSource
|
|
from wf_scheduling.dispatch import StillRunning
|
|
from wf_scheduling.models import Schedule
|
|
from wf_scheduling.ownership import SchedulerOwnership, SecondOwnerError
|
|
from wf_scheduling.poll import Scheduler
|
|
from wf_scheduling.prepare import SchedulePreparer
|
|
from wf_scheduling.store import FileScheduleStore
|
|
|
|
|
|
def ts(y: int, mo: int, d: int, h: int = 0, mi: int = 0) -> datetime:
|
|
return datetime(y, mo, d, h, mi, tzinfo=UTC)
|
|
|
|
|
|
def _sched_model(sid: str, **kw: Any) -> Schedule:
|
|
now = ts(2026, 9, 8, 12, 0)
|
|
base: dict[str, Any] = {
|
|
"id": sid,
|
|
"deployment_id": "dep-1",
|
|
"trigger": {"kind": "cron", "expression": "0 * * * *", "timezone": "UTC"},
|
|
"input_bindings": [],
|
|
"created_at": now.isoformat(),
|
|
"updated_at": now.isoformat(),
|
|
}
|
|
base.update(kw)
|
|
return Schedule.model_validate(base)
|
|
|
|
|
|
def _scheduler(
|
|
sched_store: FileScheduleStore,
|
|
run_store: FileRunStore,
|
|
ownership: SchedulerOwnership,
|
|
*,
|
|
script: dict | None = None,
|
|
) -> Scheduler:
|
|
return Scheduler(
|
|
schedule_store=sched_store,
|
|
run_store=run_store,
|
|
sources={},
|
|
capacity=4,
|
|
preparer=SchedulePreparer(
|
|
DictDeployments({"dep-1": {"rev": 1, "required": []}}),
|
|
fixture_environment,
|
|
),
|
|
dispatcher=ScriptedDispatcher(script),
|
|
ownership=ownership,
|
|
)
|
|
|
|
|
|
def _due(sched: Scheduler, store: FileScheduleStore, intended: datetime) -> None:
|
|
store.create_schedule(_sched_model("a"))
|
|
store.save_consumed("a", intended - timedelta(hours=1))
|
|
sched.sources["a"] = OneShotSource(intended)
|
|
|
|
|
|
def test_unrelated_held_lock_rejected_before_writes(tmp_path: Path) -> None:
|
|
calls: list[str] = []
|
|
|
|
def spy(admission: Any, now: datetime) -> Any:
|
|
calls.append(admission.id)
|
|
return StillRunning()
|
|
|
|
sched_store = FileScheduleStore(tmp_path / "sched")
|
|
run_store = FileRunStore(tmp_path / "runs")
|
|
ownership = SchedulerOwnership(tmp_path / "other", owner="unrelated").acquire()
|
|
try:
|
|
sched = _scheduler(sched_store, run_store, ownership)
|
|
intended = ts(2026, 9, 8, 12, 0)
|
|
_due(sched, sched_store, intended)
|
|
with pytest.raises(SecondOwnerError):
|
|
sched.poll(intended)
|
|
assert calls == []
|
|
assert run_store.list_runs() == []
|
|
assert run_store.list_admissions() == []
|
|
assert sched_store.list_occurrences("a", limit=100)["total"] == 0
|
|
finally:
|
|
ownership.release()
|
|
|
|
|
|
def test_two_lock_dirs_cannot_operate_same_stores(tmp_path: Path) -> None:
|
|
sched_store = FileScheduleStore(tmp_path / "sched")
|
|
run_store = FileRunStore(tmp_path / "runs")
|
|
intended = ts(2026, 9, 8, 12, 0)
|
|
covering = SchedulerOwnership(tmp_path, owner="covering").acquire()
|
|
other = SchedulerOwnership(tmp_path / "other", owner="other").acquire()
|
|
try:
|
|
first = _scheduler(sched_store, run_store, covering, script={"*": "hang"})
|
|
_due(first, sched_store, intended)
|
|
assert first.poll(intended)["a"].startswith("admit:run-")
|
|
# The other directory's lock acquires fine (different lock file) but
|
|
# must not authorize the same stores.
|
|
second = _scheduler(sched_store, run_store, other, script={"*": "hang"})
|
|
second.sources["a"] = OneShotSource(intended)
|
|
with pytest.raises(SecondOwnerError):
|
|
second.poll(intended + timedelta(minutes=1))
|
|
assert len(run_store.list_runs()) == 1
|
|
finally:
|
|
covering.release()
|
|
other.release()
|
|
|
|
|
|
def test_recover_with_unrelated_lock_rejected(tmp_path: Path) -> None:
|
|
from tests.artifacts.test_run_store import artifact as _artifact
|
|
from tests.artifacts.test_run_store import deployment as _deployment
|
|
from wf_api.run_lifecycle import materialize_admitted_view, persist_admission
|
|
from wf_artifacts import PinnedRunEnvironment
|
|
from wf_scheduling import recovery as sched_recovery
|
|
|
|
sched_store = FileScheduleStore(tmp_path / "sched")
|
|
run_store = FileRunStore(tmp_path / "runs")
|
|
sched_store.create_schedule(_sched_model("a"))
|
|
admission = persist_admission(
|
|
store=run_store,
|
|
run_id=run_store.allocate_run_id(),
|
|
environment=PinnedRunEnvironment(
|
|
deployment=_deployment(), root_artifact=_artifact(), child_artifacts=[]
|
|
),
|
|
resolved_input={},
|
|
max_steps=None,
|
|
)
|
|
materialize_admitted_view(store=run_store, admission=admission)
|
|
ownership = SchedulerOwnership(tmp_path / "other", owner="unrelated").acquire()
|
|
try:
|
|
with pytest.raises(SecondOwnerError):
|
|
sched_recovery.recover(
|
|
schedule_store=sched_store,
|
|
run_store=run_store,
|
|
now=ts(2026, 9, 8, 12, 0),
|
|
ownership=ownership,
|
|
)
|
|
assert run_store.get_run(admission.id).status.value == "admitted"
|
|
finally:
|
|
ownership.release()
|
|
|
|
|
|
def test_canonicalizer_handles_aliases(tmp_path: Path) -> None:
|
|
from wf_scheduling.ownership import canonical_store_path
|
|
|
|
base = tmp_path / "sched"
|
|
dotted = tmp_path / "sub" / ".." / "sched"
|
|
assert canonical_store_path(dotted) == canonical_store_path(base)
|
|
if os.name == "nt":
|
|
assert canonical_store_path("C:\\Temp\\SCHED") == canonical_store_path(
|
|
"c:/temp/sched"
|
|
)
|
|
else:
|
|
assert canonical_store_path(dotted) != canonical_store_path(tmp_path / "other")
|
|
|
|
|
|
def test_covers_rejects_empty_root_set(tmp_path: Path) -> None:
|
|
ownership = SchedulerOwnership(tmp_path, owner="test").acquire()
|
|
try:
|
|
assert ownership.held
|
|
assert ownership.covers() is False
|
|
assert ownership.covers(tmp_path) is True
|
|
finally:
|
|
ownership.release()
|