Files
napalm-proxmox/tests/test_scheduled_jobs.py
T
Christian Manivong 4e7e29b598
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / test (3.12) (pull_request) Failing after 17s
driver / types (pull_request) Failing after 30s
driver / test (3.13) (pull_request) Failing after 15s
driver / lowest (pull_request) Failing after 19s
driver / security (pull_request) Failing after 43s
driver / build (pull_request) Successful in 35s
CI / driver (pull_request) Failing after 1m35s
driver / publish (pull_request) Skipped
docs(scheduled-jobs): a vzdump line only runs because of pmxcfs (#56)
The guard is not about the link: napalm-device-types now writes the file a
link names. /etc/cron.d/vzdump points into /etc/pve, the cluster file
system, where installing a file with a mode and owner is not safe.

Refs #56
2026-11-03 05:40:27 +01:00

138 lines
5.0 KiB
Python

"""Scheduled jobs on a Proxmox VE node and a Proxmox Backup Server (#56).
The report, its parse and every write are napalm-device-types'
``PosixScheduledJobsMixin``'s; the drivers carry the command across
``run_command`` as root in one round trip and say which jobs Proxmox itself
ships (``platform``).
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from napalm_device_types import CommandResult, PosixScheduledJobsMixin, ScheduledJobRefused
from napalm_proxmox import ProxmoxBackupDriver, ProxmoxDriver
_TIMER = """|Id={name}.timer
|Unit={name}.service
|Description={name}
|TimersCalendar={{ OnCalendar=*-*-* 06:00:00 ; next_elapse=n/a }}
|NextElapseUSecRealtime=@1762236000
|LastTriggerUSec=@1762149600
|UnitFileState=enabled
|ActiveState=active
|FragmentPath={path}
|"""
_UNIT = """|Id={name}.service
|ExecStart={{ path=/usr/bin/{name} ; argv[]=/usr/bin/{name} ; }}
|Result=success
|FragmentPath={path}
|"""
def _report(*timers: tuple[str, str]) -> str:
return "\n".join(
[
"SJ_BEGIN",
"[system /etc/cron.d/vzdump]",
"|0 1 * * 6 root vzdump 100 --mode snapshot --quiet 1",
"[system /etc/cron.d/zfsutils-linux]",
"|0 0 * * 0 root /usr/lib/zfs-linux/scrub",
"[user /var/spool/cron/crontabs/root]",
"|*/5 * * * * /root/check.sh",
"[systemd]",
"[timers]",
*(_TIMER.format(name=n, path=p) for n, p in timers),
"[units]",
*(_UNIT.format(name=n, path=p.replace(".timer", ".service")) for n, p in timers),
"[tz]",
"/usr/share/zoneinfo/Europe/Berlin",
"[owners]",
"zfsutils-linux: /etc/cron.d/zfsutils-linux",
*(f"{n.split('-')[0]}: {p}" for n, p in timers),
"SJ_END",
"",
]
)
PVE_REPORT = _report(
("pve-daily-update", "/lib/systemd/system/pve-daily-update.timer"),
("pvesr", "/usr/lib/systemd/system/pvesr.timer"),
("apt-daily", "/usr/lib/systemd/system/apt-daily.timer"),
)
PBS_REPORT = _report(
("proxmox-backup-daily-update", "/usr/lib/systemd/system/proxmox-backup-daily-update.timer"),
("pve-daily-update", "/lib/systemd/system/pve-daily-update.timer"),
("apt-daily", "/usr/lib/systemd/system/apt-daily.timer"),
)
VERBS = ("get_scheduled_jobs", "manage_scheduled_job", "put_scheduled_job", "delete_scheduled_job")
def _driver(cls: type, report: str) -> MagicMock:
driver = cls("host", "root@pam", "pw")
driver.run_command = MagicMock(return_value=CommandResult(report, "", 0))
return driver
def _by_source(driver: MagicMock) -> dict[str, dict[str, object]]:
return {str(j["source"]): dict(j) for j in driver.get_scheduled_jobs()["jobs"]}
@pytest.mark.parametrize(
("cls", "report"),
[(ProxmoxDriver, PVE_REPORT), (ProxmoxBackupDriver, PBS_REPORT)],
ids=["pve", "pbs"],
)
def test_each_driver_offers_every_verb_and_reads_as_root_once(cls: type, report: str) -> None:
assert issubclass(cls, PosixScheduledJobsMixin)
for verb in VERBS:
assert callable(getattr(cls, verb))
driver = _driver(cls, report)
reading = driver.get_scheduled_jobs()
assert reading["time_zone"] == "Europe/Berlin"
assert len(reading["jobs"]) == 6
driver.run_command.assert_called_once()
assert driver.run_command.call_args.kwargs["privileged"] is True
def test_what_proxmox_ve_ships_is_platform() -> None:
jobs = _by_source(_driver(ProxmoxDriver, PVE_REPORT))
managed = {source: job["managed_by"] for source, job in jobs.items()}
assert managed == {
"/lib/systemd/system/pve-daily-update.timer": "platform",
"/usr/lib/systemd/system/pvesr.timer": "platform",
"/etc/cron.d/vzdump": "platform",
"/usr/lib/systemd/system/apt-daily.timer": "package",
"/etc/cron.d/zfsutils-linux": "package",
"/var/spool/cron/crontabs/root": "admin",
}
assert jobs["/lib/systemd/system/pve-daily-update.timer"]["actions"] == [
"enable",
"disable",
"run",
]
def test_what_a_backup_server_ships_is_platform() -> None:
jobs = _by_source(_driver(ProxmoxBackupDriver, PBS_REPORT))
managed = {source: job["managed_by"] for source, job in jobs.items()}
assert managed["/usr/lib/systemd/system/proxmox-backup-daily-update.timer"] == "platform"
# Proxmox VE's names mean nothing on a backup server.
assert managed["/lib/systemd/system/pve-daily-update.timer"] == "package"
assert managed["/etc/cron.d/vzdump"] == "admin"
assert managed["/usr/lib/systemd/system/apt-daily.timer"] == "package"
def test_a_vzdump_cron_line_only_runs() -> None:
"""``/etc/cron.d/vzdump`` links into pmxcfs (``/etc/pve``), where no switch writes."""
driver = _driver(ProxmoxDriver, PVE_REPORT)
job = _by_source(driver)["/etc/cron.d/vzdump"]
assert job["actions"] == ["run"]
with pytest.raises(ScheduledJobRefused):
driver.manage_scheduled_job(str(job["key"]), "disable", digest=str(job["digest"]))
assert driver.run_command.call_count == 2 # two readings, nothing written