"""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