From 5f2120b353dad294cf98fbc2cc704e742d87d6be Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Mon, 29 Jan 2018 15:25:39 +0100 Subject: gbplogtaster: capture INFO and DEBUG log levels too This allows to test for certain features more easily and to use it not only in the error case. Using two different streams allows us to test that messages still end up on stderr or stdout correctly. --- tests/testutils/gbplogtester.py | 44 +++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/testutils/gbplogtester.py b/tests/testutils/gbplogtester.py index 10d344e9..b42e0901 100644 --- a/tests/testutils/gbplogtester.py +++ b/tests/testutils/gbplogtester.py @@ -13,34 +13,54 @@ class GbpLogTester(object): """ def __init__(self): """Object initialization""" + # Warnings and Errors self._log = None self._loghandler = None + # Info and Debug messages + self._log_info = None + self._loghandler_info = None def _capture_log(self, capture=True): """ Capture log""" if capture: assert self._log is None, "Log capture already started" + + handlers = list(gbp.log.LOGGER.handlers) + for hdl in handlers: + gbp.log.LOGGER.removeHandler(hdl) + self._log = StringIO() self._loghandler = gbp.log.GbpStreamHandler(self._log, False) self._loghandler.addFilter(gbp.log.GbpFilter([gbp.log.WARNING, gbp.log.ERROR])) - handlers = list(gbp.log.LOGGER.handlers) - for hdl in handlers: - gbp.log.LOGGER.removeHandler(hdl) + + self._log_info = StringIO() + self._loghandler_info = gbp.log.GbpStreamHandler(self._log_info, False) + self._loghandler_info.addFilter(gbp.log.GbpFilter([gbp.log.DEBUG, + gbp.log.INFO])) gbp.log.LOGGER.addHandler(self._loghandler) + gbp.log.LOGGER.addHandler(self._loghandler_info) else: assert self._log is not None, "Log capture not started" gbp.log.LOGGER.removeHandler(self._loghandler) self._loghandler.close() - self._loghandler = None self._log.close() - self._log = None + self._loghandler = self._log = None + + gbp.log.LOGGER.removeHandler(self._loghandler_info) + self._loghandler_info.close() + self._log_info.close() + self._loghandler_info = self._log_info = None def _get_log(self): """Get the captured log output""" self._log.seek(0) return self._log.readlines() + def _get_log_info(self): + self._log_info.seek(0) + return self._log_info.readlines() + def _check_log_empty(self): """Check that nothig was logged""" output = self._get_log() @@ -58,7 +78,7 @@ class GbpLogTester(object): "Log entry '%s' doesn't match '%s'" % (output, regex)) def _check_in_log(self, regex): - """Check that the at least one line on log matches expectations""" + """Check that at least one line in log matches expectations""" found = False if self._log is None: raise Exception("BUG in unittests: no log captured!") @@ -69,6 +89,18 @@ class GbpLogTester(object): break ok_(found, "No line of %s matched '%s'" % (log, regex)) + def _check_in_info_log(self, regex): + """Check that at least one line in info log matches expectations""" + found = False + if self._log_info is None: + raise Exception("BUG in unittests: no log captured!") + log = self._get_log_info() + for line in log: + if re.match(regex, line): + found = True + break + ok_(found, "No line of %s matched '%s'" % (log, regex)) + def _clear_log(self): """Clear the mock strerr""" if self._log is not None: -- cgit v1.2.3