From a4a89b1e6823f3d730a6899846361569fa55c35b Mon Sep 17 00:00:00 2001 From: Satheesh Rajendran Date: Tue, 18 Dec 2018 16:18:40 +0530 Subject: [PATCH] Generate gcov code coverage report for qemu in postprocess This patch enables the gcov code coverage report generation as part of test postprocess if suitable params are enabled and prerequisite as mentioned in base.cfg are satisfied. This would be helpful in case of code debugging and new test scenarios development. Signed-off-by: Satheesh Rajendran --- shared/cfg/base.cfg | 19 +++++++++++++++++++ virttest/env_process.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/shared/cfg/base.cfg b/shared/cfg/base.cfg index 0e5b875499..d9d4699617 100644 --- a/shared/cfg/base.cfg +++ b/shared/cfg/base.cfg @@ -697,6 +697,25 @@ rpmbuild_path = "/root/rpmbuild/" sysprep_required = no sysprep_options = "--operations machine-id" +# Enable Code Coverage report +# prerequisite: qemu build test is run with --enable-gcov +# configure option and preserve_srcdir = yes +# and qemu build dir is available +# Enable/disable gcov for qemu, default: disable +gcov_qemu = no +# Enable/disable to reset the code coverage report +# generated/collected by previous test +gcov_qemu_reset = yes +# Additional command options for gcovr +# E:g:- "--html-details --html --exclude-directories=capstone" +# above options will be required to collect detailed html +# reports for individial source files and exclude "capstone" +# directory from code coverage report generation +# default: --html +gcov_qemu_collect_cmd_opts = "--html" +# Enable/disable to compress code coverage report +gcov_qemu_compress = no + Linux: # param for installing stress tool from repo stress_install_from_repo = "no" diff --git a/virttest/env_process.py b/virttest/env_process.py index c050935556..7dd728bb10 100644 --- a/virttest/env_process.py +++ b/virttest/env_process.py @@ -21,6 +21,7 @@ from avocado.utils import distro from avocado.utils import cpu as cpu_utils from avocado.core import exceptions +from avocado.utils import archive import six from six.moves import xrange @@ -43,6 +44,7 @@ from virttest import virsh from virttest import utils_test from virttest import utils_iptables +from virttest import utils_package from virttest.utils_version import VersionInterval from virttest.compat_52lts import decode_to_text from virttest.staging import service @@ -748,6 +750,21 @@ def preprocess(test, params, env): """ error_context.context("preprocessing") + # Check if code coverage for qemu is enabled and + # if coverage reset is enabled too, reset coverage report + gcov_qemu = params.get("gcov_qemu", "no") == "yes" + gcov_qemu_reset = params.get("gcov_qemu_reset", "no") == "yes" + if gcov_qemu and gcov_qemu_reset: + qemu_builddir = os.path.join(test.bindir, "build", "qemu") + qemu_bin = os.path.join(test.bindir, "bin", "qemu") + if os.path.isdir(qemu_builddir) and os.path.isfile(qemu_bin): + os.chdir(qemu_builddir) + # Looks like libvirt process does not have permissions to write to + # coverage files, hence give write for all files in qemu source + reset_cmd = 'make clean-coverage;%s -version;' % qemu_bin + reset_cmd += 'find %s -name "*.gcda" -exec chmod a=rwx {} \;' % qemu_builddir + a_process.system(reset_cmd, shell=True) + # Check host for any errors to start with and just report and # clear it off, so that we do not get the false test failures. if params.get("verify_host_dmesg", "yes") == "yes": @@ -1280,6 +1297,25 @@ def postprocess(test, params, env): sosreport_path = vm.sosreport() logging.info("Sosreport for guest: %s", sosreport_path) + # Collect code coverage report for qemu if enabled + if params.get("gcov_qemu", "no") == "yes": + qemu_builddir = os.path.join(test.bindir, "build", "qemu") + if os.path.isdir(qemu_builddir) and utils_package.package_install("gcovr"): + gcov_qemu_dir = utils_misc.get_path(test.debugdir, "gcov_qemu") + os.makedirs(gcov_qemu_dir) + os.chdir(qemu_builddir) + collect_cmd_opts = params.get("gcov_qemu_collect_cmd_opts", "--html") + collect_cmd = "gcovr -j %s -o %s -s %s ." % (cpu_utils.online_cpus_count(), + os.path.join(gcov_qemu_dir, "gcov.html"), + collect_cmd_opts) + a_process.system(collect_cmd, shell=True) + if params.get("gcov_qemu_compress", "no") == "yes": + os.chdir(test.debugdir) + archive.compress("gcov_qemu.tar.gz", gcov_qemu_dir) + shutil.rmtree(gcov_qemu_dir, ignore_errors=True) + else: + logging.warning("Check either qemu build directory availablilty" + " or install gcovr package for qemu coverage report") # Postprocess all VMs and images try: process(test, params, env, postprocess_image, postprocess_vm,