feat: add Docker MTU apply support and docker-ordered persistence

https://chatgpt.com/share/69733e45-96ec-800f-9aad-0cac7306dedd
This commit is contained in:
2026-01-23 10:53:29 +01:00
parent f534d025d0
commit fc5dee4dc0
11 changed files with 432 additions and 101 deletions

View File

@@ -23,7 +23,7 @@ class TestPersist(unittest.TestCase):
)
def test_strip_persist_args_removes_uninstall(self) -> None:
argv = ["automtu", "--persist", "systemd", "--uninstall", "--apply-wg-mtu"]
argv = ["automtu", "--persist", "docker", "--uninstall", "--apply-wg-mtu"]
self.assertEqual(
persist._strip_persist_args(argv),
["automtu", "--apply-wg-mtu"],
@@ -52,6 +52,43 @@ class TestPersist(unittest.TestCase):
"ExecStart=/usr/bin/automtu --auto-pmtu-from-wg --apply-wg-mtu", s
)
def test_persist_systemd_adds_docker_ordering_if_apply_docker_mtu_present(
self,
) -> None:
argv = ["automtu", "--apply-docker-mtu", "--persist", "systemd"]
with (
patch("automtu.persist.shutil.which", return_value="/usr/bin/automtu"),
patch("automtu.persist._SYSTEMD_UNIT_PATH", Path("/tmp/automtu.service")),
):
out = io.StringIO()
with redirect_stdout(out):
persist.persist_systemd(argv, dry=True)
s = out.getvalue()
self.assertIn("After=network-online.target docker.service", s)
self.assertIn("Wants=network-online.target docker.service", s)
def test_persist_docker_dry_run_prints_unit_with_docker_ordering(self) -> None:
argv = ["automtu", "--dry-run", "--persist", "docker"]
with (
patch("automtu.persist.shutil.which", return_value="/usr/bin/automtu"),
patch(
"automtu.persist._DOCKER_SYSTEMD_UNIT_PATH",
Path("/tmp/automtu-docker.service"),
),
):
out = io.StringIO()
with redirect_stdout(out):
persist.persist_docker(argv, dry=True)
s = out.getvalue()
self.assertIn("DRY-RUN", s)
self.assertIn("After=network-online.target docker.service", s)
self.assertIn("Wants=network-online.target docker.service", s)
self.assertIn("ExecStart=/usr/bin/automtu --dry-run", s)
def test_uninstall_systemd_dry_run_prints_actions(self) -> None:
with patch("automtu.persist._SYSTEMD_UNIT_PATH", Path("/tmp/automtu.service")):
out = io.StringIO()
@@ -62,52 +99,18 @@ class TestPersist(unittest.TestCase):
self.assertIn("DRY-RUN", s)
self.assertIn("systemctl disable", s)
def test_uninstall_systemd_runs_disable_and_removes_unit_when_present(self) -> None:
fake_path = Path("/tmp/automtu.service")
calls = []
def fake_run(cmd, check=False): # type: ignore[no-untyped-def]
calls.append((tuple(cmd), bool(check)))
with (
patch("automtu.persist._SYSTEMD_UNIT_PATH", fake_path),
patch("automtu.persist.subprocess.run", side_effect=fake_run),
patch.object(Path, "exists", return_value=True),
patch.object(Path, "unlink", return_value=None),
def test_uninstall_docker_dry_run_prints_actions(self) -> None:
with patch(
"automtu.persist._DOCKER_SYSTEMD_UNIT_PATH",
Path("/tmp/automtu-docker.service"),
):
persist.uninstall_systemd(dry=False)
out = io.StringIO()
with redirect_stdout(out):
persist.uninstall_docker(dry=True)
# Must disable unit and reload daemon
self.assertIn((("systemctl", "disable", "automtu.service"), True), calls)
self.assertIn((("systemctl", "daemon-reload"), True), calls)
def test_persist_systemd_writes_unit_and_enables(self) -> None:
argv = ["automtu", "--apply-wg-mtu", "--persist", "systemd"]
writes = {"text": None}
calls = []
def fake_write_text(self, txt, *args, **kwargs): # type: ignore[no-untyped-def]
writes["text"] = txt
return len(txt)
def fake_run(cmd, check=False): # type: ignore[no-untyped-def]
calls.append((tuple(cmd), bool(check)))
with (
patch("automtu.persist.shutil.which", return_value="/usr/bin/automtu"),
patch("automtu.persist._SYSTEMD_UNIT_PATH", Path("/tmp/automtu.service")),
patch.object(Path, "write_text", new=fake_write_text),
patch("automtu.persist.subprocess.run", side_effect=fake_run),
):
persist.persist_systemd(argv, dry=False)
self.assertIsNotNone(writes["text"])
self.assertIn("ExecStart=/usr/bin/automtu --apply-wg-mtu", writes["text"] or "")
self.assertIn((("systemctl", "daemon-reload"), True), calls)
self.assertIn((("systemctl", "enable", "automtu.service"), True), calls)
s = out.getvalue()
self.assertIn("DRY-RUN", s)
self.assertIn("automtu-docker.service", s)
if __name__ == "__main__":