未验证 提交 b77fc217 编写于 作者: R Rudá Moura

avocado.job: Postpone setup/init of the Job class.

The current Job object initialization is doing many setups at once,
 for example, it creates a new job results dir with an "id" file inside.
 It handles the sysinfo start, update the latest
 link, etc. Things that can be done later.

This changes allows split the initialization in subparts:
* _setup_job_results() - creates a new job results directory with an id inside.
* _update_latest_link() - update the "latest (symbolic) link" to the job results.
* _start_sysinfo() - start the system information module.
* _remove_job_results() - remove the current job results from the directory.

So when running tests that are not found, say `avocado run quuuux`
then the job results will be removed and the link for the previous
result (latest) is preserved. Everything else should work as it was before.
Signed-off-by: NRudá Moura <rmoura@redhat.com>
上级 b72e4792
......@@ -207,8 +207,6 @@ def get_job_logs_dir(args=None, unique_id=None):
"""
Create a log directory for a job, or a stand alone execution of a test.
Also, symlink the created dir with [avocado-logs-dir]/latest.
:param args: :class:`argparse.Namespace` instance with cmdline arguments
(optional).
:rtype: basestring
......@@ -224,13 +222,23 @@ def get_job_logs_dir(args=None, unique_id=None):
debugbase = 'job-%s-%s' % (start_time, unique_id[:7])
debugdir = utils_path.init_dir(logdir, debugbase)
latestdir = os.path.join(logdir, "latest")
return debugdir
def update_latest_job_logs_dir(debugdir):
"""
Update the latest job result symbolic link [avocado-logs-dir]/latest.
:param debubdir: full path for the current job result.
"""
basedir = os.path.dirname(debugdir)
basename = os.path.basename(debugdir)
latest = os.path.join(basedir, "latest")
try:
os.unlink(latestdir)
os.unlink(latest)
except OSError:
pass
os.symlink(debugbase, latestdir)
return debugdir
os.symlink(basename, latest)
class _TmpDirTracker(Borg):
......
......@@ -74,16 +74,8 @@ class Job(object):
self.unique_id = args.unique_job_id or job_id.create_unique_job_id()
else:
self.unique_id = job_id.create_unique_job_id()
if standalone:
self.logdir = tempfile.mkdtemp()
else:
self.logdir = data_dir.get_job_logs_dir(self.args, self.unique_id)
self.logfile = os.path.join(self.logdir, "job.log")
self.idfile = os.path.join(self.logdir, "id")
with open(self.idfile, 'w') as id_file_obj:
id_file_obj.write("%s\n" % self.unique_id)
self.view = output.View(app_args=self.args)
self.logdir = None
if self.args is not None:
raw_log_level = args.job_log_level
mapping = {'info': logging.INFO,
......@@ -118,13 +110,30 @@ class Job(object):
self.test_index = 1
self.status = "RUNNING"
self.result_proxy = result.TestResultProxy()
self.view = output.View(app_args=self.args)
self.sysinfo = None
def _setup_job_results(self):
if self.standalone:
self.logdir = tempfile.mkdtemp()
else:
self.logdir = data_dir.get_job_logs_dir(self.args, self.unique_id)
self.logfile = os.path.join(self.logdir, "job.log")
self.idfile = os.path.join(self.logdir, "id")
with open(self.idfile, 'w') as id_file_obj:
id_file_obj.write("%s\n" % self.unique_id)
def _update_latest_link(self):
data_dir.update_latest_job_logs_dir(self.logdir)
def _start_sysinfo(self):
if hasattr(self.args, 'sysinfo'):
if self.args.sysinfo == 'on':
sysinfo_dir = path.init_dir(self.logdir, 'sysinfo')
self.sysinfo = sysinfo.SysInfo(basedir=sysinfo_dir)
def _remove_job_results(self):
shutil.rmtree(self.logdir, ignore_errors=True)
def _make_test_loader(self):
if hasattr(self.args, 'test_loader'):
test_loader_class = self.args.test_loader
......@@ -272,6 +281,8 @@ class Job(object):
params_list = self._multiplex_params_list(params_list,
multiplex_files)
self._setup_job_results()
try:
test_suite = self.test_loader.discover(params_list)
error_msg_parts = self.test_loader.validate_ui(test_suite)
......@@ -279,6 +290,7 @@ class Job(object):
raise exceptions.JobError('Command interrupted by user...')
if error_msg_parts:
self._remove_job_results()
e_msg = '\n'.join(error_msg_parts)
raise exceptions.OptionValidationError(e_msg)
......@@ -292,6 +304,7 @@ class Job(object):
self._make_test_result()
self._make_test_runner()
self._start_sysinfo()
self.view.start_file_logging(self.logfile,
self.loglevel,
......@@ -299,6 +312,7 @@ class Job(object):
self.view.logfile = self.logfile
failures = self.test_runner.run_suite(test_suite)
self.view.stop_file_logging()
self._update_latest_link()
# If it's all good so far, set job status to 'PASS'
if self.status == 'RUNNING':
self.status = 'PASS'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册