Skip to content

Commit 1342794

Browse files
committed
functestlib.sh: make path resolution and init_env sourcing target-safe
- Added POSIX-compliant detection of utils/ directory via SCRIPT_DIR - init_env is now sourced only if present, preventing runtime errors on target - Ensures compatibility with CI, embedded targets, and non-Git environments - Preserved function structure for log and test discovery utilities Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 3f1a5bc commit 1342794

File tree

1 file changed

+227
-51
lines changed

1 file changed

+227
-51
lines changed

Runner/utils/functestlib.sh

Lines changed: 227 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,267 @@
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
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+
# Fallbacks (only if init_env didn't set them)
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
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
31+
log_info() { log "INFO" "$@" >&2; }
32+
log_pass() { log "PASS" "$@" >&2; }
33+
log_fail() { log "FAIL" "$@" >&2; }
34+
log_error() { log "ERROR" "$@" >&2; }
35+
log_skip() { log "SKIP" "$@"; >&2; }
36+
37+
# Dependency check
38+
check_dependencies() {
39+
local missing=0
40+
for cmd in "$@"; do
41+
if ! command -v "$cmd" >/dev/null 2>&1; then
42+
log_error "ERROR: Required command '$cmd' not found in PATH."
43+
missing=1
44+
fi
45+
done
46+
if [ "$missing" -ne 0 ]; then
47+
log_error "Exiting due to missing dependencies."
48+
exit 1
49+
else
50+
log_pass "Test related dependencies are present."
51+
fi
52+
}
53+
54+
# Auto-detect suites dir if not already set
55+
auto_detect_suites_dir() {
56+
if [ -z "$__RUNNER_SUITES_DIR" ]; then
57+
local base=$(pwd)
58+
while [ "$base" != "/" ]; do
59+
if [ -d "$base/suites" ]; then
60+
__RUNNER_SUITES_DIR="$base/suites"
61+
break
62+
fi
63+
parent=$(dirname "$base")
64+
[ "$parent" = "$base" ] && break
65+
base="$parent"
66+
done
67+
fi
68+
}
69+
70+
# Auto-detect utils/common dir if not set
71+
auto_detect_utils_dir() {
72+
if [ -z "$__RUNNER_UTILS_BIN_DIR" ]; then
73+
local base=$(pwd)
74+
while [ "$base" != "/" ]; do
75+
if [ -d "$base/common" ]; then
76+
__RUNNER_UTILS_BIN_DIR="$base/common"
77+
break
78+
fi
79+
parent=$(dirname "$base")
80+
[ "$parent" = "$base" ] && break
81+
base="$parent"
82+
done
83+
fi
84+
}
85+
86+
# POSIX-safe test case directory lookup
2287
find_test_case_by_name() {
2388
local test_name="$1"
24-
if [ -d "$__RUNNER_SUITES_DIR" ]; then
25-
find $__RUNNER_SUITES_DIR -type d -iname "$test_name" 2>/dev/null
26-
else
27-
find "${PWD}" -type d -iname "$test_name" 2>/dev/null
89+
auto_detect_suites_dir
90+
91+
if [ -z "$__RUNNER_SUITES_DIR" ]; then
92+
log_error "__RUNNER_SUITES_DIR not set or could not be detected"
93+
return 1
2894
fi
95+
96+
log_info "Searching for test '$test_name' in $__RUNNER_SUITES_DIR"
97+
local testpath
98+
testpath=$(find "$__RUNNER_SUITES_DIR" -type d -iname "$test_name" -print -quit 2>/dev/null)
99+
100+
if [ -z "$testpath" ]; then
101+
log_error "Test '$test_name' not found"
102+
return 1
103+
fi
104+
105+
log_info "Resolved test path for '$test_name': $testpath"
106+
echo "$testpath"
29107
}
30108

31-
# Find test case path by name
32109
find_test_case_bin_by_name() {
33110
local test_name="$1"
34-
find $__RUNNER_UTILS_BIN_DIR -type f -iname "$test_name" 2>/dev/null
111+
auto_detect_utils_dir
112+
113+
if [ -z "$__RUNNER_UTILS_BIN_DIR" ]; then
114+
log_error "__RUNNER_UTILS_BIN_DIR not set or could not be detected"
115+
return 1
116+
fi
117+
118+
find "$__RUNNER_UTILS_BIN_DIR" -type f -iname "$test_name" -print -quit 2>/dev/null
35119
}
36120

37-
# Find test case path by name
38121
find_test_case_script_by_name() {
39122
local test_name="$1"
40-
if [ -d "$__RUNNER_UTILS_BIN_DIR" ]; then
41-
find $__RUNNER_UTILS_BIN_DIR -type d -iname "$test_name" 2>/dev/null
42-
else
43-
find "${PWD}" -type d -iname "$test_name" 2>/dev/null
123+
auto_detect_utils_dir
124+
125+
if [ -z "$__RUNNER_UTILS_BIN_DIR" ]; then
126+
log_error "__RUNNER_UTILS_BIN_DIR not set or could not be detected"
127+
return 1
44128
fi
129+
130+
find "$__RUNNER_UTILS_BIN_DIR" -type d -iname "$test_name" -print -quit 2>/dev/null
45131
}
46132

