blob: 6c7fa21f289eabaf796abc040561c2f61eabe7ae [file] [log] [blame]
Anusha Dathatri229b76a2019-11-26 03:26:16 -06001#!/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
24import argparse
25import logging
26import os
27import shutil
28import sys
29import subprocess
30
31
32# Create parser.
33parser = argparse.ArgumentParser(usage='%(prog)s file target_dir',
34 description="Script generates the unit test coverage report")
35
36parser.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
41parser.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''')
44args = parser.parse_args()
45
46
47# Create target working directory.
48pwd = os.getcwd()
49working_dir = os.path.join(pwd, args.target_dir)
50try:
51 os.mkdir(working_dir)
52except 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.
67log_dir = os.path.join(working_dir, "logs")
68try:
69 os.mkdir(log_dir)
70except OSError as e:
71 print("Unable to create log directory: " + log_dir)
72 print(str(e))
73 quit()
74
75
Anusha Dathatria756e8a2020-03-05 06:48:56 -060076# List contains archived repos and those repos which are not expected to contain
77# a UT. Will be moved to a file in future.
78skip_list = ["openbmc-tools", "phosphor-mboxd", "boost-dbus", "inarp"]
79
80
Anusha Dathatri229b76a2019-11-26 03:26:16 -060081# Log files
82debug_file = os.path.join(log_dir, "debug.log")
83output_file = os.path.join(log_dir, "output.log")
84logging.basicConfig(format='%(levelname)s - %(message)s', level=logging.DEBUG,
85 filename=debug_file)
86logger = logging.getLogger(__name__)
87
88# Create handlers
89console_handler = logging.StreamHandler()
90file_handler = logging.FileHandler(output_file)
91console_handler.setLevel(logging.INFO)
92file_handler.setLevel(logging.INFO)
93
94# Create formatters and add it to handlers
95log_format = logging.Formatter('%(message)s')
96console_handler.setFormatter(log_format)
97file_handler.setFormatter(log_format)
98
99# Add handlers to the logger
100logger.addHandler(console_handler)
101logger.addHandler(file_handler)
102
103
104# Create report directory.
105report_dir = os.path.join(working_dir, "reports")
106try:
107 os.mkdir(report_dir)
108except OSError as e:
109 logger.error("Unable to create report directory: " + report_dir)
110 logger.error(str(e))
111 quit()
112
113# Clone OpenBmc build scripts.
114try:
115 output = subprocess.check_output("git clone https://github.com/openbmc/openbmc-build-scripts.git",
116 shell=True, cwd=working_dir, stderr=subprocess.STDOUT)
117 logger.debug(output)
118except subprocess.CalledProcessError as e:
119 logger.debug(e.output)
120 logger.debug(e.cmd)
121 logger.debug("Unable to clone openbmc-build-scripts")
122 quit()
123
124# Read URLs from input file.
125handle = open(args.file)
126url_list = handle.readlines()
127repo_count = len(url_list)
128logger.info("Number of repositories: " + str(repo_count))
129
130# Clone repository and run unit test.
131coverage_report = []
132counter = 0
133total_report_count = 0
134coverage_count = 0
135unit_test_count = 0
136no_report_count = 0
137error_count = 0
Anusha Dathatria756e8a2020-03-05 06:48:56 -0600138skip_count = 0
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600139for url in url_list:
140 ci_exists = "NO"
141 skip = False
142 sandbox_name = url.strip().split('/')[-1].split(";")[0].split(".")[0]
Anusha Dathatria756e8a2020-03-05 06:48:56 -0600143 if sandbox_name in skip_list:
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600144 skip = True
Anusha Dathatria756e8a2020-03-05 06:48:56 -0600145 ci_exists = "SKIPPED"
146 else:
147 checkout_cmd = "git clone " + url
148
149 try:
150 result = subprocess.check_output(checkout_cmd, shell=True, cwd=working_dir,
151 stderr=subprocess.STDOUT)
152 except subprocess.CalledProcessError as e:
153 logger.debug(e.output)
154 logger.debug(e.cmd)
155 logger.debug("Failed to clone " + sandbox_name)
156 ci_exists = "ERROR"
157 skip = True
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600158
159 if not(skip):
160 docker_cmd = "WORKSPACE=$(pwd) UNIT_TEST_PKG=" + sandbox_name + " " + \
161 "./openbmc-build-scripts/run-unit-test-docker.sh"
162 try:
163 result = subprocess.check_output(docker_cmd, cwd=working_dir, shell=True,
164 stderr=subprocess.STDOUT)
165 logger.debug(result)
166 logger.debug("UT BUILD COMPLETED FOR: " + sandbox_name)
167
168 except subprocess.CalledProcessError as e:
169 logger.debug(e.output)
170 logger.debug(e.cmd)
171 logger.debug("UT BUILD EXITED FOR: " + sandbox_name)
172 ci_exists = "ERROR"
173
174 folder_name = os.path.join(working_dir, sandbox_name)
175 repo_report_dir = os.path.join(report_dir, sandbox_name)
176
Anusha Dathatri544d83a2020-03-12 06:07:57 -0500177 report_names = ("coveragereport", "test-suite.log", "LastTest.log")
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600178 find_cmd = "".join("find " + folder_name + " -name " + report + ";"
179 for report in report_names)
180 result = subprocess.check_output(find_cmd, shell=True)
181 if result:
182 total_report_count += 1
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600183 if result.__contains__("coveragereport"):
Anusha Dathatri544d83a2020-03-12 06:07:57 -0500184 ci_exists = "YES, COVERAGE"
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600185 coverage_count += 1
Anusha Dathatri544d83a2020-03-12 06:07:57 -0500186 elif "test-suite.log" in result:
187 ci_exists = "YES, UNIT TEST"
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600188 unit_test_count += 1
Anusha Dathatri544d83a2020-03-12 06:07:57 -0500189 elif "LastTest.log" in result:
190 file_names = result.splitlines()
191 for file in file_names:
192 cmd = "sed -n '/Start testing/,/End testing/p;' " + \
193 file + "|wc -l"
194 num_of_lines = subprocess.check_output(cmd, shell=True)
195 if int(num_of_lines.strip()) > 5:
196 ci_exists = "YES, UNIT TEST"
197 unit_test_count += 1
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600198
199 result = result.splitlines()
200 for file_path in result:
201 destination = os.path.dirname(os.path.join(report_dir,
202 os.path.relpath(file_path,
203 working_dir)))
204 copy_cmd = "mkdir -p " + destination + ";cp -rf " + \
205 file_path.strip() + " " + destination
206 subprocess.check_output(copy_cmd, shell=True)
207 if ci_exists == "ERROR":
208 error_count += 1
209 elif ci_exists == "NO":
210 no_report_count += 1
Anusha Dathatria756e8a2020-03-05 06:48:56 -0600211 elif ci_exists == "SKIPPED":
212 skip_count += 1
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600213
214 coverage_report.append("{:<65}{:<10}".format(url.strip(), ci_exists))
215 counter += 1
216 logger.info(str(counter) + " in " + str(repo_count) + " completed")
217
218logger.info("*" * 30 + "UNIT TEST COVERAGE REPORT" + "*" * 30)
219for res in coverage_report:
220 logger.info(res)
221logger.info("*" * 30 + "UNIT TEST COVERAGE REPORT" + "*" * 30)
222
223logger.info("REPORTS: " + report_dir)
224logger.info("LOGS: " + log_dir)
225logger.info("*" * 85)
226logger.info("SUMMARY: ")
227logger.info("TOTAL REPOSITORIES : " + str(repo_count))
228logger.info("TESTED REPOSITORIES : " + str(total_report_count))
229logger.info("ERROR : " + str(error_count))
230logger.info("COVERAGE REPORT : " + str(coverage_count))
231logger.info("UNIT TEST REPORT : " + str(unit_test_count))
232logger.info("NO REPORT : " + str(no_report_count))
Anusha Dathatria756e8a2020-03-05 06:48:56 -0600233logger.info("SKIPPED : " + str(skip_count))
Anusha Dathatri229b76a2019-11-26 03:26:16 -0600234logger.info("*" * 85)