Anusha Dathatri | 229b76a | 2019-11-26 03:26:16 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # This script generates the unit test coverage report. |
| 4 | # |
| 5 | # Usage: |
| 6 | # get_unit_test_report.py file target_dir |
| 7 | # |
| 8 | # Description of arguments: |
| 9 | # file File with repository URLs, one on each line without quotes. |
| 10 | # Eg: git://github.com/openbmc/ibm-dbus-interfaces |
| 11 | # target_dir Target directory in pwd to place all cloned repos and logs. |
| 12 | # |
| 13 | # Eg: get_unit_test_report.py repo_names target_dir |
| 14 | # |
| 15 | # Output format: |
| 16 | # |
| 17 | # ***********************************OUTPUT*********************************** |
| 18 | # https://github.com/openbmc/phosphor-dbus-monitor.git NO |
| 19 | # https://github.com/openbmc/phosphor-sel-logger.git;protocol=git NO |
| 20 | # ***********************************OUTPUT*********************************** |
| 21 | # |
| 22 | # Other outputs and errors are redirected to output.log and debug.log in target_dir. |
| 23 | |
| 24 | import argparse |
| 25 | import logging |
| 26 | import os |
| 27 | import shutil |
| 28 | import sys |
| 29 | import subprocess |
| 30 | |
| 31 | |
| 32 | # Create parser. |
| 33 | parser = argparse.ArgumentParser(usage='%(prog)s file target_dir', |
| 34 | description="Script generates the unit test coverage report") |
| 35 | |
| 36 | parser.add_argument("file", type=str, |
| 37 | help='''Text file containing repository links separated by |
| 38 | new line |
| 39 | Eg: git://github.com/openbmc/ibm-dbus-interfaces''') |
| 40 | |
| 41 | parser.add_argument("target_dir", type=str, |
| 42 | help='''Name of a non-existing directory in pwd to store all |
| 43 | cloned repos, logs and UT reports''') |
| 44 | args = parser.parse_args() |
| 45 | |
| 46 | |
| 47 | # Create target working directory. |
| 48 | pwd = os.getcwd() |
| 49 | working_dir = os.path.join(pwd, args.target_dir) |
| 50 | try: |
| 51 | os.mkdir(working_dir) |
| 52 | except OSError as e: |
| 53 | answer = raw_input("Target directory " + working_dir + " already exists. " |
| 54 | + "Do you want to delete [Y/N]: ") |
| 55 | if answer == "Y": |
| 56 | try: |
| 57 | shutil.rmtree(working_dir) |
| 58 | os.mkdir(working_dir) |
| 59 | except OSError as e: |
| 60 | print(str(e)) |
| 61 | quit() |
| 62 | else: |
| 63 | print("Exiting....") |
| 64 | quit() |
| 65 | |
| 66 | # Create log directory. |
| 67 | log_dir = os.path.join(working_dir, "logs") |
| 68 | try: |
| 69 | os.mkdir(log_dir) |
| 70 | except OSError as e: |
| 71 | print("Unable to create log directory: " + log_dir) |
| 72 | print(str(e)) |
| 73 | quit() |
| 74 | |
| 75 | |
| 76 | # Log files |
| 77 | debug_file = os.path.join(log_dir, "debug.log") |
| 78 | output_file = os.path.join(log_dir, "output.log") |
| 79 | logging.basicConfig(format='%(levelname)s - %(message)s', level=logging.DEBUG, |
| 80 | filename=debug_file) |
| 81 | logger = logging.getLogger(__name__) |
| 82 | |
| 83 | # Create handlers |
| 84 | console_handler = logging.StreamHandler() |
| 85 | file_handler = logging.FileHandler(output_file) |
| 86 | console_handler.setLevel(logging.INFO) |
| 87 | file_handler.setLevel(logging.INFO) |
| 88 | |
| 89 | # Create formatters and add it to handlers |
| 90 | log_format = logging.Formatter('%(message)s') |
| 91 | console_handler.setFormatter(log_format) |
| 92 | file_handler.setFormatter(log_format) |
| 93 | |
| 94 | # Add handlers to the logger |
| 95 | logger.addHandler(console_handler) |
| 96 | logger.addHandler(file_handler) |
| 97 | |
| 98 | |
| 99 | # Create report directory. |
| 100 | report_dir = os.path.join(working_dir, "reports") |
| 101 | try: |
| 102 | os.mkdir(report_dir) |
| 103 | except OSError as e: |
| 104 | logger.error("Unable to create report directory: " + report_dir) |
| 105 | logger.error(str(e)) |
| 106 | quit() |
| 107 | |
| 108 | # Clone OpenBmc build scripts. |
| 109 | try: |
| 110 | output = subprocess.check_output("git clone https://github.com/openbmc/openbmc-build-scripts.git", |
| 111 | shell=True, cwd=working_dir, stderr=subprocess.STDOUT) |
| 112 | logger.debug(output) |
| 113 | except subprocess.CalledProcessError as e: |
| 114 | logger.debug(e.output) |
| 115 | logger.debug(e.cmd) |
| 116 | logger.debug("Unable to clone openbmc-build-scripts") |
| 117 | quit() |
| 118 | |
| 119 | # Read URLs from input file. |
| 120 | handle = open(args.file) |
| 121 | url_list = handle.readlines() |
| 122 | repo_count = len(url_list) |
| 123 | logger.info("Number of repositories: " + str(repo_count)) |
| 124 | |
| 125 | # Clone repository and run unit test. |
| 126 | coverage_report = [] |
| 127 | counter = 0 |
| 128 | total_report_count = 0 |
| 129 | coverage_count = 0 |
| 130 | unit_test_count = 0 |
| 131 | no_report_count = 0 |
| 132 | error_count = 0 |
| 133 | for url in url_list: |
| 134 | ci_exists = "NO" |
| 135 | skip = False |
| 136 | sandbox_name = url.strip().split('/')[-1].split(";")[0].split(".")[0] |
| 137 | checkout_cmd = "git clone " + url |
| 138 | |
| 139 | try: |
| 140 | result = subprocess.check_output(checkout_cmd, shell=True, cwd=working_dir, |
| 141 | stderr=subprocess.STDOUT) |
| 142 | except subprocess.CalledProcessError as e: |
| 143 | logger.debug(e.output) |
| 144 | logger.debug(e.cmd) |
| 145 | logger.debug("Failed to clone " + sandbox_name) |
| 146 | ci_exists = "ERROR" |
| 147 | skip = True |
| 148 | |
| 149 | if not(skip): |
| 150 | docker_cmd = "WORKSPACE=$(pwd) UNIT_TEST_PKG=" + sandbox_name + " " + \ |
| 151 | "./openbmc-build-scripts/run-unit-test-docker.sh" |
| 152 | try: |
| 153 | result = subprocess.check_output(docker_cmd, cwd=working_dir, shell=True, |
| 154 | stderr=subprocess.STDOUT) |
| 155 | logger.debug(result) |
| 156 | logger.debug("UT BUILD COMPLETED FOR: " + sandbox_name) |
| 157 | |
| 158 | except subprocess.CalledProcessError as e: |
| 159 | logger.debug(e.output) |
| 160 | logger.debug(e.cmd) |
| 161 | logger.debug("UT BUILD EXITED FOR: " + sandbox_name) |
| 162 | ci_exists = "ERROR" |
| 163 | |
| 164 | folder_name = os.path.join(working_dir, sandbox_name) |
| 165 | repo_report_dir = os.path.join(report_dir, sandbox_name) |
| 166 | |
| 167 | report_names = ("coveragereport", "test-suite.log") |
| 168 | find_cmd = "".join("find " + folder_name + " -name " + report + ";" |
| 169 | for report in report_names) |
| 170 | result = subprocess.check_output(find_cmd, shell=True) |
| 171 | if result: |
| 172 | total_report_count += 1 |
| 173 | ci_exists = "YES" |
| 174 | if result.__contains__("coveragereport"): |
| 175 | ci_exists += ", COVERAGE" |
| 176 | coverage_count += 1 |
| 177 | if result.__contains__("test-suite.log"): |
| 178 | ci_exists += ", UNIT TEST" |
| 179 | unit_test_count += 1 |
| 180 | |
| 181 | result = result.splitlines() |
| 182 | for file_path in result: |
| 183 | destination = os.path.dirname(os.path.join(report_dir, |
| 184 | os.path.relpath(file_path, |
| 185 | working_dir))) |
| 186 | copy_cmd = "mkdir -p " + destination + ";cp -rf " + \ |
| 187 | file_path.strip() + " " + destination |
| 188 | subprocess.check_output(copy_cmd, shell=True) |
| 189 | if ci_exists == "ERROR": |
| 190 | error_count += 1 |
| 191 | elif ci_exists == "NO": |
| 192 | no_report_count += 1 |
| 193 | |
| 194 | coverage_report.append("{:<65}{:<10}".format(url.strip(), ci_exists)) |
| 195 | counter += 1 |
| 196 | logger.info(str(counter) + " in " + str(repo_count) + " completed") |
| 197 | |
| 198 | logger.info("*" * 30 + "UNIT TEST COVERAGE REPORT" + "*" * 30) |
| 199 | for res in coverage_report: |
| 200 | logger.info(res) |
| 201 | logger.info("*" * 30 + "UNIT TEST COVERAGE REPORT" + "*" * 30) |
| 202 | |
| 203 | logger.info("REPORTS: " + report_dir) |
| 204 | logger.info("LOGS: " + log_dir) |
| 205 | logger.info("*" * 85) |
| 206 | logger.info("SUMMARY: ") |
| 207 | logger.info("TOTAL REPOSITORIES : " + str(repo_count)) |
| 208 | logger.info("TESTED REPOSITORIES : " + str(total_report_count)) |
| 209 | logger.info("ERROR : " + str(error_count)) |
| 210 | logger.info("COVERAGE REPORT : " + str(coverage_count)) |
| 211 | logger.info("UNIT TEST REPORT : " + str(unit_test_count)) |
| 212 | logger.info("NO REPORT : " + str(no_report_count)) |
| 213 | logger.info("*" * 85) |