fix(dhcp): a refused subnet no longer stops the rest of the set
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m19s
driver / test (3.12) (pull_request) Successful in 1m32s
driver / test (3.13) (pull_request) Successful in 1m16s
driver / lowest (pull_request) Successful in 41s
driver / build (pull_request) Successful in 35s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 3m15s
driver / publish (pull_request) Skipped

apply_dhcp_subnetset called apply_dhcp_subnet for each add and update
and committed only at the end, so one refused write raised before the
commit: the subnets written ahead of it stayed saved but inactive, and
as the device reports them as configured, no later diff committed them.

It now behaves like apply_dhcp_reservationset since 4.35.1: a refused
subnet gets a [failed] line, the others are written, any non-empty diff
is committed, then DhcpApplyError is raised. DhcpApplyError takes an
optional kind ("reservation" by default) for its message.

Closes #113
This commit is contained in:
christianmanivong committed 2026-11-12 19:19:03 +01:00
1 parent 9dfb5596bc
commit 65991d0930
3 files changed
+145 -17

No files matched your search

+90
View File
@@ -16,6 +16,7 @@ from typing import Any
import pytest
from napalm_device_types import DhcpServerMixin
from napalm_device_types.dhcp import DhcpApplyError
from napalm_device_types.models import DhcpSubnetDict
@@ -235,3 +236,92 @@ def test_calling_a_missing_device_method_fails_loudly() -> None:
def test_a_subnet_that_cannot_name_one_raises(desired):
with pytest.raises(ValueError):
_FakeDhcpServer([]).diff_dhcp_subnets(desired)
# ── apply: a refused subnet does not hold back the others (#113) ──────────────
class _RefusingDhcpServer(_FakeDhcpServer):
"""A server that refuses the subnets in *refused*, as Kea refuses one that
overlaps a subnet it already serves."""
def __init__(self, live: list[DhcpSubnetDict] | None = None, *, refused: set[str]) -> None:
super().__init__(live)
self.refused = refused
def apply_dhcp_subnet(
self, subnet: DhcpSubnetDict, *, uuid: str | None = None
) -> dict[str, Any]:
if subnet["subnet"] in self.refused:
raise RuntimeError(f"{subnet['subnet']} overlaps a subnet the server has")
return super().apply_dhcp_subnet(subnet, uuid=uuid)
def _three_subnets() -> list[DhcpSubnetDict]:
return [
_subnet(subnet="192.0.2.0/26"),
_subnet(subnet="192.0.2.64/26"),
_subnet(subnet="192.0.2.128/26"),
]
def test_a_refused_subnet_does_not_stop_the_others() -> None:
fake = _RefusingDhcpServer(refused={"192.0.2.64/26"})
with pytest.raises(DhcpApplyError):
list(fake.apply_dhcp_subnetset(_three_subnets()))
assert [e["subnet"]["subnet"] for e in fake.applied] == ["192.0.2.0/26", "192.0.2.128/26"]
def test_what_was_written_is_committed() -> None:
fake = _RefusingDhcpServer(refused={"192.0.2.64/26"})
with pytest.raises(DhcpApplyError):
list(fake.apply_dhcp_subnetset(_three_subnets()))
assert fake.commits == 1
def test_a_set_whose_every_write_is_refused_still_commits() -> None:
# A run that stopped before its commit left saved writes behind, which the device
# reports as configured: no later diff would commit them.
fake = _RefusingDhcpServer(refused={"192.0.2.64/26"})
with pytest.raises(DhcpApplyError):
list(fake.apply_dhcp_subnetset([_three_subnets()[1]]))
assert fake.commits == 1
def test_progress_names_the_refused_subnet_and_why() -> None:
fake = _RefusingDhcpServer(
[_subnet(uuid="dev-1", subnet="192.0.2.128/26")], refused={"192.0.2.64/26"}
)
lines: list[str] = []
with pytest.raises(DhcpApplyError) as raised:
for line in fake.apply_dhcp_subnetset(
[*_three_subnets()[:2], _subnet(subnet="192.0.2.128/26", description="Office")]
):
lines.append(line)
assert lines == [
"[add] 192.0.2.0/26",
"[failed] 192.0.2.64/26: 192.0.2.64/26 overlaps a subnet the server has",
"[update] 192.0.2.128/26 (description)",
"[commit] applied 1 add(s), 1 update(s)",
]
assert (raised.value.failed, raised.value.attempted) == (1, 3)
assert str(raised.value) == "1 of 3 DHCP subnet change(s) were refused"
def test_a_refused_update_is_counted_like_an_add() -> None:
live = _subnet(uuid="dev-1", subnet="192.0.2.0/24")
fake = _RefusingDhcpServer([live], refused={"192.0.2.0/24"})
with pytest.raises(DhcpApplyError, match="1 of 1"):
list(fake.apply_dhcp_subnetset([_subnet(subnet="192.0.2.0/24", description="Office")]))
assert fake.applied == []
assert fake.commits == 1