Skip to content

Commit de1a529

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 9c90f2e commit de1a529

File tree

1 file changed

+103
-120
lines changed

1 file changed

+103
-120
lines changed

Runner/utils/functestlib.sh

Lines changed: 103 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,77 @@
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
10-
11-
__RUNNER_SUITES_DIR="/var/Runner/suites"
12-
__RUNNER_UTILS_BIN_DIR="/var/common"
13-
14-
#This function used for test logging
6+
# --- Logging helpers ---
157
log() {
16-
local level="$1"
8+
level=$1
179
shift
18-
# echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a /var/test_framework.log
19-
echo "[$level] $(date '+%Y-%m-%d %H:%M:%S') - $*" | tee -a /var/test_output.log
10+
echo "[$level] $(date '+%Y-%m-%d %H:%M:%S') - $*"
2011
}
21-
# Find test case path by name
12+
log_info() { log "INFO" "$@"; }
13+
log_pass() { log "PASS" "$@"; }
14+
log_fail() { log "FAIL" "$@"; }
15+
log_error() { log "ERROR" "$@"; }
16+
log_skip() { log "SKIP" "$@"; }
17+
18+
# --- Dependency check ---
19+
check_dependencies() {
20+
missing=0
21+
for cmd in "$@"; do
22+
if ! command -v "$cmd" >/dev/null 2>&1; then
23+
log_error "Required command '$cmd' not found in PATH."
24+
missing=1
25+
fi
26+
done
27+
[ "$missing" -ne 0 ] && exit 1
28+
}
29+
30+
# --- Test case directory lookup ---
2231
find_test_case_by_name() {
23-
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
28-
fi
32+
test_name=$1
33+
base_dir="${__RUNNER_SUITES_DIR:-$ROOT_DIR/suites}"
34+
# Only search under the SUITES directory!
35+
testpath=$(find "$base_dir" -type d -iname "$test_name" -print -quit 2>/dev/null)
36+
echo "$testpath"
2937
}
3038

31-
# Find test case path by name
3239
find_test_case_bin_by_name() {
33-
local test_name="$1"
34-
find $__RUNNER_UTILS_BIN_DIR -type f -iname "$test_name" 2>/dev/null
40+
test_name=$1
41+
base_dir="${__RUNNER_UTILS_BIN_DIR:-$ROOT_DIR/common}"
42+
find "$base_dir" -type f -iname "$test_name" -print -quit 2>/dev/null
3543
}
3644

37-
# Find test case path by name
3845
find_test_case_script_by_name() {
39-
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
44-
fi
46+
test_name=$1
47+
base_dir="${__RUNNER_UTILS_BIN_DIR:-$ROOT_DIR/common}"
48+
find "$base_dir" -type d -iname "$test_name" -print -quit 2>/dev/null
4549
}
4650

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
51+
# --- Optional: POSIX-safe repo root detector ---
52+
detect_runner_root() {
53+
path=$1
54+
while [ "$path" != "/" ]; do
55+
if [ -d "$path/suites" ]; then
56+
echo "$path"
57+
return
5358
fi
59+
path=$(dirname "$path")
5460
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
61-
}
62-
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
71-
FUNCTIONS="\
72-
log_info \
73-
log_pass \
74-
log_fail \
75-
log_error \
76-
find_test_case_by_name \
77-
find_test_case_bin_by_name \
78-
find_test_case_script_by_name \
79-
log \
80-
"
81-
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"
9061
echo ""
91-
done
92-
echo "Note, these functions will probably not work with >=32 CPUs"
9362
}
9463

