Fix network API default value & tests (#2234)

This commit is contained in:
Pascal Vizeli 2020-11-10 15:08:35 +01:00 committed by GitHub
parent edc8d8960f
commit 9264d437b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 4 deletions

View File

@ -181,11 +181,23 @@ class APINetwork(CoreSysAttributes):
# Apply config # Apply config
for key, config in body.items(): for key, config in body.items():
if key == ATTR_IPV4: 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: 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: 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: elif key == ATTR_ENABLED:
interface.enabled = config interface.enabled = config

View File

@ -110,7 +110,7 @@ class NetworkManager(CoreSysAttributes):
raise HostNetworkError() from err raise HostNetworkError() from err
# Remove config from interface # Remove config from interface
elif inet and not interface.enabled: elif inet and inet.settings and not interface.enabled:
try: try:
await inet.settings.delete() await inet.settings.delete()
except DBusError as err: except DBusError as err:
@ -126,6 +126,9 @@ class NetworkManager(CoreSysAttributes):
except DBusError as err: except DBusError as err:
_LOGGER.error("Can't create new interface") _LOGGER.error("Can't create new interface")
raise HostNetworkError() from err raise HostNetworkError() from err
else:
_LOGGER.warning("Requested Network interface update is not possible")
raise HostNetworkError()
await self.update() await self.update()

View File

@ -99,6 +99,37 @@ async def test_api_network_interface_update(api_client):
assert result["result"] == "ok" 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 @pytest.mark.asyncio
async def test_api_network_interface_info_invalid(api_client): async def test_api_network_interface_info_invalid(api_client):
"""Test network manager api.""" """Test network manager api."""

View File

@ -0,0 +1 @@
()

View File

@ -0,0 +1 @@
()