Skip to content

Commit 3f44dfd

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 3f44dfd

File tree

4 files changed

+151
-197
lines changed

4 files changed

+151
-197
lines changed

Runner/init_env

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#!/bin/sh
2-
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
32
# SPDX-License-Identifier: BSD-3-Clause-Clear
4-
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
3+
4+
# Base directory of Runner
5+
BASE_DIR="$(CDPATH= cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)"
6+
export TOOLS="${BASE_DIR}/utils"
7+
export __RUNNER_SUITES_DIR="${BASE_DIR}/suites"

Runner/run-test.sh

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,91 @@
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+
# Exit on any command failure (optional for stricter CI)
7+
#set -e
88

9-
#import test functions library
10-
. "${TOOLS}"/functestlib.sh
9+
# shellcheck disable=SC1007,SC2037
10+
SCRIPT_DIR="$(CDPATH=cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)"
11+
# shellcheck source=/dev/null
12+
. "${SCRIPT_DIR}/init_env"
13+
# shellcheck source=/dev/null
14+
. "${TOOLS}/functestlib.sh"
1115

12-
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
27-
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-
}
16+
# Store results
17+
RESULTS_PASS=""
18+
RESULTS_FAIL=""
3719

3820
# Execute a test case
3921
execute_test_case() {
4022
local test_path="$1"
23+
local test_name
24+
test_name=$(basename "$test_path")
25+
4126
if [ -d "$test_path" ]; then
4227
run_script="$test_path/run.sh"
4328
if [ -f "$run_script" ]; then
44-
log "Executing test case: $test_path"
45-
sh "$run_script" 2>&1
29+
log "Executing test case: $test_name"
30+
if (cd "$test_path" && sh "./run.sh"); then
31+
log_pass "$test_name passed"
32+
RESULTS_PASS=$(printf "%s\n%s" "$RESULTS_PASS" "$test_name")
33+
else
34+
log_fail "$test_name failed"
35+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name")
36+
fi
4637
else
47-
log "No run.sh found in $test_path"
38+
log_error "No run.sh found in $test_path"
39+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (missing run.sh)")
4840
fi
4941
else
50-
log "Test case directory not found: $test_path"
42+
log_error "Test case directory not found: $test_path"
43+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (directory not found)")
5144
fi
5245
}
5346

54-
# Function to run a specific test case by name
47+
# Run test by name
5548
run_specific_test_by_name() {
56-
test_name="$1"
57-
test_path=$(find_test_case_by_name ".")
49+
local test_name="$1"
50+
local test_path
51+
test_path=$(find_test_case_by_name "$test_name")
5852
if [ -z "$test_path" ]; then
59-
log "Test case with name $test_name not found."
53+
log_error "Test case with name $test_name not found."
54+
RESULTS_FAIL=$(printf "%s\n%s" "$RESULTS_FAIL" "$test_name (not found)")
6055
else
6156
execute_test_case "$test_path"
6257
fi
6358
}
6459

65-
# Main script logic
60+
# Run all test cases
61+
run_all_tests() {
62+
find "${__RUNNER_SUITES_DIR}" -type d -name '[A-Za-z]*' -maxdepth 3 | while IFS= read -r test_dir; do
63+
if [ -f "$test_dir/run.sh" ]; then
64+
execute_test_case "$test_dir"
65+
fi
66+
done
67+
}
68+
69+
# Print final summary
70+
print_summary() {
71+
echo
72+
log_info "========== Test Summary =========="
73+
echo "PASSED:"
74+
[ -n "$RESULTS_PASS" ] && printf "%s\n" "$RESULTS_PASS" || echo " None"
75+
echo
76+
echo "FAILED:"
77+
[ -n "$RESULTS_FAIL" ] && printf "%s\n" "$RESULTS_FAIL" || echo " None"
78+
log_info "=================================="
79+
}
80+
81+
# Main
6682
if [ "$#" -eq 0 ]; then
6783
log "Usage: $0 [all | <testcase_name>]"
6884
exit 1
6985
fi
7086

87+
if [ "$1" = "all" ]; then
88+
run_all_tests
89+
else
90+
run_specific_test_by_name "$1"
91+
fi
7192

72-
run_specific_test_by_name "$1"
93+
print_summary

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

Lines changed: 33 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,54 @@
11
#!/bin/sh
22

3-
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
3+
# Copyright (c) Qualcomm Technologies, Inc.
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

