diff options
author | Guido Günther <agx@sigxcpu.org> | 2018-01-29 15:25:39 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2018-01-29 15:25:39 +0100 |
commit | 5f2120b353dad294cf98fbc2cc704e742d87d6be (patch) | |
tree | 58b9b87c9511803c34a74d4578c30d49669294c4 /tests | |
parent | 16a85f79f49f2cfad49b9e55972209d6bce487e0 (diff) |
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.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/testutils/gbplogtester.py | 44 |
1 files changed, 38 insertions, 6 deletions
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: |