fix: DNS detection and Kea search raise on a failed read, hermetic open() test (#39, #40, #41) #42

Merged
christianmanivong merged 4 commits from fix/bugs-2026-11-02 into main 2026-11-02 08:20:46 +00:00
3 changed files with 54 additions and 10 deletions
Showing only changes of commit 901df1b523 - Show all commits

No files matched your search

+5
View File
@@ -38,6 +38,11 @@ All notable changes to this driver. The format follows
public key (`wireguard-wg0-aBcD1234`, URL-safe) instead of its position, so a peer
added ahead of another no longer renumbers it (#32). Every WireGuard key changes
once with this release.
- `get_dns_entries()` raises when the service list (`/api/core/service/search`)
cannot be read, instead of returning an empty list as if neither Unbound nor
Dnsmasq ran: a transient API error made the firewall look as if it had no DNS
records. `sync_dns_zone()` raises that API error instead of "No active DNS
service (Unbound or Dnsmasq) detected" (#39).
## [0.5.0] - 2026-10-23
+12 -10
View File
@@ -2794,15 +2794,16 @@ class OPNsenseDriver(
# ------------------------------------------------------------------
def _detect_active_dns(self) -> str | None:
"""Return 'unbound' or 'dnsmasq' depending on which service is running."""
try:
data = self._get("/api/core/service/search")
for row in data.get("rows", []):
name = (row.get("name") or "").lower()
if name in ("unbound", "dnsmasq") and row.get("running"):
return name
except Exception as exc:
logger.warning("DNS service detection failed: %s", exc)
"""Return 'unbound' or 'dnsmasq' depending on which service is running.
None only when the service list was read and neither runs; a failed
read raises, so it cannot pass for "no DNS service".
"""
data = self._get("/api/core/service/search")
for row in data.get("rows", []):
name = (row.get("name") or "").lower()
if name in ("unbound", "dnsmasq") and row.get("running"):
return name
return None
def get_dns_entries(self) -> list[dict[str, Any]]:
@@ -2810,7 +2811,8 @@ class OPNsenseDriver(
Checks whether Unbound (DNS Resolver) or Dnsmasq (DNS Forwarder) is
running and returns host overrides from whichever is active. If neither
is running, returns an empty list. A failed read of the overrides raises.
is running, returns an empty list. A failed read of the service list
or of the overrides raises.
Each entry::
+37
View File
@@ -3005,6 +3005,43 @@ class TestHostOverrideReadFailure:
driver.get_dns_entries()
class TestDnsServiceDetection:
"""A failed service search raises: None would say neither Unbound nor Dnsmasq runs (#39)."""
def test_get_dns_entries_raises_when_the_service_search_fails(self, driver):
driver.session.get.side_effect = requests.exceptions.ConnectionError("reset by peer")
with pytest.raises(requests.exceptions.ConnectionError):
driver.get_dns_entries()
driver.session.post.assert_not_called()
def test_sync_dns_zone_reports_the_api_error_and_writes_nothing(self, driver):
driver.session.get.side_effect = requests.exceptions.HTTPError("502 Bad Gateway")
with pytest.raises(requests.exceptions.HTTPError, match="502"):
driver.sync_dns_zone(
"office.local",
[{"hostname": "nas", "ip": "172.22.40.8", "record_type": "A", "enabled": True}],
)
driver.session.post.assert_not_called()
def test_get_dns_entries_is_empty_when_neither_service_runs(self, driver):
driver.session.get.return_value = _make_json_response(
{
"rows": [
{"name": "unbound", "running": 0},
{"name": "dnsmasq", "running": 0},
{"name": "sshd", "running": 1},
]
}
)
assert driver.get_dns_entries() == []
driver.session.post.assert_not_called()
# ---------------------------------------------------------------------------
# uninstall_package – says what it cannot do
# ---------------------------------------------------------------------------