Merge pull request 'feat(scheduled-jobs): scheduled jobs through PosixScheduledJobsMixin (#32)' (#33) from feature/scheduled-jobs into main
driver / standard (push) Successful in 14s
driver / lint (push) Successful in 14s
driver / types (push) Successful in 55s
driver / test (3.12) (push) Successful in 57s
driver / test (3.13) (push) Successful in 54s
driver / lowest (push) Successful in 28s
driver / build (push) Successful in 26s
driver / security (push) Successful in 1m1s
CI / driver (push) Successful in 2m27s
driver / publish (push) Skipped
driver / standard (push) Successful in 14s
driver / lint (push) Successful in 14s
driver / types (push) Successful in 55s
driver / test (3.12) (push) Successful in 57s
driver / test (3.13) (push) Successful in 54s
driver / lowest (push) Successful in 28s
driver / build (push) Successful in 26s
driver / security (push) Successful in 1m1s
CI / driver (push) Successful in 2m27s
driver / publish (push) Skipped
This commit was merged in pull request #33.
This commit is contained in:
commit
e2eda54fe4
6 files changed
+630
-3
No files matched your search
+15
-2
@@ -8,6 +8,19 @@ recorded here; their history is the git log.
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- Scheduled jobs (NetOrk/netork#1933, #32), through napalm-device-types'
|
||||
`PosixScheduledJobsMixin`: `get_scheduled_jobs()` reads every crontab in
|
||||
`/etc/crontabs` -- BusyBox crond has no `/etc/crontab`, `cron.d`, run-parts, anacron
|
||||
or systemd -- with the time zone from UCI and each crontab's package from `opkg`;
|
||||
`manage_scheduled_job()` enables, disables or runs one now; `put_scheduled_job()`
|
||||
and `delete_scheduled_job()` write and remove an application's own job as marked
|
||||
lines in the user's crontab. Every write changes only the job's own line, is read
|
||||
back (BusyBox `crontab` exits 0 when it could not write) and leaves cron enabled
|
||||
and restarted. A run is the command as BusyBox crond runs it (`%` is no stdin),
|
||||
bounded by `timeout` where the router has it (OpenWrt 25.x); another user's job
|
||||
runs through `start-stop-daemon -c`, as OpenWrt's BusyBox has no `su`. `@daily` and
|
||||
the other aliases are written as five fields; `@reboot` is refused, as OpenWrt's
|
||||
crond has no special times.
|
||||
- `configure_lldp_agent(interfaces)` (napalm-device-types `LldpAgentMixin`): installs
|
||||
lldpd where it is missing, writes the interfaces given as lldpd's interface list in
|
||||
UCI, enables lldpd at boot and restarts it. A VLAN device (`br-ap.10`) is written as
|
||||
@@ -15,8 +28,8 @@ recorded here; their history is the git log.
|
||||
to do on every poll (#8). Needs napalm-device-types with `LldpAgentMixin`.
|
||||
|
||||
### Changed
|
||||
- The floor on napalm-device-types is 4.24.0, the first release with what this driver
|
||||
uses (its shared `lldpctl` parser).
|
||||
- The floor on napalm-device-types is 4.26.0, the first release with `PosixScheduledJobsMixin`
|
||||
(it was 4.24.0, for the shared `lldpctl` parser).
|
||||
- Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver
|
||||
on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6).
|
||||
- The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python
|
||||
|
||||
@@ -65,6 +65,9 @@ device.close()
|
||||
| `get_lldp_neighbors` | ✅ | Requires `lldpd` installed and running (raises otherwise); reads only, never starts or configures it |
|
||||
| `get_lldp_neighbors_detail` | ✅ | Requires `lldpd` installed and running (raises otherwise); reads only, never starts or configures it |
|
||||
| `get_listening_sockets` | ✅ | busybox `netstat -lntup`, procd service from the process's cgroup (napalm-device-types `ListeningSocketsMixin`); over an SSH exec channel |
|
||||
| `get_scheduled_jobs()` | ✅ | BusyBox crond's `/etc/crontabs/<user>`, time zone from `system.@system[0].zonename`, owner from `opkg search` (napalm-device-types `PosixScheduledJobsMixin`) |
|
||||
| `manage_scheduled_job(key, action, digest=)` | ✅ | enable, disable, run now (bounded by `timeout` where the router has it; another user's job through `start-stop-daemon -c`) |
|
||||
| `put_scheduled_job(spec)` / `delete_scheduled_job(key, digest=)` | ✅ | an application's own job: two marked lines in the user's crontab; `@daily` and the other aliases as five fields, `@reboot` refused (BusyBox crond has no special times) |
|
||||
|
||||
## Configuration management
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ from napalm_openwrt.lldp_agent import OpenWrtLldpAgentMixin
|
||||
from napalm_openwrt.lldp_mixin import OpenWrtLLDPMixin
|
||||
from napalm_openwrt.packages_mixin import OpenWrtPackageMixin
|
||||
from napalm_openwrt.routing_mixin import OpenWrtRoutingMixin
|
||||
from napalm_openwrt.scheduled_jobs_mixin import OpenWrtScheduledJobsMixin
|
||||
from napalm_openwrt.system_mixin import _RC_MARKER, OpenWrtSystemMixin, _split_status
|
||||
from napalm_openwrt.vlan_mixin import OpenWrtVLANMixin
|
||||
from napalm_openwrt.wireless_mixin import OpenWrtWirelessMixin
|
||||
@@ -77,6 +78,7 @@ class OpenWrtDriver(
|
||||
OpenWrtSystemMixin,
|
||||
OpenWrtPackageMixin,
|
||||
OpenWrtRoutingMixin,
|
||||
OpenWrtScheduledJobsMixin,
|
||||
ListeningSocketsMixin,
|
||||
SnmpAgentMixin,
|
||||
OpenWrtLldpAgentMixin,
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Scheduled jobs on OpenWrt: BusyBox crond and the crontabs in ``/etc/crontabs`` (#32).
|
||||
|
||||
napalm-device-types' :class:`~napalm_device_types.PosixScheduledJobsMixin` does the
|
||||
work; this mixin tells it what an OpenWrt router has and what its BusyBox lacks.
|
||||
|
||||
**What there is.** BusyBox crond reads ``/etc/crontabs/<user>`` and nothing else:
|
||||
no ``/etc/crontab``, no ``cron.d``, no run-parts, no anacron, no systemd. An
|
||||
application's own job is two marked lines in the user's crontab. After every write
|
||||
the cron service is enabled and restarted -- OpenWrt does not start crond at boot
|
||||
while ``/etc/crontabs`` is empty.
|
||||
|
||||
**What BusyBox does differently** (as OpenWrt builds it, 21.02 to 25.x):
|
||||
|
||||
* ``crontab -l`` says only "can't open" for a crontab that is not there, the same as
|
||||
for one that cannot be read; the file is read directly instead, and a missing one
|
||||
is empty. ``crontab FILE`` exits 0 when it could not write: every write is read
|
||||
back.
|
||||
* crond has no special times (``FEATURE_CROND_SPECIAL_TIMES`` is off): ``@daily``
|
||||
and the other aliases are written as their five fields, ``@reboot`` is refused.
|
||||
* crond hands the whole command to ``sh -c`` -- ``%`` means nothing -- and of the
|
||||
crontab's variables takes only ``SHELL`` and ``PATH``; a run now does the same.
|
||||
* There is no ``stat``, ``cksum``, ``install`` or ``su``, and ``timeout`` only from
|
||||
25.x on. Only the crontabs are written, through ``crontab``; another user's job
|
||||
runs through ``start-stop-daemon -c``; a run is bounded by ``timeout`` where the
|
||||
router has it, else by the channel's wait.
|
||||
|
||||
Every command goes over an SSH exec channel (``run_command``), so ash's
|
||||
512-character limit on a typed line does not apply; dropbear takes 9000.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import shlex
|
||||
from typing import ClassVar
|
||||
|
||||
from napalm_device_types import PosixScheduledJobsMixin
|
||||
from napalm_device_types._crontab import CronLine
|
||||
from napalm_device_types._scheduled_reading import Reading
|
||||
from napalm_device_types.cron import ALIASES, is_reboot
|
||||
from napalm_device_types.scheduled_jobs import (
|
||||
RUN_TIMEOUT,
|
||||
ScheduledJobRefused,
|
||||
ScheduledJobResultDict,
|
||||
ScheduledJobSpecDict,
|
||||
)
|
||||
|
||||
CRONTABS = "/etc/crontabs"
|
||||
INIT_CRON = "/etc/init.d/cron"
|
||||
|
||||
#: LuCI's zone names have spaces where the IANA names have underscores.
|
||||
TIME_ZONE_SH = "uci -q get 'system.@system[0].zonename' | tr ' ' _"
|
||||
|
||||
#: ``opkg search <path>`` prints ``<package> - <version>``; apk-based OpenWrt names none.
|
||||
OWNERS_SH = (
|
||||
"if command -v apk >/dev/null 2>&1; then cat >/dev/null; "
|
||||
"elif command -v opkg >/dev/null 2>&1; then while read -r p; do "
|
||||
"o=$(opkg search \"$p\" 2>/dev/null | sed -n '1s/ - .*//p'); "
|
||||
'[ -z "$o" ] || printf \'%s: %s\\n\' "$o" "$p"; done; '
|
||||
"else cat >/dev/null; fi"
|
||||
)
|
||||
|
||||
#: A user name the run's script may name unquoted (``~user``).
|
||||
_USER = re.compile(r"[A-Za-z_][A-Za-z0-9_.-]*")
|
||||
#: What a run keeps of the job's output.
|
||||
_OUTPUT_LIMIT = 64 * 1024
|
||||
|
||||
|
||||
def _cron_script(line: CronLine) -> str:
|
||||
"""*line*'s command the way BusyBox crond runs it: as the user, from the user's home."""
|
||||
user = line.run_as
|
||||
if not _USER.fullmatch(user):
|
||||
raise ScheduledJobRefused(f"not a user name to run as: {user!r}")
|
||||
env = "".join(
|
||||
f"export {k}={shlex.quote(line.env[k])}; " for k in ("SHELL", "PATH") if k in line.env
|
||||
)
|
||||
return (
|
||||
f"HOME=~{user}; export HOME LOGNAME={user} USER={user}; "
|
||||
f'cd "$HOME" 2>/dev/null || cd /; {env}{line.command}'
|
||||
)
|
||||
|
||||
|
||||
class OpenWrtScheduledJobsMixin(PosixScheduledJobsMixin):
|
||||
"""Scheduled jobs of an OpenWrt router: BusyBox crond's crontabs."""
|
||||
|
||||
CRON_SYSTEM_FILES: ClassVar[tuple[str, ...]] = ()
|
||||
CRON_D_DIRS: ClassVar[tuple[str, ...]] = ()
|
||||
CRON_SPOOL_DIRS: ClassVar[tuple[str, ...]] = (CRONTABS,)
|
||||
RUN_PARTS_DIRS: ClassVar[tuple[tuple[str, str], ...]] = ()
|
||||
#: OpenWrt has no anacron; the reading passes an empty path by.
|
||||
ANACRONTAB: ClassVar[str] = ""
|
||||
OWN_CRON_D_DIR: ClassVar[str | None] = None
|
||||
OWNERS_SH: ClassVar[str] = OWNERS_SH
|
||||
TIME_ZONE_SH: ClassVar[str] = TIME_ZONE_SH
|
||||
CRONTAB_AFTER_WRITE: ClassVar[tuple[str, ...] | None] = (
|
||||
"sh",
|
||||
"-c",
|
||||
f"{INIT_CRON} enable && {INIT_CRON} restart",
|
||||
)
|
||||
|
||||
def _sj_crontab(self, user: str) -> str:
|
||||
"""The crontab of *user* as the file has it; empty when there is none.
|
||||
|
||||
:raises RuntimeError: when it is there and cannot be read -- then nothing is written.
|
||||
"""
|
||||
path = f"{self.CRON_SPOOL_DIRS[0]}/{user}"
|
||||
result = self._sj_root(["sh", "-c", '[ ! -e "$1" ] || cat -- "$1"', "sh", path])
|
||||
if result.exit_code != 0:
|
||||
said = result.stderr.strip() or f"exit {result.exit_code}"
|
||||
raise RuntimeError(f"cannot read the crontab of {user}: {said}")
|
||||
return result.stdout
|
||||
|
||||
def _sj_write_crontab(self, user: str, text: str) -> None:
|
||||
"""Write through ``crontab``, then read back: BusyBox's exits 0 when it could not."""
|
||||
super()._sj_write_crontab(user, text)
|
||||
if self._sj_crontab(user) != text:
|
||||
raise RuntimeError(f"the crontab of {user} was not written")
|
||||
|
||||
def _sj_as(self, user: str, script: str) -> list[str]:
|
||||
"""*script* run by ``sh`` as *user*; OpenWrt's BusyBox has no ``su``."""
|
||||
if user == "root":
|
||||
return ["sh", "-c", script]
|
||||
# -p /dev/null: no process is "already running" -- only that file is looked at.
|
||||
ssd = ["start-stop-daemon", "-S", "-p", "/dev/null", "-c", user, "-x", "/bin/sh"]
|
||||
return [*ssd, "--", "-c", script]
|
||||
|
||||
def _sj_run(self, job: dict[str, object], reading: Reading) -> ScheduledJobResultDict:
|
||||
"""Run a cron line now as crond would, bounded by ``timeout`` where there is one."""
|
||||
line = self._sj_line(job, self._sj_text(str(job["source"])))
|
||||
argv = shlex.join(self._sj_as(line.run_as, _cron_script(line)))
|
||||
command = (
|
||||
f"if command -v timeout >/dev/null 2>&1; then exec timeout {RUN_TIMEOUT} {argv}; fi; "
|
||||
f"exec {argv}"
|
||||
)
|
||||
result = self.run_command(command, privileged=True, timeout=RUN_TIMEOUT + 30)
|
||||
said = "\n".join(p for p in (result.stdout.strip(), result.stderr.strip()) if p)
|
||||
said = said[-_OUTPUT_LIMIT:]
|
||||
if result.exit_code != 0:
|
||||
return {"success": False, "output": said or f"exit {result.exit_code}"}
|
||||
return {"success": True, "output": said or "ran"}
|
||||
|
||||
def put_scheduled_job(
|
||||
self, spec: ScheduledJobSpecDict, *, digest: str | None = None
|
||||
) -> ScheduledJobResultDict:
|
||||
"""Write the application's job; an alias as its five fields, ``@reboot`` refused.
|
||||
|
||||
:raises ScheduledJobRefused: for ``@reboot``: BusyBox crond on OpenWrt has no
|
||||
special times.
|
||||
"""
|
||||
schedule = (spec.get("schedule") or "").strip()
|
||||
if is_reboot(schedule):
|
||||
raise ScheduledJobRefused("BusyBox crond on OpenWrt does not run @reboot")
|
||||
if schedule.lower() in ALIASES:
|
||||
spec = {**spec, "schedule": ALIASES[schedule.lower()]}
|
||||
return super().put_scheduled_job(spec, digest=digest)
|
||||
+1
-1
@@ -21,7 +21,7 @@ classifiers = [
|
||||
]
|
||||
dependencies = [
|
||||
"napalm>=5.1", # older releases import pkg_resources, which a 3.12 venv lacks
|
||||
"napalm_device_types>=4.24.0",
|
||||
"napalm_device_types>=4.27.0",
|
||||
"netmiko>=4.0.0",
|
||||
"paramiko>=5.0.0", # CVE-2026-44405
|
||||
"netaddr>=0.7.19", # IPAddress; below it the package has none (CI lowest job)
|
||||
|
||||
@@ -0,0 +1,454 @@
|
||||
"""Scheduled jobs on OpenWrt: BusyBox crond and the crontabs in /etc/crontabs (#32).
|
||||
|
||||
Every command is run by a real ``sh`` over a directory that stands in for the
|
||||
router, with ``PATH`` holding only applets OpenWrt's BusyBox is built with -- BusyBox's
|
||||
own where the test host has BusyBox -- (no ``stat``, ``cksum``, ``install``,
|
||||
``su`` or, before 25.x, ``timeout``). ``crontab``, ``uci``, ``opkg``, ``apk``,
|
||||
``start-stop-daemon`` and the cron init script are scripts that keep a log.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from napalm_device_types import CommandResult, PosixScheduledJobsMixin
|
||||
from napalm_device_types._scheduled_reading import Paths, reading_command
|
||||
from napalm_device_types.scheduled_jobs import (
|
||||
RUN_TIMEOUT,
|
||||
ScheduledJobChanged,
|
||||
ScheduledJobRefused,
|
||||
)
|
||||
|
||||
from napalm_openwrt import OpenWrtDriver
|
||||
from napalm_openwrt.scheduled_jobs_mixin import OpenWrtScheduledJobsMixin
|
||||
|
||||
FAILBACK = "*/15 * * * * /usr/sbin/netork-reboot-check.sh"
|
||||
LUCI = "0 4 * * * /usr/bin/backup.sh"
|
||||
OWN = "# napalm-scheduled-job: speedtest nightly speed test\n30 2 * * * /usr/bin/speedtest\n"
|
||||
|
||||
#: Applets of OpenWrt's default BusyBox (package/utils/busybox/Config-defaults.in,
|
||||
#: 21.02 to 25.x) that these scripts or the test's own tools need.
|
||||
APPLETS = (
|
||||
"sh",
|
||||
"cat",
|
||||
"chmod",
|
||||
"printf",
|
||||
"sed",
|
||||
"cut",
|
||||
"mktemp",
|
||||
"rm",
|
||||
"mv",
|
||||
"test",
|
||||
"[",
|
||||
"tr",
|
||||
"ls",
|
||||
"echo",
|
||||
"pwd",
|
||||
"basename",
|
||||
"xargs",
|
||||
"readlink",
|
||||
)
|
||||
|
||||
#: BusyBox ``crontab``: ``-u USER -l`` lists, ``-u USER FILE`` replaces through
|
||||
#: ``USER.new`` and appends the user to ``cron.update`` -- and exits 0 when it
|
||||
#: could not create the file.
|
||||
CRONTAB = """#!/bin/sh
|
||||
echo "crontab $*" >> "$LOG"
|
||||
[ "$1" = -u ] || exit 2
|
||||
if [ "$3" = -l ]; then cat "$SPOOL/$2"; exit; fi
|
||||
if [ -n "$CRONTAB_BROKEN" ]; then echo "crontab: can't create $SPOOL/$2.new" >&2; exit 0; fi
|
||||
cat "$3" > "$SPOOL/$2.new" && mv "$SPOOL/$2.new" "$SPOOL/$2"
|
||||
echo "$2" >> "$SPOOL/cron.update"
|
||||
"""
|
||||
|
||||
UCI = """#!/bin/sh
|
||||
echo "uci $*" >> "$LOG"
|
||||
[ "$*" = "-q get system.@system[0].zonename" ] || exit 2
|
||||
[ -n "$ZONE" ] || exit 1
|
||||
echo "$ZONE"
|
||||
"""
|
||||
|
||||
OPKG = """#!/bin/sh
|
||||
[ "$1" = search ] || exit 2
|
||||
[ "$2" = "$OWNED" ] && echo "luci-app-backup - 1.0-1"
|
||||
exit 0
|
||||
"""
|
||||
|
||||
LOG_ONLY = """#!/bin/sh
|
||||
echo "$(basename "$0") $*" >> "$LOG"
|
||||
"""
|
||||
|
||||
#: Logs, then runs what follows ``--`` with the executable named by ``-x``.
|
||||
SSD = """#!/bin/sh
|
||||
echo "start-stop-daemon $*" >> "$LOG"
|
||||
x=""
|
||||
while [ "$1" != -- ]; do [ "$1" = -x ] && x="$2"; shift; done
|
||||
shift
|
||||
exec "$x" "$@"
|
||||
"""
|
||||
|
||||
#: coreutils' ``timeout SECS PROG ARGS``, which BusyBox has from OpenWrt 25.x on.
|
||||
TIMEOUT = """#!/bin/sh
|
||||
echo "timeout $1" >> "$LOG"
|
||||
shift
|
||||
exec "$@"
|
||||
"""
|
||||
|
||||
|
||||
def _tool(bin_dir: Path, name: str, body: str) -> None:
|
||||
path = bin_dir / name
|
||||
path.write_text(body)
|
||||
path.chmod(0o755)
|
||||
|
||||
|
||||
def _applets(bin_dir: Path) -> None:
|
||||
"""The applets, from BusyBox where the test host has it; ``sh`` is the host's own.
|
||||
|
||||
A distribution's BusyBox shell runs its built-in applets ahead of ``PATH``
|
||||
(``FEATURE_SH_STANDALONE``, off on OpenWrt) and would pass by the fakes.
|
||||
"""
|
||||
busybox = shutil.which("busybox")
|
||||
for applet in APPLETS:
|
||||
target = (busybox if applet != "sh" else None) or shutil.which(applet)
|
||||
assert target, f"the test host has no {applet}"
|
||||
(bin_dir / applet).symlink_to(target)
|
||||
|
||||
|
||||
class Router(OpenWrtScheduledJobsMixin):
|
||||
"""An OpenWrt router in a directory; every command runs in its shell, as the test user."""
|
||||
|
||||
def __init__(self, root: Path, **env: str) -> None:
|
||||
self.root = root
|
||||
self.CRON_SPOOL_DIRS = (str(root / "crontabs"),) # type: ignore[misc]
|
||||
init = root / "init.d" / "cron"
|
||||
self.CRONTAB_AFTER_WRITE = ("sh", "-c", f"{init} enable && {init} restart") # type: ignore[misc]
|
||||
self.env = env
|
||||
self.calls: list[dict[str, Any]] = []
|
||||
|
||||
def _sj_paths(self) -> Paths:
|
||||
# The test host may have systemd; the router never does.
|
||||
return replace(super()._sj_paths(), systemd_marker=str(self.root / "no-systemd"))
|
||||
|
||||
def run_command(
|
||||
self,
|
||||
command: str,
|
||||
*,
|
||||
privileged: bool = False,
|
||||
timeout: float = 60,
|
||||
stdin: bytes | None = None,
|
||||
) -> CommandResult:
|
||||
self.calls.append({"command": command, "privileged": privileged, "timeout": timeout})
|
||||
env = {
|
||||
"PATH": str(self.root / "bin"),
|
||||
"HOME": str(self.root),
|
||||
"LOG": str(self.root / "log"),
|
||||
"SPOOL": str(self.root / "crontabs"),
|
||||
"OWNED": "",
|
||||
**self.env,
|
||||
}
|
||||
done = subprocess.run(
|
||||
[str(self.root / "bin" / "sh"), "-c", command],
|
||||
input=stdin,
|
||||
capture_output=True,
|
||||
env=env,
|
||||
cwd=self.root,
|
||||
timeout=30,
|
||||
)
|
||||
return CommandResult(done.stdout.decode(), done.stderr.decode(), done.returncode)
|
||||
|
||||
def log(self) -> str:
|
||||
log = self.root / "log"
|
||||
return log.read_text() if log.exists() else ""
|
||||
|
||||
def crontab(self, user: str = "root") -> str:
|
||||
return (self.root / "crontabs" / user).read_text()
|
||||
|
||||
def job(self, key: str) -> dict[str, Any]:
|
||||
return next(j for j in self.get_scheduled_jobs()["jobs"] if j["key"] == key) # type: ignore[misc]
|
||||
|
||||
def job_like(self, needle: str) -> dict[str, Any]:
|
||||
jobs = self.get_scheduled_jobs()["jobs"]
|
||||
return next(j for j in jobs if needle in j["command"]) # type: ignore[misc]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def root(tmp_path: Path) -> Path:
|
||||
for d in ("bin", "crontabs", "init.d"):
|
||||
(tmp_path / d).mkdir()
|
||||
_applets(tmp_path / "bin")
|
||||
for name, body in (
|
||||
("crontab", CRONTAB),
|
||||
("uci", UCI),
|
||||
("opkg", OPKG),
|
||||
("start-stop-daemon", SSD),
|
||||
):
|
||||
_tool(tmp_path / "bin", name, body)
|
||||
_tool(tmp_path / "init.d", "cron", LOG_ONLY.replace('"$(basename "$0")', '"cron'))
|
||||
(tmp_path / "crontabs/root").write_text(f"{LUCI}\n{FAILBACK}\n")
|
||||
return tmp_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def router(root: Path) -> Router:
|
||||
return Router(root, ZONE="Europe/Berlin")
|
||||
|
||||
|
||||
# -- the driver ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_the_driver_has_scheduled_jobs_through_the_posix_mixin():
|
||||
assert issubclass(OpenWrtDriver, OpenWrtScheduledJobsMixin)
|
||||
assert issubclass(OpenWrtDriver, PosixScheduledJobsMixin)
|
||||
for verb in (
|
||||
"get_scheduled_jobs",
|
||||
"manage_scheduled_job",
|
||||
"put_scheduled_job",
|
||||
"delete_scheduled_job",
|
||||
):
|
||||
assert callable(getattr(OpenWrtDriver, verb))
|
||||
|
||||
|
||||
def test_busybox_crond_reads_only_the_user_crontabs():
|
||||
assert OpenWrtDriver.CRON_SPOOL_DIRS == ("/etc/crontabs",)
|
||||
assert OpenWrtDriver.CRON_SYSTEM_FILES == ()
|
||||
assert OpenWrtDriver.CRON_D_DIRS == ()
|
||||
assert OpenWrtDriver.RUN_PARTS_DIRS == ()
|
||||
assert OpenWrtDriver.OWN_CRON_D_DIR is None
|
||||
assert OpenWrtDriver.ANACRONTAB == ""
|
||||
assert OpenWrtDriver.CRONTAB_AFTER_WRITE == (
|
||||
"sh",
|
||||
"-c",
|
||||
"/etc/init.d/cron enable && /etc/init.d/cron restart",
|
||||
)
|
||||
|
||||
|
||||
def test_the_report_fits_one_dropbear_exec_request():
|
||||
"""run_command is an exec channel: no 512-character ash line, but dropbear's MAX_CMD_LEN."""
|
||||
driver = OpenWrtDriver("ap", "root", "pw")
|
||||
assert len(reading_command(driver._sj_paths())) < 9000
|
||||
|
||||
|
||||
# -- reading ---------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_every_line_of_every_crontab_is_a_job(router: Router, root: Path):
|
||||
(root / "crontabs/root").write_text(
|
||||
f"{LUCI}\n{FAILBACK}\n#napalm-disabled: 5 5 * * * /bin/off\n{OWN}"
|
||||
)
|
||||
(root / "crontabs/nobody").write_text("*/5 * * * * /usr/bin/poll\n")
|
||||
(root / "crontabs/cron.update").write_text("root\n")
|
||||
reading = router.get_scheduled_jobs()
|
||||
assert reading["time_zone"] == "Europe/Berlin"
|
||||
jobs = {j["command"]: j for j in reading["jobs"]}
|
||||
assert set(jobs) == {
|
||||
"/usr/bin/backup.sh",
|
||||
"/usr/sbin/netork-reboot-check.sh",
|
||||
"/bin/off",
|
||||
"/usr/bin/speedtest",
|
||||
"/usr/bin/poll",
|
||||
}
|
||||
failback = jobs["/usr/sbin/netork-reboot-check.sh"]
|
||||
assert (failback["managed_by"], failback["run_as"], failback["schedule"]) == (
|
||||
"admin",
|
||||
"root",
|
||||
"*/15 * * * *",
|
||||
)
|
||||
assert failback["actions"] == ["enable", "disable", "run"]
|
||||
assert jobs["/bin/off"]["enabled"] is False
|
||||
assert jobs["/usr/bin/poll"]["run_as"] == "nobody"
|
||||
own = jobs["/usr/bin/speedtest"]
|
||||
assert (own["key"], own["managed_by"], own["description"]) == (
|
||||
"job:speedtest",
|
||||
"application",
|
||||
"nightly speed test",
|
||||
)
|
||||
assert "edit" in own["actions"] and "delete" in own["actions"]
|
||||
assert router.calls[0]["privileged"] is True
|
||||
|
||||
|
||||
def test_a_zone_name_with_spaces_is_the_iana_name(root: Path):
|
||||
reading = Router(root, ZONE="America/Port of Spain").get_scheduled_jobs()
|
||||
assert reading["time_zone"] == "America/Port_of_Spain"
|
||||
|
||||
|
||||
def test_no_zone_name_is_no_time_zone(root: Path):
|
||||
assert Router(root).get_scheduled_jobs()["time_zone"] is None
|
||||
|
||||
|
||||
def _owned(root: Path) -> Router:
|
||||
return Router(root, OWNED=str(root / "crontabs" / "root"))
|
||||
|
||||
|
||||
def test_a_crontab_opkg_owns_is_a_package_s(root: Path):
|
||||
assert _owned(root).job_like("backup.sh")["managed_by"] == "package"
|
||||
|
||||
|
||||
def test_apk_based_openwrt_names_no_owner(root: Path):
|
||||
_tool(root / "bin", "apk", LOG_ONLY)
|
||||
assert _owned(root).job_like("backup.sh")["managed_by"] == "admin"
|
||||
|
||||
|
||||
def test_without_a_package_manager_nothing_is_owned(root: Path):
|
||||
(root / "bin/opkg").unlink()
|
||||
assert _owned(root).job_like("backup.sh")["managed_by"] == "admin"
|
||||
|
||||
|
||||
# -- switching -------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_switching_a_job_off_changes_its_line_and_nothing_else(router: Router):
|
||||
job = router.job_like("backup.sh")
|
||||
result = router.manage_scheduled_job(job["key"], "disable", digest=job["digest"])
|
||||
assert result["success"] is True
|
||||
assert router.crontab() == f"#napalm-disabled: {LUCI}\n{FAILBACK}\n"
|
||||
assert "crontab -u root /" in router.log()
|
||||
assert "cron enable\ncron restart\n" in router.log()
|
||||
job = router.job(job["key"])
|
||||
router.manage_scheduled_job(job["key"], "enable", digest=job["digest"])
|
||||
assert router.crontab() == f"{LUCI}\n{FAILBACK}\n"
|
||||
|
||||
|
||||
def test_a_switch_over_a_changed_crontab_writes_nothing(router: Router, root: Path):
|
||||
job = router.job_like("backup.sh")
|
||||
(root / "crontabs/root").write_text(f"#napalm-disabled: {LUCI}\n{FAILBACK}\n")
|
||||
with pytest.raises(ScheduledJobChanged):
|
||||
router.manage_scheduled_job(job["key"], "disable", digest=job["digest"])
|
||||
assert "crontab -u" not in router.log()
|
||||
|
||||
|
||||
def test_a_write_crontab_did_not_make_raises(root: Path):
|
||||
router = Router(root, ZONE="UTC", CRONTAB_BROKEN="1")
|
||||
job = router.job_like("backup.sh")
|
||||
with pytest.raises(RuntimeError, match="not written"):
|
||||
router.manage_scheduled_job(job["key"], "disable", digest=job["digest"])
|
||||
assert router.crontab() == f"{LUCI}\n{FAILBACK}\n"
|
||||
|
||||
|
||||
# -- an application's own job ----------------------------------------------------------------
|
||||
|
||||
|
||||
def _spec(**changes: Any) -> Any:
|
||||
spec = {
|
||||
"name": "speedtest",
|
||||
"schedule": "30 2 * * *",
|
||||
"run_as": "root",
|
||||
"command": "/usr/bin/speedtest",
|
||||
"enabled": True,
|
||||
"description": "nightly speed test",
|
||||
}
|
||||
return {**spec, **changes}
|
||||
|
||||
|
||||
def test_an_own_job_is_two_marked_lines_at_the_end_of_the_crontab(router: Router):
|
||||
result = router.put_scheduled_job(_spec())
|
||||
assert result == {"success": True, "output": "job:speedtest created"}
|
||||
assert router.crontab() == f"{LUCI}\n{FAILBACK}\n{OWN}"
|
||||
assert "cron enable\ncron restart\n" in router.log()
|
||||
|
||||
|
||||
def test_changing_and_deleting_an_own_job_keep_the_failback_line(router: Router):
|
||||
router.put_scheduled_job(_spec())
|
||||
job = router.job("job:speedtest")
|
||||
router.put_scheduled_job(_spec(schedule="0 3 * * *", enabled=False), digest=job["digest"])
|
||||
assert router.crontab() == (
|
||||
f"{LUCI}\n{FAILBACK}\n# napalm-scheduled-job: speedtest nightly speed test\n"
|
||||
"#napalm-disabled: 0 3 * * * /usr/bin/speedtest\n"
|
||||
)
|
||||
job = router.job("job:speedtest")
|
||||
router.delete_scheduled_job("job:speedtest", digest=job["digest"])
|
||||
assert router.crontab() == f"{LUCI}\n{FAILBACK}\n"
|
||||
|
||||
|
||||
def test_an_own_job_starts_a_crontab_where_there_is_none(router: Router, root: Path):
|
||||
(root / "crontabs/root").unlink()
|
||||
router.put_scheduled_job(_spec())
|
||||
assert router.crontab() == OWN
|
||||
|
||||
|
||||
def test_a_crontab_that_cannot_be_read_is_never_written(router: Router, root: Path):
|
||||
(root / "crontabs/nobody").mkdir() # cat fails on it, as root too
|
||||
with pytest.raises(RuntimeError, match="cannot read the crontab of nobody"):
|
||||
router.put_scheduled_job(_spec(run_as="nobody"))
|
||||
assert "crontab -u" not in router.log()
|
||||
|
||||
|
||||
def test_an_alias_is_written_as_its_five_fields(router: Router):
|
||||
router.put_scheduled_job(_spec(schedule="@daily"))
|
||||
assert "\n0 0 * * * /usr/bin/speedtest\n" in router.crontab()
|
||||
|
||||
|
||||
def test_reboot_is_refused_busybox_crond_has_no_special_times(router: Router):
|
||||
with pytest.raises(ScheduledJobRefused, match="@reboot"):
|
||||
router.put_scheduled_job(_spec(schedule="@reboot"))
|
||||
assert router.crontab() == f"{LUCI}\n{FAILBACK}\n"
|
||||
|
||||
|
||||
# -- running now -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _run(router: Router, text: str, user: str = "root") -> Any:
|
||||
(router.root / "crontabs" / user).write_text(text)
|
||||
(job,) = (j for j in router.get_scheduled_jobs()["jobs"] if j["run_as"] == user)
|
||||
return router.manage_scheduled_job(job["key"], "run", digest=job["digest"])
|
||||
|
||||
|
||||
def test_a_run_is_the_command_as_busybox_crond_has_it(router: Router):
|
||||
result = _run(
|
||||
router,
|
||||
'PATH=/bin:/sbin\nMAILTO=x\n* * * * * printf \'%s %s %s\\n\' 100% "$PATH" "$LOGNAME"\n',
|
||||
)
|
||||
# BusyBox crond gives % no meaning and exports only PATH (and SHELL) of the crontab.
|
||||
assert result == {"success": True, "output": "100% /bin:/sbin root"}
|
||||
|
||||
|
||||
def test_a_run_has_the_user_s_home(router: Router):
|
||||
result = _run(router, '* * * * * echo "$HOME $USER"\n')
|
||||
assert result["output"] == "/root root"
|
||||
|
||||
|
||||
def test_a_run_is_bounded_where_the_router_has_timeout(router: Router, root: Path):
|
||||
_tool(root / "bin", "timeout", TIMEOUT)
|
||||
result = _run(router, "* * * * * echo hi\n")
|
||||
assert result == {"success": True, "output": "hi"}
|
||||
assert f"timeout {RUN_TIMEOUT}\n" in router.log()
|
||||
assert router.calls[-1]["timeout"] > RUN_TIMEOUT
|
||||
|
||||
|
||||
def test_a_run_goes_on_without_timeout(router: Router):
|
||||
result = _run(router, "* * * * * echo hi; exit 3\n")
|
||||
assert result == {"success": False, "output": "hi"}
|
||||
assert "timeout" not in router.log()
|
||||
|
||||
|
||||
def test_a_run_says_its_exit_status_when_the_job_says_nothing(router: Router):
|
||||
assert _run(router, "* * * * * exit 4\n") == {"success": False, "output": "exit 4"}
|
||||
|
||||
|
||||
def test_a_quiet_run_says_it_ran(router: Router):
|
||||
assert _run(router, "* * * * * true\n") == {"success": True, "output": "ran"}
|
||||
|
||||
|
||||
def test_another_user_s_job_runs_through_start_stop_daemon(router: Router):
|
||||
"""OpenWrt's BusyBox has no su; start-stop-daemon -c switches the user."""
|
||||
result = _run(router, "* * * * * echo as-nobody\n", user="nobody")
|
||||
assert result == {"success": True, "output": "as-nobody"}
|
||||
assert "start-stop-daemon -S -p /dev/null -c nobody -x /bin/sh -- -c " in router.log()
|
||||
|
||||
|
||||
def test_a_user_a_shell_could_misread_is_not_run(router: Router, root: Path):
|
||||
(root / "crontabs/x$(id)").write_text("* * * * * echo no\n")
|
||||
job = router.job_like("echo no")
|
||||
with pytest.raises(ScheduledJobRefused):
|
||||
router.manage_scheduled_job(job["key"], "run", digest=job["digest"])
|
||||
assert "start-stop-daemon" not in router.log()
|
||||
|
||||
|
||||
def test_the_root_login_runs_without_sudo():
|
||||
"""run_command on OpenWrt refuses privileged commands for another login; root needs no su."""
|
||||
driver = OpenWrtDriver("ap", "root", "pw")
|
||||
assert driver._sj_as("root", "true") == ["sh", "-c", "true"]
|
||||
Reference in new issue
Block a user