Skip to content

Commit 1b76e6a

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 1b76e6a

File tree

1 file changed

+230
-51
lines changed

1 file changed

+230
-51
lines changed

Runner/utils/functestlib.sh

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

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

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

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
136+
# POSIX-safe repo root detector (used by run.sh scripts)
137+
detect_runner_root() {
138+
local path="$1"
139+
while [ "$path" != "/" ]; do
140+
if [ -d "$path/suites" ]; then
141+
echo "$path"
142+
return
53143
fi
144+
path=$(dirname "$path")
54145
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
146+
echo ""
61147
}
62148

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
149+
# Optional self-doc generator
71150
FUNCTIONS="\
72151
log_info \
73152
log_pass \
74153
log_fail \
75154
log_error \
76155
find_test_case_by_name \
77156
find_test_case_bin_by_name \
78-
find_test_case_script_by_name \
157+
find_test_case_script_by_name \
79158
log \
159+
detect_runner_root \
80160
"
81161

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

0 commit comments

Comments
 (0)