2017-10-03 03:25:04 +00:00
|
|
|
"""Test Home Assistant logging util methods."""
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
2020-05-06 21:14:57 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-10-03 03:25:04 +00:00
|
|
|
import homeassistant.util.logging as logging_util
|
|
|
|
|
|
|
|
|
|
|
|
def test_sensitive_data_filter():
|
|
|
|
"""Test the logging sensitive data filter."""
|
2019-07-31 19:25:30 +00:00
|
|
|
log_filter = logging_util.HideSensitiveDataFilter("mock_sensitive")
|
2017-10-03 03:25:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
clean_record = logging.makeLogRecord({"msg": "clean log data"})
|
2017-10-03 03:25:04 +00:00
|
|
|
log_filter.filter(clean_record)
|
|
|
|
assert clean_record.msg == "clean log data"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
sensitive_record = logging.makeLogRecord({"msg": "mock_sensitive log"})
|
2017-10-03 03:25:04 +00:00
|
|
|
log_filter.filter(sensitive_record)
|
|
|
|
assert sensitive_record.msg == "******* log"
|
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_async_handler_loop_log(loop):
|
2019-07-10 19:17:10 +00:00
|
|
|
"""Test logging data inside from inside the event loop."""
|
2017-10-03 03:25:04 +00:00
|
|
|
loop._thread_ident = threading.get_ident()
|
|
|
|
|
|
|
|
queue = asyncio.Queue(loop=loop)
|
|
|
|
base_handler = logging.handlers.QueueHandler(queue)
|
|
|
|
handler = logging_util.AsyncHandler(loop, base_handler)
|
|
|
|
|
|
|
|
# Test passthrough props and noop functions
|
|
|
|
assert handler.createLock() is None
|
|
|
|
assert handler.acquire() is None
|
|
|
|
assert handler.release() is None
|
|
|
|
assert handler.formatter is base_handler.formatter
|
|
|
|
assert handler.name is base_handler.get_name()
|
2019-07-31 19:25:30 +00:00
|
|
|
handler.name = "mock_name"
|
|
|
|
assert base_handler.get_name() == "mock_name"
|
2017-10-03 03:25:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
log_record = logging.makeLogRecord({"msg": "Test Log Record"})
|
2017-10-03 03:25:04 +00:00
|
|
|
handler.emit(log_record)
|
2020-04-07 16:33:23 +00:00
|
|
|
await handler.async_close(True)
|
2019-07-10 19:17:10 +00:00
|
|
|
assert queue.get_nowait().msg == "Test Log Record"
|
2017-10-03 03:25:04 +00:00
|
|
|
assert queue.empty()
|
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_async_handler_thread_log(loop):
|
2019-07-10 19:17:10 +00:00
|
|
|
"""Test logging data from a thread."""
|
2017-10-03 03:25:04 +00:00
|
|
|
loop._thread_ident = threading.get_ident()
|
|
|
|
|
|
|
|
queue = asyncio.Queue(loop=loop)
|
|
|
|
base_handler = logging.handlers.QueueHandler(queue)
|
|
|
|
handler = logging_util.AsyncHandler(loop, base_handler)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
log_record = logging.makeLogRecord({"msg": "Test Log Record"})
|
2017-10-03 03:25:04 +00:00
|
|
|
|
|
|
|
def add_log():
|
|
|
|
"""Emit a mock log."""
|
|
|
|
handler.emit(log_record)
|
|
|
|
handler.close()
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
await loop.run_in_executor(None, add_log)
|
|
|
|
await handler.async_close(True)
|
2017-10-03 03:25:04 +00:00
|
|
|
|
2019-07-10 19:17:10 +00:00
|
|
|
assert queue.get_nowait().msg == "Test Log Record"
|
2017-10-03 03:25:04 +00:00
|
|
|
assert queue.empty()
|
2019-04-29 16:53:22 +00:00
|
|
|
|
|
|
|
|
2020-05-06 21:14:57 +00:00
|
|
|
@pytest.mark.no_fail_on_log_exception
|
2019-04-29 16:53:22 +00:00
|
|
|
async def test_async_create_catching_coro(hass, caplog):
|
|
|
|
"""Test exception logging of wrapped coroutine."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-04-29 16:53:22 +00:00
|
|
|
async def job():
|
2019-07-31 19:25:30 +00:00
|
|
|
raise Exception("This is a bad coroutine")
|
2019-04-29 16:53:22 +00:00
|
|
|
|
|
|
|
hass.async_create_task(logging_util.async_create_catching_coro(job()))
|
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "This is a bad coroutine" in caplog.text
|
2019-10-05 09:59:34 +00:00
|
|
|
assert "in test_async_create_catching_coro" in caplog.text
|