Files
automtu/tests/unit/test_wg.py
Kevin Veen-Birkenbach 6392131577 style: satisfy the widened ruff default rule set
ruff 0.16 enables rules that earlier releases did not, so `ruff check .`
was already failing on HEAD with 35 findings - the CI job installs ruff
unpinned and would have gone red on the next push regardless of this
branch.

All changes are mechanical and behaviour-neutral:

- Optional[X] -> X | None, List[str] -> list[str], dropped the now unused
  typing imports (every module already carries
  `from __future__ import annotations`, and requires-python is >= 3.10).
- Sorted import blocks, `import automtu.x as x` -> `from automtu import x`.
- subprocess.run in pmtu.py and wg.py now passes check=False explicitly.
  That is the parameter default, so the calls behave exactly as before.
- int(round(x)) -> round(x): round() with a single argument already
  returns int.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 18:42:55 +02:00

59 lines
1.9 KiB
Python

import unittest
from unittest.mock import patch
from automtu import wg
class TestWg(unittest.TestCase):
def test_wg_peer_endpoints_from_wg_show(self) -> None:
# wg show wg0 endpoints output
out = "abcde12345\t46.4.224.77:51820\nfffff00000\t[2a01:db8::1]:51820\n"
with (
patch(
"automtu.wg._run",
side_effect=lambda cmd: out if cmd[:3] == ["wg", "show", "wg0"] else "",
),
patch("automtu.wg.iface_exists", return_value=True),
patch("automtu.wg._rc", return_value=0),
):
eps = wg.wg_peer_endpoints("wg0")
self.assertEqual(eps, ["46.4.224.77", "2a01:db8::1"])
def test_wg_peer_endpoints_fallback_showconf(self) -> None:
show_endpoints_empty = ""
showconf = (
"[Interface]\n"
"PrivateKey = x\n"
"[Peer]\n"
"Endpoint = 46.4.224.77:51820\n"
"[Peer]\n"
"Endpoint = [2a01:db8::1]:51820\n"
)
def fake_run(cmd: list[str]) -> str:
if cmd == ["wg", "show", "wg0", "endpoints"]:
return show_endpoints_empty
if cmd == ["wg", "showconf", "wg0"]:
return showconf
return ""
with patch("automtu.wg._run", side_effect=fake_run):
eps = wg.wg_peer_endpoints("wg0")
self.assertEqual(eps, ["46.4.224.77", "2a01:db8::1"])
def test_wg_is_active_uses_iface_exists_and_wg_show_rc(self) -> None:
with (
patch("automtu.wg.iface_exists", return_value=True),
patch("automtu.wg._rc", return_value=0),
):
self.assertTrue(wg.wg_is_active("wg0"))
with patch("automtu.wg.iface_exists", return_value=False):
self.assertFalse(wg.wg_is_active("wg0"))
if __name__ == "__main__":
unittest.main(verbosity=2)