"""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"]