Skip to content

Commit aabd669

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 Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 2d02349 commit aabd669

File tree

4 files changed

+293
-122
lines changed

4 files changed

+293
-122
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/CPUFreq_Validation/run.sh

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

6-
. "${PWD}/init_env"
7-
TESTNAME="CPUFreq_Validation"
8-
. "$TOOLS/functestlib.sh"
6+
# Dynamically find the testkit root by walking up from script dir
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
ROOT_DIR="$SCRIPT_DIR"
9+
10+
while [ "$ROOT_DIR" != "/" ]; do
11+
if [ -d "$ROOT_DIR/utils" ] && [ -d "$ROOT_DIR/suites" ]; then
12+
break
13+
fi
14+
ROOT_DIR=$(dirname "$ROOT_DIR")
15+
done
16+
17+
# Validate root detection
18+
if [ ! -d "$ROOT_DIR/utils" ] || [ ! -f "$ROOT_DIR/utils/functestlib.sh" ]; then
19+
echo "[ERROR] Could not detect testkit root (missing utils/ or functestlib.sh)" >&2
20+
exit 1
21+
fi
22+
23+
TOOLS="$ROOT_DIR/utils"
24+
INIT_ENV="$ROOT_DIR/init_env"
25+
FUNCLIB="$TOOLS/functestlib.sh"
926

27+
# Source init_env if exists
28+
[ -f "$INIT_ENV" ] && . "$INIT_ENV"
29+
30+
# Source functestlib.sh
31+
. "$FUNCLIB"
32+
33+
# Export fallback for manual ./run.sh usage
34+
__RUNNER_SUITES_DIR="${__RUNNER_SUITES_DIR:-$ROOT_DIR/suites}"
35+
36+
cd "$ROOT_DIR" || exit 1
37+
38+
TESTNAME="CPUFreq_Validation"
1039
test_path=$(find_test_case_by_name "$TESTNAME")
40+
1141
log_info "-----------------------------------------------------------------------------------------"
1242
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
1343
log_info "=== CPUFreq Frequency Walker with Validation ==="
1444

15-
# Color codes (ANSI escape sequences)
16-
GREEN="\033[32m"
17-
RED="\033[31m"
18-
YELLOW="\033[33m"
19-
BLUE="\033[34m"
20-
NC="\033[0m"
21-
2245
NUM_CPUS=$(nproc)
23-
printf "${YELLOW}Detected %s CPU cores.${NC}\n" "$NUM_CPUS"
46+
log_info "Detected $NUM_CPUS CPU cores."
2447

2548
overall_pass=0
2649
status_dir="/tmp/cpufreq_status.$$"
2750
mkdir -p "$status_dir"
2851

