Files
napalm-proxmox/tests/test_network_target_uplinks.py
T
Christian Manivong 72c30dd4c2
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Successful in 1m15s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m21s
CI / driver (pull_request) Successful in 3m5s
driver / publish (pull_request) Skipped
feat: get_network_targets names each bridge's uplinks (NetOrk/netork#1325)
netOrk's VM dialog follows a bridge's physical NICs to the switch port
behind it, to warn when a VM without a VLAN tag would land nowhere. The
driver knows those NICs from the node's network config.

uplinks is read from bridge_ports (Linux bridge) or ovs_ports (OVS
bridge), one group per bridge port: a bond (slaves / ovs_bonds) as its
NICs together, a NIC alone. Internal ports (OVSIntPort) and VLAN
subinterfaces leave out, as they never carry the bridge's untagged
traffic to the switch. A port missing from the config or of a kind the
driver cannot place leaves the key out rather than guessing.
2026-10-30 09:28:07 +01:00

136 lines
4.3 KiB
Python

"""get_network_targets names the physical NICs each bridge leaves the node through.
NetOrk follows their LLDP neighbours to the switch port behind the bridge, to
tell whether a VM without a VLAN tag lands in a network at all
(NetOrk/netork#1325). They come in groups, one per way out of the bridge: a
bond's slaves together, a single NIC alone. Internal ports, VLAN subinterfaces
and virtual ports are no uplinks. When a port cannot be told
apart, ``uplinks`` is left out rather than guessed.
"""
from __future__ import annotations
from unittest.mock import MagicMock
from napalm_proxmox.vm_provision_mixin import ProxmoxVMProvisionMixin
def _targets(network: list[dict]) -> dict[str, dict]:
mixin = ProxmoxVMProvisionMixin()
mixin._get_node_network = MagicMock(return_value=network)
mixin._get_sdn_vnets = MagicMock(return_value=[])
return {t["name"]: t for t in mixin.get_network_targets()}
def test_linux_bridge_on_a_nic():
targets = _targets(
[
{"iface": "eno1", "type": "eth"},
{"iface": "vmbr0", "type": "bridge", "bridge_ports": "eno1", "bridge_vlan_aware": 1},
]
)
assert targets["vmbr0"]["uplinks"] == [["eno1"]]
def test_linux_bridge_on_a_bond_lists_the_slaves():
targets = _targets(
[
{"iface": "eno1", "type": "eth"},
{"iface": "eno2", "type": "eth"},
{"iface": "bond0", "type": "bond", "slaves": "eno1 eno2"},
{"iface": "vmbr0", "type": "bridge", "bridge_ports": "bond0"},
]
)
assert targets["vmbr0"]["uplinks"] == [["eno1", "eno2"]]
def test_linux_bridge_on_a_vlan_subinterface_has_no_uplink():
"""A bridge on ``eno1.40`` leaves tagged; the port's untagged VLAN is not its."""
targets = _targets(
[
{"iface": "eno1", "type": "eth"},
{"iface": "eno1.40", "type": "vlan", "vlan-raw-device": "eno1"},
{"iface": "vmbr40", "type": "bridge", "bridge_ports": "eno1.40"},
]
)
assert targets["vmbr40"]["uplinks"] == []
def test_host_only_bridge_has_no_uplink():
targets = _targets([{"iface": "vmbr9", "type": "bridge"}])
assert targets["vmbr9"]["uplinks"] == []
def test_ovs_bridge_with_bond_and_internal_port():
"""pve-dual's shape: vmbr0 carries an LACP bond and the management OVSIntPort."""
targets = _targets(
[
{"iface": "nic0", "type": "OVSPort"},
{"iface": "nic1", "type": "OVSPort"},
{
"iface": "bond0",
"type": "OVSBond",
"ovs_bonds": "nic0 nic1",
"ovs_bridge": "vmbr0",
},
{"iface": "mgmt_vlan", "type": "OVSIntPort", "ovs_bridge": "vmbr0", "ovs_tag": 8},
{"iface": "vmbr0", "type": "OVSBridge", "ovs_ports": "bond0 mgmt_vlan"},
]
)
assert targets["vmbr0"]["uplinks"] == [["nic0", "nic1"]]
def test_ovs_bridge_on_a_single_port():
targets = _targets(
[
{"iface": "eno1", "type": "OVSPort", "ovs_bridge": "vmbr1"},
{"iface": "vmbr1", "type": "OVSBridge", "ovs_ports": "eno1"},
]
)
assert targets["vmbr1"]["uplinks"] == [["eno1"]]
def test_ovs_bridge_on_two_single_ports_has_two_groups():
targets = _targets(
[
{"iface": "eno1", "type": "OVSPort", "ovs_bridge": "vmbr1"},
{"iface": "eno2", "type": "OVSPort", "ovs_bridge": "vmbr1"},
{"iface": "vmbr1", "type": "OVSBridge", "ovs_ports": "eno1 eno2"},
]
)
assert targets["vmbr1"]["uplinks"] == [["eno1"], ["eno2"]]
def test_port_missing_from_the_network_config_leaves_uplinks_out():
targets = _targets([{"iface": "vmbr0", "type": "bridge", "bridge_ports": "enp9s0"}])
assert "uplinks" not in targets["vmbr0"]
def test_port_of_an_unknown_kind_leaves_uplinks_out():
targets = _targets(
[
{"iface": "wg0", "type": "unknown"},
{"iface": "vmbr0", "type": "bridge", "bridge_ports": "wg0"},
]
)
assert "uplinks" not in targets["vmbr0"]
def test_vnet_has_no_uplinks():
mixin = ProxmoxVMProvisionMixin()
mixin._get_node_network = MagicMock(return_value=[])
mixin._get_sdn_vnets = MagicMock(return_value=[{"vnet": "vnet0", "zone": "z", "tag": 10}])
(target,) = mixin.get_network_targets()
assert "uplinks" not in target