Files
supervisor/tests/dbus/network/test_connection.py
Stefan Agner 8a95113ebd Improve VLAN configuration (#6094)
* Fix NetworkManager connection name for VLANs

The connection name for VLANs should include the parent interface name
for better identification. This was originally the intention, but the
interface object's name property was used which appears empty at that
point.

* Disallow creating multiple connections for the same VLAN id

Only allow a single connection per interface and VLAN id. The regular
network commands can be used to alter the configuration.

* Fix pytest

* Simply connection id name generation

Always rely on the Supervisor interface representation's name attribute
to generate the NetworkManager connection id. Make sure that the name
is correctly set when creating VLAN interfaces as well.

* Special case VLAN configuration

We can't use the match information when comparing Supervisor interface
representation with D-Bus representations. Special case VLAN and
compare using VLAN ID and parent interface.

Note that this currently compares connection UUID of the parent
interface.

* Fix pytest

* Separate VLAN creation logic from apply_changes

Apply changes is really all about updating the NetworkManager settings
of a particular network interface. The base in apply_changes() is
NetworkInterface class, which is the NetworkManager Device abstraction.
All physical interfaces have such a Device hence it is always present.

The only exception is when creating a VLAN: Since it is a virtual
device, there is no device when creating a VLAN.

This separate the two cases. This makes it much easier to reason if
a VLAN already exists or not, and to handle the case where a VLAN
needs to be created.

For all other network interfaces, the apply_changes() method can
now rely on the presence of the NetworkInterface Device abstraction.

* Add VLAN test interface and VLAN exists test

Add a test which checks that an error gets raised when a VLAN for a
particular interface/id combination already exists.

* Address pylint

* Fix test_ignore_veth_only_changes pytest

* Make VLAN interface disabled to avoid test issues

* Reference setting 38 in mocked connection

* Make sure interface type matches

Require a interface type match before doing any comparision.

* Add Supervisor host network configuration tests

* Fix device type checking

* Fix pytest

* Fix tests by taking VLAN interface into account

* Fix test_load_with_network_connection_issues

This seems like a hack, but it turns out that the additional active
connection caused coresys.host.network.update() to be called, which
implicitly "fake" activated the connection. Now it seems that our
mocking causes IPv4 gateway to be set.

So in a way, the test checked a particular mock behavior instead of
actual intention.

The crucial part of this test is that we make sure the settings remain
unchanged. This is done by ensuring that the the method is still auto.

* Fix test_check_network_interface_ipv4.py

Now that we have the VLAN interface active too it will raise an issue
as well.

* Apply suggestions from code review

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>

* Fix ruff check issue

---------

Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
2025-08-22 11:09:39 +02:00

92 lines
3.1 KiB
Python

"""Test connection object."""
from dbus_fast.aio.message_bus import MessageBus
import pytest
from supervisor.dbus.const import ConnectionStateFlags
from supervisor.dbus.network import NetworkManager
from supervisor.dbus.network.connection import NetworkConnection
from tests.const import TEST_INTERFACE_ETH_NAME
from tests.dbus_service_mocks.network_active_connection import (
ActiveConnection as ActiveConnectionService,
)
pytestmark = pytest.mark.usefixtures("active_connection_service")
async def test_active_connection(
active_connection_service: ActiveConnectionService, dbus_session_bus: MessageBus
):
"""Test active connection."""
active_connection = NetworkConnection(
"/org/freedesktop/NetworkManager/ActiveConnection/1"
)
assert active_connection.id is None
await active_connection.connect(dbus_session_bus)
assert active_connection.id == "Wired connection 1"
assert active_connection.uuid == "0c23631e-2118-355c-bbb0-8943229cb0d6"
assert active_connection.state_flags == {
ConnectionStateFlags.LIFETIME_BOUND_TO_PROFILE_VISIBILITY,
ConnectionStateFlags.IP6_READY,
ConnectionStateFlags.IP4_READY,
ConnectionStateFlags.LAYER2_READY,
}
active_connection_service.emit_properties_changed({"Id": "Wired connection 2"})
await active_connection_service.ping()
assert active_connection.id == "Wired connection 2"
active_connection_service.emit_properties_changed({}, ["Id"])
await active_connection_service.ping()
await active_connection_service.ping()
assert active_connection.id == "Wired connection 1"
async def test_old_ipv4_disconnect(
network_manager: NetworkManager, active_connection_service: ActiveConnectionService
):
"""Test old IPv4 disconnects on IPv4 change."""
connection = network_manager.get(TEST_INTERFACE_ETH_NAME).connection
ipv4 = connection.ipv4
assert ipv4.is_connected is True
active_connection_service.emit_properties_changed({"Ip4Config": "/"})
await active_connection_service.ping()
assert connection.ipv4 is None
assert ipv4.is_connected is False
async def test_old_ipv6_disconnect(
network_manager: NetworkManager, active_connection_service: ActiveConnectionService
):
"""Test old IPv6 disconnects on IPv6 change."""
connection = network_manager.get(TEST_INTERFACE_ETH_NAME).connection
ipv6 = connection.ipv6
assert ipv6.is_connected is True
active_connection_service.emit_properties_changed({"Ip6Config": "/"})
await active_connection_service.ping()
assert connection.ipv6 is None
assert ipv6.is_connected is False
async def test_old_settings_disconnect(
network_manager: NetworkManager, active_connection_service: ActiveConnectionService
):
"""Test old settings disconnects on settings change."""
connection = network_manager.get(TEST_INTERFACE_ETH_NAME).connection
settings = connection.settings
assert settings.is_connected is True
active_connection_service.emit_properties_changed({"Connection": "/"})
await active_connection_service.ping()
assert connection.settings is None
assert settings.is_connected is False