Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,31 +1332,47 @@ def postprocess(test, params, env):
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
if hasattr(test_name, "uid"):
test_name = str(test_name.uid)
# Save to test-results/gcov_libvirt/ to avoid long path issues
# test.debugdir is typically: .../test-results/{test-dir}/
# Navigate up to test-results/ and create gcov_qemu/ there
test_results_dir = os.path.dirname(test.debugdir)
gcov_qemu_dir = os.path.join(test_results_dir, "gcov_qemu")
os.makedirs(gcov_qemu_dir, exist_ok=True)

if gcov_format == "lcov":
try:
path.find_command("lcov")
except path.CmdNotFoundError:
LOG.warning("lcov package not installed, cannot collect QEMU coverage")
else:
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
collect_lcov_coverage(qemu_builddir, gcov_qemu_dir, test_name, "qemu")
lcov_extra_opts = params.get(
"gcov_qemu_lcov_opts",
"--rc lcov_function_coverage=0 --no-external --ignore-errors gcov",
)
collect_lcov_coverage(
qemu_builddir,
gcov_qemu_dir,
test_name,
"qemu",
extra_opts=lcov_extra_opts,
)

if params.get("gcov_qemu_compress", "no") == "yes":
os.chdir(test.debugdir)
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
os.chdir(test_results_dir)
archive.compress("lcov_qemu.tar.gz", gcov_qemu_dir)
shutil.copy2("lcov_qemu.tar.gz", test.debugdir)
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
else:
if utils_package.package_install("gcovr"):
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
collect_gcovr_coverage(
qemu_builddir, gcov_qemu_dir, "qemu", collect_cmd_opts
)

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

# Save to test-results/gcov_libvirt/ to avoid long path issues
# test.debugdir is typically: .../test-results/{test-dir}/
# Navigate up to test-results/ and create gcov_libvirt/ there
test_results_dir = os.path.dirname(test.debugdir)
gcov_libvirt_dir = os.path.join(test_results_dir, "gcov_libvirt")
os.makedirs(gcov_libvirt_dir, exist_ok=True)

if gcov_format == "lcov":
try:
path.find_command("lcov")
Expand All @@ -1377,26 +1400,34 @@ def postprocess(test, params, env):
"lcov package not installed, cannot collect libvirt coverage"
)
else:
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
lcov_extra_opts = params.get(
"gcov_libvirt_lcov_opts",
"--rc lcov_function_coverage=0 --no-external --ignore-errors gcov",
)
collect_lcov_coverage(
libvirt_builddir, gcov_libvirt_dir, test_name, "libvirt"
libvirt_builddir,
gcov_libvirt_dir,
test_name,
"libvirt",
extra_opts=lcov_extra_opts,
)

if params.get("gcov_libvirt_compress", "no") == "yes":
os.chdir(test.debugdir)
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
os.chdir(test_results_dir)
archive.compress("lcov_libvirt.tar.gz", gcov_libvirt_dir)
shutil.copy2("lcov_libvirt.tar.gz", test.debugdir)
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
else:
if utils_package.package_install("gcovr"):
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
collect_cmd_opts = params.get("gcov_libvirt_collect_cmd_opts", "--html")
collect_gcovr_coverage(
libvirt_builddir, gcov_libvirt_dir, "libvirt", collect_cmd_opts
)

if params.get("gcov_libvirt_compress", "no") == "yes":
os.chdir(test.debugdir)
os.chdir(test_results_dir)
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
shutil.copy2("gcov_libvirt.gz", test.debugdir)
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
else:
LOG.warning(
Expand Down
26 changes: 23 additions & 3 deletions virttest/test_setup/gcov.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
import logging
import os
import re

from avocado.utils import process as a_process
from avocado.utils import service

from virttest import utils_misc
from virttest.test_setup.core import Setuper

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


def collect_lcov_coverage(build_dir, output_dir, test_name, component):
def collect_lcov_coverage(
build_dir, output_dir, test_name, component, max_name_len=80, extra_opts=""
):
"""
Collect coverage data using lcov with per-test naming.

:param build_dir: Build directory containing .gcda files
:param output_dir: Output directory for coverage files
:param test_name: Name of the test for tracefile naming
:param component: Component name (qemu or libvirt)
:param max_name_len: The max len of test name used in tracefile
:param extra_opts: Additional lcov command options
:return: Path to generated tracefile, or None on failure
"""
if not os.path.isdir(build_dir):
LOG.warning("%s build directory not found: %s", component, build_dir)
return None

os.makedirs(output_dir, exist_ok=True)
tracefile = os.path.join(output_dir, "coverage_%s.info" % test_name)
tmp_test_name = test_name
if len(test_name) > max_name_len:
match = re.search(r"io-github-autotest-[^.]+\.(.*)", test_name)
Comment thread
chunfuwen marked this conversation as resolved.
if match:
short_name = match.group(1)
else:
short_name = test_name
# Ensure short_name + suffix doesn't exceed max_name_len
# Reserve 9 chars for "_" + 8-char random string
max_short_len = max_name_len - 9
if len(short_name) > max_short_len:
short_name = short_name[:max_short_len]
tmp_test_name = short_name + "_" + utils_misc.generate_random_string(8)

tracefile = os.path.join(output_dir, "coverage_%s.info" % tmp_test_name)

# Collect coverage data with test name
collect_cmd = (
"lcov --capture "
"--directory %s "
"--output-file %s "
"--test-name %s " % (build_dir, tracefile, test_name)
"--test-name %s %s" % (build_dir, tracefile, test_name, extra_opts)
)

try:
Expand Down
Loading