Reduce patching call dbus in tests (#3866)

This commit is contained in:
Mike Degatano 2022-09-13 15:22:15 -04:00 committed by GitHub
parent d195f19fa8
commit 5f98ab7e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 65 deletions

View File

@ -1,5 +1,5 @@
"""Test NetwrokInterface API.""" """Test NetwrokInterface API."""
from unittest.mock import AsyncMock, Mock, patch from unittest.mock import AsyncMock, patch
import pytest import pytest
@ -99,13 +99,11 @@ async def test_api_network_interface_info_default(api_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_api_network_interface_update(api_client, coresys: CoreSys): async def test_api_network_interface_update(
api_client, coresys: CoreSys, dbus: list[str]
):
"""Test network manager api.""" """Test network manager api."""
with patch.object( dbus.clear()
type(coresys.host.sys_dbus.network),
"check_connectivity",
new=Mock(wraps=coresys.host.sys_dbus.network.check_connectivity),
) as check_connectivity:
resp = await api_client.post( resp = await api_client.post(
f"/network/interface/{TEST_INTERFACE}/update", f"/network/interface/{TEST_INTERFACE}/update",
json={ json={
@ -119,7 +117,10 @@ async def test_api_network_interface_update(api_client, coresys: CoreSys):
) )
result = await resp.json() result = await resp.json()
assert result["result"] == "ok" assert result["result"] == "ok"
check_connectivity.assert_called_once_with(force=True) assert (
"/org/freedesktop/NetworkManager-org.freedesktop.NetworkManager.CheckConnectivity"
in dbus
)
@pytest.mark.asyncio @pytest.mark.asyncio

View File

@ -13,17 +13,9 @@ from supervisor.host.network import Interface, IpConfig
from supervisor.utils.dbus import DBus from supervisor.utils.dbus import DBus
async def test_load(coresys: CoreSys): async def test_load(coresys: CoreSys, dbus: list[str]):
"""Test network manager load.""" """Test network manager load."""
with patch.object( dbus.clear()
type(coresys.host.sys_dbus.network),
"activate_connection",
new=Mock(wraps=coresys.host.sys_dbus.network.activate_connection),
) as activate_connection, patch.object(
type(coresys.host.sys_dbus.network),
"check_connectivity",
new=Mock(wraps=coresys.host.sys_dbus.network.check_connectivity),
) as check_connectivity:
await coresys.host.network.load() await coresys.host.network.load()
assert coresys.host.network.connectivity is True assert coresys.host.network.connectivity is True
@ -35,9 +27,7 @@ async def test_load(coresys: CoreSys):
assert coresys.host.network.interfaces[0].name == "eth0" assert coresys.host.network.interfaces[0].name == "eth0"
assert coresys.host.network.interfaces[0].enabled is True assert coresys.host.network.interfaces[0].enabled is True
assert coresys.host.network.interfaces[0].ipv4.method == InterfaceMethod.AUTO assert coresys.host.network.interfaces[0].ipv4.method == InterfaceMethod.AUTO
assert coresys.host.network.interfaces[0].ipv4.gateway == IPv4Address( assert coresys.host.network.interfaces[0].ipv4.gateway == IPv4Address("192.168.2.1")
"192.168.2.1"
)
assert coresys.host.network.interfaces[0].ipv4.ready is True assert coresys.host.network.interfaces[0].ipv4.ready is True
assert coresys.host.network.interfaces[0].ipv6.method == InterfaceMethod.AUTO assert coresys.host.network.interfaces[0].ipv6.method == InterfaceMethod.AUTO
assert coresys.host.network.interfaces[0].ipv6.gateway == IPv6Address( assert coresys.host.network.interfaces[0].ipv6.gateway == IPv6Address(
@ -47,14 +37,18 @@ async def test_load(coresys: CoreSys):
assert coresys.host.network.interfaces[1].name == "wlan0" assert coresys.host.network.interfaces[1].name == "wlan0"
assert coresys.host.network.interfaces[1].enabled is False assert coresys.host.network.interfaces[1].enabled is False
activate_connection.assert_called_once_with( assert (
"/org/freedesktop/NetworkManager/Settings/1", "/org/freedesktop/NetworkManager-org.freedesktop.NetworkManager.ActivateConnection"
"/org/freedesktop/NetworkManager/Devices/1", in dbus
)
assert (
"/org/freedesktop/NetworkManager-org.freedesktop.NetworkManager.Connectivity"
in dbus
)
assert (
"/org/freedesktop/NetworkManager-org.freedesktop.NetworkManager.CheckConnectivity"
not in dbus
) )
assert check_connectivity.call_count == 2
assert check_connectivity.call_args_list[0][1] == {"force": False}
assert check_connectivity.call_args_list[1][1] == {"force": False}
async def test_load_with_disabled_methods(coresys: CoreSys): async def test_load_with_disabled_methods(coresys: CoreSys):
@ -82,7 +76,7 @@ async def test_load_with_disabled_methods(coresys: CoreSys):
activate_connection.assert_not_called() activate_connection.assert_not_called()
async def test_load_with_network_connection_issues(coresys: CoreSys): async def test_load_with_network_connection_issues(coresys: CoreSys, dbus: list[str]):
"""Test load does not update interfaces with network connection issues.""" """Test load does not update interfaces with network connection issues."""
with patch( with patch(
"supervisor.dbus.network.connection.NetworkConnection.state_flags", "supervisor.dbus.network.connection.NetworkConnection.state_flags",
@ -90,14 +84,14 @@ async def test_load_with_network_connection_issues(coresys: CoreSys):
), patch( ), patch(
"supervisor.dbus.network.connection.NetworkConnection.ipv4", "supervisor.dbus.network.connection.NetworkConnection.ipv4",
new=PropertyMock(return_value=None), new=PropertyMock(return_value=None),
), patch.object( ):
coresys.host.sys_dbus.network, dbus.clear()
"activate_connection",
new=Mock(wraps=coresys.host.sys_dbus.network.activate_connection),
) as activate_connection:
await coresys.host.network.load() await coresys.host.network.load()
activate_connection.assert_not_called() assert (
"/org/freedesktop/NetworkManager-org.freedesktop.NetworkManager.ActivateConnection"
not in dbus
)
assert len(coresys.host.network.interfaces) == 2 assert len(coresys.host.network.interfaces) == 2
assert coresys.host.network.interfaces[0].name == "eth0" assert coresys.host.network.interfaces[0].name == "eth0"