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 <michael.degatano@gmail.com>
This commit is contained in:
Stefan Agner 2024-10-09 20:16:36 +02:00 committed by GitHub
parent e2b25fe7ce
commit 2968a5717c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 0 deletions

View File

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

View File

@ -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()