system_log improvements (#10709)
* system_log improvements * Don't use ModuleNotFoundError which is 3.6+ * Don't use FrameSummary which was added in 3.5 * Don't trace stack for exception logs * Handle test error in Python 3.4
This commit is contained in:
1 parent
dbbbe1ceef
commit
d8bf15a2f5
2 files changed
+97
-10
No files matched your search
@@ -5,6 +5,7 @@ import pytest
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
from homeassistant.components import system_log
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
_LOGGER = logging.getLogger('test_logger')
|
||||
|
||||
@@ -41,10 +42,14 @@ def assert_log(log, exception, message, level):
|
||||
assert exception in log['exception']
|
||||
assert message == log['message']
|
||||
assert level == log['level']
|
||||
assert log['source'] == 'unknown' # always unkown in tests
|
||||
assert 'timestamp' in log
|
||||
|
||||
|
||||
def get_frame(name):
|
||||
"""Get log stack frame."""
|
||||
return (name, None, None, None)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_normal_logs(hass, test_client):
|
||||
"""Test that debug and info are not logged."""
|
||||
@@ -110,3 +115,61 @@ def test_clear_logs(hass, test_client):
|
||||
|
||||
# Assert done by get_error_log
|
||||
yield from get_error_log(hass, test_client, 0)
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_unknown_path(hass, test_client):
|
||||
"""Test error logged from unknown path."""
|
||||
_LOGGER.findCaller = MagicMock(
|
||||
return_value=('unknown_path', 0, None, None))
|
||||
_LOGGER.error('error message')
|
||||
log = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'unknown_path'
|
||||
|
||||
|
||||
def log_error_from_test_path(path):
|
||||
"""Log error while mocking the path."""
|
||||
call_path = 'internal_path.py'
|
||||
with patch.object(
|
||||
_LOGGER,
|
||||
'findCaller',
|
||||
MagicMock(return_value=(call_path, 0, None, None))):
|
||||
with patch('traceback.extract_stack',
|
||||
MagicMock(return_value=[
|
||||
get_frame('main_path/main.py'),
|
||||
get_frame(path),
|
||||
get_frame(call_path),
|
||||
get_frame('venv_path/logging/log.py')])):
|
||||
_LOGGER.error('error message')
|
||||
|
||||
|
||||
@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 = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'component/component.py'
|
||||
|
||||
|
||||
@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 = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'custom_component/test.py'
|
||||
|
||||
|
||||
@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 = (yield from get_error_log(hass, test_client, 1))[0]
|
||||
assert log['source'] == 'disco_component.py'
|
||||
Reference in new issue
Block a user