Skip to content

Commit c2e1d6b

Browse files
committed
Fixup the issue where lcov capture fails due to a long path
The full test case name is longer than before. The lcov output file was created in test_result/test_debugdir, which caused the lcov command to fail. Moving the file to test_result to avoid this issue. Add "extra_opts" to collect_lcov_coverage() to accept extra lcov command options. Signed-off-by: Yingshun Cui <yicui@redhat.com>
1 parent e6a3d8b commit c2e1d6b

2 files changed

Lines changed: 66 additions & 15 deletions

File tree

virttest/env_process.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,31 +1332,47 @@ def postprocess(test, params, env):
13321332
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
13331333
if hasattr(test_name, "uid"):
13341334
test_name = str(test_name.uid)
1335+
# Save to test-results/gcov_libvirt/ to avoid long path issues
1336+
# test.debugdir is typically: .../test-results/{test-dir}/
1337+
# Navigate up to test-results/ and create gcov_qemu/ there
1338+
test_results_dir = os.path.dirname(test.debugdir)
1339+
gcov_qemu_dir = os.path.join(test_results_dir, "gcov_qemu")
1340+
os.makedirs(gcov_qemu_dir, exist_ok=True)
13351341

13361342
if gcov_format == "lcov":
13371343
try:
13381344
path.find_command("lcov")
13391345
except path.CmdNotFoundError:
13401346
LOG.warning("lcov package not installed, cannot collect QEMU coverage")
13411347
else:
1342-
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1343-
collect_lcov_coverage(qemu_builddir, gcov_qemu_dir, test_name, "qemu")
1348+
lcov_extra_opts = params.get(
1349+
"gcov_qemu_lcov_opts",
1350+
"--rc lcov_function_coverage=0 --no-external --ignore-errors gcov",
1351+
)
1352+
collect_lcov_coverage(
1353+
qemu_builddir,
1354+
gcov_qemu_dir,
1355+
test_name,
1356+
"qemu",
1357+
extra_opts=lcov_extra_opts,
1358+
)
13441359

13451360
if params.get("gcov_qemu_compress", "no") == "yes":
1346-
os.chdir(test.debugdir)
1347-
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1361+
os.chdir(test_results_dir)
1362+
archive.compress("lcov_qemu.tar.gz", gcov_qemu_dir)
1363+
shutil.copy2("lcov_qemu.tar.gz", test.debugdir)
13481364
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
13491365
else:
13501366
if utils_package.package_install("gcovr"):
1351-
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
13521367
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
13531368
collect_gcovr_coverage(
13541369
qemu_builddir, gcov_qemu_dir, "qemu", collect_cmd_opts
13551370
)
13561371

13571372
if params.get("gcov_qemu_compress", "no") == "yes":
1358-
os.chdir(test.debugdir)
1373+
os.chdir(test_results_dir)
13591374
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1375+
shutil.copy2("gcov_qemu.tar.gz", test.debugdir)
13601376
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
13611377
else:
13621378
LOG.warning("gcovr package not installed, cannot collect QEMU coverage")
@@ -1369,6 +1385,13 @@ def postprocess(test, params, env):
13691385
if hasattr(test_name, "uid"):
13701386
test_name = str(test_name.uid)
13711387

1388+
# Save to test-results/gcov_libvirt/ to avoid long path issues
1389+
# test.debugdir is typically: .../test-results/{test-dir}/
1390+
# Navigate up to test-results/ and create gcov_libvirt/ there
1391+
test_results_dir = os.path.dirname(test.debugdir)
1392+
gcov_libvirt_dir = os.path.join(test_results_dir, "gcov_libvirt")
1393+
os.makedirs(gcov_libvirt_dir, exist_ok=True)
1394+
13721395
if gcov_format == "lcov":
13731396
try:
13741397
path.find_command("lcov")
@@ -1377,26 +1400,34 @@ def postprocess(test, params, env):
13771400
"lcov package not installed, cannot collect libvirt coverage"
13781401
)
13791402
else:
1380-
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1403+
lcov_extra_opts = params.get(
1404+
"gcov_libvirt_lcov_opts",
1405+
"--rc lcov_function_coverage=0 --no-external --ignore-errors gcov",
1406+
)
13811407
collect_lcov_coverage(
1382-
libvirt_builddir, gcov_libvirt_dir, test_name, "libvirt"
1408+
libvirt_builddir,
1409+
gcov_libvirt_dir,
1410+
test_name,
1411+
"libvirt",
1412+
extra_opts=lcov_extra_opts,
13831413
)
13841414

