def test_get_logfile_by_validation(self, mock_open, mock_json, mock_glob): mock_glob.return_value = \ ['/tmp/123_foo_2020-03-30T13:17:22.447857Z.json'] vlogs = ValidationLogs('/tmp/foo') log = vlogs.get_logfile_by_validation('foo') self.assertEqual(log, ['/tmp/123_foo_2020-03-30T13:17:22.447857Z.json'])
def get_status(self, validation_id=None, uuid=None, status='FAILED', log_path=constants.VALIDATIONS_LOG_BASEDIR): """Return validations execution details by status :param validation_id: The validation id :type validation_id: ``string`` :param uuid: The UUID of the execution :type uuid: ``string`` :param status: The status of the execution (Defaults to FAILED) :type status: ``string`` :param log_path: The absolute path of the validations logs directory :type log_path: ``string`` :return: A list of validations execution with details and by status :rtype: ``tuple`` :Example: >>> actions = ValidationActions(validation_path='/foo/bar') >>> status = actions.get_status(validation_id='foo')) >>> print(status) (['name', 'host', 'status', 'task_data'], [('Check if debug mode is disabled.', 'localhost', 'FAILED', {'_ansible_no_log': False, 'action': 'fail', 'changed': False, 'failed': True, 'msg': 'Debug mode is not disabled.'}), ('Check if debug mode is disabled.', 'localhost', 'FAILED', {'_ansible_no_log': False, 'action': 'fail', 'changed': False, 'failed': True, 'msg': 'Debug mode is not disabled.'}), ('Check if debug mode is disabled.', 'localhost', 'FAILED', {'_ansible_no_log': False, 'action': 'fail', 'changed': False, 'failed': True, 'msg': 'Debug mode is not disabled.'})]) """ vlogs = ValidationLogs(log_path) if validation_id: logs = vlogs.get_logfile_by_validation(validation_id) elif uuid: logs = vlogs.get_logfile_by_uuid(uuid) else: raise RuntimeError("You need to provide a validation_id or a uuid") values = [] column_name = ['name', 'host', 'status', 'task_data'] for log in logs: vlog = ValidationLog(logfile=log) if vlog.is_valid_format(): for task in vlog.get_tasks_data: if task['status'] == status: for host in task['hosts']: values.append((task['name'], host, task['status'], task['hosts'][host])) return (column_name, values)
def show_history(self, validation_ids=None, extension='json', log_path=constants.VALIDATIONS_LOG_BASEDIR): """Return validation executions history :param validation_ids: The validation ids :type validation_ids: a list of strings :param extension: The log file extension (Defaults to ``json``) :type extension: ``string`` :param log_path: The absolute path of the validations logs directory :type log_path: ``string`` :return: Returns the information about the validation executions history :rtype: ``tuple`` :Example: >>> actions = ValidationActions(constants.ANSIBLE_VALIDATION_DIR) >>> print(actions.show_history()) (('UUID', 'Validations', 'Status', 'Execution at', 'Duration'), [('5afb1597-e2a1-4635-b2df-7afe21d00de6', 'foo', 'PASSED', '2020-11-13T11:47:04.740442Z', '0:00:02.388'), ('32a5e217-d7a9-49a5-9838-19e5f9b82a77', 'foo2', 'PASSED', '2020-11-13T11:47:07.931184Z', '0:00:02.455'), ('62d4d54c-7cce-4f38-9091-292cf49268d7', 'foo', 'PASSED', '2020-11-13T11:47:47.188876Z', '0:00:02.285'), ('04e6165c-7c33-4881-bac7-73ff3f909c24', 'foo3', 'PASSED', '2020-11-13T11:47:50.279662Z', '0:00:02.237')]) >>> actions = ValidationActions(constants.ANSIBLE_VALIDATION_DIR) >>> print(actions.show_history(validation_ids=['foo'])) (('UUID', 'Validations', 'Status', 'Execution at', 'Duration'), [('5afb1597-e2a1-4635-b2df-7afe21d00de6', 'foo', 'PASSED', '2020-11-13T11:47:04.740442Z', '0:00:02.388'), ('04e6165c-7c33-4881-bac7-73ff3f909c24', 'foo', 'PASSED', '2020-11-13T11:47:50.279662Z', '0:00:02.237')]) """ vlogs = ValidationLogs(log_path) if validation_ids: if not isinstance(validation_ids, list): validation_ids = [validation_ids] logs = [] for validation_id in validation_ids: logs.extend(vlogs.get_logfile_by_validation(validation_id)) else: logs = vlogs.get_all_logfiles(extension) values = [] column_name = ('UUID', 'Validations', 'Status', 'Execution at', 'Duration') for log in logs: vlog = ValidationLog(logfile=log) if vlog.is_valid_format(): for play in vlog.get_plays: values.append((play['id'], play['validation_id'], vlog.get_status, play['duration'].get('start'), play['duration'].get('time_elapsed'))) return (column_name, values)