2026-05-19 05:08:37 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
|
|
from django.core.management import call_command
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
|
|
from pobsync_backend.config_source import DjangoConfigSource
|
2026-05-19 05:14:29 +02:00
|
|
|
from pobsync_backend.models import GlobalConfig, HostConfig, ScheduleConfig
|
2026-05-19 05:08:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigureCommandsTests(TestCase):
|
|
|
|
|
def test_configure_global_creates_structured_config(self) -> None:
|
|
|
|
|
out = StringIO()
|
|
|
|
|
|
|
|
|
|
call_command(
|
|
|
|
|
"configure_pobsync_global",
|
|
|
|
|
backup_root="/backups",
|
|
|
|
|
retention="daily=3,weekly=2,monthly=1,yearly=0",
|
|
|
|
|
stdout=out,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
config = GlobalConfig.objects.get(name="default")
|
|
|
|
|
self.assertEqual(config.backup_root, "/backups")
|
|
|
|
|
self.assertEqual(config.retention_daily, 3)
|
2026-05-21 02:52:42 +02:00
|
|
|
self.assertIn("Created global config", out.getvalue())
|
2026-05-19 05:08:37 +02:00
|
|
|
|
|
|
|
|
def test_configure_host_uses_global_retention_defaults(self) -> None:
|
|
|
|
|
GlobalConfig.objects.create(
|
|
|
|
|
name="default",
|
|
|
|
|
backup_root="/backups",
|
|
|
|
|
retention_daily=5,
|
|
|
|
|
retention_weekly=4,
|
|
|
|
|
retention_monthly=3,
|
|
|
|
|
retention_yearly=2,
|
|
|
|
|
)
|
|
|
|
|
out = StringIO()
|
|
|
|
|
|
|
|
|
|
call_command(
|
|
|
|
|
"configure_pobsync_host",
|
|
|
|
|
"web-01",
|
|
|
|
|
address="web-01.example.test",
|
|
|
|
|
exclude_add=["/tmp/***"],
|
|
|
|
|
rsync_extra_arg=["--delete"],
|
|
|
|
|
stdout=out,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
host = HostConfig.objects.get(host="web-01")
|
|
|
|
|
self.assertEqual(host.retention_daily, 5)
|
|
|
|
|
self.assertEqual(host.excludes_add, ["/tmp/***"])
|
|
|
|
|
self.assertEqual(host.rsync_extra_args, ["--delete"])
|
|
|
|
|
|
|
|
|
|
effective = DjangoConfigSource().effective_config_for_host("web-01")
|
|
|
|
|
self.assertEqual(effective["retention"]["yearly"], 2)
|
|
|
|
|
self.assertEqual(effective["excludes_effective"], ["/tmp/***"])
|
2026-05-19 05:14:29 +02:00
|
|
|
|
|
|
|
|
def test_configure_schedule_creates_sql_schedule(self) -> None:
|
|
|
|
|
host = HostConfig.objects.create(host="web-01", address="web-01.example.test")
|
|
|
|
|
out = StringIO()
|
|
|
|
|
|
|
|
|
|
call_command(
|
|
|
|
|
"configure_pobsync_schedule",
|
|
|
|
|
host.host,
|
2026-05-21 02:52:42 +02:00
|
|
|
schedule_expression="15 2 * * *",
|
2026-05-19 05:14:29 +02:00
|
|
|
prune=True,
|
|
|
|
|
stdout=out,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
schedule = ScheduleConfig.objects.get(host=host)
|
|
|
|
|
self.assertEqual(schedule.cron_expr, "15 2 * * *")
|
|
|
|
|
self.assertTrue(schedule.prune)
|