Skip to content

Commit 4df9f97

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 4df9f97

File tree

4 files changed

+139
-95
lines changed

4 files changed

+139
-95
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: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,27 @@
33
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

6-
. "${PWD}/init_env"
6+
# Resolve the directory containing this script
7+
# shellcheck disable=SC2037
8+
SCRIPT_DIR=$(CDPATH=cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)
9+
10+
# Traverse upward to find Runner root containing utils/
11+
RUNNER_DIR="$SCRIPT_DIR"
12+
while [ ! -d "$RUNNER_DIR/utils" ] && [ "$RUNNER_DIR" != "/" ]; do
13+
RUNNER_DIR=$(dirname "$RUNNER_DIR")
14+
done
15+
16+
FUNCLIB="$RUNNER_DIR/utils/functestlib.sh"
17+
18+
if [ ! -f "$FUNCLIB" ]; then
19+
echo "Error: Could not locate functestlib.sh from $SCRIPT_DIR" >&2
20+
exit 1
21+
fi
22+
23+
# shellcheck disable=SC1090
24+
. "$FUNCLIB"
25+
726
TESTNAME="CPUFreq_Validation"
8-
. "$TOOLS/functestlib.sh"
927

1028
test_path=$(find_test_case_by_name "$TESTNAME")
1129
log_info "-----------------------------------------------------------------------------------------"
@@ -75,7 +93,7 @@ validate_cpu_core() {
7593
done
7694

7795
echo "Restoring $cpu governor to 'ondemand'..."
78-
echo "ondemand" | sudo tee "$cpu/cpufreq/scaling_governor" > /dev/null
96+
echo "ondemand" | tee "$cpu/cpufreq/scaling_governor" > /dev/null
7997
}
8098

8199
cpu_index=0

Runner/utils/functestlib.sh

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
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-
# Import test suite definitions
7-
. "${PWD}"/init_env
8-
#import platform
9-
. "${TOOLS}"/platform.sh
6+
# Resolve the directory of this script
7+
# shellcheck disable=SC2037
8+
UTILS_DIR=$(CDPATH=cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)
109

11-
__RUNNER_SUITES_DIR="/var/Runner/suites"
12-
__RUNNER_UTILS_BIN_DIR="/var/common"
10+
# Safely source init_env if present
11+
if [ -f "$UTILS_DIR/init_env" ]; then
12+
# shellcheck disable=SC1090
13+
. "$UTILS_DIR/init_env"
14+
fi
1315

14-
#This function used for test logging
16+
# Import platform script if available
17+
if [ -f "${TOOLS}/platform.sh" ]; then
18+
# shellcheck disable=SC1090
19+
. "${TOOLS}/platform.sh"
20+
fi
21+
22+
# Set common directories (fallback if init_env not sourced)
23+
__RUNNER_SUITES_DIR="${__RUNNER_SUITES_DIR:-${ROOT_DIR}/suites}"
24+
#__RUNNER_UTILS_BIN_DIR="${__RUNNER_UTILS_BIN_DIR:-${ROOT_DIR}/common}"
25+
26+
# Logging function
1527
log() {
1628
local level="$1"
1729
shift
18-
# echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a /var/test_framework.log
1930
echo "[$level] $(date '+%Y-%m-%d %H:%M:%S') - $*" | tee -a /var/test_output.log
2031
}
21-
# Find test case path by name
22-
find_test_case_by_name() {
23-
local test_name="$1"
24-
find $__RUNNER_SUITES_DIR -type d -iname "$test_name" 2>/dev/null
25-
}
26-
27-
# Find test case path by name
28-
find_test_case_bin_by_name() {
29-
local test_name="$1"
30-
find $__RUNNER_UTILS_BIN_DIR -type f -iname "$test_name" 2>/dev/null
31-
}
3232

33-
# Find test case path by name
34-
find_test_case_script_by_name() {
35-
local test_name="$1"
36-
find $__RUNNER_UTILS_BIN_DIR -type d -iname "$test_name" 2>/dev/null
37-
}
33+
# Logging levels
34+
log_info() { log "INFO" "$@"; }
35+
log_pass() { log "PASS" "$@"; }
36+
log_fail() { log "FAIL" "$@"; }
37+
log_error() { log "ERROR" "$@"; }
3838

39+
# Dependency check
3940
check_dependencies() {
4041
local missing=0
4142
for cmd in "$@"; do
42-
if ! command -v "$cmd" > /dev/null 2>&1; then
43+
if ! command -v "$cmd" >/dev/null 2>&1; then
4344
log_error "ERROR: Required command '$cmd' not found in PATH."
4445
missing=1
4546
fi
@@ -48,38 +49,46 @@ check_dependencies() {
4849
log_error "Exiting due to missing dependencies."
4950
exit 1
5051
else
51-
log_pass "Test related dependencies are present."
52+
log_pass "Test related dependencies are present."
5253
fi
5354
}
5455

55-
# Logging levels
56-
log_info() { log "INFO" "$@"; }
57-
log_pass() { log "PASS" "$@"; }
58-
log_fail() { log "FAIL" "$@"; }
59-
log_error() { log "ERROR" "$@"; }
56+
# Test case path search functions
57+
find_test_case_by_name() {
58+
local test_name="$1"
59+
find "$__RUNNER_SUITES_DIR" -type d -iname "$test_name" 2>/dev/null
60+
}
6061

62+
find_test_case_bin_by_name() {
63+
local test_name="$1"
64+
find "$__RUNNER_UTILS_BIN_DIR" -type f -iname "$test_name" 2>/dev/null
65+
}
66+
67+
find_test_case_script_by_name() {
68+
local test_name="$1"
69+
find "$__RUNNER_UTILS_BIN_DIR" -type d -iname "$test_name" 2>/dev/null
70+
}
6171

62-
## this doc fn comes last
72+
# Documentation printer
6373
FUNCTIONS="\
6474
log_info \
6575
log_pass \
6676
log_fail \
6777
log_error \
6878
find_test_case_by_name \
6979
find_test_case_bin_by_name \
70-
find_test_case_script_by_name \
80+
find_test_case_script_by_name \
7181
log \
7282
"
7383

74-
functestlibdoc()
75-
{
76-
echo "functestlib.sh"
77-
echo ""
78-
echo "Functions:"
79-
for fn in $FUNCTIONS; do
80-
echo $fn
81-
eval $fn"_doc"
84+
functestlibdoc() {
85+
echo "functestlib.sh"
8286
echo ""
83-
done
84-
echo "Note, these functions will probably not work with >=32 CPUs"
87+
echo "Functions:"
88+
for fn in $FUNCTIONS; do
89+
echo "$fn"
90+
eval "$fn""_doc"
91+
echo ""
92+
done
93+
echo "Note: These functions may not behave as expected on systems with >=32 CPUs"
8594
}

0 commit comments

Comments
 (0)