diff --git a/validations_libs/cli/common.py b/validations_libs/cli/common.py index 1d0aa4bd..6d6eea18 100644 --- a/validations_libs/cli/common.py +++ b/validations_libs/cli/common.py @@ -15,7 +15,14 @@ # under the License. import json +import logging from prettytable import PrettyTable +import re +try: + from junit_xml import TestSuite, TestCase, to_xml_report_string + JUNIT_XML_FOUND = True +except ImportError: + JUNIT_XML_FOUND = False from validations_libs import constants from validations_libs import utils as v_utils @@ -58,3 +65,36 @@ def write_output(output_log, results): with open(output_log, 'w') as output: output.write(json.dumps({'results': results}, indent=4, sort_keys=True)) + + +def write_junitxml(output_junitxml, results): + """Write output file as JUnitXML format""" + if not JUNIT_XML_FOUND: + log = logging.getLogger(__name__ + ".write_junitxml") + log.warning('junitxml output disabled: the `junit_xml` python module ' + 'is missing.') + return + test_cases = [] + duration_re = re.compile('([0-9]+):([0-9]+):([0-9]+).([0-9]+)') + for vitem in results: + if vitem.get('Validations'): + parsed_duration = 0 + test_duration = vitem.get('Duration', '') + matched_duration = duration_re.match(test_duration) + if matched_duration: + parsed_duration = (int(matched_duration[1])*3600 + + int(matched_duration[2])*60 + + int(matched_duration[3]) + + float('0.{}'.format(matched_duration[4]))) + + test_stdout = vitem.get('Status_by_Host', '') + + test_case = TestCase('validations', vitem['Validations'], + parsed_duration, test_stdout) + if vitem['Status'] == 'FAILED': + test_case.add_failure_info('FAILED') + test_cases.append(test_case) + + ts = TestSuite("Validations", test_cases) + with open(output_junitxml, 'w') as output: + output.write(to_xml_report_string([ts])) diff --git a/validations_libs/cli/run.py b/validations_libs/cli/run.py index 6df03af7..a898dd32 100644 --- a/validations_libs/cli/run.py +++ b/validations_libs/cli/run.py @@ -66,6 +66,11 @@ class Run(Command): default=None, help=("Path where the run result will be stored.")) + parser.add_argument('--junitxml', dest='junitxml', + default=None, + help=("Path where the run result in JUnitXML " + "format will be stored.")) + parser.add_argument( '--extra-env-vars', action=KeyValueAction, @@ -166,6 +171,8 @@ class Run(Command): _rc = any([r for r in results if r['Status'] == 'FAILED']) if parsed_args.output_log: common.write_output(parsed_args.output_log, results) + if parsed_args.junitxml: + common.write_junitxml(parsed_args.junitxml, results) common.print_dict(results) if _rc: raise RuntimeError("One or more validations have failed.")