diff --git a/napalm_device_types/scheduled_jobs_posix.py b/napalm_device_types/scheduled_jobs_posix.py index ec63fbf..91bfe61 100644 --- a/napalm_device_types/scheduled_jobs_posix.py +++ b/napalm_device_types/scheduled_jobs_posix.py @@ -108,7 +108,8 @@ class PosixScheduledJobsMixin(ScheduledJobsMixin): ANACRONTAB: ClassVar[str] = "/etc/anacrontab" JOB_SCRIPT_DIR: ClassVar[str] = "/usr/local/libexec/scheduled-jobs" UNIT_DIR: ClassVar[str] = "/etc/systemd/system" - #: Where a host without systemd takes an application's job as a file; None: its crontab. + #: Where a host without systemd takes an application's job as a file, if the directory + #: is there; else, or None, the user's crontab. OWN_CRON_D_DIR: ClassVar[str | None] = "/etc/cron.d" ROOT_GROUP: ClassVar[str] = "root" #: Prints " " of the path appended. @@ -393,8 +394,16 @@ class PosixScheduledJobsMixin(ScheduledJobsMixin): spec["name"], spec.get("description") or "", line, enabled=spec["enabled"] ) + def _sj_cron_d(self) -> str | None: + """:attr:`OWN_CRON_D_DIR` where the host has it; BusyBox crond reads none.""" + directory = self.OWN_CRON_D_DIR + if directory is None: + return None + found = self.run_command(_command(["test", "-d", directory]), timeout=30) + return directory if found.exit_code == 0 else None + def _sj_put_cron(self, spec: ScheduledJobSpecDict, current: dict[str, object] | None) -> None: - if self.OWN_CRON_D_DIR is not None: + if self._sj_cron_d() is not None: self._sj_group(spec["run_as"]) text = "\n".join(self._sj_cron_text(spec, system=True)) + "\n" self._sj_install( diff --git a/tests/test_scheduled_jobs_posix.py b/tests/test_scheduled_jobs_posix.py index 9570318..d1d5200 100644 --- a/tests/test_scheduled_jobs_posix.py +++ b/tests/test_scheduled_jobs_posix.py @@ -484,6 +484,12 @@ class TestApplicationJobs: host.delete_scheduled_job("job:netork-tidy", digest=job["digest"]) assert (root / "spool/nobody").read_text() == "" + def test_a_cron_d_that_is_not_there_is_the_crontab(self, root: Path): + host = Host(root, systemd=False) + host.OWN_CRON_D_DIR = str(root / "etc/no-cron.d") # type: ignore[misc] + host.put_scheduled_job(SPEC) + assert "netork-tidy" in (root / "spool/root").read_text() + def test_a_cron_line_holds_one_line(self, root: Path): with pytest.raises(ScheduledJobRefused, match="one line"): Host(root, systemd=False).put_scheduled_job({**SPEC, "command": "a\nb"})