Skip to content

Commit 85f9af4

Browse files
committed
Implement lcov coverage collection for libvirt
Signed-off-by: Yingshun Cui <yicui@redhat.com>
1 parent 0ac71ee commit 85f9af4

3 files changed

Lines changed: 305 additions & 30 deletions

File tree

virttest/env_process.py

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from avocado.utils import archive
1818
from avocado.utils import cpu as cpu_utils
1919
from avocado.utils import crypto
20+
from avocado.utils import path
2021
from avocado.utils import process as a_process
2122
from six.moves import xrange
2223

@@ -43,7 +44,11 @@
4344
from virttest._wrappers import lazy_import
4445
from virttest.test_setup.aexpect import KillTailThreads
4546
from virttest.test_setup.core import SetupManager
46-
from virttest.test_setup.gcov import ResetQemuGCov
47+
from virttest.test_setup.gcov import (
48+
ResetGCov,
49+
collect_html_coverage,
50+
collect_lcov_coverage,
51+
)
4752
from virttest.test_setup.kernel import KSMSetup, ReloadKVMModules
4853
from virttest.test_setup.libvirt_setup import LibvirtdDebugLogConfig
4954
from virttest.test_setup.memory import HugePagesSetup, TransparentHugePagesSetup
@@ -1000,7 +1005,7 @@ def preprocess(test, params, env):
10001005
# and last running during pre/postprocess. That way vms will be actually
10011006
# off to ensure data is written to disk.
10021007
_setup_manager.register(ProcessVMOff)
1003-
_setup_manager.register(ResetQemuGCov)
1008+
_setup_manager.register(ResetGCov)
10041009
_setup_manager.register(VerifyHostDMesg)
10051010
_setup_manager.register(SwitchSMTOff)
10061011
_setup_manager.register(CheckRunningAsRoot)
@@ -1320,31 +1325,84 @@ def postprocess(test, params, env):
13201325
# Collect code coverage report for qemu if enabled
13211326
if params.get("gcov_qemu", "no") == "yes":
13221327
qemu_builddir = os.path.join(test.bindir, "build", "qemu")
1323-
if os.path.isdir(qemu_builddir) and utils_package.package_install("gcovr"):
1324-
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1325-
os.makedirs(gcov_qemu_dir)
1326-
os.chdir(qemu_builddir)
1327-
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
1328-
online_count = (
1329-
cpu_utils.online_count()
1330-
if hasattr(cpu_utils, "online_count")
1331-
else cpu_utils.online_cpus_count()
1332-
)
1333-
collect_cmd = "gcovr -j %s -o %s -s %s ." % (
1334-
online_count,
1335-
os.path.join(gcov_qemu_dir, "gcov.html"),
1336-
collect_cmd_opts,
1337-
)
1338-
a_process.system(collect_cmd, shell=True)
1339-
if params.get("gcov_qemu_compress", "no") == "yes":
1340-
os.chdir(test.debugdir)
1341-
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1342-
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
1328+
gcov_format = params.get("gcov_qemu_format", "html")
1329+
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
1330+
if hasattr(test_name, "uid"):
1331+
test_name = str(test_name.uid)
1332+
1333+
if gcov_format == "lcov":
1334+
try:
1335+
path.find_command("lcov")
1336+
except path.CmdNotFoundError:
1337+
LOG.warning("lcov package not installed, cannot collect QEMU coverage")
1338+
else:
1339+
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1340+
collect_lcov_coverage(qemu_builddir, gcov_qemu_dir, test_name, "qemu")
1341+
1342+
if params.get("gcov_qemu_compress", "no") == "yes":
1343+
os.chdir(test.debugdir)
1344+
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1345+
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
13431346
else:
1344-
LOG.warning(
1345-
"Check either qemu build directory availablilty"
1346-
" or install gcovr package for qemu coverage report"
1347-
)
1347+
if utils_package.package_install("gcovr"):
1348+
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1349+
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
1350+
collect_html_coverage(
1351+
qemu_builddir, gcov_qemu_dir, "qemu", collect_cmd_opts
1352+
)
1353+
1354+
if params.get("gcov_qemu_compress", "no") == "yes":
1355+
os.chdir(test.debugdir)
1356+
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1357+
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
1358+
else:
1359+
LOG.warning("gcovr package not installed, cannot collect QEMU coverage")
1360+
1361+
# Collect code coverage report for libvirt if enabled
1362+
if params.get("gcov_libvirt", "no") == "yes":
1363+
libvirt_builddir = params.get("gcov_libvirt_builddir", "/var/tmp/libvirt")
1364+
gcov_format = params.get("gcov_libvirt_format", "html")
1365+
# FIXME: update to a proper testname
1366+
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
1367+
if hasattr(test_name, "uid"):
1368+
test_name = str(test_name.uid)
1369+
1370+
if gcov_format == "lcov":
1371+
try:
1372+
path.find_command("lcov")
1373+
except path.CmdNotFoundError:
1374+
LOG.warning(
1375+
"lcov package not installed, cannot collect libvirt coverage"
1376+
)
1377+
else:
1378+
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1379+
collect_lcov_coverage(
1380+
libvirt_builddir, gcov_libvirt_dir, test_name, "libvirt"
1381+
)
1382+
1383+
if params.get("gcov_libvirt_compress", "no") == "yes":
1384+
os.chdir(test.debugdir)
1385+
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1386+
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
1387+
else:
1388+
if utils_package.package_install("gcovr"):
1389+
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1390+
collect_cmd_opts = params.get(
1391+
"gcov_libvirt_collect_cmd_opts", "--html"
1392+
)
1393+
collect_html_coverage(
1394+
libvirt_builddir, gcov_libvirt_dir, "libvirt", collect_cmd_opts
1395+
)
1396+
1397+
if params.get("gcov_libvirt_compress", "no") == "yes":
1398+
os.chdir(test.debugdir)
1399+
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1400+
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
1401+
else:
1402+
LOG.warning(
1403+
"gcovr package not installed, cannot collect libvirt coverage"
1404+
)
1405+
13481406
# Postprocess all VMs and images
13491407
try:
13501408
process(

virttest/shared/cfg/base.cfg

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,18 @@ sysprep_options = "--operations machine-id"
904904

905905
# Enable Code Coverage report
906906
# prerequisite: qemu build test is run with --enable-gcov
907+
# or libvirt is built with --enable-coverage#
907908
# configure option and preserve_srcdir = yes
908909
# and qemu build dir is available
909910
# Enable/disable gcov for qemu, default: disable
910911
gcov_qemu = no
911912
# Enable/disable to reset the code coverage report
912913
# generated/collected by previous test
913914
gcov_qemu_reset = yes
915+
# Coverage output format: lcov (generates .info tracefiles) or html (generates HTML reports)
916+
# For per-test lcov tracefiles with --test-name, use "lcov"
917+
# For aggregated HTML reports with gcovr, use "html"
918+
gcov_qemu_format = html
914919
# Additional command options for gcovr
915920
# E:g:- "--html-details --html --exclude-directories=capstone"
916921
# above options will be required to collect detailed html
@@ -921,6 +926,25 @@ gcov_qemu_collect_cmd_opts = "--html"
921926
# Enable/disable to compress code coverage report
922927
gcov_qemu_compress = no
923928

929+
# Libvirt code coverage configuration
930+
# Enable/disable gcov for libvirt, default: disable
931+
gcov_libvirt = no
932+
# Enable/disable to reset the libvirt coverage report
933+
# generated/collected by previous test
934+
gcov_libvirt_reset = yes
935+
# Coverage output format: lcov or html
936+
gcov_libvirt_format = lcov
937+
# Libvirt build directory where .gcda files are generated
938+
gcov_libvirt_builddir = /var/tmp/libvirt
939+
# Additional command options for gcovr when format is html
940+
# E.g.: "--html-details --html --exclude-unreachable-branches"
941+
gcov_libvirt_collect_cmd_opts = "--html"
942+
# Enable/disable to compress libvirt code coverage report
943+
gcov_libvirt_compress = no
944+
# Libvirt daemon to restart after coverage reset (if needed)
945+
gcov_libvirt_daemon = libvirtd
946+
# Enable/disable restarting libvirt daemon after coverage reset
947+
gcov_libvirt_restart_daemon = no
924948
Linux:
925949
# param for installing stress tool from repo
926950
stress_install_from_repo = "no"

0 commit comments

Comments
 (0)