From 11c7c2663a7b9bbe4bab11d281e17cde27d68f79 Mon Sep 17 00:00:00 2001 From: Mathieu Bultel Date: Mon, 12 Oct 2020 23:08:11 +0200 Subject: [PATCH] Raise if no validation has been executed If no validations has been run, the CLI should raise a exceptions.CommandError instead of trying to build the prettytable from results. Change-Id: I2ed22b9c01ebb64281ad5b8b3b569cc53a4f122e (cherry picked from commit efcf2d98be109c919d627f3148522a559d13667a) (cherry picked from commit a60ec726e4d40b6040f4c08b0f35d35744a8835c) --- tripleoclient/v1/tripleo_validator.py | 67 +++++++++++++++------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/tripleoclient/v1/tripleo_validator.py b/tripleoclient/v1/tripleo_validator.py index 9f5ccbb3f..c9b241fdf 100644 --- a/tripleoclient/v1/tripleo_validator.py +++ b/tripleoclient/v1/tripleo_validator.py @@ -363,37 +363,44 @@ class TripleOValidatorRun(command.Command): v_consts.DEFAULT_VALIDATIONS_BASEDIR = constants.\ DEFAULT_VALIDATIONS_BASEDIR actions = ValidationActions() - results = actions.run_validations( - inventory=static_inventory, - limit_hosts=limit, - group=parsed_args.group, - extra_vars=extra_vars, - validations_dir=constants.ANSIBLE_VALIDATION_DIR, - validation_name=parsed_args.validation_name, - extra_env_vars=parsed_args.extra_env_vars, - python_interpreter=parsed_args.python_interpreter, - quiet=True) + try: + results = actions.run_validations( + inventory=static_inventory, + limit_hosts=limit, + group=parsed_args.group, + extra_vars=extra_vars, + validations_dir=constants.ANSIBLE_VALIDATION_DIR, + validation_name=parsed_args.validation_name, + extra_env_vars=parsed_args.extra_env_vars, + python_interpreter=parsed_args.python_interpreter, + quiet=True) + except RuntimeError as e: + raise exceptions.CommandError(e) - # Build output - t = PrettyTable(border=True, header=True, padding_width=1) - # Set Field name by getting the result dict keys - t.field_names = results[0].keys() - for r in results: - if r.get('Status_by_Host'): - h = [] - for host in r['Status_by_Host'].split(', '): - _name, _status = host.split(',') - color = (GREEN if _status == 'PASSED' else RED) - _name = '{}{}{}'.format(color, _name, RESET) - h.append(_name) - r['Status_by_Host'] = ', '.join(h) - if r.get('status'): - status = r.get('status') - color = (CYAN if status in ['starting', 'running'] - else GREEN if status == 'PASSED' else RED) - r['status'] = '{}{}{}'.format(color, status, RESET) - t.add_row(r.values()) - print(t) + if results: + # Build output + t = PrettyTable(border=True, header=True, padding_width=1) + # Set Field name by getting the result dict keys + t.field_names = results[0].keys() + for r in results: + if r.get('Status_by_Host'): + h = [] + for host in r['Status_by_Host'].split(', '): + _name, _status = host.split(',') + color = (GREEN if _status == 'PASSED' else RED) + _name = '{}{}{}'.format(color, _name, RESET) + h.append(_name) + r['Status_by_Host'] = ', '.join(h) + if r.get('status'): + status = r.get('status') + color = (CYAN if status in ['starting', 'running'] + else GREEN if status == 'PASSED' else RED) + r['status'] = '{}{}{}'.format(color, status, RESET) + t.add_row(r.values()) + print(t) + else: + msg = "No Validation has been run, please check your parameters." + raise exceptions.CommandError(msg) if not parsed_args.static_inventory: LOG.debug(_('Removing static tripleo ansible inventory file'))