From 65991d0930a007f538c21b1179bfea2697528956 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 12 Nov 2026 19:19:03 +0100 Subject: [PATCH] fix(dhcp): a refused subnet no longer stops the rest of the set 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 --- CHANGELOG.md | 7 +++ napalm_device_types/dhcp.py | 65 ++++++++++++++------ tests/test_dhcp_subnet_diff_apply.py | 90 ++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aedbd0..9d85bc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,13 @@ recorded here; their history is the git log. order: its changes are still tried, and where the server refuses them the `[failed]` line says it was a swap and with which reservations. Everything else is still written and committed, and `DhcpApplyError` follows (#106). +- `apply_dhcp_subnetset` no longer stops at the first subnet the device refuses. As + `apply_dhcp_reservationset` has since 4.35.1, a refused subnet gets a `[failed]` line, + the others are still written, any non-empty diff is committed -- even one whose every + write was refused -- and `DhcpApplyError` follows. Before, the subnets written ahead of + the refused one stayed saved but inactive, and since the device reports them as + configured, no later apply committed them. `DhcpApplyError` takes an optional `kind` + (`"reservation"` by default, `"subnet"` here) that its message names (#113). ## [4.37.0] - 2026-11-12 diff --git a/napalm_device_types/dhcp.py b/napalm_device_types/dhcp.py index abb845b..a3ee82b 100644 --- a/napalm_device_types/dhcp.py +++ b/napalm_device_types/dhcp.py @@ -66,16 +66,18 @@ _SWAP_NOTE = ( class DhcpApplyError(RuntimeError): - """Some reservations of an applied set were refused by the device. + """Some changes of an applied reservation or subnet set were refused by the device. Raised after the rest were written and committed: ``failed`` of ``attempted`` changes did not go through, and each was named in a ``[failed]`` progress line. + ``kind`` is what was applied: ``"reservation"`` or ``"subnet"``. """ - def __init__(self, failed: int, attempted: int) -> None: + def __init__(self, failed: int, attempted: int, kind: str = "reservation") -> None: self.failed = failed self.attempted = attempted - super().__init__(f"{failed} of {attempted} DHCP reservation change(s) were refused") + self.kind = kind + super().__init__(f"{failed} of {attempted} DHCP {kind} change(s) were refused") def normalize_mac(mac: str | None) -> str: @@ -455,24 +457,53 @@ class DhcpServerMixin: As with ``apply_dhcp_reservationset``, an empty diff does **not** commit: reconfiguring the DHCP daemon is too expensive for a no-op. + A subnet the device refuses (say, one that overlaps a subnet it serves) + does not stop the others: it gets a ``[failed]`` line, the rest are + written and committed, and ``DhcpApplyError`` is raised at the end. Any + non-empty diff commits, even one whose every write was refused, so that + writes an earlier, interrupted run saved take effect (#113). + :param desired: The complete desired subnet set. - :yields: Progress lines, one per applied add/update, plus a final - commit line. + :yields: Progress lines, one per applied, updated or refused change, + plus a final commit line. + :raises DhcpApplyError: after the commit, when a change was refused. """ diff = self.diff_dhcp_subnets(desired) - - for subnet in diff["add"]: - self.apply_dhcp_subnet(subnet) - yield f"[add] {subnet['subnet']}" - - for entry in diff["update"]: - self.apply_dhcp_subnet(entry["subnet"], uuid=entry["uuid"]) - fields = ", ".join(entry["changed_fields"]) - yield f"[update] {entry['subnet']['subnet']} ({fields})" - - if not diff["add"] and not diff["update"]: + attempted = len(diff["add"]) + len(diff["update"]) + if not attempted: yield "[commit] no changes" return + changes: list[tuple[DhcpSubnetDict, str | None, str]] = [ + (subnet, None, "") for subnet in diff["add"] + ] + changes += [ + (e["subnet"], e["uuid"], ", ".join(e["changed_fields"])) for e in diff["update"] + ] + added = updated = refused = 0 + + for subnet, uuid, fields in changes: + line, ok = self._write_subnet(subnet, uuid, fields) + refused += not ok + added += ok and uuid is None + updated += ok and uuid is not None + yield line + + # Committed even when every write was refused; see apply_dhcp_reservationset. self.commit_dhcp_subnets() - yield (f"[commit] applied {len(diff['add'])} add(s), {len(diff['update'])} update(s)") + yield f"[commit] applied {added} add(s), {updated} update(s)" + + if refused: + raise DhcpApplyError(refused, attempted, "subnet") + + def _write_subnet( + self, subnet: DhcpSubnetDict, uuid: str | None, fields: str + ) -> tuple[str, bool]: + """Write one subnet; its progress line, and whether the device took it.""" + try: + self.apply_dhcp_subnet(subnet, uuid=uuid) + except Exception as exc: + return f"[failed] {subnet['subnet']}: {exc}", False + if uuid is None: + return f"[add] {subnet['subnet']}", True + return f"[update] {subnet['subnet']} ({fields})", True diff --git a/tests/test_dhcp_subnet_diff_apply.py b/tests/test_dhcp_subnet_diff_apply.py index 39d3f1c..48cf7a9 100644 --- a/tests/test_dhcp_subnet_diff_apply.py +++ b/tests/test_dhcp_subnet_diff_apply.py @@ -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 -- 2.54.1