Publish errors on the event bus (#11964)
* Publish errors on the event bus * Add block till done to test. * Update test_system_log.py * Remove old logger handlers
This commit is contained in:
1 parent
12182d6e49
commit
dfd2d631ae
2 files changed
+111
-78
No files matched your search
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import system_log
|
||||
|
||||
@@ -13,7 +14,7 @@ _LOGGER = logging.getLogger('test_logger')
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@asyncio.coroutine
|
||||
def setup_test_case(hass):
|
||||
def setup_test_case(hass, test_client):
|
||||
"""Setup system_log component before test case."""
|
||||
config = {'system_log': {'max_entries': 2}}
|
||||
yield from async_setup_component(hass, system_log.DOMAIN, config)
|
||||
@@ -85,6 +86,25 @@ def test_error(hass, test_client):
|
||||
assert_log(log, '', 'error message', 'ERROR')
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_error_posted_as_event(hass, test_client):
|
||||
"""Test that error are posted as events."""
|
||||
events = []
|
||||
|
||||
@callback
|
||||
def event_listener(event):
|
||||
"""Listen to events of type system_log_event."""
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen(system_log.EVENT_SYSTEM_LOG, event_listener)
|
||||
|
||||
_LOGGER.error('error message')
|
||||
yield from hass.async_block_till_done()
|
||||
|
||||
assert len(events) == 1
|
||||
assert_log(events[0].data, '', 'error message', 'ERROR')
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_critical(hass, test_client):
|
||||
"""Test that critical are logged and retrieved correctly."""
|
||||
@@ -189,10 +209,10 @@ def log_error_from_test_path(path):
|
||||
@asyncio.coroutine
|
||||
def test_homeassistant_path(hass, test_client):
|
||||
"""Test error logged from homeassistant path."""
|
||||
log_error_from_test_path('venv_path/homeassistant/component/component.py')
|
||||
|
||||
with patch('homeassistant.components.system_log.HOMEASSISTANT_PATH',
|
||||
new=['venv_path/homeassistant']):
|
||||
log_error_from_test_path(
|
||||
'venv_path/homeassistant/component/component.py')
|
||||
log = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'component/component.py'
|
||||
|
||||
@@ -200,9 +220,8 @@ def test_homeassistant_path(hass, test_client):
|
||||
@asyncio.coroutine
|
||||
def test_config_path(hass, test_client):
|
||||
"""Test error logged from config path."""
|
||||
log_error_from_test_path('config/custom_component/test.py')
|
||||
|
||||
with patch.object(hass.config, 'config_dir', new='config'):
|
||||
log_error_from_test_path('config/custom_component/test.py')
|
||||
log = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'custom_component/test.py'
|
||||
|
||||
@@ -210,9 +229,8 @@ def test_config_path(hass, test_client):
|
||||
@asyncio.coroutine
|
||||
def test_netdisco_path(hass, test_client):
|
||||
"""Test error logged from netdisco path."""
|
||||
log_error_from_test_path('venv_path/netdisco/disco_component.py')
|
||||
|
||||
with patch.dict('sys.modules',
|
||||
netdisco=MagicMock(__path__=['venv_path/netdisco'])):
|
||||
log_error_from_test_path('venv_path/netdisco/disco_component.py')
|
||||
log = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'disco_component.py'
|
||||
Reference in new issue
Block a user