64+
# ----------------------------
65+
# Additional Utility Functions
66+
# ----------------------------
9567
# Function is to check for network connectivity status
9668
check_network_status() {
9769
echo "[INFO] Checking network connectivity..."
98-
70+
9971
# Get first active IPv4 address (excluding loopback)
10072
ip_addr=$(ip -4 addr show scope global up | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -n 1)
101-
73+
10274
if [ -n "$ip_addr" ]; then
10375
echo "[PASS] Network is active. IP address: $ip_addr"
104-
76+
10577
if ping -c 1 -W 2 8.8.8.8 >/dev/null 2>&1; then
10678
echo "[PASS] Internet is reachable."
10779
return 0
@@ -117,66 +89,77 @@ check_network_status() {
11789

11890
# If the tar file already exists,then function exit. Otherwise function to check the network connectivity and it will download tar from internet.
11991
extract_tar_from_url() {
120-
local url="$1"
121-
local filename
122-
local extracted_files
123-
124-
# Extract the filename from the URL
92+
url=$1
12593
filename=$(basename "$url")
126-
if check_tar_file "$filename"; then
127-
echo "[PASS] file already exists, Hence skipping downloading"
94+
95+
check_tar_file "$url"
96+
status=$?
97+
if [ "$status" -eq 0 ]; then
98+
log_info "Already extracted. Skipping download."
12899
return 0
100+
elif [ "$status" -eq 1 ]; then
101+
log_info "File missing or invalid. Will download and extract."
102+
check_network_status || return 1
103+
log_info "Downloading $url..."
104+
wget -O "$filename" "$url" || {
105+
log_fail "Failed to download $filename"
106+
return 1
107+
}
108+
log_info "Extracting $filename..."
109+
tar -xvf "$filename" || {
110+
log_fail "Failed to extract $filename"
111+
return 1
112+
}
113+
elif [ "$status" -eq 2 ]; then
114+
log_info "File exists and is valid, but not yet extracted. Proceeding to extract."
115+
tar -xvf "$filename" || {
116+
log_fail "Failed to extract $filename"
117+
return 1
118+
}
129119
fi
130-
131-
check_network_status
132-
network_status=$?
133-
if [ $network_status -ne 0 ]; then
134-
extract_tar_from_url "$TAR_URL"
135-
fi
136-
137-
# Download the file using wget
138-
echo "[INFO] Downloading $url..."
139-
wget -O "$filename" "$url"
140120

141-
# Check if wget was successful
142-
if [ $? -ne 0 ]; then
143-
echo "[FAIL] Failed to download the file."
121+
# Optionally, check that extraction succeeded
122+
first_entry=$(tar -tf "$filename" 2>/dev/null | head -n1 | cut -d/ -f1)
123+
if [ -n "$first_entry" ] && [ -e "$first_entry" ]; then
124+
log_pass "Files extracted successfully ($first_entry exists)."
125+
return 0
126+
else
127+
log_fail "Extraction did not create expected entry: $first_entry"
144128
return 1
145129
fi
130+
}
146131

147-
# Extract the tar file
148-
echo "[INFO] Extracting $filename..."
149-
tar -xvf "$filename"
132+
# Function to check if a tar file exists
133+
check_tar_file() {
134+
url=$1
135+
filename=$(basename "$url")
150136

151-
# Check if tar was successful
152-
if [ $? -ne 0 ]; then
153-
echo "[FAIL] Failed to extract the file."
137+
# 1. Check file exists
138+
if [ ! -f "$filename" ]; then
139+
log_error "File $filename does not exist."
154140
return 1
155141
fi
156142

157-
# Check if any files were extracted
158-
extracted_files=$(tar -tf "$filename")
159-
if [ -z "$extracted_files" ]; then
160-
echo "[FAIL] No files were extracted."
143+
# 2. Check file is non-empty
144+
if [ ! -s "$filename" ]; then
145+
log_error "File $filename exists but is empty."
161146
return 1
162-
else
163-
echo "[PASS] Files extracted successfully:"
164-
echo "[INFO] $extracted_files"
165-
return 0
166147
fi
167-
}
168148

169-
# Function to check if a tar file exists
170-
check_tar_file() {
171-
local url="$1"
172-
local filename
173-
local extracted_files
149+
# 3. Check file is a valid tar archive
150+
if ! tar -tf "$filename" >/dev/null 2>&1; then
151+
log_error "File $filename is not a valid tar archive."
152+
return 1
153+
fi
174154

175-
# Extract the filename from the URL
176-
filename=$(basename "$url")
177-
if [ -f "$filename" ]; then
155+
# 4. Check if already extracted: does the first entry in the tar exist?
156+
first_entry=$(tar -tf "$filename" 2>/dev/null | head -n1 | cut -d/ -f1)
157+
if [ -n "$first_entry" ] && [ -e "$first_entry" ]; then
158+
log_pass "$filename has already been extracted ($first_entry exists)."
178159
return 0
179-
else
180-
return 1
181160
fi
161+
162+
log_info "$filename exists and is valid, but not yet extracted."
163+
return 2
182164
}
165+

0 commit comments

Comments
 (0)