2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Logger component."""
|
2016-01-30 18:23:35 +00:00
|
|
|
from collections import namedtuple
|
2016-01-30 18:03:46 +00:00
|
|
|
import logging
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from homeassistant.components import logger
|
2019-12-09 13:22:30 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2016-01-30 18:03:46 +00:00
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
RECORD = namedtuple("record", ("name", "levelno"))
|
2016-01-30 18:23:35 +00:00
|
|
|
|
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"}}}
|
2017-02-26 22:15:44 +00:00
|
|
|
|
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
|
|
|
|
2016-01-30 18:23:35 +00:00
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-02-26 22:15:44 +00:00
|
|
|
self.log_filter = None
|
2016-01-30 18:03:46 +00:00
|
|
|
|
2016-01-30 18:23:35 +00:00
|
|
|
def tearDown(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2016-01-30 18:23:35 +00:00
|
|
|
del logging.root.handlers[-1]
|
2016-10-27 07:16:23 +00:00
|
|
|
self.hass.stop()
|
2016-01-30 18:23:35 +00:00
|
|
|
|
2017-02-26 22:15:44 +00:00
|
|
|
def setup_logger(self, config):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up logger and save log filter."""
|
2017-02-26 22:15:44 +00:00
|
|
|
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."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.log_filter.filter(RECORD(name, level))
|
2017-02-26 22:15:44 +00:00
|
|
|
|
|
|
|
def assert_not_logged(self, name, level):
|
|
|
|
"""Assert that a certain record was not logged."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not self.log_filter.filter(RECORD(name, level))
|
2017-02-26 22:15:44 +00:00
|
|
|
|
2016-01-30 18:23:35 +00:00
|
|
|
def test_logger_setup(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Use logger to create a logging filter."""
|
2017-02-26 22:15:44 +00:00
|
|
|
self.setup_logger(TEST_CONFIG)
|
2016-01-30 18:03:46 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert len(logging.root.handlers) > 0
|
2016-01-30 18:03:46 +00:00
|
|
|
handler = logging.root.handlers[-1]
|
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
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
|
2016-01-30 18:23:35 +00:00
|
|
|
|
|
|
|
def test_logger_test_filters(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test resulting filter operation."""
|
2017-02-26 22:15:44 +00:00
|
|
|
self.setup_logger(TEST_CONFIG)
|
2016-01-30 18:23:35 +00:00
|
|
|
|
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-01-30 18:23:35 +00:00
|
|
|
|
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-01-30 18:23:35 +00:00
|
|
|
|
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-01-30 18:23:35 +00:00
|
|
|
|
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)
|
2017-02-26 22:15:44 +00:00
|
|
|
|
|
|
|
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)
|
2017-02-26 22:15:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(logger.DOMAIN, "set_level", {"test": "debug"})
|
2017-02-26 22:15:44 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_logged("test", logging.DEBUG)
|
2017-02-26 22:15:44 +00:00
|
|
|
|
|
|
|
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)
|
2017-02-26 22:15:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(
|
|
|
|
logger.DOMAIN, "set_level", {"asdf": "debug", "dummy": "info"}
|
|
|
|
)
|
2017-02-26 22:15:44 +00:00
|
|
|
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)
|
2018-06-07 21:03:04 +00:00
|
|
|
|
|
|
|
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)
|
2018-06-07 21:03:04 +00:00
|
|
|
|
|
|
|
self.hass.services.call(
|
2019-07-31 19:25:30 +00:00
|
|
|
logger.DOMAIN, "set_default_level", {"level": "warning"}
|
|
|
|
)
|
2018-06-07 21:03:04 +00:00
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_not_logged("test", logging.DEBUG)
|
2018-06-07 21:03:04 +00:00
|
|
|
|
|
|
|
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)
|
2018-06-07 21:03:04 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(logger.DOMAIN, "set_default_level", {"level": "debug"})
|
2018-06-07 21:03:04 +00:00
|
|
|
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)
|