Working on igd

This commit is contained in:
Steven Looman committed 2018-09-08 00:24:47 +02:00
1 parent f4a54e2483
commit 6433f2e2e3
6 files changed
+255 -134

No files matched your search

+76 -28
View File
@@ -1,15 +1,51 @@
"""Test IGD setup process."""
from ipaddress import ip_address
from unittest.mock import patch, MagicMock
from homeassistant.setup import async_setup_component
from homeassistant.components import igd
from homeassistant.components.igd.device import Device
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from tests.common import MockConfigEntry
from tests.common import mock_coro
class MockDevice(Device):
"""Mock device for Device."""
def __init__(self, udn):
"""Initializer."""
super().__init__(None)
self._udn = udn
self.added_port_mappings = []
self.removed_port_mappings = []
@classmethod
async def async_create_device(cls, hass, ssdp_description):
"""Return self."""
return cls()
@property
def udn(self):
"""Get the UDN."""
return self._udn
async def _async_add_port_mapping(self,
external_port,
local_ip,
internal_port):
"""Add a port mapping."""
entry = [external_port, local_ip, internal_port]
self.added_port_mappings.append(entry)
async def _async_delete_port_mapping(self, external_port):
"""Remove a port mapping."""
entry = external_port
self.removed_port_mappings.append(entry)
async def test_async_setup_no_auto_config(hass):
"""Test async_setup."""
# setup component, enable auto_config
@@ -18,6 +54,7 @@ async def test_async_setup_no_auto_config(hass):
assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': False,
'port_forward': False,
'ports': {},
'sensors': False,
}
@@ -30,6 +67,7 @@ async def test_async_setup_auto_config(hass):
assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': True,
'port_forward': False,
'ports': {},
'sensors': True,
}
@@ -38,12 +76,13 @@ async def test_async_setup_auto_config_port_forward(hass):
"""Test async_setup."""
# setup component, enable auto_config
await async_setup_component(hass, 'igd', {
'igd': {'port_forward': True},
'igd': {'port_forward': True, 'ports': {8123: 8123}},
'discovery': {}})
assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': True,
'port_forward': True,
'ports': {8123: 8123},
'sensors': True,
}
@@ -58,6 +97,7 @@ async def test_async_setup_auto_config_no_sensors(hass):
assert hass.data[igd.DOMAIN]['auto_config'] == {
'active': True,
'port_forward': False,
'ports': {},
'sensors': False,
}
@@ -75,27 +115,30 @@ async def test_async_setup_entry_default(hass):
# ensure hass.http is available
await async_setup_component(hass, 'igd')
# mock async_upnp_client.igd.IgdDevice
mock_igd_device = MagicMock()
mock_igd_device.udn = udn
mock_igd_device.async_add_port_mapping.return_value = mock_coro()
mock_igd_device.async_delete_port_mapping.return_value = mock_coro()
with patch.object(igd, '_async_create_igd_device') as mock_create_device:
# mock homeassistant.components.igd.device.Device
mock_device = MagicMock()
mock_device.udn = udn
mock_device.async_add_port_mappings.return_value = mock_coro()
mock_device.async_delete_port_mappings.return_value = mock_coro()
with patch.object(Device, 'async_create_device') as mock_create_device:
mock_create_device.return_value = mock_coro(
return_value=mock_igd_device)
with patch('homeassistant.components.igd.get_local_ip',
return_value=mock_device)
with patch('homeassistant.components.igd.device.get_local_ip',
return_value='192.168.1.10'):
assert await igd.async_setup_entry(hass, entry) is True
# ensure device is stored/used
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_igd_device
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_device
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
# ensure cleaned up
assert udn not in hass.data[igd.DOMAIN]['devices']
assert len(mock_igd_device.async_add_port_mapping.mock_calls) == 0
assert len(mock_igd_device.async_delete_port_mapping.mock_calls) == 0
# ensure no port-mapping-methods called
assert len(mock_device.async_add_port_mappings.mock_calls) == 0
assert len(mock_device.async_delete_port_mappings.mock_calls) == 0
async def test_async_setup_entry_port_forward(hass):
@@ -109,25 +152,30 @@ async def test_async_setup_entry_port_forward(hass):
})
# ensure hass.http is available
await async_setup_component(hass, 'igd')
await async_setup_component(hass, 'igd', {
'igd': {'port_forward': True, 'ports': {8123: 8123}},
'discovery': {}})
mock_igd_device = MagicMock()
mock_igd_device.udn = udn
mock_igd_device.async_add_port_mapping.return_value = mock_coro()
mock_igd_device.async_delete_port_mapping.return_value = mock_coro()
with patch.object(igd, '_async_create_igd_device') as mock_create_device:
mock_create_device.return_value = mock_coro(
return_value=mock_igd_device)
with patch('homeassistant.components.igd.get_local_ip',
mock_device = MockDevice(udn)
with patch.object(Device, 'async_create_device') as mock_create_device:
mock_create_device.return_value = mock_coro(return_value=mock_device)
with patch('homeassistant.components.igd.device.get_local_ip',
return_value='192.168.1.10'):
assert await igd.async_setup_entry(hass, entry) is True
# ensure device is stored/used
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_igd_device
assert hass.data[igd.DOMAIN]['devices'][udn] == mock_device
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
# ensure add-port-mapping-methods called
assert mock_device.added_port_mappings == [
[8123, ip_address('192.168.1.10'), 8123]
]
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
# ensure cleaned up
assert udn not in hass.data[igd.DOMAIN]['devices']
assert len(mock_igd_device.async_add_port_mapping.mock_calls) > 0
assert len(mock_igd_device.async_delete_port_mapping.mock_calls) > 0
# ensure delete-port-mapping-methods called
assert mock_device.removed_port_mappings == [8123]