Files
napalm-linux/tests/test_scheduled_jobs.py
T
Christian Manivong ca32b89686
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 13s
driver / test (3.12) (pull_request) Failing after 51s
driver / types (pull_request) Failing after 59s
driver / test (3.13) (pull_request) Failing after 47s
driver / lowest (pull_request) Failing after 29s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Failing after 1m14s
CI / driver (pull_request) Failing after 2m29s
driver / publish (pull_request) Skipped
feat(scheduled-jobs): scheduled jobs through PosixScheduledJobsMixin (#67)
get_scheduled_jobs reads /etc/crontab, /etc/cron.d, the user crontabs,
run-parts, anacron and every systemd timer in one round trip as root;
manage_scheduled_job enables, disables or runs one now; put_ and
delete_scheduled_job write and remove an application's own job.

get_cron_jobs goes: it read without root, missed most of what a host
schedules, and nothing called it.

Requires napalm-device-types 4.26.0.

Closes #67
2026-11-03 05:32:01 +01:00

59 lines
1.6 KiB
Python

"""Scheduled jobs on a Linux host: napalm-device-types' POSIX implementation (#67).
The report, its parse and every write are ``PosixScheduledJobsMixin``'s; the
driver carries the command across ``run_command``, as root, in one round trip.
"""
from __future__ import annotations
from unittest.mock import MagicMock
from napalm_device_types import CommandResult, PosixScheduledJobsMixin
from napalm_linux import LinuxDriver
REPORT = """SJ_BEGIN
[system /etc/cron.d/certbot]
|0 */12 * * * root certbot -q renew
[tz]
/usr/share/zoneinfo/Europe/Berlin
[owners]
certbot: /etc/cron.d/certbot
SJ_END
"""
def _driver() -> LinuxDriver:
driver = LinuxDriver("host", "user", "pw")
driver.run_command = MagicMock(return_value=CommandResult(REPORT, "", 0)) # type: ignore[method-assign]
return driver
def test_a_linux_host_offers_every_verb():
assert issubclass(LinuxDriver, PosixScheduledJobsMixin)
for verb in (
"get_scheduled_jobs",
"manage_scheduled_job",
"put_scheduled_job",
"delete_scheduled_job",
):
assert callable(getattr(LinuxDriver, verb))
def test_the_jobs_are_read_as_root_in_one_round_trip():
driver = _driver()
reading = driver.get_scheduled_jobs()
assert reading["time_zone"] == "Europe/Berlin"
(job,) = reading["jobs"]
assert (job["run_as"], job["managed_by"], job["command"]) == (
"root",
"package",
"certbot -q renew",
)
driver.run_command.assert_called_once()
assert driver.run_command.call_args.kwargs["privileged"] is True
def test_the_old_cron_reader_is_gone():
assert not hasattr(LinuxDriver, "get_cron_jobs")