ANDROID: Collect coverage data from KUnit

kunit.sh supports --gcov. It invokes bazel and TradeFed with additional
parameters. It outputs coverage data that lcov can analyze.

Test: common/tools/testing/android/bin/kunit.sh --gcov \
      -d $PWD/out/kunit/dist
Bug: 358439710
Change-Id: If9d406ab3809f405e99508ce455f3c640a09dc01
Signed-off-by: Hsin-Yi Chen <hsinyichen@google.com>
This commit is contained in:
Hsin-Yi Chen
2024-08-22 16:21:25 +08:00
parent 5677cab2b2
commit 08e0bfacae
2 changed files with 30 additions and 3 deletions
+2
View File
@@ -121,6 +121,8 @@ while test $# -gt 0; do
shift
;;
*)
echo "unknown argument: $1"
exit 1
;;
esac
done
+28 -3
View File
@@ -26,6 +26,7 @@ print_help() {
echo " If serial is specified, virtual device launch will be skipped"
echo " -t, --test=TEST_NAME The test target name. Can be repeated"
echo " If test is not specified, all tests will be run"
echo " --gcov Collect coverage data from the test result"
echo " -h, --help Display this help message and exit"
echo ""
echo "Examples:"
@@ -44,6 +45,7 @@ SERIAL_NUMBER=
MODULE_NAME="kunit"
TEST_FILTERS=
SELECTED_TESTS=
GCOV=false
while test $# -gt 0; do
case "$1" in
@@ -114,15 +116,28 @@ while test $# -gt 0; do
TEST_FILTERS+="--include-filter '$MODULE_NAME $TEST_NAME'"
shift
;;
--gcov)
GCOV=true
shift
;;
*)
echo "unknown argument: $1"
exit 1
;;
esac
done
# Kernel and modules must be built with the same flags.
BUILD_FLAGS=
if $GCOV; then
BUILD_FLAGS+=" --gcov"
fi
if $BUILD_KERNEL; then
echo "Building kernel..."
# TODO: add support to build kernel for physical device
$BAZEL run //common-modules/virtual-device:virtual_device_x86_64_dist -- --dist_dir=$DIST_DIR
$BAZEL run $BUILD_FLAGS //common-modules/virtual-device:virtual_device_x86_64_dist -- \
--dist_dir=$DIST_DIR
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Command succeeded"
@@ -162,11 +177,11 @@ echo "Building KUnit tests according to device $SERIAL_NUMBER ro.product.cpu.abi
case $ABI in
arm64*)
TESTSDIR+="_arm64"
$BAZEL run //common:kunit_tests_arm64 -- -v --destdir $TESTSDIR
$BAZEL run $BUILD_FLAGS //common:kunit_tests_arm64 -- -v --destdir $TESTSDIR
;;
x86_64*)
TESTSDIR+="_x86_64"
$BAZEL run //common:kunit_tests_x86_64 -- -v --destdir $TESTSDIR
$BAZEL run $BUILD_FLAGS //common:kunit_tests_x86_64 -- -v --destdir $TESTSDIR
;;
*)
echo "$ABI not supported"
@@ -193,6 +208,10 @@ template/local_min --template:map test=suite/test_mapping_suite \
$TEST_FILTERS --tests-dir=$TESTSDIR --log-file-path=$LOG_DIR \
--primary-abi-only -s $SERIAL_NUMBER"
if $GCOV; then
tf_cli+=" --coverage --coverage-toolchain GCOV_KERNEL --auto-collect GCOV_KERNEL_COVERAGE"
fi
echo "Runing tradefed command: $tf_cli"
eval $tf_cli
@@ -201,3 +220,9 @@ if $LAUNCH_CVD && $KILL_CVD; then
echo "Test finished. Deleting cvd instance $INSTANCE_NAME ..."
$ACLOUD delete --instance-names $INSTANCE_NAME
fi
if $GCOV; then
echo "Creating tracefile ..."
common/tools/testing/android/bin/create-tracefile.py -t $LOG_DIR -o $LOG_DIR/cov.info && \
echo "Created tracefile at $LOG_DIR/cov.info"
fi