Skip to content

Commit 37b7568

Browse files
authored
Merge pull request avocado-framework#4326 from Yingshun/lcov_libvirt
Implement lcov coverage collection for libvirt
2 parents a3cfa5a + 33570aa commit 37b7568

3 files changed

Lines changed: 353 additions & 33 deletions

File tree

virttest/env_process.py

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from avocado.core import exceptions
1717
from avocado.utils import archive
1818
from avocado.utils import cpu as cpu_utils
19-
from avocado.utils import crypto
19+
from avocado.utils import crypto, path
2020
from avocado.utils import process as a_process
2121
from six.moves import xrange
2222

@@ -45,7 +45,11 @@
4545
from virttest._wrappers import lazy_import
4646
from virttest.test_setup.aexpect import KillTailThreads
4747
from virttest.test_setup.core import SetupManager
48-
from virttest.test_setup.gcov import ResetQemuGCov
48+
from virttest.test_setup.gcov import (
49+
ResetGCov,
50+
collect_gcovr_coverage,
51+
collect_lcov_coverage,
52+
)
4953
from virttest.test_setup.kernel import KSMSetup, ReloadKVMModules
5054
from virttest.test_setup.libvirt_setup import LibvirtdDebugLogConfig
5155
from virttest.test_setup.memory import HugePagesSetup, TransparentHugePagesSetup
@@ -1002,7 +1006,7 @@ def preprocess(test, params, env):
10021006
# and last running during pre/postprocess. That way vms will be actually
10031007
# off to ensure data is written to disk.
10041008
_setup_manager.register(ProcessVMOff)
1005-
_setup_manager.register(ResetQemuGCov)
1009+
_setup_manager.register(ResetGCov)
10061010
_setup_manager.register(VerifyHostDMesg)
10071011
_setup_manager.register(SwitchSMTOff)
10081012
_setup_manager.register(CheckRunningAsRoot)
@@ -1321,32 +1325,84 @@ def postprocess(test, params, env):
13211325

