def __init__(self, args=None, standalone=False): """ Creates an instance of Job class. :param args: an instance of :class:`argparse.Namespace`. :param standalone: do not create any content and present the job log on the output. """ self.args = args self.standalone = standalone if args is not None: self.unique_id = args.unique_job_id or job_id.create_unique_job_id( ) else: self.unique_id = job_id.create_unique_job_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, 'debug': logging.DEBUG, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } if raw_log_level is not None and raw_log_level in mapping: self.loglevel = mapping[raw_log_level] else: self.loglevel = logging.DEBUG if multiplexer.MULTIPLEX_CAPABLE: self.multiplex_files = args.multiplex_files self.show_job_log = args.show_job_log self.silent = args.silent else: self.loglevel = logging.DEBUG self.multiplex_files = None self.show_job_log = False self.silent = False if standalone: self.show_job_log = True if self.args is not None: setattr(self.args, 'show_job_log', True) if self.show_job_log: if not self.silent: test_logger = logging.getLogger('avocado.test') output.add_console_handler(test_logger) test_logger.setLevel(self.loglevel) self.test_dir = data_dir.get_test_dir() self.test_index = 1 self.status = "RUNNING" self.result_proxy = result.TestResultProxy() self.sysinfo = None
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 """ start_time = time.strftime('%Y-%m-%dT%H.%M') if args is not None: logdir = args.logdir or get_logs_dir() else: logdir = get_logs_dir() # Stand alone tests handling if unique_id is None: unique_id = job_id.create_unique_job_id() debugbase = 'job-%s-%s' % (start_time, unique_id[:7]) debugdir = path.init_dir(logdir, debugbase) latestdir = os.path.join(logdir, "latest") try: os.unlink(latestdir) except OSError: pass os.symlink(debugbase, latestdir) return debugdir
def __init__(self, args=None): """ Creates an instance of Job class. :param args: an instance of :class:`argparse.Namespace`. """ self.args = args if args is not None: self.unique_id = args.unique_job_id or job_id.create_unique_job_id() else: self.unique_id = job_id.create_unique_job_id() 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) if self.args is not None: raw_log_level = args.job_log_level mapping = {'info': logging.INFO, 'debug': logging.DEBUG, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL} if raw_log_level is not None and raw_log_level in mapping: self.loglevel = mapping[raw_log_level] else: self.loglevel = logging.DEBUG self.multiplex_files = args.multiplex_files self.show_job_log = args.show_job_log self.silent = args.silent else: self.loglevel = logging.DEBUG self.multiplex_files = None self.show_job_log = False self.silent = False if self.show_job_log: if not self.silent: test_logger = logging.getLogger('avocado.test') output.add_console_handler(test_logger) test_logger.setLevel(self.loglevel) self.test_dir = data_dir.get_test_dir() self.test_index = 1 self.status = "RUNNING" self.result_proxy = result.TestResultProxy() self.view = output.View(app_args=self.args)
def __init__(self, args=None): """ Creates an instance of Job class. :param args: an instance of :class:`argparse.Namespace`. """ if args is None: args = argparse.Namespace() self.args = args self.standalone = getattr(self.args, 'standalone', False) unique_id = getattr(self.args, 'unique_job_id', None) if unique_id is None: unique_id = job_id.create_unique_job_id() self.unique_id = unique_id self.view = output.View(app_args=self.args) self.logdir = None raw_log_level = settings.get_value('job.output', 'loglevel', default='debug') mapping = { 'info': logging.INFO, 'debug': logging.DEBUG, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL } if raw_log_level in mapping: self.loglevel = mapping[raw_log_level] else: self.loglevel = logging.DEBUG self.show_job_log = getattr(self.args, 'show_job_log', False) self.silent = getattr(self.args, 'silent', False) if self.standalone: self.show_job_log = True if self.args is not None: setattr(self.args, 'show_job_log', True) if self.show_job_log: if not self.silent: output.add_console_handler(_TEST_LOGGER) _TEST_LOGGER.setLevel(self.loglevel) self.test_dir = data_dir.get_test_dir() self.test_index = 1 self.status = "RUNNING" self.result_proxy = result.TestResultProxy() self.sysinfo = None
def __init__(self, args=None): """ Creates an instance of Job class. :param args: an instance of :class:`argparse.Namespace`. """ if args is None: args = argparse.Namespace() self.args = args self.standalone = getattr(self.args, 'standalone', False) unique_id = getattr(self.args, 'unique_job_id', None) if unique_id is None: unique_id = job_id.create_unique_job_id() self.unique_id = unique_id self.view = output.View(app_args=self.args) self.logdir = None raw_log_level = settings.get_value('job.output', 'loglevel', default='debug') mapping = {'info': logging.INFO, 'debug': logging.DEBUG, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL} if raw_log_level in mapping: self.loglevel = mapping[raw_log_level] else: self.loglevel = logging.DEBUG self.show_job_log = getattr(self.args, 'show_job_log', False) self.silent = getattr(self.args, 'silent', False) if self.standalone: self.show_job_log = True if self.args is not None: setattr(self.args, 'show_job_log', True) if self.show_job_log: if not self.silent: output.add_console_handler(_TEST_LOGGER) _TEST_LOGGER.setLevel(self.loglevel) self.test_dir = data_dir.get_test_dir() self.test_index = 1 self.status = "RUNNING" self.result_proxy = result.TestResultProxy() self.sysinfo = None self.timeout = getattr(self.args, 'job_timeout', 0)
def run_but_fail_before_create_job_dir(self, complement_args, expected_rc): """ Runs avocado but checks that it fails before creating the job dir :param complement_args: the complement arguments to an 'avocado run' command line """ os.chdir(basedir) log_dir = data_dir.get_logs_dir() self.assertIsNotNone(log_dir) job = job_id.create_unique_job_id() cmd_line = '%s run --sysinfo=off --force-job-id=%%s %%s' % AVOCADO cmd_line %= (job, complement_args) result = process.run(cmd_line, ignore_status=True) self.assertEqual(result.exit_status, expected_rc, 'Avocado did not return rc %d:\n%s' % (expected_rc, result)) path_job_glob = os.path.join(log_dir, "job-*-%s" % job[0:7]) self.assertEquals(glob.glob(path_job_glob), [])
def run_but_fail_before_create_job_dir(self, complement_args, expected_rc): """ Runs avocado but checks that it fails before creating the job dir :param complement_args: the complement arguments to an 'avocado run' command line """ os.chdir(BASEDIR) log_dir = data_dir.get_logs_dir() self.assertIsNotNone(log_dir) job = job_id.create_unique_job_id() cmd_line = '%s run --disable-sysinfo --force-job-id=%%s %%s' % AVOCADO cmd_line %= (job, complement_args) result = process.run(cmd_line, ignore_status=True) self.assertEqual(result.exit_status, expected_rc, 'Avocado did not return rc %d:\n%s' % (expected_rc, result)) path_job_glob = os.path.join(log_dir, "job-*-%s" % job[0:7]) self.assertEqual(glob.glob(path_job_glob), [])
def create_job_logs_dir(logdir=None, unique_id=None): """ Create a log directory for a job, or a stand alone execution of a test. :param logdir: Base log directory, if `None`, use value from configuration. :param unique_id: The unique identification. If `None`, create one. :rtype: basestring """ start_time = time.strftime('%Y-%m-%dT%H.%M') if logdir is None: logdir = get_logs_dir() # Stand alone tests handling if unique_id is None: unique_id = job_id.create_unique_job_id() debugbase = 'job-%s-%s' % (start_time, unique_id[:7]) debugdir = utils_path.init_dir(logdir, debugbase) return debugdir
def create_job_logs_dir(logdir=None, unique_id=None): """ Create a log directory for a job, or a stand alone execution of a test. :param logdir: Base log directory, if `None`, use value from configuration. :param unique_id: The unique identification. If `None`, create one. :rtype: basestring """ start_time = time.strftime('%Y-%m-%dT%H.%M') if logdir is None: logdir = get_logs_dir() # Stand alone tests handling if unique_id is None: unique_id = job_id.create_unique_job_id() debugbase = 'job-%s-%s' % (start_time, unique_id[:7]) debugdir = utils_path.init_dir(logdir, debugbase) return debugdir
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. :param args: :class:`argparse.Namespace` instance with cmdline arguments (optional). :rtype: basestring """ start_time = time.strftime('%Y-%m-%dT%H.%M') if args is not None: logdir = args.logdir or get_logs_dir() else: logdir = get_logs_dir() # Stand alone tests handling if unique_id is None: unique_id = job_id.create_unique_job_id() debugbase = 'job-%s-%s' % (start_time, unique_id[:7]) debugdir = utils_path.init_dir(logdir, debugbase) return debugdir
def run_but_fail_before_create_job_dir(self, complement_args, expected_rc): """ Runs avocado but checks that it fails before creating the job dir :param complement_args: the complement arguments to an 'avocado run' command line """ os.chdir(BASEDIR) config = settings.as_dict() log_dir = config.get('datadir.paths.logs_dir') self.assertIsNotNone(log_dir) job = job_id.create_unique_job_id() cmd_line = (f'{AVOCADO} run --disable-sysinfo ' f'--force-job-id={job} {complement_args}') result = process.run(cmd_line, ignore_status=True) self.assertEqual(result.exit_status, expected_rc, 'Avocado did not return rc {expected_rc}:\n{result}') path_job_glob = os.path.join(log_dir, f"job-*-{job[0:7]}") self.assertEqual(glob.glob(path_job_glob), [])
def create_job_logs_dir(base_dir=None, unique_id=None): """ Create a log directory for a job, or a stand alone execution of a test. :param base_dir: Base log directory, if `None`, use value from configuration. :param unique_id: The unique identification. If `None`, create one. :rtype: str """ start_time = time.strftime('%Y-%m-%dT%H.%M') if base_dir is None: base_dir = get_logs_dir() if not base_dir: LOG_UI.error("No writable location for logs found, use " "'avocado config --datadir' to get the " "locations and check system permissions.") sys.exit(exit_codes.AVOCADO_FAIL) if not os.path.exists(base_dir): utils_path.init_dir(base_dir) # Stand alone tests handling if unique_id is None: unique_id = job_id.create_unique_job_id() logdir = os.path.join(base_dir, 'job-%s-%s' % (start_time, unique_id[:7])) for i in range(7, len(unique_id)): try: os.mkdir(logdir) except OSError: logdir += unique_id[i] continue return logdir logdir += "." for i in range(1000): try: os.mkdir(logdir + str(i)) except OSError: continue return logdir + str(i) raise IOError("Unable to create unique logdir in 1000 iterations: %s" % (logdir))
def unique_id(self): if self._unique_id is None: self._unique_id = self.config.get('run.unique_job_id') \ or create_unique_job_id() return self._unique_id
def test_get_job_results_dir(self): from avocado.core import data_dir, job_id # First let's mock a jobs results directory # logs_dir = self.mapping.get('logs_dir') self.assertNotEqual(None, logs_dir) unique_id = job_id.create_unique_job_id() # Expected job results dir expected_jrd = data_dir.create_job_logs_dir(logs_dir, unique_id) # Now let's test some cases # self.assertEqual(None, data_dir.get_job_results_dir(expected_jrd, logs_dir), ("If passing a directory reference, it expects the id" "file")) # Create the id file. id_file_path = os.path.join(expected_jrd, 'id') with open(id_file_path, 'w') as id_file: id_file.write("%s\n" % unique_id) id_file.flush() os.fsync(id_file) self.assertEqual(expected_jrd, data_dir.get_job_results_dir(expected_jrd, logs_dir), "It should get from the path to the directory") results_dirname = os.path.basename(expected_jrd) self.assertEqual( None, data_dir.get_job_results_dir(results_dirname, logs_dir), "It should not get from a valid path to the directory") pwd = os.getcwd() os.chdir(logs_dir) self.assertEqual( expected_jrd, data_dir.get_job_results_dir(results_dirname, logs_dir), "It should get from relative path to the directory") os.chdir(pwd) self.assertEqual(expected_jrd, data_dir.get_job_results_dir(id_file_path, logs_dir), "It should get from the path to the id file") self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id, logs_dir), "It should get from the id") another_id = job_id.create_unique_job_id() self.assertNotEqual(unique_id, another_id) self.assertEqual(None, data_dir.get_job_results_dir(another_id, logs_dir), "It should not get from unexisting job") self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id[:7], logs_dir), "It should get from partial id equals to 7 digits") self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id[:4], logs_dir), "It should get from partial id less than 7 digits") almost_id = unique_id[:7] + ('a' * (len(unique_id) - 7)) self.assertNotEqual(unique_id, almost_id) self.assertEqual(None, data_dir.get_job_results_dir(almost_id, logs_dir), ("It should not get if the id is equal on only" "the first 7 characters")) os.symlink(expected_jrd, os.path.join(logs_dir, 'latest')) self.assertEqual(expected_jrd, data_dir.get_job_results_dir('latest', logs_dir), "It should get from the 'latest' id") stg = settings.Settings() with unittest.mock.patch('avocado.core.stgs', stg): import avocado.core avocado.core.register_core_options() stg.process_config_path(self.config_file_path) stg.merge_with_configs() with unittest.mock.patch('avocado.core.data_dir.settings', stg): self.assertEqual(expected_jrd, data_dir.get_job_results_dir(unique_id), "It should use the default base logs directory")
def unique_id(self): if self._unique_id is None: self._unique_id = ( self.config.get("run.unique_job_id") or create_unique_job_id() ) return self._unique_id