47-
check_dependencies() {
48-
local missing=0
49-
for cmd in "$@"; do
50-
if ! command -v "$cmd" > /dev/null 2>&1; then
51-
log_error "ERROR: Required command '$cmd' not found in PATH."
52-
missing=1
133+
# POSIX-safe repo root detector (used by run.sh scripts)
134+
detect_runner_root() {
135+
local path="$1"
136+
while [ "$path" != "/" ]; do
137+
if [ -d "$path/suites" ]; then
138+
echo "$path"
139+
return
53140
fi
141+
path=$(dirname "$path")
54142
done
55-
if [ "$missing" -ne 0 ]; then
56-
log_error "Exiting due to missing dependencies."
57-
exit 1
58-
else
59-
log_pass "Test related dependencies are present."
60-
fi
143+
echo ""
61144
}
62145

63-
# Logging levels
64-
log_info() { log "INFO" "$@"; }
65-
log_pass() { log "PASS" "$@"; }
66-
log_fail() { log "FAIL" "$@"; }
67-
log_error() { log "ERROR" "$@"; }
68-
69-
70-
## this doc fn comes last
146+
# Optional self-doc generator
71147
FUNCTIONS="\
72148
log_info \
73149
log_pass \
74150
log_fail \
75151
log_error \
76152
find_test_case_by_name \
77153
find_test_case_bin_by_name \
78-
find_test_case_script_by_name \
154+
find_test_case_script_by_name \
79155
log \
156+
detect_runner_root \
80157
"
81158

82-
functestlibdoc()
83-
{
84-
echo "functestlib.sh"
85-
echo ""
86-
echo "Functions:"
87-
for fn in $FUNCTIONS; do
88-
echo $fn
89-
eval $fn"_doc"
159+
functestlibdoc() {
160+
echo "functestlib.sh"
90161
echo ""
91-
done
92-
echo "Note, these functions will probably not work with >=32 CPUs"
162+
echo "Functions:"
163+
for fn in $FUNCTIONS; do
164+
echo "$fn"
165+
eval "$fn""_doc"
166+
echo ""
167+
done
168+
}
169+
170+
# ----------------------------
171+
# Additional Utility Functions
172+
# ----------------------------
173+
174+
check_network_status() {
175+
log_info "Checking network connectivity..."
176+
177+
ip_addr=$(ip -4 addr show scope global up | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}' | head -n 1)
178+
179+
if [ -n "$ip_addr" ]; then
180+
log_pass "Network is active. IP address: $ip_addr"
181+
if ping -c 1 -W 2 8.8.8.8 >/dev/null 2>&1; then
182+
log_pass "Internet is reachable."
183+
return 0
184+
else
185+
log_error "Network active but no internet access."
186+
return 2
187+
fi
188+
else
189+
log_fail "No active network interface found."
190+
return 1
191+
fi
192+
}
193+
194+
check_tar_file() {
195+
local url="$1"
196+
local filename
197+
local foldername
198+
199+
filename=$(basename "$url")
200+
foldername="${filename%.tar*}" # assumes .tar, .tar.gz, etc.
201+
202+
# 1. Check file exists
203+
if [ ! -f "$filename" ]; then
204+
log_error "File $filename does not exist."
205+
return 1
206+
fi
207+
208+
# 2. Check file is non-empty
209+
if [ ! -s "$filename" ]; then
210+
log_error "File $filename exists but is empty."
211+
return 1
212+
fi
213+
214+
# 3. Check file is a valid tar archive
215+
if ! tar -tf "$filename" >/dev/null 2>&1; then
216+
log_error "File $filename is not a valid tar archive."
217+
return 1
218+
fi
219+
220+
# 4. Check if already extracted
221+
if [ -d "$foldername" ]; then
222+
log_pass "$filename has already been extracted to $foldername/"
223+
return 0
224+
fi
225+
226+
log_info "$filename exists and is valid, but not yet extracted."
227+
return 2
228+
}
229+
230+
extract_tar_from_url() {
231+
local url="$1"
232+
local filename
233+
local extracted_files
234+
235+
filename=$(basename "$url")
236+
237+
check_tar_file "$url"
238+
status=$?
239+
if [ "$status" -eq 0 ]; then
240+
log_info "Already extracted. Skipping download."
241+
return 0
242+
elif [ "$status" -eq 1 ]; then
243+
log_info "File missing or invalid. Will download and extract."
244+
fi
245+
246+
check_network_status || return 1
247+
248+
log_info "Downloading $url..."
249+
wget -O "$filename" "$url" || {
250+
log_fail "Failed to download $filename"
251+
return 1
252+
}
253+
254+
log_info "Extracting $filename..."
255+
tar -xvf "$filename" || {
256+
log_fail "Failed to extract $filename"
257+
return 1
258+
}
259+
260+
extracted_files=$(tar -tf "$filename")
261+
if [ -z "$extracted_files" ]; then
262+
log_fail "No files were extracted from $filename."
263+
return 1
264+
else
265+
log_pass "Files extracted successfully:"
266+
echo "$extracted_files"
267+
return 0
268+
fi
93269
}

0 commit comments

Comments
 (0)