Add cron next-run display and speed up timer lookup
This commit is contained in:
11 files changed
+328
-14
No files matched your search
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from schedls.cronexpr import next_occurrence
|
||||
|
||||
|
||||
def test_next_occurrence_for_hourly_schedule() -> None:
|
||||
after = datetime(2026, 10, 2, 12, 17, 30, tzinfo=UTC)
|
||||
|
||||
assert next_occurrence("17 * * * *", after=after) == datetime(2026, 10, 2, 13, 17, tzinfo=UTC)
|
||||
|
||||
|
||||
def test_next_occurrence_uses_cron_day_of_month_or_weekday() -> None:
|
||||
after = datetime(2026, 10, 12, 12, 0, tzinfo=UTC)
|
||||
|
||||
assert next_occurrence("0 0 13 * fri", after=after) == datetime(2026, 10, 13, 0, 0, tzinfo=UTC)
|
||||
|
||||
|
||||
def test_next_occurrence_treats_day_steps_as_restricted() -> None:
|
||||
after = datetime(2026, 10, 2, 12, 0, tzinfo=UTC)
|
||||
|
||||
assert next_occurrence("0 0 */2 * *", after=after) == datetime(2026, 10, 3, 0, 0, tzinfo=UTC)
|
||||
assert next_occurrence("0 0 * * */2", after=after) == datetime(2026, 10, 3, 0, 0, tzinfo=UTC)
|
||||
|
||||
|
||||
def test_next_occurrence_supports_nicknames_and_reboot() -> None:
|
||||
after = datetime(2026, 10, 2, 12, 0, tzinfo=UTC)
|
||||
|
||||
assert next_occurrence("@daily", after=after) == datetime(2026, 10, 3, 0, 0, tzinfo=UTC)
|
||||
assert next_occurrence("@reboot", after=after) is None
|
||||
|
||||
|
||||
def test_next_occurrence_skips_nonexistent_local_time() -> None:
|
||||
london = ZoneInfo("Europe/London")
|
||||
after = datetime(2026, 3, 29, 0, 0, tzinfo=london)
|
||||
|
||||
assert next_occurrence("30 1 * * *", after=after) == datetime(2026, 3, 30, 1, 30, tzinfo=london)
|
||||
|
||||
|
||||
def test_next_occurrence_handles_long_leap_day_gap() -> None:
|
||||
after = datetime(2096, 3, 1, 0, 0, tzinfo=UTC)
|
||||
|
||||
assert next_occurrence("0 0 29 2 *", after=after) == datetime(2104, 2, 29, 0, 0, tzinfo=UTC)
|
||||
@@ -15,6 +15,7 @@ from schedls.interact import Interaction
|
||||
from schedls.models import (
|
||||
Backend,
|
||||
Command,
|
||||
CronDetails,
|
||||
JobSource,
|
||||
JobSpec,
|
||||
Schedule,
|
||||
@@ -72,6 +73,45 @@ def test_render_show() -> None:
|
||||
assert "systemd user timer" in text
|
||||
|
||||
|
||||
def test_render_show_explains_unavailable_cron_history() -> None:
|
||||
job = ScheduledJob(
|
||||
name="cron-1",
|
||||
backend=Backend.CRON,
|
||||
scope=Scope.USER,
|
||||
managed=False,
|
||||
enabled=None,
|
||||
schedule=Schedule(ScheduleKind.CRON, "0 2 * * *"),
|
||||
command=Command(raw="/usr/bin/backup"),
|
||||
source=JobSource("current user's crontab", line=1),
|
||||
cron=CronDetails(expression="0 2 * * *", line=1),
|
||||
)
|
||||
output, stream = _output()
|
||||
|
||||
inspect_ops.render_show(output, job)
|
||||
|
||||
assert "Previous unavailable (cron does not provide per-job history)" in stream.getvalue()
|
||||
|
||||
|
||||
def test_render_show_keeps_cron_next_run_null_in_json() -> None:
|
||||
job = ScheduledJob(
|
||||
name="cron-1",
|
||||
backend=Backend.CRON,
|
||||
scope=Scope.USER,
|
||||
managed=False,
|
||||
enabled=None,
|
||||
schedule=Schedule(ScheduleKind.CRON, "0 2 * * *"),
|
||||
command=Command(raw="/usr/bin/backup"),
|
||||
source=JobSource("current user's crontab", line=1),
|
||||
cron=CronDetails(expression="0 2 * * *", line=1),
|
||||
)
|
||||
output, stream = _output()
|
||||
output.json_mode = True
|
||||
|
||||
inspect_ops.render_show(output, job)
|
||||
|
||||
assert '"next_run": null' in stream.getvalue()
|
||||
|
||||
|
||||
def test_filter_jobs() -> None:
|
||||
jobs = [_job(), _job("other")]
|
||||
assert inspect_ops.filter_jobs(jobs, managed=True) == jobs
|
||||
@@ -216,6 +256,32 @@ def test_systemd_backend_discovery_parses_units() -> None:
|
||||
assert job.command.argv == ("/usr/local/bin/backup", "/srv/data")
|
||||
assert job.enabled is True
|
||||
assert job.next_run is not None
|
||||
assert job.last_result == "success"
|
||||
|
||||
|
||||
def test_systemd_find_uses_direct_lookup_and_cached_manager_check() -> None:
|
||||
def systemctl(argv, _input):
|
||||
if "is-system-running" in argv:
|
||||
return ("running\n", "")
|
||||
if "show" in argv:
|
||||
unit = argv[argv.index("show") + 1]
|
||||
if unit == "schedls-backup.timer":
|
||||
return ("LoadState=not-found\n", "")
|
||||
if unit == "backup.timer":
|
||||
return ("LoadState=loaded\nUnitFileState=enabled\n", "")
|
||||
return ("", "")
|
||||
|
||||
runner = FakeRunner(handlers={"systemctl": systemctl}, available={"systemctl"})
|
||||
backend = SystemdBackend(runner)
|
||||
|
||||
assert backend.available() is True
|
||||
job = backend.find("backup")
|
||||
|
||||
assert job is not None
|
||||
assert job.name == "backup"
|
||||
calls = [argv for argv, _, _ in runner.calls]
|
||||
assert sum("is-system-running" in argv for argv in calls) == 1
|
||||
assert not any("list-unit-files" in argv or "list-units" in argv for argv in calls)
|
||||
|
||||
|
||||
def test_plan_update_refuses_unmanaged() -> None:
|
||||
|
||||
Reference in new issue
Block a user