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
|
|
|
|
|
2016-09-20 04:12:56 +00:00
|
|
|
from homeassistant.bootstrap import setup_component
|
2016-01-30 18:03:46 +00:00
|
|
|
from homeassistant.components import logger
|
|
|
|
|
2016-10-27 07:16:23 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2016-01-30 18:23:35 +00:00
|
|
|
RECORD = namedtuple('record', ('name', 'levelno'))
|
|
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Setup things to be run when tests are started."""
|
2016-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-01-30 18:23:35 +00:00
|
|
|
self.log_config = {'logger':
|
|
|
|
{'default': 'warning', 'logs': {'test': 'info'}}}
|
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
|
|
|
|
|
|
|
def test_logger_setup(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Use logger to create a logging filter."""
|
2016-10-27 07:16:23 +00:00
|
|
|
setup_component(self.hass, logger.DOMAIN, self.log_config)
|
2016-01-30 18:03:46 +00:00
|
|
|
|
|
|
|
self.assertTrue(len(logging.root.handlers) > 0)
|
|
|
|
handler = logging.root.handlers[-1]
|
|
|
|
|
|
|
|
self.assertEqual(len(handler.filters), 1)
|
|
|
|
log_filter = handler.filters[0].logfilter
|
|
|
|
|
|
|
|
self.assertEqual(log_filter['default'], logging.WARNING)
|
|
|
|
self.assertEqual(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."""
|
2016-10-27 07:16:23 +00:00
|
|
|
setup_component(self.hass, logger.DOMAIN, self.log_config)
|
2016-01-30 18:23:35 +00:00
|
|
|
|
|
|
|
log_filter = logging.root.handlers[-1].filters[0]
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Blocked default record
|
2016-01-30 18:23:35 +00:00
|
|
|
record = RECORD('asdf', logging.DEBUG)
|
|
|
|
self.assertFalse(log_filter.filter(record))
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Allowed default record
|
2016-01-30 18:23:35 +00:00
|
|
|
record = RECORD('asdf', logging.WARNING)
|
|
|
|
self.assertTrue(log_filter.filter(record))
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Blocked named record
|
2016-01-30 18:23:35 +00:00
|
|
|
record = RECORD('test', logging.DEBUG)
|
|
|
|
self.assertFalse(log_filter.filter(record))
|
|
|
|
|
2016-03-09 09:25:50 +00:00
|
|
|
# Allowed named record
|
2016-01-30 18:23:35 +00:00
|
|
|
record = RECORD('test', logging.INFO)
|
|
|
|
self.assertTrue(log_filter.filter(record))
|