2021-05-05 09:32:56 -03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Runs tests with gcov coverage support.
|
|
|
|
|
|
|
|
AP_FLAKE8_CLEAN
|
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import os
|
2021-05-19 18:01:56 -03:00
|
|
|
import tempfile
|
2021-05-05 09:32:56 -03:00
|
|
|
import time
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
os.environ['PYTHONUNBUFFERED'] = '1'
|
|
|
|
os.set_blocking(sys.stdout.fileno(), True)
|
2021-05-19 18:01:56 -03:00
|
|
|
os.set_blocking(sys.stderr.fileno(), True)
|
2021-05-05 09:32:56 -03:00
|
|
|
|
|
|
|
tools_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
root_dir = os.path.realpath(os.path.join(tools_dir, '../..'))
|
|
|
|
|
|
|
|
|
|
|
|
class CoverageRunner(object):
|
|
|
|
"""Coverage Runner Class."""
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def __init__(self, verbose=False, check_tests=True) -> None:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Set the files Path."""
|
|
|
|
self.REPORT_DIR = os.path.join(root_dir, "reports/lcov-report")
|
|
|
|
self.INFO_FILE = os.path.join(root_dir, self.REPORT_DIR, "lcov.info")
|
|
|
|
self.INFO_FILE_BASE = os.path.join(root_dir, self.REPORT_DIR, "lcov_base.info")
|
|
|
|
self.LCOV_LOG = os.path.join(root_dir, "GCOV_lcov.log")
|
|
|
|
self.GENHTML_LOG = os.path.join(root_dir, "GCOV_genhtml.log")
|
|
|
|
|
|
|
|
self.autotest = os.path.join(root_dir, "Tools/autotest/autotest.py")
|
2021-05-19 18:01:56 -03:00
|
|
|
self.verbose = verbose
|
2021-11-05 19:19:02 -03:00
|
|
|
self.check_tests = check_tests
|
2021-05-05 09:32:56 -03:00
|
|
|
self.start_time = time.time()
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def progress(self, text) -> None:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Pretty printer."""
|
|
|
|
delta_time = time.time() - self.start_time
|
|
|
|
formatted_text = "****** AT-%06.1f: %s" % (delta_time, text)
|
|
|
|
print(formatted_text)
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def init_coverage(self, use_example=False) -> None:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Initialize ArduPilot for coverage.
|
|
|
|
|
|
|
|
This needs to be run with the binaries built.
|
|
|
|
"""
|
|
|
|
self.progress("Initializing Coverage...")
|
|
|
|
self.progress("Removing previous reports")
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self.REPORT_DIR)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs(self.REPORT_DIR)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.progress("Checking that vehicles binaries are set up and built")
|
|
|
|
binaries_dir = os.path.join(root_dir, 'build/sitl/bin')
|
2021-05-19 18:01:56 -03:00
|
|
|
dirs_to_check = [["binaries", binaries_dir], ["tests", os.path.join(root_dir, 'build/linux/tests')]]
|
|
|
|
if use_example:
|
2023-05-23 17:13:35 -03:00
|
|
|
self.progress("Adding examples")
|
2021-05-19 18:01:56 -03:00
|
|
|
dirs_to_check.append(["examples", os.path.join(root_dir, 'build/linux/examples')])
|
|
|
|
for dirc in dirs_to_check:
|
|
|
|
if not (self.check_build(dirc[0], dirc[1])):
|
|
|
|
self.run_build()
|
|
|
|
break
|
2021-05-05 09:32:56 -03:00
|
|
|
|
|
|
|
self.progress("Zeroing previous build")
|
|
|
|
retcode = subprocess.call(["lcov", "--zerocounters", "--directory", root_dir])
|
|
|
|
if retcode != 0:
|
|
|
|
self.progress("Failed with retcode (%s)" % retcode)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
self.progress("Initializing Coverage with current build")
|
|
|
|
try:
|
|
|
|
result = subprocess.run(["lcov",
|
|
|
|
"--no-external",
|
|
|
|
"--initial",
|
|
|
|
"--capture",
|
|
|
|
"--exclude", root_dir + "/build/sitl/modules/*",
|
|
|
|
"--directory", root_dir,
|
|
|
|
"-o", self.INFO_FILE_BASE,
|
|
|
|
], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=True)
|
2021-05-19 18:01:56 -03:00
|
|
|
if self.verbose:
|
2021-05-05 09:32:56 -03:00
|
|
|
print(*result.args)
|
|
|
|
print(result.stdout)
|
|
|
|
with open(self.LCOV_LOG, 'w') as log_file:
|
|
|
|
log_file.write(result.stdout)
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print("ERROR :")
|
|
|
|
print(err.cmd)
|
|
|
|
print(err.output)
|
|
|
|
exit(1)
|
|
|
|
self.progress("Initialization done")
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def check_build(self, name, path) -> bool:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Check that build directory is not empty and that binaries are built with the coverage flags."""
|
|
|
|
self.progress("Checking that %s are set up and built" % name)
|
|
|
|
if os.path.exists(path):
|
|
|
|
if not os.listdir(path):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
self.progress("Checking %s build configuration for coverage" % name)
|
|
|
|
with open(os.path.join(path, "../compile_commands.json"), "r") as searchfile:
|
|
|
|
for line in searchfile:
|
|
|
|
if "-ftest-coverage" in line:
|
|
|
|
return True
|
|
|
|
self.progress("%s was't built with coverage support" % name)
|
|
|
|
return False
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def run_build(self, use_example=False) -> None:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Clean the build directory and build binaries for coverage."""
|
|
|
|
|
|
|
|
os.chdir(root_dir)
|
|
|
|
waf_light = os.path.join(root_dir, "modules/waf/waf-light")
|
2021-05-19 18:01:56 -03:00
|
|
|
self.progress("Removing previous build binaries")
|
|
|
|
subprocess.run([waf_light, "configure", "--debug"], check=True)
|
|
|
|
subprocess.run([waf_light, "clean"], check=True)
|
|
|
|
|
|
|
|
self.progress("Building examples and SITL binaries")
|
|
|
|
|
2021-05-05 09:32:56 -03:00
|
|
|
try:
|
|
|
|
if use_example:
|
2023-05-23 17:13:35 -03:00
|
|
|
self.progress("Building examples")
|
2021-05-19 18:01:56 -03:00
|
|
|
subprocess.run([waf_light, "configure", "--board=linux", "--debug", "--coverage"], check=True)
|
2021-05-05 09:32:56 -03:00
|
|
|
subprocess.run([waf_light, "examples"], check=True)
|
|
|
|
subprocess.run(
|
|
|
|
[self.autotest,
|
|
|
|
"--debug",
|
2021-05-19 18:01:56 -03:00
|
|
|
"--coverage",
|
|
|
|
"build.unit_tests"],
|
|
|
|
check=True)
|
|
|
|
subprocess.run([waf_light, "configure", "--debug", "--coverage"], check=True)
|
2021-05-05 09:32:56 -03:00
|
|
|
subprocess.run([waf_light], check=True)
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print("ERROR :")
|
|
|
|
print(err.cmd)
|
|
|
|
print(err.output)
|
|
|
|
exit(1)
|
|
|
|
self.progress("Build examples and vehicle binaries done !")
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def run_full(self, use_example=False) -> None:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Run full coverage on maximum of ArduPilot binaries and test functions."""
|
|
|
|
self.progress("Running full test suite...")
|
|
|
|
self.run_build()
|
|
|
|
self.init_coverage()
|
|
|
|
self.progress("Running tests")
|
|
|
|
SPEEDUP = 5
|
|
|
|
TIMEOUT = 14400
|
|
|
|
|
|
|
|
if use_example:
|
|
|
|
self.progress("Running run.examples")
|
|
|
|
subprocess.run([self.autotest,
|
|
|
|
"--timeout=" + str(TIMEOUT),
|
|
|
|
"--debug",
|
2023-05-23 17:13:35 -03:00
|
|
|
"--coverage",
|
2021-05-05 09:32:56 -03:00
|
|
|
"--no-clean",
|
|
|
|
"--speedup=" + str(SPEEDUP),
|
2021-11-05 19:19:40 -03:00
|
|
|
"run.examples"], check=self.check_tests)
|
2021-05-05 09:32:56 -03:00
|
|
|
self.progress("Running run.unit_tests")
|
|
|
|
subprocess.run(
|
|
|
|
[self.autotest,
|
|
|
|
"--timeout=" + str(TIMEOUT),
|
|
|
|
"--debug",
|
|
|
|
"--no-clean",
|
2021-11-05 19:19:02 -03:00
|
|
|
"run.unit_tests"], check=self.check_tests)
|
|
|
|
subprocess.run(["reset"], check=True)
|
2021-05-05 09:32:56 -03:00
|
|
|
os.set_blocking(sys.stdout.fileno(), True)
|
2021-05-19 18:01:56 -03:00
|
|
|
os.set_blocking(sys.stderr.fileno(), True)
|
2021-11-05 19:19:40 -03:00
|
|
|
test_list = ["Plane", "QuadPlane", "Sub", "Copter", "Helicopter", "Rover", "Tracker", "BalanceBot", "Sailboat"]
|
2021-05-05 09:32:56 -03:00
|
|
|
for test in test_list:
|
|
|
|
self.progress("Running test.%s" % test)
|
2023-05-24 12:27:38 -03:00
|
|
|
try:
|
|
|
|
subprocess.run([self.autotest,
|
|
|
|
"--timeout=" + str(TIMEOUT),
|
|
|
|
"--debug",
|
|
|
|
"--no-clean",
|
|
|
|
"test.%s" % test], check=self.check_tests)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
# pass in case of failing tests
|
|
|
|
pass
|
2021-05-05 09:32:56 -03:00
|
|
|
# TODO add any other execution path/s we can to maximise the actually
|
|
|
|
# used code, can we run other tests or things? Replay, perhaps?
|
|
|
|
self.update_stats()
|
|
|
|
|
2023-05-23 17:13:35 -03:00
|
|
|
def update_stats(self) -> None:
|
2021-05-05 09:32:56 -03:00
|
|
|
"""Update Coverage statistics only.
|
|
|
|
|
|
|
|
Assumes that coverage tests have been run.
|
|
|
|
"""
|
|
|
|
self.progress("Generating Coverage statistics")
|
|
|
|
with open(self.LCOV_LOG, 'a') as log_file:
|
|
|
|
# we cannot use subprocess.PIPE and result.stdout to get the output as it will be too long and trigger
|
|
|
|
# BlockingIOError: [Errno 11] write could not complete without blocking
|
|
|
|
# thus we ouput to temp file, and print the file line by line...
|
2021-05-19 18:01:56 -03:00
|
|
|
with tempfile.NamedTemporaryFile(mode="w+") as tmp_file:
|
2021-05-05 09:32:56 -03:00
|
|
|
try:
|
2021-05-19 18:01:56 -03:00
|
|
|
self.progress("Capturing Coverage statistics")
|
2021-05-05 09:32:56 -03:00
|
|
|
subprocess.run(["lcov",
|
|
|
|
"--no-external",
|
|
|
|
"--capture",
|
|
|
|
"--directory", root_dir,
|
|
|
|
"-o", self.INFO_FILE,
|
|
|
|
], stdout=tmp_file, stderr=subprocess.STDOUT, text=True, check=True)
|
2021-05-19 18:01:56 -03:00
|
|
|
if self.verbose:
|
2021-05-05 09:32:56 -03:00
|
|
|
tmp_file.seek(0)
|
|
|
|
content = tmp_file.read().splitlines()
|
|
|
|
for line in content:
|
|
|
|
print(line, flush=True)
|
|
|
|
log_file.write(line)
|
|
|
|
|
2021-05-19 18:01:56 -03:00
|
|
|
self.progress("Matching Coverage with binaries")
|
2021-05-05 09:32:56 -03:00
|
|
|
subprocess.run(["lcov",
|
|
|
|
"--add-tracefile", self.INFO_FILE_BASE,
|
|
|
|
"--add-tracefile", self.INFO_FILE,
|
|
|
|
], stdout=tmp_file, stderr=subprocess.STDOUT, text=True, check=True)
|
2021-05-19 18:01:56 -03:00
|
|
|
if self.verbose:
|
2021-05-05 09:32:56 -03:00
|
|
|
tmp_file.seek(0)
|
|
|
|
content = tmp_file.read().splitlines()
|
|
|
|
for line in content:
|
2021-05-19 18:01:56 -03:00
|
|
|
# print(line, flush=True) # not usefull to print
|
2021-05-05 09:32:56 -03:00
|
|
|
log_file.write(line)
|
|
|
|
# remove files we do not intentionally test:
|
2021-05-19 18:01:56 -03:00
|
|
|
self.progress("Removing unwanted coverage statistics")
|
2021-05-05 09:32:56 -03:00
|
|
|
subprocess.run(["lcov",
|
|
|
|
"--remove", self.INFO_FILE,
|
|
|
|
".waf*",
|
|
|
|
root_dir + "/modules/gtest/*",
|
2021-11-11 08:17:49 -04:00
|
|
|
root_dir + "/modules/DroneCAN/libcanard/*",
|
2021-05-05 09:32:56 -03:00
|
|
|
root_dir + "/build/linux/libraries/*",
|
2023-05-23 17:12:33 -03:00
|
|
|
root_dir + "/build/linux/modules/*",
|
2021-05-05 09:32:56 -03:00
|
|
|
root_dir + "/build/sitl/libraries/*",
|
|
|
|
root_dir + "/build/sitl/modules/*",
|
2021-06-14 05:36:34 -03:00
|
|
|
root_dir + "/build/sitl_periph_gps/libraries/*",
|
|
|
|
root_dir + "/build/sitl_periph_gps/modules/*",
|
2021-05-19 18:01:56 -03:00
|
|
|
root_dir + "/libraries/*/examples/*",
|
2021-06-19 12:04:17 -03:00
|
|
|
root_dir + "/libraries/*/tests/*",
|
2021-05-05 09:32:56 -03:00
|
|
|
"-o", self.INFO_FILE
|
|
|
|
], stdout=tmp_file, stderr=subprocess.STDOUT, text=True, check=True)
|
2021-05-19 18:01:56 -03:00
|
|
|
if self.verbose:
|
2021-05-05 09:32:56 -03:00
|
|
|
tmp_file.seek(0)
|
|
|
|
content = tmp_file.read().splitlines()
|
|
|
|
for line in content:
|
2021-05-19 18:01:56 -03:00
|
|
|
# print(line, flush=True) # not usefull to print
|
2021-05-05 09:32:56 -03:00
|
|
|
log_file.write(line)
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print("ERROR :")
|
|
|
|
print(err.cmd)
|
|
|
|
print(err.output)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with open(self.GENHTML_LOG, 'w+') as log_file:
|
|
|
|
try:
|
2021-05-19 18:01:56 -03:00
|
|
|
self.progress("Generating HTML files")
|
2021-05-05 09:32:56 -03:00
|
|
|
subprocess.run(["genhtml", self.INFO_FILE,
|
|
|
|
"-o", self.REPORT_DIR,
|
2021-05-19 18:01:56 -03:00
|
|
|
"--demangle-cpp",
|
2021-05-05 09:32:56 -03:00
|
|
|
], stdout=log_file, stderr=subprocess.STDOUT, text=True, check=True)
|
2021-05-19 18:01:56 -03:00
|
|
|
|
|
|
|
log_file.seek(0)
|
|
|
|
content = log_file.read().splitlines()
|
|
|
|
if self.verbose:
|
|
|
|
for line in content[1: -3]:
|
2021-05-05 09:32:56 -03:00
|
|
|
print(line, flush=True)
|
2021-05-19 18:01:56 -03:00
|
|
|
for line in content[-3:]:
|
|
|
|
print(line, flush=True)
|
2021-05-05 09:32:56 -03:00
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print("ERROR :")
|
|
|
|
print(err.cmd)
|
|
|
|
print(err.output)
|
|
|
|
exit(1)
|
|
|
|
self.progress("Coverage successful. Open " + self.REPORT_DIR + "/index.html")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Runs tests with gcov coverage support.')
|
2021-05-19 18:01:56 -03:00
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help='Output everything on terminal.')
|
2023-05-23 17:13:35 -03:00
|
|
|
parser.add_argument('-c', '--no-check-tests', action='store_true',
|
|
|
|
help='Do not fail if tests do not run.')
|
|
|
|
parser.add_argument('--add-examples', action='store_true',
|
|
|
|
help='Add examples to coverage.')
|
2021-05-05 09:32:56 -03:00
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument('-i', '--init', action='store_true',
|
|
|
|
help='Initialise ArduPilot for coverage. It should be run after building the binaries.')
|
|
|
|
group.add_argument('-f', '--full', action='store_true',
|
|
|
|
help='Run ArduPilot full coverage. This will run all tests and example. It is really long.')
|
|
|
|
group.add_argument('-b', '--build', action='store_true',
|
|
|
|
help='Clean the build directory and build binaries for coverage.')
|
|
|
|
group.add_argument('-u', '--update', action='store_true',
|
|
|
|
help='Update coverage statistics. To be used after running some tests.')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-11-05 19:19:02 -03:00
|
|
|
runner = CoverageRunner(verbose=args.verbose, check_tests=not args.no_check_tests)
|
2021-05-05 09:32:56 -03:00
|
|
|
if args.init:
|
2023-05-23 17:13:35 -03:00
|
|
|
runner.init_coverage(args.add_examples)
|
2021-05-05 09:32:56 -03:00
|
|
|
sys.exit(0)
|
|
|
|
if args.full:
|
2023-05-23 17:13:35 -03:00
|
|
|
runner.run_full(args.add_examples)
|
2021-05-05 09:32:56 -03:00
|
|
|
sys.exit(0)
|
|
|
|
if args.build:
|
2023-05-23 17:13:35 -03:00
|
|
|
runner.run_build(args.add_examples)
|
2021-05-05 09:32:56 -03:00
|
|
|
sys.exit(0)
|
|
|
|
if args.update:
|
|
|
|
runner.update_stats()
|
|
|
|
sys.exit(0)
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(0)
|