From 2968a5717cf59fb948d65caf93c2da74b8820507 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 9 Oct 2024 20:16:36 +0200 Subject: [PATCH] Update DNS plug-in on network change (#5331) * Update DNS plug-in on network change Restart the DNS plug-in when the primary network changes network changes. This makes sure any potential host OS DNS configuration changes get picked up by the DNS plug-in as well. * Add a test case --------- Co-authored-by: Mike Degatano --- supervisor/host/network.py | 11 +++++++++++ tests/host/test_connectivity.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/supervisor/host/network.py b/supervisor/host/network.py index ef3aa3dea..5058c3168 100644 --- a/supervisor/host/network.py +++ b/supervisor/host/network.py @@ -10,7 +10,9 @@ from ..coresys import CoreSys, CoreSysAttributes from ..dbus.const import ( DBUS_ATTR_CONNECTION_ENABLED, DBUS_ATTR_CONNECTIVITY, + DBUS_ATTR_PRIMARY_CONNECTION, DBUS_IFACE_NM, + DBUS_OBJECT_BASE, DBUS_SIGNAL_NM_CONNECTION_ACTIVE_CHANGED, ConnectionStateType, ConnectivityState, @@ -148,6 +150,15 @@ class NetworkManager(CoreSysAttributes): connectivity_check: bool | None = changed.get(DBUS_ATTR_CONNECTION_ENABLED) connectivity: bool | None = changed.get(DBUS_ATTR_CONNECTIVITY) + # This potentially updated the DNS configuration. Make sure the DNS plug-in + # picks up the latest settings. + if ( + DBUS_ATTR_PRIMARY_CONNECTION in changed + and changed[DBUS_ATTR_PRIMARY_CONNECTION] + and changed[DBUS_ATTR_PRIMARY_CONNECTION] != DBUS_OBJECT_BASE + ): + await self.sys_plugins.dns.restart() + if ( connectivity_check is True or DBUS_ATTR_CONNECTION_ENABLED in invalidated diff --git a/tests/host/test_connectivity.py b/tests/host/test_connectivity.py index 72984be06..77e3c7483 100644 --- a/tests/host/test_connectivity.py +++ b/tests/host/test_connectivity.py @@ -7,6 +7,7 @@ from unittest.mock import PropertyMock, patch import pytest from supervisor.coresys import CoreSys +from supervisor.plugins.dns import PluginDns from tests.dbus_service_mocks.network_manager import ( NetworkManager as NetworkManagerService, @@ -84,3 +85,20 @@ async def test_connectivity_events(coresys: CoreSys, force: bool): }, } ) + + +async def test_dns_restart_on_connection_change( + coresys: CoreSys, network_manager_service: NetworkManagerService +): + """Test dns plugin is restarted when primary connection changes.""" + await coresys.host.network.load() + with patch.object(PluginDns, "restart") as restart: + network_manager_service.emit_properties_changed({"PrimaryConnection": "/"}) + await network_manager_service.ping() + restart.assert_not_called() + + network_manager_service.emit_properties_changed( + {"PrimaryConnection": "/org/freedesktop/NetworkManager/ActiveConnection/2"} + ) + await network_manager_service.ping() + restart.assert_called_once()