Skip to content

Commit 64f1f60

Browse files
committed
test: refactor run.sh to use common functestlib.sh functions
- Integrated log_info, log_pass, log_fail, log_error, and check_dependencies - Ensured compatibility with both standalone and run-test.sh execution - Improved maintainability and consistency across test suites - Added Kselfttest suite Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 2d02349 commit 64f1f60

File tree

36 files changed

+1567
-317
lines changed

36 files changed

+1567
-317
lines changed

Runner/init_env

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
#!/bin/sh
2+
23
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
34
# SPDX-License-Identifier: BSD-3-Clause-Clear
45

5-
# Source this file to setup the test suite environment
6-
BASEDIR=$(pwd)
7-
export BASEDIR
8-
TOOLS=$(pwd)/utils
9-
export TOOLS
10-
SUITES=$(pwd)/suites
11-
export SUITES
6+
# Dynamically walk up to find the repo root containing both suites/ and utils/
7+
INIT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
ROOT_DIR="$INIT_DIR"
9+
10+
while [ "$ROOT_DIR" != "/" ]; do
11+
if [ -d "$ROOT_DIR/suites" ] && [ -d "$ROOT_DIR/utils" ]; then
12+
break
13+
fi
14+
ROOT_DIR=$(dirname "$ROOT_DIR")
15+
done
16+
17+
# Validate discovery
18+
if [ ! -d "$ROOT_DIR/suites" ] || [ ! -d "$ROOT_DIR/utils" ]; then
19+
echo "[ERROR] Could not detect testkit root from init_env!" >&2
20+
exit 1
21+
fi
22+
23+
export ROOT_DIR
24+
export TOOLS="$ROOT_DIR/utils"
25+
export __RUNNER_SUITES_DIR="$ROOT_DIR/suites"
26+
export __RUNNER_UTILS_BIN_DIR="$ROOT_DIR/common"
27+
28+
echo "[INFO] init_env loaded."
29+
echo "[INFO] ROOT_DIR=$ROOT_DIR"
30+
echo "[INFO] TOOLS=$TOOLS"
31+
echo "[INFO] __RUNNER_SUITES_DIR=$__RUNNER_SUITES_DIR"
32+
echo "[INFO] __RUNNER_UTILS_BIN_DIR=$__RUNNER_UTILS_BIN_DIR"

Runner/run-test.sh

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,112 @@
33
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

6-
# Import test suite definitions
7-
. "${PWD}"/init_env
6+
# Resolve the real path of this script
7+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
88

9-
#import test functions library
10-
. "${TOOLS}"/functestlib.sh
9+
# Set TOOLS path to utils under the script directory
10+
TOOLS="$SCRIPT_DIR/utils"
1111

12+
# Safely source init_env from the same directory as this script
13+
if [ -f "$SCRIPT_DIR/init_env" ]; then
14+
# shellcheck source=/dev/null
15+
. "$SCRIPT_DIR/init_env"
16+
else
17+
echo "[INFO] init_env not found at $SCRIPT_DIR/init_env — skipping."
18+
fi
1219

