avocado: Add avocado.utils.stacktrace for stack trace related utilities

Refactor the test code and create a library containing
stack trace related utilities for avocado. They'll be
useful in a later patch, that improves visibility of
errors in the test loading stage.

Changes from v1:
 * Changed module name from tb to stacktrace, per rmoura's suggestion.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 d8ad6cea
......@@ -23,7 +23,6 @@ import os
import shutil
import sys
import time
import traceback
import unittest
from avocado.core import data_dir
......@@ -31,48 +30,12 @@ from avocado.core import exceptions
from avocado.utils import io
from avocado.utils import path
from avocado.utils import process
from avocado.utils import stacktrace
from avocado.utils.params import Params
from avocado import sysinfo
from avocado.version import VERSION
log = logging.getLogger("avocado.test")
def tb_info(exc_info):
"""
Prepare traceback info.
:param exc_info: Exception info produced by sys.exc_info()
"""
exc_type, exc_value, exc_traceback = exc_info
tb_info = traceback.format_exception(exc_type, exc_value,
exc_traceback.tb_next)
return tb_info
def log_exc_info(exc_info):
"""
Log exception info.
:param exc_info: Exception info produced by sys.exc_info()
"""
log.error('')
for line in tb_info(exc_info):
for l in line.splitlines():
log.error(l)
log.error('')
def prepare_exc_info(exc_info):
"""
Prepare traceback info.
:param exc_info: Exception info produced by sys.exc_info()
"""
return "".join(tb_info(exc_info))
class Test(unittest.TestCase):
"""
......@@ -412,18 +375,18 @@ class Test(unittest.TestCase):
try:
self.setup()
except Exception, details:
log_exc_info(sys.exc_info())
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
raise exceptions.TestSetupFail(details)
try:
self.action()
except Exception, details:
log_exc_info(sys.exc_info())
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
action_exception = details
finally:
try:
self.cleanup()
except Exception, details:
log_exc_info(sys.exc_info())
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
cleanup_exception = details
whiteboard_file = os.path.join(self.logdir, 'whiteboard')
......@@ -441,12 +404,12 @@ class Test(unittest.TestCase):
try:
self.check_reference_stdout()
except Exception, details:
log_exc_info(sys.exc_info())
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
stdout_check_exception = details
try:
self.check_reference_stderr()
except Exception, details:
log_exc_info(sys.exc_info())
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
stderr_check_exception = details
elif not job_standalone:
......@@ -493,20 +456,18 @@ class Test(unittest.TestCase):
self.status = detail.status
self.fail_class = detail.__class__.__name__
self.fail_reason = detail
self.traceback = prepare_exc_info(sys.exc_info())
self.traceback = stacktrace.prepare_exc_info(sys.exc_info())
except AssertionError, detail:
self.status = 'FAIL'
self.fail_class = detail.__class__.__name__
self.fail_reason = detail
self.traceback = prepare_exc_info(sys.exc_info())
self.traceback = stacktrace.prepare_exc_info(sys.exc_info())
except Exception, detail:
self.status = 'FAIL'
self.fail_class = detail.__class__.__name__
self.fail_reason = detail
exc_type, exc_value, exc_traceback = sys.exc_info()
tb_info = traceback.format_exception(exc_type, exc_value,
exc_traceback.tb_next)
self.traceback = "".join(tb_info)
tb_info = stacktrace.tb_info(sys.exc_info())
self.traceback = stacktrace.prepare_exc_info(sys.exc_info())
for e_line in tb_info:
self.log.error(e_line)
finally:
......
"""
Traceback standard module plus some additional APIs.
"""
from traceback import format_exception
import logging
def tb_info(exc_info):
"""
Prepare traceback info.
:param exc_info: Exception info produced by sys.exc_info()
"""
exc_type, exc_value, exc_traceback = exc_info
return format_exception(exc_type, exc_value, exc_traceback.tb_next)
def prepare_exc_info(exc_info):
"""
Prepare traceback info.
:param exc_info: Exception info produced by sys.exc_info()
"""
return "".join(tb_info(exc_info))
def log_exc_info(exc_info, logger='root'):
"""
Log exception info to logger_name.
:param exc_info: Exception info produced by sys.exc_info()
:param logger: Name of the logger (defaults to root)
"""
log = logging.getLogger(logger)
log.error('')
for line in tb_info(exc_info):
for l in line.splitlines():
log.error(l)
log.error('')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册