6-
. "${PWD}/init_env"
7-
TESTNAME="CPUFreq_Validation"
8-
. "$TOOLS/functestlib.sh"
9-
10-
test_path=$(find_test_case_by_name "$TESTNAME")
11-
log_info "-----------------------------------------------------------------------------------------"
12-
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
13-
log_info "=== CPUFreq Frequency Walker with Validation ==="
14-
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-
22-
NUM_CPUS=$(nproc)
23-
printf "${YELLOW}Detected %s CPU cores.${NC}\n" "$NUM_CPUS"
24-
25-
overall_pass=0
26-
status_dir="/tmp/cpufreq_status.$$"
27-
mkdir -p "$status_dir"
28-
29-
validate_cpu_core() {
30-
local cpu=$1
31-
local core_id=$2
32-
status_file="$status_dir/core_$core_id"
33-
34-
printf "${BLUE}Processing %s...${NC}\n" "$cpu"
35-
36-
if [ ! -d "$cpu/cpufreq" ]; then
37-
printf "${BLUE}[SKIP]${NC} %s does not support cpufreq.\n" "$cpu"
38-
echo "skip" > "$status_file"
39-
return
40-
fi
6+
# shellcheck disable=SC2037
7+
SCRIPT_DIR=$(CDPATH=cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)
418

42-
available_freqs=$(cat "$cpu/cpufreq/scaling_available_frequencies" 2>/dev/null)
43-
44-
if [ -z "$available_freqs" ]; then
45-
printf "${YELLOW}[INFO]${NC} No available frequencies for %s. Skipping...\n" "$cpu"
46-
echo "skip" > "$status_file"
47-
return
48-
fi
49-
50-
if echo "userspace" | tee "$cpu/cpufreq/scaling_governor" > /dev/null; then
51-
printf "${YELLOW}[INFO]${NC} Set governor to userspace.\n"
52-
else
53-
printf "${RED}[ERROR]${NC} Cannot set userspace governor for %s.\n" "$cpu"
54-
echo "fail" > "$status_file"
55-
return
56-
fi
9+
# Traverse upward to find Runner root containing utils/
10+
RUNNER_DIR="$SCRIPT_DIR"
11+
while [ ! -d "$RUNNER_DIR/utils" ] && [ "$RUNNER_DIR" != "/" ]; do
12+
RUNNER_DIR=$(dirname "$RUNNER_DIR")
13+
done
5714

58-
echo "pass" > "$status_file"
15+
FUNCLIB="$RUNNER_DIR/utils/functestlib.sh"
5916

60-
for freq in $available_freqs; do
61-
log_info "Setting $cpu to frequency $freq kHz..."
62-
if echo "$freq" | tee "$cpu/cpufreq/scaling_setspeed" > /dev/null; then
63-
sleep 0.2
64-
actual_freq=$(cat "$cpu/cpufreq/scaling_cur_freq")
65-
if [ "$actual_freq" = "$freq" ]; then
66-
printf "${GREEN}[PASS]${NC} %s set to %s kHz.\n" "$cpu" "$freq"
67-
else
68-
printf "${RED}[FAIL]${NC} Tried to set %s to %s kHz, but current is %s kHz.\n" "$cpu" "$freq" "$actual_freq"
69-
echo "fail" > "$status_file"
70-
fi
71-
else
72-
printf "${RED}[ERROR]${NC} Failed to set %s to %s kHz.\n" "$cpu" "$freq"
73-
echo "fail" > "$status_file"
74-
fi
75-
done
17+
if [ ! -f "$FUNCLIB" ]; then
18+
echo "Error: Could not locate functestlib.sh from $SCRIPT_DIR" >&2
19+
exit 1
20+
fi
7621

77-
echo "Restoring $cpu governor to 'ondemand'..."
78-
echo "ondemand" | sudo tee "$cpu/cpufreq/scaling_governor" > /dev/null
79-
}
22+
# shellcheck disable=SC1090
23+
. "$FUNCLIB"
8024

81-
cpu_index=0
82-
for cpu in /sys/devices/system/cpu/cpu[0-9]*; do
83-
validate_cpu_core "$cpu" "$cpu_index" &
84-
cpu_index=$((cpu_index + 1))
85-
done
25+
TESTNAME="CPUFreq_Validation"
8626

87-
wait
27+
test_path=$(find_test_case_by_name "$TESTNAME")
8828

89-
log_info ""
90-
log_info "=== Per-Core Test Summary ==="
91-
for status_file in "$status_dir"/core_*; do
92-
idx=$(basename "$status_file" | cut -d_ -f2)
93-
status=$(cat "$status_file")
94-
case "$status" in
95-
pass)
96-
printf "CPU%s: ${GREEN}[PASS]${NC}\n" "$idx"
97-
;;
98-
fail)
99-
printf "CPU%s: ${RED}[FAIL]${NC}\n" "$idx"
100-
overall_pass=1
101-
;;
102-
skip)
103-
printf "CPU%s: ${BLUE}[SKIPPED]${NC}\n" "$idx"
104-
;;
105-
*)
106-
printf "CPU%s: ${RED}[UNKNOWN STATUS]${NC}\n" "$idx"
107-
overall_pass=1
108-
;;
109-
esac
110-
done
29+
# Debug: show resolved path
30+
log_info "Resolved test_path: $test_path"
31+
if [ -z "$test_path" ]; then
32+
log_error "$TESTNAME : Could not resolve test directory."
33+
exit 1
34+
fi
11135

112-
log_info ""
113-
log_info "=== Overall CPUFreq Validation Result ==="
36+
# Debug: check write permission
37+
touch "$test_path/.write_check" 2>/dev/null || {
38+
log_error "Write permission denied at $test_path"
39+
exit 1
40+
log_info "Restoring $cpu governor to 'ondemand'..."
41+
res_file="$test_path/$TESTNAME.res"
11442
if [ "$overall_pass" -eq 0 ]; then
115-
printf "${GREEN}[OVERALL PASS]${NC} All CPUs validated successfully.\n"
11643
log_pass "$TESTNAME : Test Passed"
117-
echo "$TESTNAME PASS" > "$test_path/$TESTNAME.res"
44+
log_info "Writing PASS to $res_file"
45+
echo "$TESTNAME PASS" > "$res_file"
11846
rm -r "$status_dir"
11947
exit 0
12048
else
121-
printf "${RED}[OVERALL FAIL]${NC} Some CPUs failed frequency validation.\n"
12249
log_fail "$TESTNAME : Test Failed"
123-
echo "$TESTNAME FAIL" > "$test_path/$TESTNAME.res"
50+
log_info "Writing FAIL to $res_file"
51+
echo "$TESTNAME FAIL" > "$res_file"
12452
rm -r "$status_dir"
12553
exit 1
12654
fi

0 commit comments

Comments
 (0)