13-
# Find test case path by name
14-
find_test_case_by_name() {
15-
# Check if the file is a directory
16-
if [ -d "$1" ]; then
17-
# Get the directory name
18-
dir_name_in_dir=${1##*/}
19-
20-
# Check if the directory name matches the user input
21-
if [ "${dir_name_in_dir}" = "$test_name" ]; then
22-
# Get the absolute path of the directory
23-
abs_path=$(readlink -f "$1")
24-
echo "$abs_path"
25-
fi
26-
fi
20+
# Source functestlib.sh from utils/
21+
if [ -f "$TOOLS/functestlib.sh" ]; then
22+
# shellcheck source=/dev/null
23+
. "$TOOLS/functestlib.sh"
24+
# Export key vars so they are visible to child scripts like ./run.sh
25+
export ROOT_DIR
26+
export TOOLS
27+
export __RUNNER_SUITES_DIR
28+
export __RUNNER_UTILS_BIN_DIR
29+
else
30+
echo "[ERROR] functestlib.sh not found at $TOOLS/functestlib.sh"
31+
exit 1
32+
fi
2733

28-
# Recursively search for the directory in the subdirectory
29-
for file in "$1"/*; do
30-
# Check if the file is a directory
31-
if [ -d "$file" ]; then
32-
# Recursively search for the directory in the subdirectory
33-
find_test_case_by_name "$file"
34-
fi
35-
done
36-
}
34+
# Store results
35+
RESULTS_PASS=""
36+
RESULTS_FAIL=""
3737

38-
# Execute a test case
3938
execute_test_case() {
4039
local test_path="$1"
40+
local test_name
41+
test_name=$(basename "$test_path")
42+
4143
if [ -d "$test_path" ]; then
4244
run_script="$test_path/run.sh"
4345
if [ -f "$run_script" ]; then
44-
log "Executing test case: $test_path"
45-
sh "$run_script" 2>&1
46+
log "Executing test case: $test_name"
47+
if (cd "$test_path" && sh "./run.sh"); then
48+
log_pass "$test_name passed"
49+
if [ -z "$RESULTS_PASS" ]; then
50+
RESULTS_PASS="$test_name"
51+
else
52+
RESULTS_PASS=$(printf "%s\n%s" "$RESULTS_PASS" "$test_name")
53+
fi
54+
else
55+
log_fail "$test_name failed"
56+
if [ -z "$RESULTS_FAIL" ]; then
57+
RESULTS_FAIL="$test_name"
58+
else
59+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name")
60+
fi
61+
fi
4662
else
47-
log "No run.sh found in $test_path"
63+
log_error "No run.sh found in $test_path"
64+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (missing run.sh)")
4865
fi
4966
else
50-
log "Test case directory not found: $test_path"
67+
log_error "Test case directory not found: $test_path"
68+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (directory not found)")
5169
fi
5270
}
5371

54-
# Function to run a specific test case by name
5572
run_specific_test_by_name() {
56-
test_name="$1"
57-
test_path=$(find_test_case_by_name ".")
73+
local test_name="$1"
74+
local test_path
75+
test_path=$(find_test_case_by_name "$test_name")
5876
if [ -z "$test_path" ]; then
59-
log "Test case with name $test_name not found."
77+
log_error "Test case with name $test_name not found."
78+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (not found)")
6079
else
6180
execute_test_case "$test_path"
6281
fi
6382
}
6483

65-
# Main script logic
84+
run_all_tests() {
85+
find "${__RUNNER_SUITES_DIR}" -type d -name '[A-Za-z]*' -maxdepth 3 | while IFS= read -r test_dir; do
86+
if [ -f "$test_dir/run.sh" ]; then
87+
execute_test_case "$test_dir"
88+
fi
89+
done
90+
}
91+
92+
print_summary() {
93+
echo
94+
log_info "========== Test Summary =========="
95+
echo "PASSED:"
96+
[ -n "$RESULTS_PASS" ] && printf "%s\n" "$RESULTS_PASS" || echo " None"
97+
echo
98+
echo "FAILED:"
99+
[ -n "$RESULTS_FAIL" ] && printf "%s\n" "$RESULTS_FAIL" || echo " None"
100+
log_info "=================================="
101+
}
102+
66103
if [ "$#" -eq 0 ]; then
67104
log "Usage: $0 [all | <testcase_name>]"
68105
exit 1
69106
fi
70107

108+
if [ "$1" = "all" ]; then
109+
run_all_tests
110+
else
111+
run_specific_test_by_name "$1"
112+
fi
71113

72-
run_specific_test_by_name "$1"
114+
print_summary

Runner/suites/Kernel/FunctionalArea/baseport/BWMON/run.sh

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,40 @@
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

66
# Import test suite definitions
7-
. "${PWD}"/init_env
8-
TESTNAME="BWMON"
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
ROOT_DIR="$SCRIPT_DIR"
9+
while [ "$ROOT_DIR" != "/" ]; do
10+
if [ -d "$ROOT_DIR/utils" ] && [ -d "$ROOT_DIR/suites" ]; then
11+
break
12+
fi
13+
ROOT_DIR=$(dirname "$ROOT_DIR")
14+
done
15+
16+
if [ ! -d "$ROOT_DIR/utils" ] || [ ! -f "$ROOT_DIR/utils/functestlib.sh" ]; then
17+
echo "[ERROR] Could not detect testkit root (missing utils/ or functestlib.sh)" >&2
18+
exit 1
19+
fi
20+
21+
TOOLS="$ROOT_DIR/utils"
22+
INIT_ENV="$ROOT_DIR/init_env"
23+
FUNCLIB="$TOOLS/functestlib.sh"
924

10-
#import test functions library
11-
. "${TOOLS}"/functestlib.sh
25+
[ -f "$INIT_ENV" ] && . "$INIT_ENV"
26+
. "$FUNCLIB"
27+
28+
__RUNNER_SUITES_DIR="${__RUNNER_SUITES_DIR:-$ROOT_DIR/suites}"
29+
30+
TESTNAME="BWMON"
1231
test_path=$(find_test_case_by_name "$TESTNAME")
13-
log_info "--------------------------------------------------------------------------"
32+
cd "$test_path" || exit 1
33+
res_file="./$TESTNAME.res"
34+
35+
log_info "-----------------------------------------------------------------------------------------"
1436
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
37+
log_info "=== Test Initialization ==="
38+
39+
sync
40+
sleep 1
1541

1642
log_info "Checking if dependency binary is available"
1743
check_dependencies bw_mem
@@ -56,9 +82,9 @@ done
5682

5783
if $incremented; then
5884
log_pass "$TESTNAME : Test Passed"
59-
echo "$TESTNAME PASS" > $test_path/$TESTNAME.res
85+
echo "$TESTNAME PASS" > "$res_file"
6086
else
6187
log_fail "$TESTNAME : Test Failed"
62-
echo "$TESTNAME FAIL" > $test_path/$TESTNAME.res
88+
echo "$TESTNAME FAIL" > "$res_file"
6389
fi
6490
log_info "-------------------Completed $TESTNAME Testcase----------------------------"

Runner/suites/Kernel/FunctionalArea/baseport/Buses/run.sh

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,52 @@
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

66
# Import test suite definitions
7-
. "${PWD}"/init_env
8-
TESTNAME="Buses"
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
ROOT_DIR="$SCRIPT_DIR"
9+
while [ "$ROOT_DIR" != "/" ]; do
10+
if [ -d "$ROOT_DIR/utils" ] && [ -d "$ROOT_DIR/suites" ]; then
11+
break
12+
fi
13+
ROOT_DIR=$(dirname "$ROOT_DIR")
14+
done
15+
16+
if [ ! -d "$ROOT_DIR/utils" ] || [ ! -f "$ROOT_DIR/utils/functestlib.sh" ]; then
17+
echo "[ERROR] Could not detect testkit root (missing utils/ or functestlib.sh)" >&2
18+
exit 1
19+
fi
20+
21+
TOOLS="$ROOT_DIR/utils"
22+
INIT_ENV="$ROOT_DIR/init_env"
23+
FUNCLIB="$TOOLS/functestlib.sh"
24+
25+
[ -f "$INIT_ENV" ] && . "$INIT_ENV"
26+
. "$FUNCLIB"
927

10-
#import test functions library
11-
. "${TOOLS}"/functestlib.sh
28+
__RUNNER_SUITES_DIR="${__RUNNER_SUITES_DIR:-$ROOT_DIR/suites}"
29+
30+
TESTNAME="Buses"
1231
test_path=$(find_test_case_by_name "$TESTNAME")
32+
cd "$test_path" || exit 1
33+
res_file="./$TESTNAME.res"
34+
1335
log_info "-----------------------------------------------------------------------------------------"
1436
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
37+
log_info "=== Test Initialization ==="
38+
39+
sync
40+
sleep 1
1541

1642
log_info "Checking if dependency binary is available"
1743
check_dependencies i2c-msm-test
1844

1945
log_info "running i2c binary"
2046
output=$(i2c-msm-test -v -D /dev/i2c-0 -l | grep "ret:1")
2147

22-
2348
if echo "$output" | grep -q "Reading"; then
2449
log_pass "$TESTNAME : Test Passed"
25-
echo "$TESTNAME PASS" > $test_path/$TESTNAME.res
50+
echo "$TESTNAME PASS" > "$res_file"
2651
else
2752
log_fail "$TESTNAME : Test Failed"
28-
echo "$TESTNAME FAIL" > $test_path/$TESTNAME.res
53+
echo "$TESTNAME FAIL" > "$res_file"
2954
fi
3055
log_info "-------------------Completed $TESTNAME Testcase----------------------------"

0 commit comments

Comments
 (0)