提交 beb9dac8 编写于 作者: C Cleber Rosa

Record all logged messages during test run into the test log

Each test has its own log file within a given job result dir,
located at "test-results/$(test-id)/debug.log".

If the test is an INSTRUMENTED test, using the standard Python logging
module/API, all produced content should be recorded into the test log
file.  This adds a handler that does exactly that to the logging
module root logger.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 4e7f3df5
...@@ -667,6 +667,11 @@ class Test(unittest.TestCase, TestData): ...@@ -667,6 +667,11 @@ class Test(unittest.TestCase, TestData):
self.file_handler.setFormatter(formatter) self.file_handler.setFormatter(formatter)
self.log.addHandler(self.file_handler) self.log.addHandler(self.file_handler)
# add the test log handler to the root logger so that
# everything logged while the test is running, for every
# logger, also makes its way into the test log file
logging.root.addHandler(self.file_handler)
stream_fmt = '%(message)s' stream_fmt = '%(message)s'
stream_formatter = logging.Formatter(fmt=stream_fmt) stream_formatter = logging.Formatter(fmt=stream_fmt)
......
...@@ -118,6 +118,16 @@ class SharedLibTest(Test): ...@@ -118,6 +118,16 @@ class SharedLibTest(Test):
''' '''
TEST_OTHER_LOGGERS_CONTENT = '''
import logging
from avocado import Test
class My(Test):
def test(self):
logging.getLogger("some.other.logger").info("SHOULD BE ON debug.log")
'''
def probe_binary(binary): def probe_binary(binary):
try: try:
return utils_path.find_command(binary) return utils_path.find_command(binary)
...@@ -570,6 +580,26 @@ class RunnerOperationTest(unittest.TestCase): ...@@ -570,6 +580,26 @@ class RunnerOperationTest(unittest.TestCase):
result.stdout) result.stdout)
self.assertIn(b"Sleeping for 0.01 seconds", result.stdout) self.assertIn(b"Sleeping for 0.01 seconds", result.stdout)
def test_other_loggers(self):
with script.TemporaryScript(
'mytest.py',
TEST_OTHER_LOGGERS_CONTENT,
'avocado_functional_test_other_loggers') as mytest:
cmd_line = ('%s run --sysinfo=off --job-results-dir %s '
'-- %s' % (AVOCADO, self.tmpdir, mytest))
result = process.run(cmd_line, ignore_status=True)
expected_rc = exit_codes.AVOCADO_ALL_OK
self.assertEqual(result.exit_status, expected_rc,
"Avocado did not return rc %d:\n%s" %
(expected_rc, result))
test_log_dir = glob.glob(os.path.join(self.tmpdir, 'job-*',
'test-results', '1-*'))[0]
test_log_path = os.path.join(test_log_dir, 'debug.log')
with open(test_log_path, 'rb') as test_log:
self.assertIn(b'SHOULD BE ON debug.log', test_log.read())
def tearDown(self): def tearDown(self):
shutil.rmtree(self.tmpdir) shutil.rmtree(self.tmpdir)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册