avocado.test: Add Test __getstate__ for serialization

In order to make the Test class serializable, implement
__getstate__ in a way that only exports the pickleable
attributes. This will be necessary for the following
patches that will insulate individual test executions
in forked processes.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 c90452e8
......@@ -51,9 +51,11 @@ def log_exc_info(exc_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):
......@@ -102,6 +104,7 @@ class Test(unittest.TestCase):
if params is None:
params = {}
self.params = Params(params)
self._raw_params = params
shortname = self.params.get('shortname')
s_tag = None
......@@ -152,6 +155,13 @@ class Test(unittest.TestCase):
self.log.debug('Test instance params override defaults whenever available')
self.log.debug('')
# If there's a timeout set, log a timeout reminder
if hasattr(self.params, 'timeout'):
self.log.info('Test timeout set. Will wait %.2f s for '
'PID %s to end',
float(self.params.timeout), os.getpid())
self.log.info('')
self.debugdir = None
self.resultsdir = None
self.status = None
......@@ -169,6 +179,26 @@ class Test(unittest.TestCase):
def __repr__(self):
return "Test(%r)" % self.tagged_name
def __getstate__(self):
"""
Pickle only selected attributes of the class for serialization.
The fact we serialize the class means you'll have to modify this
class if you intend to make significant changes to its structure.
"""
orig = dict(self.__dict__)
d = {}
preserve_attr = ['basedir', 'debugdir', 'depsdir', 'fail_class',
'fail_reason', 'logdir', 'logfile', 'name',
'resultsdir', 'srcdir', 'status', 'sysinfodir',
'tag', 'tagged_name', 'text_output', 'time_elapsed',
'traceback', 'workdir']
for key in sorted(orig):
if key in preserve_attr:
d[key] = orig[key]
d['params'] = orig['_raw_params']
return d
def _set_default(self, key, default):
try:
self.params[key]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册