13221326
# Collect code coverage report for qemu if enabled
13231327
if params.get("gcov_qemu", "no") == "yes":
1324-
qemu_builddir = os.path.join(test.bindir, "build", "qemu")
1325-
if os.path.isdir(qemu_builddir) and utils_package.package_install("gcovr"):
1326-
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1327-
os.makedirs(gcov_qemu_dir)
1328-
os.chdir(qemu_builddir)
1329-
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
1330-
online_count = (
1331-
cpu_utils.online_count()
1332-
if hasattr(cpu_utils, "online_count")
1333-
else cpu_utils.online_cpus_count()
1334-
)
1335-
collect_cmd = "gcovr -j %s -o %s -s %s ." % (
1336-
online_count,
1337-
os.path.join(gcov_qemu_dir, "gcov.html"),
1338-
collect_cmd_opts,
1339-
)
1340-
a_process.system(collect_cmd, shell=True)
1341-
if params.get("gcov_qemu_compress", "no") == "yes":
1342-
os.chdir(test.debugdir)
1343-
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1344-
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
1328+
qemu_builddir = params.get(
1329+
"gcov_qemu_builddir", os.path.join(test.bindir, "build", "qemu")
1330+
)
1331+
gcov_format = params.get("gcov_qemu_format", "html")
1332+
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
1333+
if hasattr(test_name, "uid"):
1334+
test_name = str(test_name.uid)
1335+
1336+
if gcov_format == "lcov":
1337+
try:
1338+
path.find_command("lcov")
1339+
except path.CmdNotFoundError:
1340+
LOG.warning("lcov package not installed, cannot collect QEMU coverage")
1341+
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")
1344+
1345+
if params.get("gcov_qemu_compress", "no") == "yes":
1346+
os.chdir(test.debugdir)
1347+
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1348+
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
13451349
else:
1346-
LOG.warning(
1347-
"Check either qemu build directory availablilty"
1348-
" or install gcovr package for qemu coverage report"
1349-
)
1350+
if utils_package.package_install("gcovr"):
1351+
gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu")
1352+
collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html")
1353+
collect_gcovr_coverage(
1354+
qemu_builddir, gcov_qemu_dir, "qemu", collect_cmd_opts
1355+
)
1356+
1357+
if params.get("gcov_qemu_compress", "no") == "yes":
1358+
os.chdir(test.debugdir)
1359+
archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir)
1360+
shutil.rmtree(gcov_qemu_dir, ignore_errors=True)
1361+
else:
1362+
LOG.warning("gcovr package not installed, cannot collect QEMU coverage")
1363+
1364+
# Collect code coverage report for libvirt if enabled
1365+
if params.get("gcov_libvirt", "no") == "yes":
1366+
libvirt_builddir = params.get("gcov_libvirt_builddir", "/var/tmp/libvirt")
1367+
gcov_format = params.get("gcov_libvirt_format", "html")
1368+
test_name = params.get("shortname", getattr(test, "name", "unknown_test"))
1369+
if hasattr(test_name, "uid"):
1370+
test_name = str(test_name.uid)
1371+
1372+
if gcov_format == "lcov":
1373+
try:
1374+
path.find_command("lcov")
1375+
except path.CmdNotFoundError:
1376+
LOG.warning(
1377+
"lcov package not installed, cannot collect libvirt coverage"
1378+
)
1379+
else:
1380+
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1381+
collect_lcov_coverage(
1382+
libvirt_builddir, gcov_libvirt_dir, test_name, "libvirt"
1383+
)
1384+
1385+
if params.get("gcov_libvirt_compress", "no") == "yes":
1386+
os.chdir(test.debugdir)
1387+
archive.compress("gcov_libvirt.tar.gz", gcov_libvirt_dir)
1388+
shutil.rmtree(gcov_libvirt_dir, ignore_errors=True)
1389+
else:
1390+
if utils_package.package_install("gcovr"):
1391+
gcov_libvirt_dir = utils_misc.get_path(test.debugdir, "gcov_libvirt")
1392+
collect_cmd_opts = params.get("gcov_libvirt_collect_cmd_opts", "--html")
1393+
collect_gcovr_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+
13501406
# Postprocess all VMs and images
13511407
try:
13521408
process(

virttest/shared/cfg/base.cfg

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

909909
# Enable Code Coverage report
910910
# prerequisite: qemu build test is run with --enable-gcov
911+
# or libvirt is built with --enable-coverage
911912
# configure option and preserve_srcdir = yes
912913
# and qemu build dir is available
913914
# Enable/disable gcov for qemu, default: disable
914915
gcov_qemu = no
915916
# Enable/disable to reset the code coverage report
916917
# generated/collected by previous test
917918
gcov_qemu_reset = yes
919+
# Coverage output format: lcov (generates .info tracefiles) or html (generates HTML reports)
920+
# For per-test lcov tracefiles with --test-name, use "lcov"
921+
# For aggregated HTML reports with gcovr, use "html"
922+
gcov_qemu_format = html
918923
# Additional command options for gcovr
919924
# E:g:- "--html-details --html --exclude-directories=capstone"
920925
# above options will be required to collect detailed html
@@ -924,6 +929,32 @@ gcov_qemu_reset = yes
924929
gcov_qemu_collect_cmd_opts = "--html"
925930
# Enable/disable to compress code coverage report
926931
gcov_qemu_compress = no
932+
# Qemu build directory where .gcda files are generated
933+
gcov_qemu_builddir = /var/tmp/qemu
934+
# Fail test if coverage reset fails
935+
gcov_qemu_reset_strict = yes
936+
937+
# Libvirt code coverage configuration
938+
# Enable/disable gcov for libvirt, default: disable
939+
gcov_libvirt = no
940+
# Enable/disable to reset the libvirt coverage report
941+
# generated/collected by previous test
942+
gcov_libvirt_reset = yes
943+
# Coverage output format: lcov or html
944+
gcov_libvirt_format = lcov
945+
# Libvirt build directory where .gcda files are generated
946+
gcov_libvirt_builddir = /var/tmp/libvirt
947+
# Additional command options for gcovr when format is html
948+
# E.g.: "--html-details --html --exclude-unreachable-branches"
949+
gcov_libvirt_collect_cmd_opts = "--html"
950+
# Enable/disable to compress libvirt code coverage report
951+
gcov_libvirt_compress = no
952+
# Libvirt daemon to restart after coverage reset (if needed)
953+
gcov_libvirt_daemon = libvirtd
954+
# Enable/disable restarting libvirt daemon after coverage reset
955+
gcov_libvirt_restart_daemon = no
956+
# Fail test if coverage reset fails
957+
gcov_libvirt_reset_strict = yes
927958

928959
Linux:
929960
# param for installing stress tool from repo

0 commit comments

Comments
 (0)