Skip to content

Commit 9fbd35b

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 9fbd35b

File tree

4 files changed

+137
-95
lines changed

4 files changed

+137
-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
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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
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+
SCRIPT_DIR=$(CDPATH=cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)
8+
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
14+
15+
FUNCLIB="$RUNNER_DIR/utils/functestlib.sh"
16+
17+
if [ ! -f "$FUNCLIB" ]; then
18+
echo "Error: Could not locate functestlib.sh from $SCRIPT_DIR" >&2
19+
exit 1
20+
fi
21+
22+
# shellcheck disable=SC1090
23+
. "$FUNCLIB"
24+
725
TESTNAME="CPUFreq_Validation"
8-
. "$TOOLS/functestlib.sh"
926

1027
test_path=$(find_test_case_by_name "$TESTNAME")
1128
log_info "-----------------------------------------------------------------------------------------"
@@ -75,7 +92,7 @@ validate_cpu_core() {
7592
done
7693

7794
echo "Restoring $cpu governor to 'ondemand'..."
78-
echo "ondemand" | sudo tee "$cpu/cpufreq/scaling_governor" > /dev/null
95+
echo "ondemand" | tee "$cpu/cpufreq/scaling_governor" > /dev/null
7996
}
8097

8198
cpu_index=0

Runner/utils/functestlib.sh

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
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+
UTILS_DIR=$(CDPATH=cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd)
108

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

14-
#This function used for test logging
15+
# Import platform script if available
16+
if [ -f "${TOOLS}/platform.sh" ]; then
17+
# shellcheck disable=SC1090
18+
. "${TOOLS}/platform.sh"
19+
fi
20+
21+
# Set common directories (fallback if init_env not sourced)
22+
__RUNNER_SUITES_DIR="${__RUNNER_SUITES_DIR:-${ROOT_DIR}/suites}"
23+
#__RUNNER_UTILS_BIN_DIR="${__RUNNER_UTILS_BIN_DIR:-${ROOT_DIR}/common}"
24+
25+
# Logging function
1526
log() {
1627
local level="$1"
1728
shift
18-
# echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a /var/test_framework.log
1929
echo "[$level] $(date '+%Y-%m-%d %H:%M:%S') - $*" | tee -a /var/test_output.log
2030
}
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-
}
3231

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-
}
32+
# Logging levels
33+
log_info() { log "INFO" "$@"; }
34+
log_pass() { log "PASS" "$@"; }
35+
log_fail() { log "FAIL" "$@"; }
36+
log_error() { log "ERROR" "$@"; }
3837

38+
# Dependency check
3939
check_dependencies() {
4040
local missing=0
4141
for cmd in "$@"; do
42-
if ! command -v "$cmd" > /dev/null 2>&1; then
42+
if ! command -v "$cmd" >/dev/null 2>&1; then
4343
log_error "ERROR: Required command '$cmd' not found in PATH."
4444
missing=1
4545
fi
@@ -48,38 +48,46 @@ check_dependencies() {
4848
log_error "Exiting due to missing dependencies."
4949
exit 1
5050
else
51-
log_pass "Test related dependencies are present."
51+
log_pass "Test related dependencies are present."
5252
fi
5353
}
5454

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

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

62-
## this doc fn comes last
71+
# Documentation printer
6372
FUNCTIONS="\
6473
log_info \
6574
log_pass \
6675
log_fail \
6776
log_error \
6877
find_test_case_by_name \
6978
find_test_case_bin_by_name \
70-
find_test_case_script_by_name \
79+
find_test_case_script_by_name \
7180
log \
7281
"
7382

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"
83+
functestlibdoc() {
84+
echo "functestlib.sh"
8285
echo ""
83-
done
84-
echo "Note, these functions will probably not work with >=32 CPUs"
86+
echo "Functions:"
87+
for fn in $FUNCTIONS; do
88+
echo "$fn"
89+
eval "$fn""_doc"
90+
echo ""
91+
done
92+
echo "Note: These functions may not behave as expected on systems with >=32 CPUs"
8593
}

0 commit comments

Comments
 (0)