From 9264d437b15fed4eaeb588fe387f912bb52063ba Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Tue, 10 Nov 2020 15:08:35 +0100 Subject: [PATCH] Fix network API default value & tests (#2234) --- supervisor/api/network.py | 18 +++++++++-- supervisor/host/network.py | 5 ++- tests/api/test_network.py | 31 +++++++++++++++++++ ...rkManager-AddAndActivateConnection.fixture | 1 + ...p_NetworkManager_Settings_1-Delete.fixture | 1 + 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/org_freedesktop_NetworkManager-AddAndActivateConnection.fixture create mode 100644 tests/fixtures/org_freedesktop_NetworkManager_Settings_1-Delete.fixture diff --git a/supervisor/api/network.py b/supervisor/api/network.py index bf93f6ee8..345c5b09a 100644 --- a/supervisor/api/network.py +++ b/supervisor/api/network.py @@ -181,11 +181,23 @@ class APINetwork(CoreSysAttributes): # Apply config for key, config in body.items(): if key == ATTR_IPV4: - interface.ipv4 = attr.evolve(interface.ipv4, **config) + interface.ipv4 = attr.evolve( + interface.ipv4 or IpConfig(InterfaceMethod.STATIC, [], None, []), + **config, + ) elif key == ATTR_IPV6: - interface.ipv6 = attr.evolve(interface.ipv6, **config) + interface.ipv6 = attr.evolve( + interface.ipv6 or IpConfig(InterfaceMethod.STATIC, [], None, []), + **config, + ) elif key == ATTR_WIFI: - interface.wifi = attr.evolve(interface.wifi, **config) + interface.wifi = attr.evolve( + interface.wifi + or WifiConfig( + WifiMode.INFRASTRUCTURE, "", AuthMethod.OPEN, None, None + ), + **config, + ) elif key == ATTR_ENABLED: interface.enabled = config diff --git a/supervisor/host/network.py b/supervisor/host/network.py index 6fabc4bb9..eac11f51b 100644 --- a/supervisor/host/network.py +++ b/supervisor/host/network.py @@ -110,7 +110,7 @@ class NetworkManager(CoreSysAttributes): raise HostNetworkError() from err # Remove config from interface - elif inet and not interface.enabled: + elif inet and inet.settings and not interface.enabled: try: await inet.settings.delete() except DBusError as err: @@ -126,6 +126,9 @@ class NetworkManager(CoreSysAttributes): except DBusError as err: _LOGGER.error("Can't create new interface") raise HostNetworkError() from err + else: + _LOGGER.warning("Requested Network interface update is not possible") + raise HostNetworkError() await self.update() diff --git a/tests/api/test_network.py b/tests/api/test_network.py index b903d30c9..f4f2739ae 100644 --- a/tests/api/test_network.py +++ b/tests/api/test_network.py @@ -99,6 +99,37 @@ async def test_api_network_interface_update(api_client): assert result["result"] == "ok" +@pytest.mark.asyncio +async def test_api_network_interface_update_wifi(api_client): + """Test network manager api.""" + resp = await api_client.post( + f"/network/interface/{TEST_INTERFACE_WLAN}/update", + json={ + "enabled": True, + "ipv4": { + "method": "static", + "nameservers": ["1.1.1.1"], + "address": ["192.168.2.148/24"], + "gateway": "192.168.1.1", + }, + "wifi": {"ssid": "MY_TEST", "auth": "wpa-psk", "psk": "myWifiPassword"}, + }, + ) + result = await resp.json() + assert result["result"] == "ok" + + +@pytest.mark.asyncio +async def test_api_network_interface_update_remove(api_client): + """Test network manager api.""" + resp = await api_client.post( + f"/network/interface/{TEST_INTERFACE}/update", + json={"enabled": False}, + ) + result = await resp.json() + assert result["result"] == "ok" + + @pytest.mark.asyncio async def test_api_network_interface_info_invalid(api_client): """Test network manager api.""" diff --git a/tests/fixtures/org_freedesktop_NetworkManager-AddAndActivateConnection.fixture b/tests/fixtures/org_freedesktop_NetworkManager-AddAndActivateConnection.fixture new file mode 100644 index 000000000..dd626a0f3 --- /dev/null +++ b/tests/fixtures/org_freedesktop_NetworkManager-AddAndActivateConnection.fixture @@ -0,0 +1 @@ +() \ No newline at end of file diff --git a/tests/fixtures/org_freedesktop_NetworkManager_Settings_1-Delete.fixture b/tests/fixtures/org_freedesktop_NetworkManager_Settings_1-Delete.fixture new file mode 100644 index 000000000..6a452c185 --- /dev/null +++ b/tests/fixtures/org_freedesktop_NetworkManager_Settings_1-Delete.fixture @@ -0,0 +1 @@ +()