1HOW TO RUN KUNIT TESTS IN ANDROID 2================================= 3 4Prerequisites 5 * If you want to run a vendor module KUnit tests, please run the tests with a 6 "no trim" kernel (e.g. add `--notrim` to bazel build command). 7 8Run tests on a physical or virtual device: 9 $ kernel/tests/tools/run_test_only.sh -t kunit -s <serial_number> -td <test_dir> 10 11 test_dir is the same directory as specified when running: 12 $ tools/bazel run //common:kunit_tests_arm64 -- -v --destdir <test_dir> 13 14Before the tests, you can use the following command to launch a virtual device: 15 $ kernel/tests/tools/launch_cvd.sh 16 17After the tests, you can use the following command to remove the virtual device: 18 $ prebuilts/asuite/acloud/linux-x86/acloud delete 19 20The following are command examples: 21 * Build kernel and launch a virtual device from a specific platform build: 22 $ kernel/tests/tools/launch_cvd.sh -pb \ 23 ab://aosp-main/aosp_cf_x86_64_phone-trunk_staging-userdebug/12505199 24 25 * Run a specific test: 26 $ kernel/tests/tools/run_test_only.sh \ 27 -t 'kunit soc-utils-test' -s <serial_number> 28 29 * Check other available options: 30 $ kernel/tests/tools/launch_cvd.sh -h 31 $ kernel/tests/tools/run_test_only.sh -h 32 33Load and run a test module on Android device manually 34 * Push the KUnit test framework module kunit.ko over to the device. For 35 example: 36 37 $ adb push kunit.ko /data 38 39 * Load test module on device: 40 $ cd /data 41 $ insmod kunit.ko enable=1 42 43 If the kunit.ko has been installed already but without enable=1 passed, 44 it needs to remove it first via the rmmod command, and install again 45 via the insmod command 46 47 * Push the KUnit test module over to the device. For example using adb: 48 $ adb push kunit-example-test.ko /data 49 50 * (Optional) - Mount debugfs on device: 51 $ mount -t debugfs debugfs /sys/kernel/debug 52 53 * Load test module on device: 54 $ cd /data 55 $ insmod kunit-example-test.ko 56 57View test results 58 * If debugfs is mounted: 59 $ cat /sys/kernel/debug/kunit/<test name>/results 60 KTAP version 1 61 1..1 62 KTAP version 1 63 # Subtest: example 64 1..4 65 # example_simple_test: initializing 66 67 ok 1 example_simple_test 68 <truncated> 69 70 * Via dmesg (check before log cycles out): 71 $ dmesg 72 .... 73 [172434.032618] 1..1 74 [172434.032618] KTAP version 1 75 [172434.032618] # Subtest: example 76 [172434.032618] 1..4 77 [172434.032618] # example_simple_test: initializing 78 [172434.032618] 79 [172434.032618] ok 1 example_simple_test 80 <truncated> 81 .... 82 83Run KUnit tests on Android Device via test automation infrastructure tradefed 84 * Build ACK KUnit tests and install (e.g. /tmp/kunit_tests): 85 $ tools/bazel run -- //common:kunit_tests_x86_64 -v --destdir /tmp/kunit_tests 86 Or 87 $ tools/bazel run -- //common:kunit_tests_arm64 -v --destdir /tmp/kunit_tests 88 89 90 * With device connected and accessible via adb run the tests: 91 $ prebuilts/tradefed/filegroups/tradefed/tradefed.sh run commandAndExit \ 92 template/local_min --template:map test=suite/test_mapping_suite \ 93 --include-filter kunit --tests-dir=/tmp/kunit_tests \ 94 -s <your_device_serial_number> 95 .... 96 ======================================================= 97 =============== Summary =============== 98 Total Run time: 23s 99 1/1 modules completed 100 Total Tests : 9 101 PASSED : 9 102 FAILED : 0 103 ============== End of Results ============== 104 ============================================ 105 .... 106 107TROUBLESHOOTING 108=============== 109 1101. Test module fails to load. 111 112Check dmesg for load errors. If undefined symbol errors are shown, you're 113likely running with a trimmed kernel where the symbols are not available. 114Run with a "no trim" kernel. 115 116Check the test module dependency with `modinfo <module_name>.ko` on your local 117host machine or on the Android device with `adb shell modinfo <module_name.ko>`. 118All dependent modules need to be installed before the test module can be 119installed successfully. 120 121Check if the module is already installed with `adb shell lsmod`. The `adb shell 122rmmod` can be used to remove the already installed test module, and installing 123the test module again will trigger the test rerun. 124 125`adb shell lsmod` will also show the module dependency for your test module in 126the `Used by` column. You can not remove a module with `adb shell rmmod` if it 127is being used by another module. Other modules that are using it need to be 128removed first. 129 1302. Test module loaded but no test results 131 132Check dmesg for KUnit errors. 133 $ dmesg | grep kunit 134 135If "kunit: disabled" is shown then kunit.ko is not installed with `enable=1`. 136 137If kunit.ko or kunit_<*test>.ko fails to install, check for whether they are 138already installed with `adb shell lsmod`. 139