Files
NetBox-Import-Desktop/tests/test_switch_sync.py
T
MrBlakeandClaude Opus 5.5 39a29b9230 feat: VLAN labels, automatic port mapping and neighbour review for switches
- Read VLAN membership (untagged and tagged) from the running configuration
  on Aruba CX, ArubaOS-Switch and HPE Comware and write it to the NetBox
  interface label (e.g. "10", "1,20-22", "all")
- Keep only VLAN-related configuration lines in the raw data / offline export
- Map switch ports to NetBox interfaces with differing names automatically:
  equal name, equal port number (1/1/5 <-> 5), then natural order
- New "Review mapping" dialog before the preview: correct port mappings and
  assign each LLDP/CDP neighbour to a device of the switch's tenant group;
  choices are stored per switch in the profile
- Ignore the local Gitea token file

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 13:16:22 +01:00

127 lines
6.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from types import SimpleNamespace
from fake_netbox import FakeNetBox, Record
from netbox_sync.core.switches import build_snapshot, match_interfaces, plan_switch_sync, sync_snapshot_to_netbox
from test_switches import COMWARE_BRIEF, COMWARE_LLDP
def _setup():
nb = FakeNetBox()
tenant = SimpleNamespace(id=7, name="Muster")
nb.dcim.devices.records += [
Record(nb.dcim.devices, id=1, name="access-01", tenant=tenant),
Record(nb.dcim.devices, id=2, name="core-sw", tenant=None),
]
# Interfaces created from the NetBox device type use full Comware names.
for index, name in enumerate(("GigabitEthernet1/0/1", "GigabitEthernet1/0/2"), start=1):
nb.dcim.interfaces.records.append(Record(nb.dcim.interfaces, id=index, name=name, device=SimpleNamespace(id=1),
type=SimpleNamespace(value="1000base-x-sfp"), enabled=True, cable=None))
nb.dcim.interfaces.records.append(Record(nb.dcim.interfaces, id=50, name="GigabitEthernet1/0/24",
device=SimpleNamespace(id=2), enabled=True, cable=None))
snapshot = build_snapshot({"interfaces": COMWARE_BRIEF, "neighbors": COMWARE_LLDP}, "hpe_comware")
switch = {"id": "s", "name": "access-01", "device_id": 1}
return nb, switch, snapshot
def test_dry_run_writes_nothing():
nb, switch, snapshot = _setup()
report = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=True, dry_run=True)
assert report["errors"] == []
assert report["interfaces_created"] == 1 # Ten-GigabitEthernet1/0/49
assert report["interfaces_updated"] == 1 # GE1/0/2 is ADM -> disable
assert report["interfaces_unchanged"] == 1
assert report["cables_created"] == 1 # FQDN "core-sw.example.local" -> core-sw
assert nb.dcim.interfaces.created == [] and nb.dcim.interfaces.updates == [] and nb.dcim.cables.created == []
def test_sync_matches_short_names_and_keeps_existing_types():
nb, switch, snapshot = _setup()
report = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=True)
assert report["errors"] == []
names = [payload["name"] for payload in nb.dcim.interfaces.created]
assert names == ["Ten-GigabitEthernet1/0/49"], "no GE1/0/x duplicates next to template interfaces"
assert nb.dcim.interfaces.created[0]["type"] == "10gbase-x-sfpp"
# Only the admin state changed – the template type must not be overwritten.
assert nb.dcim.interfaces.updates == [(2, {"enabled": False})]
cable = nb.dcim.cables.created[0]
assert cable["a_terminations"][0]["object_id"] == 1
assert cable["b_terminations"][0]["object_id"] == 50
# A second run finds nothing left to do.
nb.dcim.interfaces.get(1).cable = True
again = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=True, dry_run=True)
assert again["changes"] == []
def test_offline_switch_is_resolved_by_name():
nb, _switch, snapshot = _setup()
report = sync_snapshot_to_netbox(nb, {"id": "s", "name": "access-01", "device_id": None}, snapshot,
create_cables=False, dry_run=True)
assert report["errors"] == []
assert report["interfaces_unchanged"] == 1
missing = sync_snapshot_to_netbox(nb, {"id": "s", "name": "unbekannt"}, snapshot, dry_run=True)
assert missing["errors"] == ["NetBox-Gerät „unbekannt“ nicht gefunden"]
def _numbered_setup():
"""NetBox device type with ports "1" … "3", switch reports "1/1/x"."""
nb = FakeNetBox()
group = SimpleNamespace(id=3, name="Kunde")
nb.tenancy.tenants.records.append(Record(nb.tenancy.tenants, id=7, name="Muster", group=group))
nb.dcim.devices.records += [
Record(nb.dcim.devices, id=1, name="access-01", tenant=SimpleNamespace(id=7), tenant_group=group),
Record(nb.dcim.devices, id=2, name="server-01", tenant=SimpleNamespace(id=7), tenant_group=group),
Record(nb.dcim.devices, id=3, name="fremd", tenant=None, tenant_group=None),
]
for index, name in enumerate(("1", "2", "3"), start=1):
nb.dcim.interfaces.records.append(Record(nb.dcim.interfaces, id=index, name=name, device=SimpleNamespace(id=1),
type=SimpleNamespace(value="1000base-t"), enabled=True, cable=None,
label=""))
nb.dcim.interfaces.records.append(Record(nb.dcim.interfaces, id=20, name="eth0", device=SimpleNamespace(id=2),
type=SimpleNamespace(value="1000base-t"), enabled=True, cable=None))
interfaces = [
{"name": f"1/1/{number}", "enabled": True, "connected": True, "media": "1GbT", "speed_mbps": 1000,
"ip_addresses": [], "vlans": {"untagged": [10 * number], "tagged": [], "tagged_all": False}}
for number in (1, 2, 3)
]
neighbor = {"local_port": "1/1/2", "system_name": "unbekannt", "remote_port": "eth0", "port_description": "",
"management_ip": "", "protocol": "LLDP"}
snapshot = {"interfaces": interfaces, "neighbors": [neighbor]}
return nb, {"id": "s", "name": "access-01", "device_id": 1}, snapshot
def test_ports_are_mapped_by_number_and_labelled_with_vlans():
nb, switch, snapshot = _numbered_setup()
report = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=False)
assert report["errors"] == [] and report["interfaces_created"] == 0
assert sorted(nb.dcim.interfaces.updates) == [(1, {"label": "10"}), (2, {"label": "20"}), (3, {"label": "30"})]
def test_ports_are_mapped_by_order_and_manual_choice_wins():
nb, switch, snapshot = _numbered_setup()
for item, name in zip(snapshot["interfaces"], ("1/0/1", "1/0/2", "2/0/1")):
item["name"] = name # "2/0/1" breaks the unique port numbers
matches = match_interfaces(snapshot["interfaces"], nb.dcim.interfaces.filter(device_id=1))
assert {name: (record.name, method) for name, (record, method) in matches.items()} == {
"1/0/1": ("1", "order"), "1/0/2": ("2", "number"), "2/0/1": ("3", "order")}
manual = match_interfaces(snapshot["interfaces"], nb.dcim.interfaces.filter(device_id=1), {"2/0/1": ""})
assert manual["2/0/1"] == (None, "new")
def test_plan_offers_tenant_group_devices_and_manual_neighbor_creates_cable():
nb, switch, snapshot = _numbered_setup()
plan = plan_switch_sync(nb, switch, snapshot)
assert plan["device_scope"] == "Mandantengruppe Kunde"
assert [device["name"] for device in plan["devices"]] == ["server-01"]
assert plan["neighbors"][0]["device_id"] is None
assert {item["name"]: item["method"] for item in plan["interfaces"]}["1/1/1"] == "number"
report = sync_snapshot_to_netbox(nb, switch, snapshot, dry_run=True)
assert report["cables_created"] == 0
switch["neighbor_devices"] = {"1/1/2": 2}
report = sync_snapshot_to_netbox(nb, switch, snapshot)
cable = nb.dcim.cables.created[0]
assert cable["a_terminations"][0]["object_id"] == 2 and cable["b_terminations"][0]["object_id"] == 20