Files
napalm-ubiquiti/tests/test_parse_vlans.py
T

181 lines
6.4 KiB
Python

"""A switch's VLANs with their ports: the controller's view, and the switch's own config.
The controller cases follow docs/PORT_VLANS.md; the networks and profiles are written
here with the fields the controller uses, so each rule is visible in its test.
"""
from __future__ import annotations
from typing import Any
import pytest
from napalm_ubiquiti.parsers.vlans import (
parse_running_cfg,
parse_vlans_detail_classic,
parse_vlans_detail_running_cfg,
taggable_networks,
)
NETWORKS: list[dict[str, Any]] = [
{"_id": "wan", "name": "Primary (WAN1)", "purpose": "wan"},
{"_id": "lan", "name": "Default", "purpose": "corporate", "attr_hidden_id": "LAN"},
{"_id": "iot", "name": "IoT", "purpose": "vlan-only", "vlan_enabled": True, "vlan": 30},
{"_id": "guest", "name": "Guest", "purpose": "guest", "vlan_enabled": True, "vlan": "40"},
{"_id": "vpn", "name": "VPN", "purpose": "remote-user-vpn"},
{"_id": "off", "name": "Off", "purpose": "corporate", "vlan_enabled": False, "vlan": 50},
]
def _switch(
*ports: dict[str, Any], overrides: list[dict[str, Any]] | None = None
) -> dict[str, Any]:
return {"port_table": list(ports), "port_overrides": overrides or []}
class TestTaggableNetworks:
def test_switchable_purposes_with_their_vlan_ids(self) -> None:
assert taggable_networks(NETWORKS) == {
"lan": (1, "Default"),
"iot": (30, "IoT"),
"guest": (40, "Guest"),
"off": (1, "Off"),
}
class TestClassic:
def test_a_port_without_override_or_profile_carries_everything(self) -> None:
vlans = parse_vlans_detail_classic(_switch({"port_idx": 1}), NETWORKS, [])
assert vlans["1"] == {"name": "Default", "tagged": [], "untagged": ["Port 1"]}
assert vlans["30"] == {"name": "IoT", "tagged": ["Port 1"], "untagged": []}
assert vlans["40"]["tagged"] == ["Port 1"]
def test_an_access_port_on_a_newer_controller(self) -> None:
override = {"port_idx": 2, "native_networkconf_id": "iot", "tagged_vlan_mgmt": "block_all"}
vlans = parse_vlans_detail_classic(
_switch({"port_idx": 2}, overrides=[override]), NETWORKS, []
)
assert vlans["30"]["untagged"] == ["Port 2"]
assert vlans["1"] == {"name": "Default", "tagged": [], "untagged": []}
assert vlans["40"]["tagged"] == []
def test_a_custom_trunk_tags_all_but_the_excluded(self) -> None:
override = {
"port_idx": 3,
"tagged_vlan_mgmt": "custom",
"excluded_networkconf_ids": ["guest"],
}
vlans = parse_vlans_detail_classic(
_switch({"port_idx": 3}, overrides=[override]), NETWORKS, []
)
assert vlans["1"]["untagged"] == ["Port 3"]
assert vlans["30"]["tagged"] == ["Port 3"]
assert vlans["40"]["tagged"] == []
def test_a_profile_from_an_older_controller(self) -> None:
profile = {
"_id": "p1",
"forward": "customize",
"native_networkconf_id": "guest",
"tagged_networkconf_ids": ["iot", "guest"],
}
vlans = parse_vlans_detail_classic(
_switch({"port_idx": 4, "portconf_id": "p1"}), NETWORKS, [profile]
)
assert vlans["40"]["untagged"] == ["Port 4"]
assert vlans["40"]["tagged"] == []
assert vlans["30"]["tagged"] == ["Port 4"]
assert vlans["1"]["untagged"] == []
def test_the_override_names_the_profile_and_overrides_a_field(self) -> None:
profile = {"_id": "p1", "forward": "native", "native_networkconf_id": "iot"}
override = {"port_idx": 5, "portconf_id": "p1", "native_networkconf_id": "guest"}
vlans = parse_vlans_detail_classic(
_switch({"port_idx": 5, "portconf_id": "other"}, overrides=[override]),
NETWORKS,
[profile],
)
assert vlans["40"]["untagged"] == ["Port 5"]
assert vlans["30"]["untagged"] == []
assert vlans["30"]["tagged"] == []
@pytest.mark.parametrize(
"field", [{"forward": "disabled"}, {"forward": "disabled", "tagged_vlan_mgmt": "auto"}]
)
def test_a_disabled_port_carries_nothing(self, field: dict[str, Any]) -> None:
override = dict(field, port_idx=6)
vlans = parse_vlans_detail_classic(
_switch({"port_idx": 6}, overrides=[override]), NETWORKS, []
)
assert all(v["tagged"] == [] and v["untagged"] == [] for v in vlans.values())
def test_an_unknown_native_network_falls_back_to_the_default(self) -> None:
override = {"port_idx": 7, "native_networkconf_id": "gone", "tagged_vlan_mgmt": "block_all"}
vlans = parse_vlans_detail_classic(
_switch({"port_idx": 7}, overrides=[override]), NETWORKS, []
)
assert vlans["1"]["untagged"] == ["Port 7"]
def test_ports_are_listed_in_order(self) -> None:
vlans = parse_vlans_detail_classic(_switch({"port_idx": 10}, {"port_idx": 2}), NETWORKS, [])
assert vlans["1"]["untagged"] == ["Port 2", "Port 10"]
class TestRunningCfg:
def test_key_value_lines(self) -> None:
assert parse_running_cfg("# c\nswitch.port.2.pvid=11\nbad line\nx=a=b\n") == {
"switch.port.2.pvid": "11",
"x": "a=b",
}
def test_untagged_ports_by_pvid(self, us24_cfg: str) -> None:
vlans = parse_vlans_detail_running_cfg(parse_running_cfg(us24_cfg), list(range(1, 27)))
assert sorted(vlans, key=int) == [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"100",
]
assert vlans["11"] == {"name": "", "tagged": [], "untagged": ["Port 2"]}
assert vlans["6"]["untagged"] == ["Port 12", "Port 15", "Port 16"]
def test_a_port_without_pvid_is_in_vlan_1_unless_disabled(self, us24_cfg: str) -> None:
vlans = parse_vlans_detail_running_cfg(parse_running_cfg(us24_cfg), list(range(1, 27)))
assert "Port 1" not in vlans["1"]["untagged"]
assert "Port 3" in vlans["1"]["untagged"]
assert "Port 2" not in vlans["1"]["untagged"]
def test_a_disabled_vlan_is_left_out(self) -> None:
cfg = {
"switch.vlan.1.id": "1",
"switch.vlan.2.id": "20",
"switch.vlan.2.status": "disabled",
}
assert list(parse_vlans_detail_running_cfg(cfg, [1])) == ["1"]