(refactor) Remove YAML config import and export path
Drop the pre-Django YAML import/export management commands and remove the file-based config loader fallback from the backup and retention engines. Keep the runtime config bridge backed by Django models, and add tests that ensure engine operations require an explicit Django config source.
This commit is contained in:
@@ -1,71 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from pobsync.config.load import load_global_config, load_host_config
|
||||
from pobsync_backend.config_repository import export_runtime_configs
|
||||
from pobsync_backend.config_repository import global_config_data, host_config_data
|
||||
from pobsync_backend.models import GlobalConfig, HostConfig
|
||||
|
||||
|
||||
class ConfigRepositoryTests(TestCase):
|
||||
def test_exports_database_configs_to_engine_yaml(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
prefix = Path(tmp)
|
||||
GlobalConfig.objects.create(
|
||||
name="default",
|
||||
backup_root="/backups",
|
||||
pobsync_home=str(prefix),
|
||||
ssh_user="backup",
|
||||
ssh_port=2222,
|
||||
rsync_args=["--archive"],
|
||||
excludes_default=["/proc/***"],
|
||||
retention_daily=7,
|
||||
retention_weekly=4,
|
||||
retention_monthly=3,
|
||||
retention_yearly=1,
|
||||
data={
|
||||
"backup_root": "/ignored",
|
||||
"pobsync_home": "/ignored",
|
||||
"ssh": {"user": "ignored", "port": 22, "options": []},
|
||||
"unknown": "must-not-leak",
|
||||
"retention_defaults": {"daily": 99, "weekly": 99, "monthly": 99, "yearly": 99},
|
||||
},
|
||||
)
|
||||
HostConfig.objects.create(
|
||||
host="web-01",
|
||||
address="web-01.example.test",
|
||||
ssh_user="root",
|
||||
includes=[],
|
||||
excludes_add=["/tmp/***"],
|
||||
retention_daily=7,
|
||||
retention_weekly=4,
|
||||
retention_monthly=3,
|
||||
retention_yearly=1,
|
||||
config={
|
||||
"host": "ignored",
|
||||
"address": "ignored",
|
||||
"retention": {"daily": 99, "weekly": 99, "monthly": 99, "yearly": 99},
|
||||
"excludes_add": ["/ignored/***"],
|
||||
"unknown": "must-not-leak",
|
||||
},
|
||||
)
|
||||
def test_builds_runtime_config_from_database_fields(self) -> None:
|
||||
GlobalConfig.objects.create(
|
||||
name="default",
|
||||
backup_root="/backups",
|
||||
pobsync_home="/var/lib/pobsync",
|
||||
ssh_user="backup",
|
||||
ssh_port=2222,
|
||||
rsync_args=["--archive"],
|
||||
excludes_default=["/proc/***"],
|
||||
retention_daily=7,
|
||||
retention_weekly=4,
|
||||
retention_monthly=3,
|
||||
retention_yearly=1,
|
||||
data={
|
||||
"backup_root": "/ignored",
|
||||
"pobsync_home": "/ignored",
|
||||
"ssh": {"user": "ignored", "port": 22, "options": []},
|
||||
"unknown": "must-not-leak",
|
||||
"retention_defaults": {"daily": 99, "weekly": 99, "monthly": 99, "yearly": 99},
|
||||
},
|
||||
)
|
||||
HostConfig.objects.create(
|
||||
host="web-01",
|
||||
address="web-01.example.test",
|
||||
ssh_user="root",
|
||||
includes=[],
|
||||
excludes_add=["/tmp/***"],
|
||||
retention_daily=7,
|
||||
retention_weekly=4,
|
||||
retention_monthly=3,
|
||||
retention_yearly=1,
|
||||
config={
|
||||
"host": "ignored",
|
||||
"address": "ignored",
|
||||
"retention": {"daily": 99, "weekly": 99, "monthly": 99, "yearly": 99},
|
||||
"excludes_add": ["/ignored/***"],
|
||||
"unknown": "must-not-leak",
|
||||
},
|
||||
)
|
||||
|
||||
written = export_runtime_configs(prefix=prefix, host="web-01")
|
||||
global_cfg = global_config_data()
|
||||
host_cfg = host_config_data("web-01")
|
||||
|
||||
self.assertEqual(len(written), 2)
|
||||
global_cfg = load_global_config(prefix / "config" / "global.yaml")
|
||||
host_cfg = load_host_config(prefix / "config" / "hosts" / "web-01.yaml")
|
||||
self.assertEqual(global_cfg["backup_root"], "/backups")
|
||||
self.assertEqual(global_cfg["pobsync_home"], str(prefix))
|
||||
self.assertEqual(global_cfg["ssh"]["user"], "backup")
|
||||
self.assertEqual(global_cfg["ssh"]["port"], 2222)
|
||||
self.assertEqual(global_cfg["retention_defaults"]["daily"], 7)
|
||||
self.assertEqual(host_cfg["host"], "web-01")
|
||||
self.assertEqual(host_cfg["address"], "web-01.example.test")
|
||||
self.assertEqual(host_cfg["retention"]["daily"], 7)
|
||||
self.assertEqual(host_cfg["excludes_add"], ["/tmp/***"])
|
||||
self.assertNotIn("unknown", global_cfg)
|
||||
self.assertNotIn("unknown", host_cfg)
|
||||
self.assertEqual(global_cfg["backup_root"], "/backups")
|
||||
self.assertEqual(global_cfg["pobsync_home"], "/var/lib/pobsync")
|
||||
self.assertEqual(global_cfg["ssh"]["user"], "backup")
|
||||
self.assertEqual(global_cfg["ssh"]["port"], 2222)
|
||||
self.assertEqual(global_cfg["retention_defaults"]["daily"], 7)
|
||||
self.assertEqual(host_cfg["host"], "web-01")
|
||||
self.assertEqual(host_cfg["address"], "web-01.example.test")
|
||||
self.assertEqual(host_cfg["retention"]["daily"], 7)
|
||||
self.assertEqual(host_cfg["excludes_add"], ["/tmp/***"])
|
||||
self.assertNotIn("unknown", global_cfg)
|
||||
self.assertNotIn("unknown", host_cfg)
|
||||
|
||||
@@ -7,6 +7,7 @@ from tempfile import TemporaryDirectory
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from pobsync.commands.retention_plan import run_retention_plan
|
||||
from pobsync.errors import ConfigError
|
||||
from pobsync.util import write_yaml_atomic
|
||||
|
||||
|
||||
@@ -24,6 +25,15 @@ class FakeConfigSource:
|
||||
|
||||
|
||||
class RetentionConfigSourceTests(SimpleTestCase):
|
||||
def test_retention_plan_requires_explicit_config_source(self) -> None:
|
||||
with self.assertRaisesMessage(ConfigError, "A Django config source is required."):
|
||||
run_retention_plan(
|
||||
prefix=Path("/missing-prefix"),
|
||||
host="web-01",
|
||||
kind="scheduled",
|
||||
protect_bases=False,
|
||||
)
|
||||
|
||||
def test_retention_plan_uses_injected_config_source(self) -> None:
|
||||
with TemporaryDirectory() as tmp:
|
||||
root = Path(tmp) / "backups"
|
||||
|
||||
@@ -7,6 +7,7 @@ from unittest.mock import patch
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from pobsync.commands.run_scheduled import run_scheduled
|
||||
from pobsync.errors import ConfigError
|
||||
from pobsync.rsync import RsyncResult
|
||||
|
||||
|
||||
@@ -34,6 +35,10 @@ class FakeConfigSource:
|
||||
|
||||
|
||||
class RunScheduledConfigSourceTests(SimpleTestCase):
|
||||
def test_requires_explicit_config_source(self) -> None:
|
||||
with self.assertRaisesMessage(ConfigError, "A Django config source is required."):
|
||||
run_scheduled(prefix=Path("/missing-prefix"), host="web-01", dry_run=True)
|
||||
|
||||
def test_dry_run_uses_injected_config_source(self) -> None:
|
||||
with patch("pobsync.commands.run_scheduled.run_rsync") as run_rsync:
|
||||
run_rsync.return_value = RsyncResult(exit_code=0, command=["rsync", "--archive"])
|
||||
|
||||
Reference in New Issue
Block a user