Add system cron discovery and management; bump to 0.2.0
This commit is contained in:
19 files changed
+1129
-55
No files matched your search
+20
-1
@@ -102,8 +102,27 @@ def test_spec_for_new_rejects_cron_expr_for_timer() -> None:
|
||||
_spec_for_new(args, ["/bin/true"])
|
||||
|
||||
|
||||
def test_spec_for_new_rejects_system_scope_for_cron() -> None:
|
||||
def test_spec_for_new_system_cron() -> None:
|
||||
args = build_parser().parse_args(["new", "x", "--cron", "--daily", "02:00", "--system"])
|
||||
spec = _spec_for_new(args, ["/bin/true"])
|
||||
assert spec.scope is Scope.SYSTEM
|
||||
assert spec.run_as == "root"
|
||||
|
||||
|
||||
def test_spec_for_new_system_cron_run_as() -> None:
|
||||
args = build_parser().parse_args(["new", "x", "--cron", "--daily", "02:00", "--system", "--run-as", "www-data"])
|
||||
spec = _spec_for_new(args, ["/bin/true"])
|
||||
assert spec.run_as == "www-data"
|
||||
|
||||
|
||||
def test_spec_for_new_rejects_run_as_for_user_cron() -> None:
|
||||
args = build_parser().parse_args(["new", "x", "--cron", "--daily", "02:00", "--run-as", "www-data"])
|
||||
with pytest.raises(UsageError):
|
||||
_spec_for_new(args, ["/bin/true"])
|
||||
|
||||
|
||||
def test_spec_for_new_rejects_run_as_for_timer() -> None:
|
||||
args = build_parser().parse_args(["new", "x", "--timer", "--daily", "02:00", "--run-as", "www-data"])
|
||||
with pytest.raises(UsageError):
|
||||
_spec_for_new(args, ["/bin/true"])
|
||||
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from schedls.backends.base import FileChange, Plan
|
||||
from schedls.backends.cron import CronBackend, CronPaths
|
||||
from schedls.errors import ConflictError, InvalidScheduleError, SafetyRefusalError
|
||||
from schedls.models import Backend, Command, JobSpec, Scope
|
||||
from schedls.renderers import cron as renderer
|
||||
|
||||
|
||||
def _make_paths(tmp_path) -> CronPaths:
|
||||
root = tmp_path / "etc"
|
||||
root.mkdir()
|
||||
return CronPaths(
|
||||
crontab=str(root / "crontab"),
|
||||
cron_d=str(root / "cron.d"),
|
||||
hourly=str(root / "cron.hourly"),
|
||||
daily=str(root / "cron.daily"),
|
||||
weekly=str(root / "cron.weekly"),
|
||||
monthly=str(root / "cron.monthly"),
|
||||
spool_crontabs=str(tmp_path / "var" / "spool" / "cron" / "crontabs"),
|
||||
spool=str(tmp_path / "var" / "spool" / "cron"),
|
||||
)
|
||||
|
||||
|
||||
def _write(path, text: str, mode: int = 0o644):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text)
|
||||
path.chmod(mode)
|
||||
return path
|
||||
|
||||
|
||||
def test_available_without_crontab_uses_file_sources(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
assert CronBackend(fake_runner, paths).available() is False
|
||||
_write(tmp_path / "etc/cron.d/backup", "0 2 * * * root /usr/bin/backup\n")
|
||||
assert CronBackend(fake_runner, paths).available() is True
|
||||
|
||||
|
||||
def test_discovers_system_crontabs(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(
|
||||
tmp_path / "etc/crontab",
|
||||
"SHELL=/bin/sh\n17 * * * * root /usr/bin/hourly\n@daily www-data /usr/bin/report\n",
|
||||
)
|
||||
_write(tmp_path / "etc/cron.d/backup", "0 2 * * * root /usr/bin/backup\n")
|
||||
_write(
|
||||
tmp_path / "etc/cron.d/schedls-cleanup",
|
||||
renderer.render_system_file("cleanup", ["30 3 * * 0 deploy /usr/bin/cleanup"]),
|
||||
)
|
||||
_write(tmp_path / "etc/cron.d/with.dot", "0 2 * * * root /usr/bin/ignored\n")
|
||||
_write(tmp_path / "etc/cron.d/.placeholder", "")
|
||||
|
||||
jobs = CronBackend(fake_runner, paths).discover([Scope.SYSTEM])
|
||||
by_name = {job.name: job for job in jobs}
|
||||
assert set(by_name) == {"crontab:2", "crontab:3", "backup:1", "cleanup"}
|
||||
|
||||
assert by_name["crontab:2"].scope is Scope.SYSTEM
|
||||
assert by_name["crontab:2"].cron.user == "root"
|
||||
assert by_name["crontab:2"].command.raw == "/usr/bin/hourly"
|
||||
assert by_name["crontab:3"].cron.user == "www-data"
|
||||
assert by_name["backup:1"].managed is False
|
||||
assert by_name["backup:1"].schedule.expression == "0 2 * * *"
|
||||
assert by_name["backup:1"].source.path == paths.cron_d + "/backup"
|
||||
|
||||
managed = by_name["cleanup"]
|
||||
assert managed.managed is True
|
||||
assert managed.cron.user == "deploy"
|
||||
assert managed.schedule.expression == "30 3 * * 0"
|
||||
assert managed.source.path == paths.cron_d + "/schedls-cleanup"
|
||||
|
||||
|
||||
def test_discovers_periodic_directories(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "etc/cron.daily/logrotate", "#!/bin/sh\n", mode=0o755)
|
||||
_write(tmp_path / "etc/cron.daily/README", "text\n", mode=0o644)
|
||||
_write(tmp_path / "etc/cron.hourly/.hidden", "#!/bin/sh\n", mode=0o755)
|
||||
|
||||
jobs = CronBackend(fake_runner, paths).discover([Scope.SYSTEM])
|
||||
assert [job.name for job in jobs] == ["logrotate"]
|
||||
job = jobs[0]
|
||||
assert job.schedule.expression == "@daily"
|
||||
assert job.command.display() == paths.daily + "/logrotate"
|
||||
assert job.cron.user == "root"
|
||||
assert job.managed is False
|
||||
assert "cron.daily" in job.source.detail
|
||||
|
||||
|
||||
def test_spool_crontabs_require_root(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "var/spool/cron/crontabs/alice", "5 4 * * * /usr/bin/alice\n")
|
||||
_write(tmp_path / "var/spool/cron/crontabs/root", "5 4 * * * /usr/bin/root\n")
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 1000)
|
||||
assert backend.discover([Scope.USER]) == []
|
||||
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 0)
|
||||
monkeypatch.setattr(CronBackend, "_current_user", staticmethod(lambda: "root"))
|
||||
jobs = backend.discover([Scope.USER])
|
||||
assert [job.name for job in jobs] == ["alice:1"]
|
||||
assert jobs[0].scope is Scope.USER
|
||||
assert jobs[0].cron.user == "alice"
|
||||
assert jobs[0].source.path.endswith("crontabs/alice")
|
||||
|
||||
|
||||
def test_spool_falls_back_to_flat_layout(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "var/spool/cron/alice", "5 4 * * * /usr/bin/alice\n")
|
||||
(tmp_path / "var/spool/cron/atjobs").mkdir(parents=True)
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 0)
|
||||
monkeypatch.setattr(CronBackend, "_current_user", staticmethod(lambda: "root"))
|
||||
|
||||
jobs = CronBackend(fake_runner, paths).discover([Scope.USER])
|
||||
assert [job.name for job in jobs] == ["alice:1"]
|
||||
|
||||
|
||||
def test_malformed_drop_in_is_unmanaged(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "etc/cron.d/schedls-broken", "0 2 * * * root /bin/true\n")
|
||||
|
||||
jobs = CronBackend(fake_runner, paths).discover([Scope.SYSTEM])
|
||||
assert [job.name for job in jobs] == ["schedls-broken:1"]
|
||||
assert jobs[0].managed is False
|
||||
assert jobs[0].warnings
|
||||
|
||||
|
||||
def _system_spec(**overrides) -> JobSpec:
|
||||
values = {
|
||||
"name": "backup",
|
||||
"backend": Backend.CRON,
|
||||
"scope": Scope.SYSTEM,
|
||||
"command": Command(argv=("/usr/bin/backup", "/srv/data")),
|
||||
"cron_expression": "0 2 * * *",
|
||||
"run_as": "www-data",
|
||||
}
|
||||
values.update(overrides)
|
||||
return JobSpec(**values)
|
||||
|
||||
|
||||
def test_plan_create_system_cron(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
(tmp_path / "etc/cron.d").mkdir(parents=True, exist_ok=True)
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 0)
|
||||
monkeypatch.setattr("schedls.backends.cron.check_trusted_directory", lambda path, *, expected_uid: None)
|
||||
|
||||
plan = CronBackend(fake_runner, paths).plan_create(_system_spec())
|
||||
assert plan.payload["mode"] == "file"
|
||||
assert plan.payload["expected_uid"] == 0
|
||||
assert not plan.commands
|
||||
change = plan.files[0]
|
||||
assert change.path == paths.cron_d + "/schedls-backup"
|
||||
assert "0 2 * * * www-data /usr/bin/backup /srv/data" in change.content
|
||||
assert "# schedls:begin name=backup" in change.content
|
||||
assert ("Run as", "www-data") in plan.summary
|
||||
|
||||
|
||||
def test_plan_create_system_cron_requires_root(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 1000)
|
||||
with pytest.raises(SafetyRefusalError):
|
||||
CronBackend(fake_runner, paths).plan_create(_system_spec())
|
||||
|
||||
|
||||
def test_plan_create_system_cron_rejects_dotted_name(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 0)
|
||||
with pytest.raises(InvalidScheduleError):
|
||||
CronBackend(fake_runner, paths).plan_create(_system_spec(name="my.job"))
|
||||
|
||||
|
||||
def test_plan_create_system_cron_conflict(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "etc/cron.d/schedls-backup", "0 2 * * * root /bin/true\n")
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 0)
|
||||
monkeypatch.setattr("schedls.backends.cron.check_trusted_directory", lambda path, *, expected_uid: None)
|
||||
with pytest.raises(ConflictError):
|
||||
CronBackend(fake_runner, paths).plan_create(_system_spec())
|
||||
|
||||
|
||||
def test_apply_file_mode_create_and_remove(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
(tmp_path / "etc/cron.d").mkdir(parents=True, exist_ok=True)
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
uid = os.getuid()
|
||||
path = tmp_path / "etc/cron.d/schedls-backup"
|
||||
content = renderer.render_system_file("backup", ["0 2 * * * root /usr/bin/backup"])
|
||||
|
||||
create = Plan(backend="cron", action="create")
|
||||
create.files = [FileChange(path=str(path), content=content, mode=0o644, expected_uid=uid)]
|
||||
create.payload = {
|
||||
"mode": "file",
|
||||
"action": "create",
|
||||
"name": "backup",
|
||||
"path": str(path),
|
||||
"expected_uid": uid,
|
||||
"snapshots": {str(path): None},
|
||||
}
|
||||
result = backend.apply(create)
|
||||
assert result.changed
|
||||
assert path.read_text() == content
|
||||
|
||||
remove = Plan(backend="cron", action="remove")
|
||||
remove.files = [FileChange(path=str(path), content=None, expected_uid=uid)]
|
||||
remove.payload = {
|
||||
"mode": "file",
|
||||
"action": "remove",
|
||||
"name": "backup",
|
||||
"path": str(path),
|
||||
"expected_uid": uid,
|
||||
"snapshots": {str(path): content},
|
||||
}
|
||||
result = backend.apply(remove)
|
||||
assert result.changed
|
||||
assert not path.exists()
|
||||
|
||||
|
||||
def test_apply_file_mode_refuses_changed_snapshot(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "etc/cron.d/schedls-backup", "changed\n")
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
path = tmp_path / "etc/cron.d/schedls-backup"
|
||||
|
||||
plan = Plan(backend="cron", action="remove")
|
||||
plan.files = [FileChange(path=str(path), content=None, expected_uid=os.getuid())]
|
||||
plan.payload = {
|
||||
"mode": "file",
|
||||
"action": "remove",
|
||||
"name": "backup",
|
||||
"path": str(path),
|
||||
"expected_uid": os.getuid(),
|
||||
"snapshots": {str(path): "expected\n"},
|
||||
}
|
||||
with pytest.raises(SafetyRefusalError):
|
||||
backend.apply(plan)
|
||||
assert path.exists()
|
||||
|
||||
|
||||
def test_apply_file_mode_rolls_back_on_failure(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
(tmp_path / "etc/cron.d").mkdir(parents=True, exist_ok=True)
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
uid = os.getuid()
|
||||
first = tmp_path / "etc/cron.d/schedls-one"
|
||||
second = tmp_path / "etc/cron.d/schedls-two"
|
||||
_write(second, "conflict\n")
|
||||
|
||||
plan = Plan(backend="cron", action="create")
|
||||
plan.files = [
|
||||
FileChange(path=str(first), content=renderer.render_system_file("one", ["0 2 * * * root /bin/true"])),
|
||||
FileChange(path=str(second), content=renderer.render_system_file("two", ["0 3 * * * root /bin/true"])),
|
||||
]
|
||||
plan.payload = {
|
||||
"mode": "file",
|
||||
"action": "create",
|
||||
"name": "one",
|
||||
"path": str(first),
|
||||
"expected_uid": uid,
|
||||
"snapshots": {str(first): None, str(second): None},
|
||||
}
|
||||
with pytest.raises(SafetyRefusalError):
|
||||
backend.apply(plan)
|
||||
assert not first.exists()
|
||||
|
||||
|
||||
def test_plan_remove_system_cron(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(
|
||||
tmp_path / "etc/cron.d/schedls-cleanup",
|
||||
renderer.render_system_file("cleanup", ["30 3 * * 0 root /usr/bin/cleanup"]),
|
||||
)
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
job = backend.discover([Scope.SYSTEM])[0]
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 0)
|
||||
|
||||
plan = backend.plan_remove(job)
|
||||
assert plan.payload["mode"] == "file"
|
||||
assert plan.files[0].path == paths.cron_d + "/schedls-cleanup"
|
||||
assert plan.files[0].content is None
|
||||
|
||||
|
||||
def test_plan_remove_system_cron_requires_root(tmp_path, fake_runner, monkeypatch) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(
|
||||
tmp_path / "etc/cron.d/schedls-cleanup",
|
||||
renderer.render_system_file("cleanup", ["30 3 * * 0 root /usr/bin/cleanup"]),
|
||||
)
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
job = backend.discover([Scope.SYSTEM])[0]
|
||||
monkeypatch.setattr(os, "geteuid", lambda: 1000)
|
||||
with pytest.raises(SafetyRefusalError):
|
||||
backend.plan_remove(job)
|
||||
|
||||
|
||||
def test_plan_remove_refuses_unmanaged(tmp_path, fake_runner) -> None:
|
||||
paths = _make_paths(tmp_path)
|
||||
_write(tmp_path / "etc/cron.d/backup", "0 2 * * * root /usr/bin/backup\n")
|
||||
backend = CronBackend(fake_runner, paths)
|
||||
job = backend.discover([Scope.SYSTEM])[0]
|
||||
with pytest.raises(SafetyRefusalError):
|
||||
backend.plan_remove(job)
|
||||
@@ -93,3 +93,48 @@ def test_empty_crontab() -> None:
|
||||
updated = document.with_block("x", ["0 0 * * * /bin/true"])
|
||||
assert updated.endswith("\n")
|
||||
assert "name=x" in updated
|
||||
|
||||
|
||||
SYSTEM_SAMPLE = (
|
||||
"SHELL=/bin/sh\n"
|
||||
"PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin\n"
|
||||
"\n"
|
||||
"# system jobs\n"
|
||||
"17 * * * * root cd / && run-parts --report /etc/cron.hourly\n"
|
||||
"@daily www-data /usr/local/bin/report\n"
|
||||
"\n"
|
||||
"# schedls:begin name=cleanup\n"
|
||||
"30 3 * * 0 root /usr/local/bin/cleanup\n"
|
||||
"# schedls:end name=cleanup\n"
|
||||
)
|
||||
|
||||
|
||||
def test_system_crontab_parses_user_field() -> None:
|
||||
document = CrontabDocument(SYSTEM_SAMPLE, system=True)
|
||||
jobs = [entry for entry in document.entries if entry.kind == "job"]
|
||||
assert jobs[0].expression == "17 * * * *"
|
||||
assert jobs[0].user == "root"
|
||||
assert jobs[0].command == "cd / && run-parts --report /etc/cron.hourly"
|
||||
assert jobs[1].expression == "@daily"
|
||||
assert jobs[1].user == "www-data"
|
||||
assert jobs[1].command == "/usr/local/bin/report"
|
||||
|
||||
|
||||
def test_system_crontab_detects_managed_block() -> None:
|
||||
document = CrontabDocument(SYSTEM_SAMPLE, system=True)
|
||||
block = document.find_block("cleanup")
|
||||
assert block is not None
|
||||
assert block.job_lines[0].user == "root"
|
||||
assert document.render() == SYSTEM_SAMPLE
|
||||
|
||||
|
||||
def test_user_crontab_still_parses_five_fields() -> None:
|
||||
document = CrontabDocument("15 7 * * * ~/bin/foo\n", system=False)
|
||||
assert document.entries[0].expression == "15 7 * * *"
|
||||
assert document.entries[0].user is None
|
||||
assert document.entries[0].command == "~/bin/foo"
|
||||
|
||||
|
||||
def test_system_entry_without_user_is_not_a_job() -> None:
|
||||
document = CrontabDocument("15 7 * * * ~/bin/foo\n", system=True)
|
||||
assert document.entries[0].kind == "comment"
|
||||
@@ -150,12 +150,13 @@ def test_eof_raises_confirmation_required() -> None:
|
||||
def test_wizard_new_systemd(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "-i"])
|
||||
answers = ["backup", "", "n", "/usr/local/bin/backup /srv/data", "", "02:00", "n"]
|
||||
answers = ["backup", "", "", "n", "/usr/local/bin/backup /srv/data", "", "02:00", "n"]
|
||||
prompter, _ = make_prompter(answers)
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.name == "backup"
|
||||
assert spec.backend is Backend.SYSTEMD
|
||||
assert spec.scope is Scope.USER
|
||||
assert spec.calendar == ("*-*-* 02:00:00",)
|
||||
assert spec.command.argv == ("/usr/local/bin/backup", "/srv/data")
|
||||
|
||||
@@ -163,17 +164,19 @@ def test_wizard_new_systemd(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
def test_wizard_new_cron(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "-i"])
|
||||
answers = ["cleanup", "2", "n", "/usr/local/bin/cleanup", "", "04:00"]
|
||||
answers = ["cleanup", "2", "", "n", "/usr/local/bin/cleanup", "", "04:00"]
|
||||
prompter, _ = make_prompter(answers)
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.backend is Backend.CRON
|
||||
assert spec.scope is Scope.USER
|
||||
assert spec.cron_expression == "0 4 * * *"
|
||||
assert spec.run_as is None
|
||||
|
||||
|
||||
def test_wizard_new_skips_provided_fields(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "backup", "-i", "--timer", "--daily", "02:00"])
|
||||
args = build_parser().parse_args(["new", "backup", "-i", "--timer", "--daily", "02:00", "--user"])
|
||||
prompter, fake = make_prompter(["n"])
|
||||
args, tail = _wizard_new(args, ["/bin/true"], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
@@ -181,6 +184,71 @@ def test_wizard_new_skips_provided_fields(monkeypatch: pytest.MonkeyPatch) -> No
|
||||
assert fake.prompts == ["Set advanced timer options? [y/N] "]
|
||||
|
||||
|
||||
def test_wizard_new_scope_prompt_defaults_to_user(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "cleanup", "-i", "--cron", "--daily", "04:00"])
|
||||
prompter, fake = make_prompter(["", "n", "/usr/local/bin/cleanup"])
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.scope is Scope.USER
|
||||
assert spec.run_as is None
|
||||
assert any(prompt.startswith("Scope:") for prompt in fake.prompts)
|
||||
assert not any(prompt.startswith("Run as user") for prompt in fake.prompts)
|
||||
|
||||
|
||||
def test_wizard_new_system_cron_prompts_run_as(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "report", "-i", "--cron", "--daily", "04:00"])
|
||||
prompter, _ = make_prompter(["2", "www-data", "n", "/usr/local/bin/report"])
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.scope is Scope.SYSTEM
|
||||
assert spec.run_as == "www-data"
|
||||
|
||||
|
||||
def test_wizard_new_system_cron_run_as_defaults_to_root(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "report", "-i", "--cron", "--daily", "04:00"])
|
||||
prompter, _ = make_prompter(["2", "", "n", "/usr/local/bin/report"])
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.scope is Scope.SYSTEM
|
||||
assert spec.run_as == "root"
|
||||
|
||||
|
||||
def test_wizard_new_system_cron_run_as_reprompts(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "report", "-i", "--cron", "--daily", "04:00"])
|
||||
prompter, fake = make_prompter(["2", "bad user", "www-data", "n", "/usr/local/bin/report"])
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.run_as == "www-data"
|
||||
assert fake.prompts.count("Run as user [root]: ") == 2
|
||||
|
||||
|
||||
def test_wizard_new_provided_scope_and_run_as_are_not_reasked(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(
|
||||
["new", "report", "-i", "--cron", "--system", "--run-as", "root", "--daily", "04:00"]
|
||||
)
|
||||
prompter, fake = make_prompter(["n", "/usr/local/bin/report"])
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.scope is Scope.SYSTEM
|
||||
assert spec.run_as == "root"
|
||||
assert all("Scope:" not in prompt and "Run as user" not in prompt for prompt in fake.prompts)
|
||||
|
||||
|
||||
def test_wizard_new_systemd_scope_prompt(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
args = build_parser().parse_args(["new", "backup", "-i", "--timer", "--daily", "02:00"])
|
||||
prompter, _ = make_prompter(["2", "n", "/usr/local/bin/backup", "n"])
|
||||
args, tail = _wizard_new(args, [], prompter)
|
||||
spec = _spec_for_new(args, tail)
|
||||
assert spec.scope is Scope.SYSTEM
|
||||
assert spec.calendar == ("*-*-* 02:00:00",)
|
||||
|
||||
|
||||
def test_wizard_edit_changes_schedule(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("sys.stdin", FakeStdin(isatty=True))
|
||||
job = _systemd_job()
|
||||
|
||||
@@ -125,5 +125,38 @@ def test_managed_block() -> None:
|
||||
assert block == ("# schedls:begin name=backup\n0 2 * * * /bin/true\n# schedls:end name=backup\n")
|
||||
|
||||
|
||||
def test_render_system_line() -> None:
|
||||
line = renderer.render_system_line("0 2 * * *", Command(argv=("/bin/echo", "100%")), "www-data")
|
||||
assert line == "0 2 * * * www-data /bin/echo 100\\%"
|
||||
|
||||
|
||||
def test_render_system_file_has_marker() -> None:
|
||||
content = renderer.render_system_file("backup", ["0 2 * * * root /bin/true"])
|
||||
assert content.startswith("# Managed by schedls\n# schedls:begin name=backup\n")
|
||||
assert content.endswith("# schedls:end name=backup\n")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["backup", "my-job", "job_1"])
|
||||
def test_system_name_accepts(name: str) -> None:
|
||||
assert renderer.validate_system_name(name) == name
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["my.job", ".hidden", "-leading", "with space", ""])
|
||||
def test_system_name_rejects(name: str) -> None:
|
||||
with pytest.raises(InvalidScheduleError):
|
||||
renderer.validate_system_name(name)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("user", ["root", "www-data", "nginx", "user.name", "_svc"])
|
||||
def test_run_as_accepts(user: str) -> None:
|
||||
assert renderer.validate_run_as(user) == user
|
||||
|
||||
|
||||
@pytest.mark.parametrize("user", ["", "-root", "a b", "a;b", "a$b", "x" * 33])
|
||||
def test_run_as_rejects(user: str) -> None:
|
||||
with pytest.raises(InvalidScheduleError):
|
||||
renderer.validate_run_as(user)
|
||||
|
||||
|
||||
def test_shlex_reference() -> None:
|
||||
assert shlex.quote("a b") == "'a b'"
|
||||
Reference in new issue
Block a user