2952
validate_cpu_core() {
30-
local cpu=$1
31-
local core_id=$2
53+
local cpu="$1"
54+
local core_id="$2"
3255
status_file="$status_dir/core_$core_id"
3356

34-
printf "${BLUE}Processing %s...${NC}\n" "$cpu"
57+
log_info "Processing $cpu..."
3558

3659
if [ ! -d "$cpu/cpufreq" ]; then
37-
printf "${BLUE}[SKIP]${NC} %s does not support cpufreq.\n" "$cpu"
60+
log_info "[SKIP] $cpu does not support cpufreq."
3861
echo "skip" > "$status_file"
3962
return
4063
fi
4164

4265
available_freqs=$(cat "$cpu/cpufreq/scaling_available_frequencies" 2>/dev/null)
4366

4467
if [ -z "$available_freqs" ]; then
45-
printf "${YELLOW}[INFO]${NC} No available frequencies for %s. Skipping...\n" "$cpu"
68+
log_info "[INFO] No available frequencies for $cpu. Skipping..."
4669
echo "skip" > "$status_file"
4770
return
4871
fi
4972

50-
if echo "userspace" | tee "$cpu/cpufreq/scaling_governor" > /dev/null; then
51-
printf "${YELLOW}[INFO]${NC} Set governor to userspace.\n"
73+
if echo "userspace" > "$cpu/cpufreq/scaling_governor"; then
74+
log_info "[INFO] Set governor to userspace."
5275
else
53-
printf "${RED}[ERROR]${NC} Cannot set userspace governor for %s.\n" "$cpu"
76+
log_error "Cannot set userspace governor for $cpu."
5477
echo "fail" > "$status_file"
5578
return
5679
fi
@@ -59,23 +82,23 @@ validate_cpu_core() {
5982

6083
for freq in $available_freqs; do
6184
log_info "Setting $cpu to frequency $freq kHz..."
62-
if echo "$freq" | tee "$cpu/cpufreq/scaling_setspeed" > /dev/null; then
85+
if echo "$freq" > "$cpu/cpufreq/scaling_setspeed"; then
6386
sleep 0.2
6487
actual_freq=$(cat "$cpu/cpufreq/scaling_cur_freq")
6588
if [ "$actual_freq" = "$freq" ]; then
66-
printf "${GREEN}[PASS]${NC} %s set to %s kHz.\n" "$cpu" "$freq"
89+
log_info "[PASS] $cpu set to $freq kHz."
6790
else
68-
printf "${RED}[FAIL]${NC} Tried to set %s to %s kHz, but current is %s kHz.\n" "$cpu" "$freq" "$actual_freq"
91+
log_error "[FAIL] Tried to set $cpu to $freq kHz, but current is $actual_freq kHz."
6992
echo "fail" > "$status_file"
7093
fi
7194
else
72-
printf "${RED}[ERROR]${NC} Failed to set %s to %s kHz.\n" "$cpu" "$freq"
95+
log_error "[ERROR] Failed to set $cpu to $freq kHz."
7396
echo "fail" > "$status_file"
7497
fi
7598
done
7699

77-
echo "Restoring $cpu governor to 'ondemand'..."
78-
echo "ondemand" | sudo tee "$cpu/cpufreq/scaling_governor" > /dev/null
100+
log_info "Restoring $cpu governor to 'ondemand'..."
101+
echo "ondemand" > "$cpu/cpufreq/scaling_governor"
79102
}
80103

81104
cpu_index=0
@@ -93,34 +116,36 @@ for status_file in "$status_dir"/core_*; do
93116
status=$(cat "$status_file")
94117
case "$status" in
95118
pass)
96-
printf "CPU%s: ${GREEN}[PASS]${NC}\n" "$idx"
119+
log_info "CPU$idx: [PASS]"
97120
;;
98121
fail)
99-
printf "CPU%s: ${RED}[FAIL]${NC}\n" "$idx"
122+
log_error "CPU$idx: [FAIL]"
100123
overall_pass=1
101124
;;
102125
skip)
103-
printf "CPU%s: ${BLUE}[SKIPPED]${NC}\n" "$idx"
126+
log_info "CPU$idx: [SKIPPED]"
104127
;;
105128
*)
106-
printf "CPU%s: ${RED}[UNKNOWN STATUS]${NC}\n" "$idx"
129+
log_error "CPU$idx: [UNKNOWN STATUS]"
107130
overall_pass=1
108131
;;
109132
esac
110133
done
111134

112135
log_info ""
113136
log_info "=== Overall CPUFreq Validation Result ==="
137+
138+
res_file="$test_path/$TESTNAME.res"
114139
if [ "$overall_pass" -eq 0 ]; then
115-
printf "${GREEN}[OVERALL PASS]${NC} All CPUs validated successfully.\n"
116140
log_pass "$TESTNAME : Test Passed"
117-
echo "$TESTNAME PASS" > "$test_path/$TESTNAME.res"
141+
log_info "Writing PASS to $res_file"
142+
echo "$TESTNAME PASS" > "$res_file"
118143
rm -r "$status_dir"
119144
exit 0
120145
else
121-
printf "${RED}[OVERALL FAIL]${NC} Some CPUs failed frequency validation.\n"
122146
log_fail "$TESTNAME : Test Failed"
123-
echo "$TESTNAME FAIL" > "$test_path/$TESTNAME.res"
147+
log_info "Writing FAIL to $res_file"
148+
echo "$TESTNAME FAIL" > "$res_file"
124149
rm -r "$status_dir"
125150
exit 1
126151
fi

0 commit comments

Comments
 (0)