11#! /bin/bash
22
3+ # run_selftest.sh will run the tests within /${PROJECT_NAME}/selftests/bpf
4+ # If no specific test names are given, all test will be ran, otherwise, it will
5+ # run the test passed as parameters.
6+ # There is 2 ways to pass test names.
7+ # 1) command-line arguments to this script
8+ # 2) a comma-separated list of test names passed as `run_tests` boot parameters.
9+ # test names passed as any of those methods will be ran.
10+
311set -euo pipefail
412
513source $( cd $( dirname $0 ) && pwd) /helpers.sh
@@ -8,6 +16,8 @@ ARCH=$(uname -m)
816
917STATUS_FILE=/exitstatus
1018
19+ declare -a TEST_NAMES=()
20+
1121read_lists () {
1222 (for path in " $@ " ; do
1323 if [[ -s " $path " ]]; then
@@ -22,6 +32,28 @@ TEST_PROGS_ARGS=""
2232# TEST_PROGS_ARGS="-j"
2333# fi
2434
35+ read_test_names () {
36+ foldable start read_test_names " Reading test names from boot parameters and command line arguments"
37+ # Check if test names were passed as boot parameter.
38+ # We expect `run_tests` to be a comma-separated list of test names.
39+ IFS=' ,' read -r -a test_names_from_boot <<< \
40+ "$( sed -n ' s/.*run_tests=\([^ ]*\).*/\1/p' /proc/cmdline) "
41+
42+ echo "${# test_names_from_boot[@]} tests extracted from boot parameters: ${test_names_from_boot[*]} "
43+ # Sort and only keep unique test names from both boot params and arguments
44+ # TEST_NAMES will contain a sorted list of uniq tests to be ran.
45+ # Only do this if any of $test_names_from_boot [@] or $@ has elements as
46+ # "printf '%s\0'" will otherwise generate an empty element.
47+ if [[ ${# test_names_from_boot[@]} -gt 0 || $# -gt 0 ]]
48+ then
49+ readarray -t TEST_NAMES < \
50+ <(printf '%s\0' "${test_names_from_boot[@]} " "$@ " | \
51+ sort --zero-terminated --unique | \
52+ xargs --null --max-args=1)
53+ fi
54+ foldable end read_test_names
55+ }
56+
2557test_progs() {
2658 foldable start test_progs "Testing test_progs"
2759 # "&& true" does not change the return code (it is not executed
@@ -81,13 +113,18 @@ echo "ALLOWLIST: ${ALLOWLIST}"
81113
82114cd ${PROJECT_NAME} /selftests/bpf
83115
84- if [ $# -eq 0 ]; then
116+ # populate TEST_NAMES
117+ read_test_names "$@ "
118+ # if we don't have any test name provided to the script, we run all tests.
119+ if [ ${# TEST_NAMES[@]} -eq 0 ]; then
85120 test_progs
86121 test_progs_no_alu32
87122 test_maps
88123 test_verifier
89124else
90- for test_name in " $@ " ; do
125+ # else we run the tests passed as command-line arguments and through boot
126+ # parameter.
127+ for test_name in "${TEST_NAMES[@]} "; do
91128 "${test_name} "
92129 done
93130fi
0 commit comments