Skip to content

Commit f173ccb

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 f173ccb

File tree

1 file changed

+229
-51
lines changed

1 file changed

+229
-51
lines changed

Runner/utils/functestlib.sh

Lines changed: 229 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,269 @@
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
58+
base="$(pwd)"
59+
while [ "$base" != "/" ]; do
60+
if [ -d "$base/suites" ]; then
61+
__RUNNER_SUITES_DIR="$base/suites"
62+
break
63+
fi
64+
parent=$(dirname "$base")
65+
[ "$parent" = "$base" ] && break
66+
base="$parent"
67+
done
68+
fi
69+
}
70+
71+
# Auto-detect utils/common dir if not set
72+
auto_detect_utils_dir() {
73+
if [ -z "$__RUNNER_UTILS_BIN_DIR" ]; then
74+
local base
75+
base="$(pwd)"
76+
while [ "$base" != "/" ]; do
77+
if [ -d "$base/common" ]; then
78+
__RUNNER_UTILS_BIN_DIR="$base/common"
79+
break
80+
fi
81+
parent=$(dirname "$base")
82+
[ "$parent" = "$base" ] && break
83+
base="$parent"
84+
done
85+
fi
86+
}
87+
88+
# POSIX-safe test case directory lookup
2289
find_test_case_by_name() {
2390
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
91+
auto_detect_suites_dir
92+
93+
if [ -z "$__RUNNER_SUITES_DIR" ]; then
94+
log_error "__RUNNER_SUITES_DIR not set or could not be detected"
95+
return 1
2896
fi
97+
98+
log_info "Searching for test '$test_name' in $__RUNNER_SUITES_DIR"
99+
local testpath
100+
testpath=$(find "$__RUNNER_SUITES_DIR" -type d -iname "$test_name" -print -quit 2>/dev/null)
101+
102+
if [ -z "$testpath" ]; then
103+
log_error "Test '$test_name' not found"
104+
return 1
105+
fi
106+
107+
log_info "Resolved test path for '$test_name': $testpath"
108+
echo "$testpath"
29109
}
30110

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

37-
# Find test case path by name
38123
find_test_case_script_by_name() {
39124
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
125+
auto_detect_utils_dir
126+
127+
if [ -z "$__RUNNER_UTILS_BIN_DIR" ]; then
128+
log_error "__RUNNER_UTILS_BIN_DIR not set or could not be detected"
129+
return 1
44130
fi
131+
132+
find "$__RUNNER_UTILS_BIN_DIR" -type d -iname "$test_name" -print -quit 2>/dev/null
45133
}
46134

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
135+
# POSIX-safe repo root detector (used by run.sh scripts)
136+
detect_runner_root() {
137+
local path="$1"
138+
while [ "$path" != "/" ]; do
139+
if [ -d "$path/suites" ]; then
140+
echo "$path"
141+
return
53142
fi
143+
path=$(dirname "$path")
54144
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
145+
echo ""
61146
}
62147

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
148+
# Optional self-doc generator
71149
FUNCTIONS="\
72150
log_info \
73151
log_pass \
74152
log_fail \
75153
log_error \
76154
find_test_case_by_name \
77155
find_test_case_bin_by_name \
78-
find_test_case_script_by_name \
156+
find_test_case_script_by_name \
79157
log \
158+
detect_runner_root \
80159
"
81160

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

0 commit comments

Comments
 (0)