fix(dhcp): raise when the Kea reservation search fails

create_dhcp_reservation searched for an existing reservation so a retry
updates it, but a failed search was logged at debug level and the add
went ahead: a write based on a read that did not happen. Raise a
RuntimeError naming searchReservation before anything is written.

Closes #40
This commit is contained in:
Christian Manivong committed 2026-11-02 08:37:06 +01:00
1 parent 901df1b523
commit 0f730a4b9d
3 files changed
+39 -6

No files matched your search

+27
View File
@@ -2030,6 +2030,33 @@ class TestCreateDhcpReservation:
driver.session.post.assert_not_called()
# The search is what makes a retry update its reservation instead of adding a
# second one for the address: a write must not follow a search that failed (#40).
@pytest.mark.parametrize(
"error",
[
requests.exceptions.HTTPError("500 Server Error"),
requests.exceptions.ConnectionError("reset by peer"),
],
ids=["http-error", "connection-error"],
)
def test_failed_reservation_search_raises_before_any_write(self, driver, error):
driver.session.get.return_value = _make_json_response(KEA_SUBNETS_RESPONSE)
driver.session.post.side_effect = [
error, # searchReservation
_make_json_response({"result": "saved", "uuid": "new-uuid-123"}),
_make_json_response({"status": "ok"}),
]
with pytest.raises(RuntimeError, match="searchReservation") as raised:
driver.create_dhcp_reservation(mac="02:aa:bb:cc:dd:ee", ip="172.22.8.253")
assert raised.value.__cause__ is error
assert [c.args[0] for c in driver.session.post.call_args_list] == [
"https://opnsense.example.com/api/kea/dhcpv4/searchReservation"
]
# ---------------------------------------------------------------------------
# delete_dhcp_reservation_and_lease()