Files
Netbox-Utilities/netbox_utilities/tests/test_switch_ports.py
T
MrBlakeandClaude Opus 5.5 8eb075db9f feat: export switch port usage as PDF instead of a Ports tab
Switches get an "Export switch port usage" button that downloads an A4
landscape PDF: key figures, a front panel with one colour per VLAN label
(full colour = connected, pale = free, dashed with a cross = disabled,
text colour chosen for contrast), a VLAN legend and the full port list
with peers. The PDF follows the language of the user's NetBox
preferences (German or English). The Ports tab with its script and
styles is removed. ReportLab becomes a dependency.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 09:25:29 +01:00

140 lines
6.0 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 django.test import SimpleTestCase
from netbox_utilities import switch_ports
from netbox_utilities.switch_ports import Port
def port(name, **kwargs):
return Port(pk=hash(name) & 0xFFFF, name=name, **kwargs)
class NameTest(SimpleTestCase):
def test_split_name(self):
self.assertEqual(switch_ports.split_name("GigabitEthernet1/0/24"), ("GigabitEthernet1/0/", 24))
self.assertEqual(switch_ports.split_name("ethernet1/49"), ("ethernet1/", 49))
self.assertEqual(switch_ports.split_name("Port 7"), ("Port ", 7))
self.assertEqual(switch_ports.split_name("mgmt"), ("mgmt", None))
def test_natural_order(self):
names = ["Gi1/0/10", "Gi1/0/2", "Gi1/0/1"]
self.assertEqual(sorted(names, key=switch_ports.natural_key), ["Gi1/0/1", "Gi1/0/2", "Gi1/0/10"])
def test_optical_types(self):
self.assertTrue(switch_ports.is_optical("10gbase-x-sfpp"))
self.assertTrue(switch_ports.is_optical("100gbase-x-qsfp28"))
self.assertFalse(switch_ports.is_optical("1000base-t"))
def test_roles(self):
self.assertTrue(switch_ports.is_switch(SimpleNamespace(role=SimpleNamespace(name="switch")), ["Switch"]))
self.assertFalse(switch_ports.is_switch(SimpleNamespace(role=SimpleNamespace(name="Server")), ["Switch"]))
class VlanTest(SimpleTestCase):
def test_same_vlan_same_colour(self):
self.assertEqual(switch_ports.vlan_key("VLAN 10"), "10")
self.assertEqual(switch_ports.vlan_key("vlan10"), "10")
self.assertEqual(switch_ports.vlan_key(" Trunk 10,20 "), "Trunk 10,20")
self.assertEqual(switch_ports.vlan_color("VLAN 10"), switch_ports.vlan_color("10"))
self.assertIn(switch_ports.vlan_color("Server"), switch_ports.VLAN_PALETTE)
self.assertIsNone(switch_ports.vlan_color(""))
def test_legend_counts(self):
ports = [
port("Gi1/0/1", label="10", connected=True),
port("Gi1/0/2", label="VLAN 10"),
port("Gi1/0/3", label="20", connected=True),
port("Gi1/0/4"),
port("Po1", type="lag", label="10", virtual=True),
]
legend = [(vlan, total, connected) for vlan, _, total, connected in switch_ports.vlan_legend(ports)]
self.assertEqual(legend, [("10", 2, 1), ("20", 1, 1), ("", 1, 0)])
class LayoutTest(SimpleTestCase):
def test_odd_top_even_bottom(self):
ports = [port(f"Gi1/0/{number}") for number in range(1, 7)]
group = switch_ports.build_groups(ports)[0]
self.assertEqual([[p.number for p in column] for column in group.columns], [[1, 2], [3, 4], [5, 6]])
def test_groups_by_member_and_medium(self):
ports = [
port("Gi2/0/1"),
port("Gi1/0/1"),
port("Te1/1/1", type="10gbase-x-sfpp"),
port("Vlan10", type="virtual", virtual=True),
]
titles = [group.title for group in switch_ports.build_groups(ports)]
self.assertEqual(titles, ["Gi1/0", "Gi2/0", "Te1/1 (SFP)"])
def test_single_row_without_numbers(self):
group = switch_ports.build_groups([port("mgmt"), port("console")])
self.assertEqual(sum(len(g.ports) for g in group), 2)
def test_summary(self):
ports = [
port("1", connected=True),
port("2"),
port("3", enabled=False, connected=True),
port("Po1", virtual=True, connected=True),
]
self.assertEqual(
switch_ports.summary(ports), {"total": 3, "connected": 1, "free": 1, "disabled": 1, "percent": 33}
)
class ColorAssignmentTest(SimpleTestCase):
def test_every_vlan_gets_its_own_colour(self):
ports = [
port(f"Gi1/0/{index}", label=label) for index, label in enumerate(["10", "VLAN 10", "20", "Trunk", ""])
]
colors = switch_ports.assign_vlan_colors(ports)
self.assertEqual(len(set(colors.values())), 3)
self.assertEqual(ports[0].color, ports[1].color)
self.assertIsNone(ports[4].color)
class PDFTest(SimpleTestCase):
def ports(self):
return [
port("Gi1/0/1", label="10", connected=True, peer="srv1 – eth0"),
port("Gi1/0/2", label="20"),
port("Gi1/0/3", enabled=False),
port("Te1/1/1", type="10gbase-x-sfpp", label="Trunk", connected=True, peer=switch_ports.MARKED_CONNECTED),
port("Po1", type="lag", virtual=True),
]
def test_builds_pdf_in_both_languages(self):
from netbox_utilities import switch_port_pdf
device = switch_port_pdf.DeviceInfo(name="sw1", site="HQ", rack="R1 / U10", model="C9300")
for lang in ("de", "en"):
with self.subTest(lang=lang):
data = switch_port_pdf.build_pdf(device, self.ports(), lang=lang)
self.assertTrue(data.startswith(b"%PDF"))
self.assertGreater(len(data), 2000)
def test_language_follows_user_setting(self):
from netbox_utilities import switch_port_pdf
self.assertEqual(switch_port_pdf.language("en-us"), "en")
self.assertEqual(switch_port_pdf.language("de"), "de")
self.assertEqual(switch_port_pdf.language(None), "de")
def test_text_contrasts_with_fill(self):
from reportlab.lib import colors
from netbox_utilities import switch_port_pdf
self.assertEqual(switch_port_pdf.text_color(colors.HexColor("#ff7f0e")), colors.black)
self.assertEqual(switch_port_pdf.text_color(colors.HexColor("#2ca02c")), colors.black)
self.assertEqual(switch_port_pdf.text_color(colors.HexColor("#1f77b4")), colors.white)
self.assertEqual(switch_port_pdf.text_color(colors.HexColor("#393b79")), colors.white)
for color in switch_ports.VLAN_PALETTE:
with self.subTest(color=color):
fill = colors.HexColor(color)
ink = switch_port_pdf.text_color(fill)
light, dark = sorted((switch_port_pdf.luminance(fill), switch_port_pdf.luminance(ink)), reverse=True)
self.assertGreaterEqual((light + 0.05) / (dark + 0.05), 3)