Replace global IntegerKeyValueSetting with per-view CatalogZone model

Track the catalog zone SOA serial per view instead of a single global value.
The new CatalogZone model has a 1:1 FK to netbox_dns.models.View and its own
soa_serial field; each view's catalog zone now increments independently. The
existing global serial is preserved during migration by copying it into every
view's new CatalogZone row.

Change CatalogZoneMemberIdentifier.name from globally unique to unique per
catalog zone (UniqueConstraint(name, catalog_zone)) and add a catalog_zone FK
to enforce it.

Remove the dns-settings management command, which was a generic interface over
the now-removed IntegerKeyValueSetting model.

Increment both catalog zone serials when a zone moves between views: the old
view's catalog zone loses a member and the new view's gains one, so both must
be bumped. Also refresh the member identifier's catalog_zone FK on a view move
so it does not go stale. The pre_save handler now captures _old_view_id and the
post_save handler detects view changes, increments the old view's serial inside
on_commit (before the new view's), and re-creates the identifier.

AI generated, code manually verified.
This commit is contained in:
sven committed 2026-08-17 10:02:59 +02:00
1 parent 4ad110ca53
commit 9f5e40ae48
17 files changed
+429 -240

No files matched your search

+5 -4
View File
@@ -180,11 +180,12 @@ def make_record(db):
@pytest.fixture
def catz_serial(db):
from netbox_dns_bridge.models import IntegerKeyValueSetting
"""Initial SOA serial value (1) for freshly created catalog zones.
IntegerKeyValueSetting.objects.update_or_create(
key="catalog-zone-soa-serial", defaults={"value": 1}
)
A ``CatalogZone`` is auto-created with ``soa_serial=1`` by the zone
post_save signal (via ``update_member_identifier``) whenever a zone is
created. Tests compare against this initial value.
"""
return 1
@@ -8,27 +8,28 @@ from netbox_dns_bridge.management.commands.dns_transfer_endpoint import (
catalog_zone_manager as catzm,
)
from netbox_dns_bridge.models import (
IntegerKeyValueSetting,
CatalogZone,
CatalogZoneMemberIdentifier,
)
@pytest.mark.django_db
class TestIncrementSoaSerial:
def test_creates_setting_if_not_exists(self):
IntegerKeyValueSetting.objects.filter(
key="catalog-zone-soa-serial"
).delete()
result = catzm.increment_soa_serial()
assert result == 2 # created with value=1, then incremented to 2
def test_creates_setting_if_not_exists(self, make_view):
view = make_view(name="cz-create-view")
CatalogZone.objects.filter(view=view).delete()
result = catzm.increment_soa_serial(view)
assert result == 2 # created with soa_serial=1, then incremented to 2
def test_increments_existing(self, catz_serial):
result = catzm.increment_soa_serial()
def test_increments_existing(self, catz_serial, make_view):
view = make_view(name="cz-inc-view")
result = catzm.increment_soa_serial(view)
assert result == catz_serial + 1
def test_consecutive_increments(self, catz_serial):
a = catzm.increment_soa_serial()
b = catzm.increment_soa_serial()
def test_consecutive_increments(self, catz_serial, make_view):
view = make_view(name="cz-cons-view")
a = catzm.increment_soa_serial(view)
b = catzm.increment_soa_serial(view)
assert b == a + 1
def test_wraps_at_max(self, catz_serial):
@@ -37,17 +38,17 @@ class TestIncrementSoaSerial:
from unittest.mock import patch, MagicMock
mock_obj = MagicMock()
mock_obj.value = 0xFFFFFFFF
mock_obj.soa_serial = 0xFFFFFFFF
mock_qs = MagicMock()
mock_qs.select_for_update.return_value.get_or_create.return_value = (
mock_obj, False
)
with patch(
"netbox_dns_bridge.management.commands.dns_transfer_endpoint."
"catalog_zone_manager.IntegerKeyValueSetting.objects",
"catalog_zone_manager.CatalogZone.objects",
mock_qs,
):
result = catzm.increment_soa_serial()
result = catzm.increment_soa_serial(MagicMock())
assert result == 1
def test_wrap_logs_warning(self, catz_serial, caplog):
@@ -55,18 +56,18 @@ class TestIncrementSoaSerial:
from unittest.mock import patch, MagicMock
mock_obj = MagicMock()
mock_obj.value = 0xFFFFFFFF
mock_obj.soa_serial = 0xFFFFFFFF
mock_qs = MagicMock()
mock_qs.select_for_update.return_value.get_or_create.return_value = (
mock_obj, False
)
with patch(
"netbox_dns_bridge.management.commands.dns_transfer_endpoint."
"catalog_zone_manager.IntegerKeyValueSetting.objects",
"catalog_zone_manager.CatalogZone.objects",
mock_qs,
):
with caplog.at_level(logging.WARNING, logger="dns-bridge"):
catzm.increment_soa_serial()
catzm.increment_soa_serial(MagicMock())
assert any("reached max" in r.message for r in caplog.records)
@@ -190,7 +191,7 @@ class TestCreateZone:
assert zone.rdclass == dns.rdataclass.IN
def test_empty_view_returns_zone_with_soa_ns_version(self, make_view):
view = make_view(name="cz-empty-view")
make_view(name="cz-empty-view")
zone = catzm.create_zone("catz", "cz-empty-view")
assert zone is not None
origin = dns.name.from_text("catz", dns.name.root)
@@ -1,4 +1,4 @@
"""Tests for management commands: dns-settings, dns-export-zone, dns-export-zones,
"""Tests for management commands: dns-export-zone, dns-export-zones,
dns-transfer-endpoint."""
import os
import tempfile
@@ -9,60 +9,6 @@ import pytest
from django.core.management import call_command
from django.core.management.base import CommandError
from netbox_dns_bridge.models import IntegerKeyValueSetting
@pytest.mark.django_db
class TestDnsSettingsCommand:
def test_list_empty(self):
IntegerKeyValueSetting.objects.all().delete()
out = StringIO()
call_command("dns-settings", "list", stdout=out)
assert "No settings found" in out.getvalue()
def test_list_with_entries(self):
IntegerKeyValueSetting.objects.create(key="k1", value=10)
IntegerKeyValueSetting.objects.create(key="k2", value=20)
out = StringIO()
call_command("dns-settings", "list", stdout=out)
output = out.getvalue()
assert "k1: 10" in output
assert "k2: 20" in output
def test_get_existing(self):
IntegerKeyValueSetting.objects.create(key="serial", value=42)
out = StringIO()
call_command("dns-settings", "get", "serial", stdout=out)
assert "serial: 42" in out.getvalue()
def test_get_missing_raises(self):
with pytest.raises(CommandError, match="No setting"):
call_command("dns-settings", "get", "nonexistent")
def test_get_without_key_raises(self):
with pytest.raises(CommandError, match="key is required"):
call_command("dns-settings", "get")
def test_set_existing(self):
IntegerKeyValueSetting.objects.create(key="counter", value=5)
out = StringIO()
call_command("dns-settings", "set", "counter", 99, stdout=out)
assert "Updated counter from 5 to 99" in out.getvalue()
assert IntegerKeyValueSetting.objects.get(key="counter").value == 99
def test_set_missing_key_raises(self):
with pytest.raises(CommandError, match="setting is required"):
call_command("dns-settings", "set")
def test_set_without_value_raises(self):
IntegerKeyValueSetting.objects.create(key="needval", value=1)
with pytest.raises(CommandError, match="value is required"):
call_command("dns-settings", "set", "needval")
def test_set_nonexistent_setting_raises(self):
with pytest.raises(CommandError, match="No setting found"):
call_command("dns-settings", "set", "ghost", 1)
@pytest.mark.django_db
class TestDnsExportZoneCommand:
+61 -29
View File
@@ -2,29 +2,46 @@
import pytest
from netbox_dns_bridge.models import (
IntegerKeyValueSetting,
CatalogZone,
CatalogZoneMemberIdentifier,
SeenTransferClients,
)
@pytest.mark.django_db
class TestIntegerKeyValueSetting:
def test_str_representation(self):
obj = IntegerKeyValueSetting.objects.create(key="my-key", value=42)
assert str(obj) == "my-key: 42"
class TestCatalogZone:
def test_str_representation(self, make_view):
view = make_view(name="cz-str-view")
cz = CatalogZone.objects.create(view=view, soa_serial=42)
assert str(cz) == "cz-str-view: 42"
def test_unique_key_constraint(self):
IntegerKeyValueSetting.objects.create(key="dup-key", value=1)
from django.db import IntegrityError
def test_default_soa_serial(self, make_view):
view = make_view(name="cz-default-view")
cz = CatalogZone.objects.create(view=view)
assert cz.soa_serial == 1
with pytest.raises(IntegrityError):
IntegerKeyValueSetting.objects.create(key="dup-key", value=2)
def test_one_to_one_relationship(self, make_view):
view = make_view(name="cz-oto-view")
cz = CatalogZone.objects.create(view=view, soa_serial=7)
assert view.catalog_zone == cz
def test_create_and_retrieve(self):
IntegerKeyValueSetting.objects.create(key="catz-serial", value=99)
obj = IntegerKeyValueSetting.objects.get(key="catz-serial")
assert obj.value == 99
def test_create_and_retrieve(self, make_view):
view = make_view(name="cz-retrieve-view")
CatalogZone.objects.create(view=view, soa_serial=99)
cz = CatalogZone.objects.get(view=view)
assert cz.soa_serial == 99
def test_ordering_by_view_name(self, make_view):
v1 = make_view(name="z-view")
v2 = make_view(name="a-view")
v3 = make_view(name="m-view")
CatalogZone.objects.create(view=v1, soa_serial=1)
CatalogZone.objects.create(view=v2, soa_serial=2)
CatalogZone.objects.create(view=v3, soa_serial=3)
names = list(
CatalogZone.objects.values_list("view__name", flat=True)
)
assert names == ["a-view", "m-view", "z-view"]
@pytest.mark.django_db
@@ -34,19 +51,35 @@ class TestCatalogZoneMemberIdentifier:
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert str(m) == m.name
def test_unique_name_constraint(self, make_zone):
zone = make_zone(name="uniq.test")
existing = CatalogZoneMemberIdentifier.objects.get(zone=zone)
existing.name = "shared-name"
existing.save()
def test_unique_name_per_catalog_zone(self, make_zone, make_view):
view = make_view(name="shared-view")
zone1 = make_zone(name="uniq1.test", view=view)
zone2 = make_zone(name="uniq2.test", view=view)
other = make_zone(name="other.test")
other_ident = CatalogZoneMemberIdentifier.objects.get(zone=other)
ident1 = CatalogZoneMemberIdentifier.objects.get(zone=zone1)
ident1.name = "shared-name"
ident1.save()
ident2 = CatalogZoneMemberIdentifier.objects.get(zone=zone2)
from django.db import IntegrityError
with pytest.raises(IntegrityError):
other_ident.name = "shared-name"
other_ident.save()
ident2.name = "shared-name"
ident2.save()
def test_same_name_allowed_in_different_catalog_zones(self, make_zone, make_view):
view1 = make_view(name="view-a")
view2 = make_view(name="view-b")
zone1 = make_zone(name="diff1.test", view=view1)
zone2 = make_zone(name="diff2.test", view=view2)
ident1 = CatalogZoneMemberIdentifier.objects.get(zone=zone1)
ident1.name = "shared-name"
ident1.save()
ident2 = CatalogZoneMemberIdentifier.objects.get(zone=zone2)
ident2.name = "shared-name"
ident2.save()
def test_cascade_delete_when_zone_deleted(self, make_zone):
zone = make_zone(name="cascade.test")
@@ -60,10 +93,11 @@ class TestCatalogZoneMemberIdentifier:
m = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert zone.catz_identifier == m
def test_ordering_by_name(self, make_zone):
z1 = make_zone(name="z1.test")
z2 = make_zone(name="z2.test")
z3 = make_zone(name="z3.test")
def test_ordering_by_name(self, make_zone, make_view):
view = make_view(name="ord-view")
z1 = make_zone(name="z1.test", view=view)
z2 = make_zone(name="z2.test", view=view)
z3 = make_zone(name="z3.test", view=view)
CatalogZoneMemberIdentifier.objects.filter(zone=z3).update(name="gamma")
CatalogZoneMemberIdentifier.objects.filter(zone=z1).update(name="alpha")
CatalogZoneMemberIdentifier.objects.filter(zone=z2).update(name="beta")
@@ -78,8 +112,6 @@ class TestCatalogZoneMemberIdentifier:
@pytest.mark.django_db
class TestSeenTransferClients:
def test_str_representation(self, make_view):
from netbox_dns.models import View
view = make_view(name="str-view")
client = SeenTransferClients.objects.create(
source_ip="192.0.2.1",
@@ -203,12 +203,12 @@ class TestScheduleNsNotify:
class TestSendClientDNSNotifyRun:
def test_sends_notify_to_seen_clients(self, make_zone, make_view, monkeypatch):
import netbox_dns_bridge.signals.notify_handler as nh
from netbox_dns_bridge.models import SeenTransferClients, IntegerKeyValueSetting
from netbox_dns_bridge.models import SeenTransferClients, CatalogZone
from django.utils import timezone
view = make_view(name="notify-run-view")
IntegerKeyValueSetting.objects.update_or_create(
key="catalog-zone-soa-serial", defaults={"value": 42}
CatalogZone.objects.update_or_create(
view=view, defaults={"soa_serial": 42}
)
SeenTransferClients.objects.create(
source_ip="192.0.2.99", view=view, last_seen=timezone.now()
@@ -236,7 +236,7 @@ class TestSendClientDNSNotifyRun:
assert mock_udp.call_count >= 3 # zone + catz + view.catz
def test_handles_missing_serial_setting(self, monkeypatch, make_view):
# When the IntegerKeyValueSetting for catz serial doesn't exist,
# When the CatalogZone for the view doesn't exist,
# the job should log an error and not send any notifies
view = make_view(name="missing-serial-view")
+63 -24
View File
@@ -12,7 +12,7 @@ from django.test.testcases import TestCase as _DjangoTestCase
captureOnCommitCallbacks = _DjangoTestCase.captureOnCommitCallbacks
from netbox_dns_bridge.models import (
IntegerKeyValueSetting,
CatalogZone,
CatalogZoneMemberIdentifier,
)
@@ -42,13 +42,13 @@ class TestZonePostSaveCreate:
def test_increments_soa_serial_on_commit(self, make_zone, catz_serial):
with captureOnCommitCallbacks(execute=True):
zone = make_zone(name="serial-inc.test")
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial + 1
obj = CatalogZone.objects.get(view=zone.view)
assert obj.soa_serial == catz_serial + 1
def test_does_not_increment_without_on_commit(self, make_zone, catz_serial):
make_zone(name="no-commit.test")
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial
zone = make_zone(name="no-commit.test")
obj = CatalogZone.objects.get(view=zone.view)
assert obj.soa_serial == catz_serial
def test_schedule_client_notify_called_when_enabled(self, make_zone, patch_settings):
patch_settings({"notify_clients": True})
@@ -114,24 +114,63 @@ class TestZonePostSaveUpdate:
with captureOnCommitCallbacks(execute=True):
zone.soa_serial = zone.soa_serial + 1
zone.save()
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial + 1
obj = CatalogZone.objects.get(view=zone.view)
assert obj.soa_serial == catz_serial + 1
@pytest.mark.django_db
class TestZonePostDelete:
def test_increments_soa_serial_on_commit_on_delete(self, make_zone, catz_serial):
zone = make_zone(name="delete-serial.test")
view = zone.view
with captureOnCommitCallbacks(execute=True):
zone.delete()
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial + 1
obj = CatalogZone.objects.get(view=view)
assert obj.soa_serial == catz_serial + 1
def test_does_not_increment_without_on_commit(self, make_zone, catz_serial):
zone = make_zone(name="delete-no-commit.test")
view = zone.view
zone.delete()
obj = IntegerKeyValueSetting.objects.get(key="catalog-zone-soa-serial")
assert obj.value == catz_serial
obj = CatalogZone.objects.get(view=view)
assert obj.soa_serial == catz_serial
@pytest.mark.django_db
class TestZoneViewChange:
def test_increments_both_catalog_zone_serials(self, make_zone, make_view, catz_serial):
view_a = make_view(name="view-a")
view_b = make_view(name="view-b")
# view_b has no zones yet, so no CatalogZone was auto-created for it.
CatalogZone.objects.create(view=view_b, soa_serial=5)
zone = make_zone(name="move.test", view=view_a)
serial_a_before = CatalogZone.objects.get(view=view_a).soa_serial
serial_b_before = CatalogZone.objects.get(view=view_b).soa_serial
with captureOnCommitCallbacks(execute=True):
zone.view = view_b
zone.save()
serial_a_after = CatalogZone.objects.get(view=view_a).soa_serial
serial_b_after = CatalogZone.objects.get(view=view_b).soa_serial
assert serial_a_after == serial_a_before + 1
assert serial_b_after == serial_b_before + 1
def test_updates_identifier_catalog_zone_fk(self, make_zone, make_view):
view_a = make_view(name="fk-view-a")
view_b = make_view(name="fk-view-b")
zone = make_zone(name="fk-move.test", view=view_a)
ident = CatalogZoneMemberIdentifier.objects.get(zone=zone)
assert ident.catalog_zone == CatalogZone.objects.get(view=view_a)
zone.view = view_b
zone.save()
ident.refresh_from_db()
assert ident.catalog_zone == CatalogZone.objects.get(view=view_b)
@pytest.mark.django_db
@@ -141,9 +180,9 @@ class TestSignalDedup:
from django.contrib.auth import get_user_model
zone = make_zone(name="dedup.test")
original_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
original_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
User = get_user_model()
user = User(username="dedup-user", is_superuser=True)
@@ -163,16 +202,16 @@ class TestSignalDedup:
zone.soa_serial = zone.soa_serial + 2
zone.save()
new_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
new_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
assert new_serial == original_serial + 1
def test_no_dedup_without_request(self, make_zone):
zone = make_zone(name="nodedup.test")
original_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
original_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
with captureOnCommitCallbacks(execute=True):
zone.soa_serial = zone.soa_serial + 1
@@ -181,9 +220,9 @@ class TestSignalDedup:
zone.soa_serial = zone.soa_serial + 2
zone.save()
new_serial = IntegerKeyValueSetting.objects.get(
key="catalog-zone-soa-serial"
).value
new_serial = CatalogZone.objects.get(
view=zone.view
).soa_serial
assert new_serial == original_serial + 2
+2 -2
View File
@@ -13,11 +13,11 @@ def test_plugin_importable():
def test_models_importable():
from netbox_dns_bridge.models import (
IntegerKeyValueSetting,
CatalogZone,
CatalogZoneMemberIdentifier,
SeenTransferClients,
)
assert IntegerKeyValueSetting._meta.model_name
assert CatalogZone._meta.model_name
assert CatalogZoneMemberIdentifier._meta.model_name
assert SeenTransferClients._meta.model_name
+10 -7
View File
@@ -43,18 +43,21 @@ class TestCatzOverviewView:
resp = client.get("/plugins/dns-bridge/catz/")
assert resp.status_code == 200
def test_context_contains_soa_serial(self, client, superuser, catz_serial):
def test_context_contains_catalog_zones(self, client, superuser, catz_serial, make_zone):
zone = make_zone(name="ctx.test")
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catz/")
assert "soa_serial" in resp.context
assert resp.context["soa_serial"] == catz_serial
assert "catalog_zones" in resp.context
cz_list = list(resp.context["catalog_zones"])
assert len(cz_list) >= 1
cz = next(c for c in cz_list if c.view_id == zone.view_id)
assert cz.soa_serial == catz_serial
def test_context_soa_serial_none_when_not_set(self, client, superuser):
from netbox_dns_bridge.models import IntegerKeyValueSetting
IntegerKeyValueSetting.objects.filter(key="catalog-zone-soa-serial").delete()
def test_context_no_catalog_zones_when_empty(self, client, superuser):
client.login(username="admin", password="testpass")
resp = client.get("/plugins/dns-bridge/catz/")
assert resp.context["soa_serial"] is None
assert "catalog_zones" in resp.context
assert list(resp.context["catalog_zones"]) == []
@pytest.mark.django_db