core/tests/util/test_logging.py

81 lines
2.5 KiB
Python
Raw Normal View History

"""Test Home Assistant logging util methods."""
import asyncio
import logging
import threading
import pytest
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")
2019-07-31 19:25:30 +00:00
clean_record = logging.makeLogRecord({"msg": "clean log data"})
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"})
log_filter.filter(sensitive_record)
assert sensitive_record.msg == "******* log"
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."""
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"
2019-07-31 19:25:30 +00:00
log_record = logging.makeLogRecord({"msg": "Test Log Record"})
handler.emit(log_record)
await handler.async_close(True)
2019-07-10 19:17:10 +00:00
assert queue.get_nowait().msg == "Test Log Record"
assert queue.empty()
async def test_async_handler_thread_log(loop):
2019-07-10 19:17:10 +00:00
"""Test logging data from a thread."""
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"})
def add_log():
"""Emit a mock log."""
handler.emit(log_record)
handler.close()
await loop.run_in_executor(None, add_log)
await handler.async_close(True)
2019-07-10 19:17:10 +00:00
assert queue.get_nowait().msg == "Test Log Record"
assert queue.empty()
@pytest.mark.no_fail_on_log_exception
async def test_async_create_catching_coro(hass, caplog):
"""Test exception logging of wrapped coroutine."""
2019-07-31 19:25:30 +00:00
async def job():
2019-07-31 19:25:30 +00:00
raise Exception("This is a bad coroutine")
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
assert "in test_async_create_catching_coro" in caplog.text