13851415
if params.get("gcov_libvirt_compress", "no") == "yes":
1386-
os.chdir(test.debugdir)
1387-
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1416+
os.chdir(test_results_dir)
1417+
archive.compress("lcov_libvirt.tar.gz", gcov_libvirt_dir)
1418+
shutil.copy2("lcov_libvirt.tar.gz", test.debugdir)
13881419
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
13891420
else:
13901421
if utils_package.package_install("gcovr"):
1391-
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
13921422
collect_cmd_opts = params.get("gcov_libvirt_collect_cmd_opts", "--html")
13931423
collect_gcovr_coverage(
13941424
libvirt_builddir, gcov_libvirt_dir, "libvirt", collect_cmd_opts
13951425
)
13961426

13971427
if params.get("gcov_libvirt_compress", "no") == "yes":
1398-
os.chdir(test.debugdir)
1428+
os.chdir(test_results_dir)
13991429
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1430+
shutil.copy2("gcov_libvirt.gz", test.debugdir)
14001431
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
14011432
else:
14021433
LOG.warning(

virttest/test_setup/gcov.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
11
import logging
22
import os
3+
import re
34

45
from avocado.utils import process as a_process
56
from avocado.utils import service
67

8+
from virttest import utils_misc
79
from virttest.test_setup.core import Setuper
810

911
LOG = logging.getLogger("avocado." + __name__)
1012

1113

12-
def collect_lcov_coverage(build_dir, output_dir, test_name, component):
14+
def collect_lcov_coverage(
15+
build_dir, output_dir, test_name, component, max_name_len=80, extra_opts=""
16+
):
1317
"""
1418
Collect coverage data using lcov with per-test naming.
1519
1620
:param build_dir: Build directory containing .gcda files
1721
:param output_dir: Output directory for coverage files
1822
:param test_name: Name of the test for tracefile naming
1923
:param component: Component name (qemu or libvirt)
24+
:param max_name_len: The max len of test name used in tracefile
25+
:param extra_opts: Additional lcov command options
2026
:return: Path to generated tracefile, or None on failure
2127
"""
2228
if not os.path.isdir(build_dir):
2329
LOG.warning("%s build directory not found: %s", component, build_dir)
2430
return None
2531

2632
os.makedirs(output_dir, exist_ok=True)
27-
tracefile = os.path.join(output_dir, "coverage_%s.info" % test_name)
33+
tmp_test_name = test_name
34+
if len(test_name) > max_name_len:
35+
match = re.search(r"io-github-autotest-[^.]+\.(.*)", test_name)
36+
if match:
37+
short_name = match.group(1)
38+
else:
39+
short_name = test_name
40+
# Ensure short_name + suffix doesn't exceed max_name_len
41+
# Reserve 9 chars for "_" + 8-char random string
42+
max_short_len = max_name_len - 9
43+
if len(short_name) > max_short_len:
44+
short_name = short_name[:max_short_len]
45+
tmp_test_name = short_name + "_" + utils_misc.generate_random_string(8)
46+
47+
tracefile = os.path.join(output_dir, "coverage_%s.info" % tmp_test_name)
2848

2949
# Collect coverage data with test name
3050
collect_cmd = (
3151
"lcov --capture "
3252
"--directory %s "
3353
"--output-file %s "
34-
"--test-name %s " % (build_dir, tracefile, test_name)
54+
"--test-name %s %s" % (build_dir, tracefile, test_name, extra_opts)
3555
)
3656

3757
try:

0 commit comments

Comments
 (0)