Identify network interfaces by mac over name (#4416)

* Identify network interfaces by mac over name

* Refactor long if statement into method
This commit is contained in:
Mike Degatano
2023-07-06 16:26:19 -04:00
committed by GitHub
parent 96d5fc244e
commit abbf8b9b65
18 changed files with 434 additions and 334 deletions

View File

@@ -1,7 +1,9 @@
"""Test NetwrokInterface API."""
from unittest.mock import AsyncMock, patch
from aiohttp.test_utils import TestClient
from dbus_fast import Variant
import pytest
from supervisor.const import DOCKER_NETWORK, DOCKER_NETWORK_MASK
from supervisor.coresys import CoreSys
@@ -17,7 +19,7 @@ from tests.dbus_service_mocks.network_manager import (
from tests.dbus_service_mocks.network_settings import Settings as SettingsService
async def test_api_network_info(api_client, coresys: CoreSys):
async def test_api_network_info(api_client: TestClient, coresys: CoreSys):
"""Test network manager api."""
resp = await api_client.get("/network/info")
result = await resp.json()
@@ -32,8 +34,10 @@ async def test_api_network_info(api_client, coresys: CoreSys):
if interface["interface"] == TEST_INTERFACE:
assert interface["primary"]
assert interface["ipv4"]["gateway"] == "192.168.2.1"
assert interface["mac"] == "AA:BB:CC:DD:EE:FF"
if interface["interface"] == TEST_INTERFACE_WLAN:
assert not interface["primary"]
assert interface["mac"] == "FF:EE:DD:CC:BB:AA"
assert interface["ipv4"] == {
"address": [],
"gateway": None,
@@ -55,9 +59,10 @@ async def test_api_network_info(api_client, coresys: CoreSys):
assert result["data"]["docker"]["gateway"] == str(coresys.docker.network.gateway)
async def test_api_network_interface_info(api_client):
@pytest.mark.parametrize("intr_id", [TEST_INTERFACE, "AA:BB:CC:DD:EE:FF"])
async def test_api_network_interface_info(api_client: TestClient, intr_id: str):
"""Test network manager api."""
resp = await api_client.get(f"/network/interface/{TEST_INTERFACE}/info")
resp = await api_client.get(f"/network/interface/{intr_id}/info")
result = await resp.json()
assert result["data"]["ipv4"]["address"][-1] == "192.168.2.148/24"
assert result["data"]["ipv4"]["gateway"] == "192.168.2.1"
@@ -76,7 +81,7 @@ async def test_api_network_interface_info(api_client):
assert result["data"]["interface"] == TEST_INTERFACE
async def test_api_network_interface_info_default(api_client):
async def test_api_network_interface_info_default(api_client: TestClient):
"""Test network manager default api."""
resp = await api_client.get("/network/interface/default/info")
result = await resp.json()
@@ -97,21 +102,21 @@ async def test_api_network_interface_info_default(api_client):
assert result["data"]["interface"] == TEST_INTERFACE
@pytest.mark.parametrize("intr_id", [TEST_INTERFACE, "AA:BB:CC:DD:EE:FF"])
async def test_api_network_interface_update(
api_client,
api_client: TestClient,
coresys: CoreSys,
network_manager_service: NetworkManagerService,
connection_settings_service: ConnectionSettingsService,
intr_id: str,
):
"""Test network manager api."""
network_manager_service.CheckConnectivity.calls.clear()
connection_settings_service.Update.calls.clear()
assert (
coresys.dbus.network.interfaces[TEST_INTERFACE].settings.ipv4.method == "auto"
)
assert coresys.dbus.network.get(TEST_INTERFACE).settings.ipv4.method == "auto"
resp = await api_client.post(
f"/network/interface/{TEST_INTERFACE}/update",
f"/network/interface/{intr_id}/update",
json={
"ipv4": {
"method": "static",
@@ -128,12 +133,10 @@ async def test_api_network_interface_update(
await connection_settings_service.ping()
await connection_settings_service.ping()
assert (
coresys.dbus.network.interfaces[TEST_INTERFACE].settings.ipv4.method == "manual"
)
assert coresys.dbus.network.get(TEST_INTERFACE).settings.ipv4.method == "manual"
async def test_api_network_interface_update_wifi(api_client):
async def test_api_network_interface_update_wifi(api_client: TestClient):
"""Test network manager api."""
resp = await api_client.post(
f"/network/interface/{TEST_INTERFACE_WLAN}/update",
@@ -152,7 +155,7 @@ async def test_api_network_interface_update_wifi(api_client):
assert result["result"] == "ok"
async def test_api_network_interface_update_remove(api_client):
async def test_api_network_interface_update_remove(api_client: TestClient):
"""Test network manager api."""
resp = await api_client.post(
f"/network/interface/{TEST_INTERFACE}/update",
@@ -162,7 +165,7 @@ async def test_api_network_interface_update_remove(api_client):
assert result["result"] == "ok"
async def test_api_network_interface_info_invalid(api_client):
async def test_api_network_interface_info_invalid(api_client: TestClient):
"""Test network manager api."""
resp = await api_client.get("/network/interface/invalid/info")
result = await resp.json()
@@ -171,7 +174,7 @@ async def test_api_network_interface_info_invalid(api_client):
assert result["result"] == "error"
async def test_api_network_interface_update_invalid(api_client):
async def test_api_network_interface_update_invalid(api_client: TestClient):
"""Test network manager api."""
resp = await api_client.post("/network/interface/invalid/update", json={})
result = await resp.json()
@@ -192,7 +195,7 @@ async def test_api_network_interface_update_invalid(api_client):
)
async def test_api_network_wireless_scan(api_client):
async def test_api_network_wireless_scan(api_client: TestClient):
"""Test network manager api."""
with patch("asyncio.sleep", return_value=AsyncMock()):
resp = await api_client.get(
@@ -207,7 +210,9 @@ async def test_api_network_wireless_scan(api_client):
async def test_api_network_reload(
api_client, coresys, network_manager_service: NetworkManagerService
api_client: TestClient,
coresys: CoreSys,
network_manager_service: NetworkManagerService,
):
"""Test network manager reload api."""
network_manager_service.CheckConnectivity.calls.clear()
@@ -220,7 +225,7 @@ async def test_api_network_reload(
async def test_api_network_vlan(
api_client,
api_client: TestClient,
coresys: CoreSys,
network_manager_services: dict[str, DBusServiceMock | dict[str, DBusServiceMock]],
):