|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Python script that generates a user-readable report for a given DeviceFarm test run. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import subprocess |
| 8 | +import argparse |
| 9 | +import logging |
| 10 | +import boto3 |
| 11 | +from botocore.config import Config |
| 12 | +from junit_xml import TestSuite, TestCase |
| 13 | + |
| 14 | +LOG_FORMATTER = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') |
| 15 | +CONSOLE_HANDLER = logging.StreamHandler() |
| 16 | +CONSOLE_HANDLER.setFormatter(LOG_FORMATTER) |
| 17 | +LOGGER = logging.getLogger("DeviceFarmTestRunReportGenerator") |
| 18 | +LOGGER.setLevel(os.getenv("LOG_LEVEL") if os.getenv("LOG_LEVEL") is not None else "INFO") |
| 19 | +LOGGER.addHandler(CONSOLE_HANDLER) |
| 20 | + |
| 21 | +client = boto3.client('devicefarm', config=Config(region_name='us-west-2')) |
| 22 | + |
| 23 | +def parse_arguments(): |
| 24 | + parser = argparse.ArgumentParser(description="Utility that generates a report for a DeviceFarm test run.") |
| 25 | + parser.add_argument("--run_arn", help="The ARN of the DeviceFarm test run.", required=True) |
| 26 | + parser.add_argument("--module_name", help="The module name for the test suite.", required=True) |
| 27 | + parser.add_argument("--output_path", help="Destination path for the build reports.", required=True) |
| 28 | + parser.add_argument("--pr", help="The github PR number.") |
| 29 | + return parser.parse_args() |
| 30 | + |
| 31 | +def generate_junit_report(run_arn, output_path): |
| 32 | + LOGGER.debug(f"Retrieving test jobs for run {run_arn}") |
| 33 | + jobs = get_test_jobs(run_arn) |
| 34 | + for job in jobs: |
| 35 | + LOGGER.debug(f"Retrieving test suites for job {job['arn']}") |
| 36 | + suites = get_test_suites(job['arn']) |
| 37 | + for suite in suites: |
| 38 | + LOGGER.debug(f"Retrieving tests for suite {suite['arn']}") |
| 39 | + tests = get_tests(suite['arn']) |
| 40 | + test_cases = [] |
| 41 | + for test in tests: |
| 42 | + tc = TestCase(test['name'], |
| 43 | + classname=suite['name'], |
| 44 | + elapsed_sec=test['deviceMinutes']['total']*60, |
| 45 | + stdout=test['message'], |
| 46 | + status=test['result'] ) |
| 47 | + if test['result'] == 'FAILED': |
| 48 | + tc.add_failure_info(message=test['message']) |
| 49 | + if test['result'] == 'ERROR': |
| 50 | + tc.add_error_info(message=test['message']) |
| 51 | + test_cases.append(tc) |
| 52 | + ts = TestSuite(suite['name'],test_cases=test_cases) |
| 53 | + ts_output = TestSuite.to_xml_string([ts]) |
| 54 | + LOGGER.info(f"Saving test suite {suite['name']} report.") |
| 55 | + if not os.path.exists(output_path): |
| 56 | + os.makedirs(output_path) |
| 57 | + f = open(output_path + suite['name'] + ".xml", "w") |
| 58 | + f.write(ts_output) |
| 59 | + f.close() |
| 60 | + |
| 61 | +def get_test_jobs(run_arn): |
| 62 | + result = client.list_jobs(arn=run_arn) |
| 63 | + return result['jobs'] if result is not None else [] |
| 64 | + |
| 65 | +def get_test_suites(job_arn): |
| 66 | + result = client.list_suites(arn=job_arn) |
| 67 | + return result['suites'] if result is not None else [] |
| 68 | + |
| 69 | +def get_tests(suite_arn): |
| 70 | + result = client.list_tests(arn=suite_arn) |
| 71 | + return result['tests'] if result is not None else [] |
| 72 | + |
| 73 | +def get_problems(run_arn): |
| 74 | + return client.list_unique_problems( |
| 75 | + arn=run_arn |
| 76 | + ) |
| 77 | + |
| 78 | +def main(arguments): |
| 79 | + args = parse_arguments() |
| 80 | + build_id = os.getenv("CODEBUILD_BUILD_ID") |
| 81 | + source_version = os.getenv("CODEBUILD_SOURCE_VERSION") |
| 82 | + arn_suffix = args.run_arn.split(':')[-1] |
| 83 | + LOGGER.info(f"devicefarm_run: {arn_suffix} build_id: {build_id} source_version: {source_version}") |
| 84 | + generate_junit_report(run_arn=args.run_arn, |
| 85 | + output_path=args.output_path) |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + sys.exit(main(sys.argv[1:])) |
0 commit comments