core/tests/components/logger/test_init.py

124 lines
3.9 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Logger component."""
from collections import namedtuple
2016-01-30 18:03:46 +00:00
import logging
import unittest
from homeassistant.components import logger
from homeassistant.setup import setup_component
2016-01-30 18:03:46 +00:00
from tests.common import get_test_home_assistant
2019-07-31 19:25:30 +00:00
RECORD = namedtuple("record", ("name", "levelno"))
2019-07-31 19:25:30 +00:00
NO_DEFAULT_CONFIG = {"logger": {}}
NO_LOGS_CONFIG = {"logger": {"default": "info"}}
TEST_CONFIG = {"logger": {"default": "warning", "logs": {"test": "info"}}}
2016-01-30 18:03:46 +00:00
class TestUpdater(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test logger component."""
2016-01-30 18:03:46 +00:00
def setUp(self):
2018-08-19 20:29:08 +00:00
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.log_filter = None
2016-01-30 18:03:46 +00:00
def tearDown(self):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
del logging.root.handlers[-1]
self.hass.stop()
def setup_logger(self, config):
2018-08-19 20:29:08 +00:00
"""Set up logger and save log filter."""
setup_component(self.hass, logger.DOMAIN, config)
self.log_filter = logging.root.handlers[-1].filters[0]
def assert_logged(self, name, level):
"""Assert that a certain record was logged."""
assert self.log_filter.filter(RECORD(name, level))
def assert_not_logged(self, name, level):
"""Assert that a certain record was not logged."""
assert not self.log_filter.filter(RECORD(name, level))
def test_logger_setup(self):
2016-03-09 09:25:50 +00:00
"""Use logger to create a logging filter."""
self.setup_logger(TEST_CONFIG)
2016-01-30 18:03:46 +00:00
assert len(logging.root.handlers) > 0
2016-01-30 18:03:46 +00:00
handler = logging.root.handlers[-1]
assert len(handler.filters) == 1
2016-01-30 18:03:46 +00:00
log_filter = handler.filters[0].logfilter
2019-07-31 19:25:30 +00:00
assert log_filter["default"] == logging.WARNING
assert log_filter["logs"]["test"] == logging.INFO
def test_logger_test_filters(self):
2016-03-09 09:25:50 +00:00
"""Test resulting filter operation."""
self.setup_logger(TEST_CONFIG)
2016-03-09 09:25:50 +00:00
# Blocked default record
2019-07-31 19:25:30 +00:00
self.assert_not_logged("asdf", logging.DEBUG)
2016-03-09 09:25:50 +00:00
# Allowed default record
2019-07-31 19:25:30 +00:00
self.assert_logged("asdf", logging.WARNING)
2016-03-09 09:25:50 +00:00
# Blocked named record
2019-07-31 19:25:30 +00:00
self.assert_not_logged("test", logging.DEBUG)
2016-03-09 09:25:50 +00:00
# Allowed named record
2019-07-31 19:25:30 +00:00
self.assert_logged("test", logging.INFO)
def test_set_filter_empty_config(self):
"""Test change log level from empty configuration."""
self.setup_logger(NO_LOGS_CONFIG)
2019-07-31 19:25:30 +00:00
self.assert_not_logged("test", logging.DEBUG)
2019-07-31 19:25:30 +00:00
self.hass.services.call(logger.DOMAIN, "set_level", {"test": "debug"})
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.assert_logged("test", logging.DEBUG)
def test_set_filter(self):
"""Test change log level of existing filter."""
self.setup_logger(TEST_CONFIG)
2019-07-31 19:25:30 +00:00
self.assert_not_logged("asdf", logging.DEBUG)
self.assert_logged("dummy", logging.WARNING)
2019-07-31 19:25:30 +00:00
self.hass.services.call(
logger.DOMAIN, "set_level", {"asdf": "debug", "dummy": "info"}
)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.assert_logged("asdf", logging.DEBUG)
self.assert_logged("dummy", logging.WARNING)
def test_set_default_filter_empty_config(self):
"""Test change default log level from empty configuration."""
self.setup_logger(NO_DEFAULT_CONFIG)
2019-07-31 19:25:30 +00:00
self.assert_logged("test", logging.DEBUG)
self.hass.services.call(
2019-07-31 19:25:30 +00:00
logger.DOMAIN, "set_default_level", {"level": "warning"}
)
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.assert_not_logged("test", logging.DEBUG)
def test_set_default_filter(self):
"""Test change default log level with existing default."""
self.setup_logger(TEST_CONFIG)
2019-07-31 19:25:30 +00:00
self.assert_not_logged("asdf", logging.DEBUG)
self.assert_logged("dummy", logging.WARNING)
2019-07-31 19:25:30 +00:00
self.hass.services.call(logger.DOMAIN, "set_default_level", {"level": "debug"})
self.hass.block_till_done()
2019-07-31 19:25:30 +00:00
self.assert_logged("asdf", logging.DEBUG)
self.assert_logged("dummy", logging.WARNING)