fix(dns): raise when the DNS service detection fails

_detect_active_dns caught every error from /api/core/service/search and
returned None, so get_dns_entries() reported a transient API error as an
empty list (no DNS service) and sync_dns_zone() blamed the DNS service.
Let the request error propagate; None only when the list was read and
neither Unbound nor Dnsmasq runs.

Closes #39
This commit is contained in:
Christian Manivong committed 2026-11-02 08:35:49 +01:00
1 parent 91a17b54a8
commit 901df1b523
3 files changed
+54 -10

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 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 added ahead of another no longer renumbers it (#32). Every WireGuard key changes
once with this release. 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 ## [0.5.0] - 2026-10-23
+12 -10
View File
@@ -2794,15 +2794,16 @@ class OPNsenseDriver(
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def _detect_active_dns(self) -> str | None: def _detect_active_dns(self) -> str | None:
"""Return 'unbound' or 'dnsmasq' depending on which service is running.""" """Return 'unbound' or 'dnsmasq' depending on which service is running.
try:
data = self._get("/api/core/service/search") None only when the service list was read and neither runs; a failed
for row in data.get("rows", []): read raises, so it cannot pass for "no DNS service".
name = (row.get("name") or "").lower() """
if name in ("unbound", "dnsmasq") and row.get("running"): data = self._get("/api/core/service/search")
return name for row in data.get("rows", []):
except Exception as exc: name = (row.get("name") or "").lower()
logger.warning("DNS service detection failed: %s", exc) if name in ("unbound", "dnsmasq") and row.get("running"):
return name
return None return None
def get_dns_entries(self) -> list[dict[str, Any]]: 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 Checks whether Unbound (DNS Resolver) or Dnsmasq (DNS Forwarder) is
running and returns host overrides from whichever is active. If neither 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:: Each entry::
+37
View File
@@ -3005,6 +3005,43 @@ class TestHostOverrideReadFailure:
driver.get_dns_entries() 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 # uninstall_package – says what it cannot do
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------