You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.9 KiB
Python
156 lines
5.9 KiB
Python
"""Network-role and public-filter boundary contracts."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import pytest
|
|
|
|
from virsh_panel.network_roles import (
|
|
PRIVATE_INTERFACE_ROLE,
|
|
PRIVATE_LIBVIRT_ALIAS,
|
|
PUBLIC_INTERFACE_ROLE,
|
|
PUBLIC_LIBVIRT_ALIAS,
|
|
assert_public_interface,
|
|
interface_role,
|
|
private_interface_has_public_filter,
|
|
role_for_cloud_key,
|
|
select_private_interface,
|
|
select_public_interface,
|
|
)
|
|
|
|
|
|
def test_explicit_roles_do_not_depend_on_xml_order():
|
|
public = ET.fromstring(
|
|
"<interface><alias name='ua-public0'/><source bridge='br0'/></interface>"
|
|
)
|
|
private = ET.fromstring(
|
|
"<interface><source bridge='vpc0'/><alias name='ua-private0'/></interface>"
|
|
)
|
|
assert interface_role(public) == "public"
|
|
assert interface_role(private) == "private"
|
|
assert_public_interface(public)
|
|
with pytest.raises(ValueError):
|
|
assert_public_interface(private)
|
|
|
|
|
|
def test_unlabelled_multi_nic_is_ambiguous_and_private_filter_is_rejected():
|
|
unlabelled = ET.fromstring(
|
|
"<interface><source bridge='vpc0'/><filterref filter='clean-traffic'/></interface>"
|
|
)
|
|
assert interface_role(unlabelled) == "unknown"
|
|
assert private_interface_has_public_filter(unlabelled)
|
|
with pytest.raises(ValueError):
|
|
assert_public_interface(unlabelled)
|
|
|
|
|
|
def test_explicit_public_with_unlabelled_second_nic_fails_closed():
|
|
public = ET.fromstring(
|
|
"<interface><alias name='ua-public0'/><source bridge='br0'/></interface>"
|
|
)
|
|
unknown = ET.fromstring("<interface><source bridge='vpc0'/></interface>")
|
|
with pytest.raises(ValueError, match="声明"):
|
|
select_public_interface([public, unknown])
|
|
|
|
|
|
def test_explicit_public_private_pair_selects_public_interface():
|
|
public = ET.fromstring("<interface><alias name='ua-public0'/></interface>")
|
|
private = ET.fromstring("<interface><alias name='ua-private0'/></interface>")
|
|
assert select_public_interface([private, public]) is public
|
|
|
|
|
|
def test_legacy_single_public_nic_can_still_be_updated():
|
|
legacy = ET.fromstring("<interface><source bridge='br0'/></interface>")
|
|
assert interface_role(legacy) == "unknown"
|
|
assert interface_role(legacy, legacy_public=True) == "public"
|
|
assert_public_interface(legacy, legacy_public=True)
|
|
|
|
generated = ET.fromstring(
|
|
"<interface><alias name='net0'/><source bridge='br0'/></interface>"
|
|
)
|
|
assert interface_role(generated) == "unknown"
|
|
assert select_public_interface([generated]) is generated
|
|
assert_public_interface(generated, legacy_public=True)
|
|
|
|
renamed = ET.fromstring(
|
|
"<interface><alias name='operator0'/><source bridge='br0'/></interface>"
|
|
)
|
|
with pytest.raises(ValueError):
|
|
select_public_interface([renamed])
|
|
|
|
|
|
def test_private_selection_requires_one_explicit_private_role():
|
|
private = ET.fromstring(
|
|
"<interface><alias name='ua-private0'/><source bridge='vpc0'/></interface>"
|
|
)
|
|
public = ET.fromstring(
|
|
"<interface><alias name='ua-public0'/><source bridge='br0'/></interface>"
|
|
)
|
|
assert select_private_interface([public, private]) is private
|
|
assert select_private_interface([public]) is None
|
|
with pytest.raises(ValueError):
|
|
select_private_interface([private, ET.fromstring(
|
|
"<interface><alias name='ua-private0'/></interface>")])
|
|
|
|
|
|
@pytest.mark.parametrize("alias", ["net0", "ua-net-0"])
|
|
def test_libvirt_generated_single_nic_alias_is_legacy_public(alias):
|
|
legacy = ET.fromstring(
|
|
f"<interface><alias name='{alias}'/><source bridge='br0'/></interface>"
|
|
)
|
|
assert interface_role(legacy) == "unknown"
|
|
assert interface_role(legacy, legacy_public=True) == "public"
|
|
assert select_public_interface([legacy]) is legacy
|
|
assert_public_interface(legacy, legacy_public=True)
|
|
|
|
|
|
def test_generated_alias_is_not_guessed_in_a_multi_nic_domain():
|
|
public = ET.fromstring(
|
|
"<interface><alias name='ua-public0'/><source bridge='br0'/></interface>"
|
|
)
|
|
legacy = ET.fromstring(
|
|
"<interface><alias name='net1'/><source bridge='vpc0'/></interface>"
|
|
)
|
|
with pytest.raises(ValueError, match="声明"):
|
|
select_public_interface([public, legacy])
|
|
|
|
|
|
def test_cloud_keys_and_libvirt_aliases_are_separate_contracts():
|
|
assert PUBLIC_INTERFACE_ROLE == "public0"
|
|
assert PRIVATE_INTERFACE_ROLE == "private0"
|
|
assert PUBLIC_LIBVIRT_ALIAS == "ua-public0"
|
|
assert PRIVATE_LIBVIRT_ALIAS == "ua-private0"
|
|
assert role_for_cloud_key(PUBLIC_INTERFACE_ROLE) == "public"
|
|
assert role_for_cloud_key(PRIVATE_INTERFACE_ROLE) == "private"
|
|
with pytest.raises(ValueError):
|
|
role_for_cloud_key(PUBLIC_LIBVIRT_ALIAS)
|
|
with pytest.raises(ValueError):
|
|
role_for_cloud_key(PRIVATE_LIBVIRT_ALIAS)
|
|
|
|
|
|
@pytest.mark.parametrize("alias", ["public0", "private0"])
|
|
def test_pre_fix_panel_aliases_remain_readable_but_are_not_canonical(alias):
|
|
interface = ET.fromstring(f"<interface><alias name='{alias}'/></interface>")
|
|
expected = "public" if alias == "public0" else "private"
|
|
assert interface_role(interface) == expected
|
|
assert alias not in {PUBLIC_LIBVIRT_ALIAS, PRIVATE_LIBVIRT_ALIAS}
|
|
|
|
|
|
@pytest.mark.parametrize("alias", ["net0", "ua-net-0"])
|
|
def test_generated_public_with_explicit_private_is_a_safe_live_migration(alias):
|
|
public = ET.fromstring(
|
|
f"<interface><alias name='{alias}'/><source bridge='br0'/></interface>"
|
|
)
|
|
private = ET.fromstring(
|
|
"<interface><alias name='ua-private0'/><source bridge='vpc0'/></interface>"
|
|
)
|
|
assert select_public_interface([private, public]) is public
|
|
assert select_private_interface([public, private]) is private
|
|
|
|
|
|
def test_generated_public_with_unknown_extra_nic_still_fails_closed():
|
|
generated = ET.fromstring("<interface><alias name='net0'/></interface>")
|
|
unknown = ET.fromstring("<interface><alias name='operator0'/></interface>")
|
|
with pytest.raises(ValueError, match="公网网卡角色不明确"):
|
|
select_public_interface([generated, unknown])
|