81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
|
|
"""Юнит-тесты парсеров на фикстурах (без сервера)."""
|
||
|
|
from app.core.cron_svc import parse_crontab, humanize, render, CronLine
|
||
|
|
from app.core.packages import Package, split_packages
|
||
|
|
|
||
|
|
|
||
|
|
# ---------- cron ----------
|
||
|
|
|
||
|
|
CRONTAB_FIXTURE = """\
|
||
|
|
# комментарий
|
||
|
|
SHELL=/bin/bash
|
||
|
|
0 3 * * * /usr/bin/backup.sh --full
|
||
|
|
# 30 4 * * 0 /usr/bin/weekly.sh
|
||
|
|
@daily /home/dimon/cleanup.sh
|
||
|
|
*/15 * * * * curl -s https://example.com/ping
|
||
|
|
not a cron line
|
||
|
|
"""
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_enabled_entry():
|
||
|
|
lines = parse_crontab(CRONTAB_FIXTURE)
|
||
|
|
entry = next(l for l in lines if "backup.sh" in l.raw)
|
||
|
|
assert entry.is_entry and entry.enabled
|
||
|
|
assert entry.schedule == "0 3 * * *"
|
||
|
|
assert entry.command == "/usr/bin/backup.sh --full"
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_disabled_entry():
|
||
|
|
lines = parse_crontab(CRONTAB_FIXTURE)
|
||
|
|
entry = next(l for l in lines if "weekly.sh" in l.raw)
|
||
|
|
assert entry.is_entry and not entry.enabled
|
||
|
|
assert entry.schedule == "30 4 * * 0"
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_special_at():
|
||
|
|
lines = parse_crontab(CRONTAB_FIXTURE)
|
||
|
|
entry = next(l for l in lines if "cleanup.sh" in l.raw)
|
||
|
|
assert entry.is_entry and entry.schedule == "@daily"
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_comment_not_entry():
|
||
|
|
lines = parse_crontab(CRONTAB_FIXTURE)
|
||
|
|
assert lines[0].comment == "# комментарий" or not lines[0].is_entry
|
||
|
|
non_entry = [l for l in lines if not l.is_entry]
|
||
|
|
assert any("SHELL" in l.raw for l in non_entry)
|
||
|
|
|
||
|
|
|
||
|
|
def test_render_roundtrip():
|
||
|
|
lines = parse_crontab(CRONTAB_FIXTURE)
|
||
|
|
out = render(lines)
|
||
|
|
lines2 = parse_crontab(out)
|
||
|
|
entries1 = [(l.schedule, l.command, l.enabled) for l in lines if l.is_entry]
|
||
|
|
entries2 = [(l.schedule, l.command, l.enabled) for l in lines2 if l.is_entry]
|
||
|
|
assert entries1 == entries2
|
||
|
|
|
||
|
|
|
||
|
|
def test_humanize_daily():
|
||
|
|
assert humanize("@daily") == "ежедневно"
|
||
|
|
|
||
|
|
|
||
|
|
def test_humanize_valid_cron():
|
||
|
|
assert "следующий запуск" in humanize("0 3 * * *")
|
||
|
|
|
||
|
|
|
||
|
|
def test_humanize_invalid():
|
||
|
|
assert humanize("99 99 * * *") in ("неверное выражение",)
|
||
|
|
|
||
|
|
|
||
|
|
# ---------- packages ----------
|
||
|
|
|
||
|
|
def test_split_packages():
|
||
|
|
pkgs = [
|
||
|
|
Package("bash", "5.2", "required", True),
|
||
|
|
Package("dpkg", "1.22", "important", True),
|
||
|
|
Package("nginx", "1.24", "optional", True),
|
||
|
|
Package("libnginx-mod-http", "1.24", "optional", False),
|
||
|
|
]
|
||
|
|
res = split_packages(pkgs)
|
||
|
|
assert {p.name for p in res["system"]} == {"bash", "dpkg"}
|
||
|
|
assert {p.name for p in res["user"]} == {"nginx"}
|
||
|
|
assert {p.name for p in res["deps"]} == {"libnginx-mod-http"}
|