Skip to content

Commit a7fcb59

Browse files
committed
Ethernet test: Make interface detection dynamic, robustify bring-up and ping validation
- Add logic to auto-detect active Ethernet interface using ip/ipconfig - Enhance bring-up sequence with retries and logging - Improve ping test error handling and reporting - Refactor for better CI diagnostics Signed-off-by: Srikanth Muppandam <smuppand@qti.qualcomm.com>
1 parent edfad16 commit a7fcb59

File tree

3 files changed

+191
-46
lines changed

3 files changed

+191
-46
lines changed

Runner/suites/Connectivity/Ethernet/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ cd <this-repo>
2828
scp -r common Runner user@target_device_ip:<Path in device>
2929
ssh user@target_device_ip
3030
cd <Path in device>/Runner && ./run-test.sh Ethernet
31+
# Optional: specify preferred interface (e.g., eth1)
32+
./run.sh [preferred-interface]
3133
```
3234

3335
## Prerequisites
@@ -53,5 +55,14 @@ Test result will be saved in `Ethernet.res` as:
5355
## Output
5456
A .res file is generated in the same directory:
5557

56-
`PASS Ethernet` OR `FAIL Ethernet`
58+
`Ethernet PASS` OR `Ethernet FAIL`
5759

60+
## Sample Log
61+
```
62+
Output
63+
64+
[INFO] 2025-06-11 10:12:23 - Detected Ethernet interface: enP1p4s0u1u1
65+
[PASS] 2025-06-11 10:12:30 - enP1p4s0u1u1 is UP
66+
[INFO] 2025-06-11 10:12:31 - Assigned IP: 10.0.0.55
67+
[PASS] 2025-06-11 10:12:35 - Ping successful on enP1p4s0u1u1
68+
```

Runner/suites/Connectivity/Ethernet/run.sh

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,57 +36,87 @@ test_path=$(find_test_case_by_name "$TESTNAME") || {
3636
cd "$test_path" || exit 1
3737
res_file="./$TESTNAME.res"
3838
rm -f "$res_file"
39+
summary_file="./$TESTNAME.summary"
40+
rm -f "$res_file" "$summary_file"
3941

4042
log_info "--------------------------------------------------------------------------"
4143
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
42-
44+
45+
# Check for dependencies
4346
check_dependencies ip ping
44-
45-
IFACE="eth0"
46-
RETRIES=3
47-
SLEEP_SEC=3
48-
49-
# Check interface existence
50-
if ! ip link show "$IFACE" >/dev/null 2>&1; then
51-
log_fail "Ethernet interface $IFACE not found"
52-
echo "FAIL $TESTNAME" > "$res_file"
53-
exit 1
47+
48+
# User-specified interface (argument) or all detected
49+
# Accept user-preferred interface as argument
50+
user_iface="$1"
51+
52+
iface_passed=0
53+
iface_failed=0
54+
iface_skipped=0
55+
any_tested=0
56+
57+
# Get interfaces to test
58+
if [ -n "$user_iface" ]; then
59+
if is_ethernet_interface "$user_iface"; then
60+
eth_ifaces="$user_iface"
61+
log_info "User specified interface: $user_iface"
62+
else
63+
log_fail "User-specified interface '$user_iface' is not a valid Ethernet interface"
64+
echo "$TESTNAME SKIP" > "$res_file"
65+
exit 0
66+
fi
67+
else
68+
eth_ifaces="$(get_ethernet_interfaces)"
69+
log_info "Auto-detected Ethernet interfaces: $eth_ifaces"
5470
fi
55-
56-
# Bring up interface with retries
57-
log_info "Ensuring $IFACE is UP..."
58-
i=0
59-
while [ $i -lt $RETRIES ]; do
60-
ip link set "$IFACE" up
61-
sleep "$SLEEP_SEC"
62-
if ip link show "$IFACE" | grep -q "state UP"; then
63-
log_info "$IFACE is UP"
64-
break
71+
72+
for iface in $eth_ifaces; do
73+
log_info "---- Testing interface: $iface ----"
74+
if ! is_interface_up "$iface"; then
75+
log_warn "$iface is DOWN, skipping"
76+
echo "$iface: SKIP (down/no cable)" >> "$summary_file"
77+
iface_skipped=$((iface_skipped+1))
78+
continue
79+
fi
80+
81+
# Get IP (avoid link-local)
82+
ip_addr=$(wait_for_ip_address "$iface" 10)
83+
if [ -z "$ip_addr" ]; then
84+
log_warn "$iface did not get a valid IP, skipping"
85+
echo "$iface: SKIP (no valid IP)" >> "$summary_file"
86+
iface_skipped=$((iface_skipped+1))
87+
continue
88+
fi
89+
if echo "$ip_addr" | grep -q '^169\.254'; then
90+
log_warn "$iface got only link-local IP ($ip_addr), skipping"
91+
echo "$iface: SKIP (link-local only)" >> "$summary_file"
92+
iface_skipped=$((iface_skipped+1))
93+
continue
94+
fi
95+
log_info "$iface got IP: $ip_addr"
96+
any_tested=1
97+
98+
# Ping test
99+
if ping -I "$iface" -c 4 -W 2 8.8.8.8 >/dev/null 2>&1; then
100+
log_pass "$iface connectivity verified via ping"
101+
echo "$iface: PASS" >> "$summary_file"
102+
iface_passed=$((iface_passed+1))
103+
else
104+
log_fail "$iface ping test failed"
105+
echo "$iface: FAIL (ping failed)" >> "$summary_file"
106+
iface_failed=$((iface_failed+1))
65107
fi
66-
log_warn "$IFACE is still DOWN (attempt $((i + 1))/$RETRIES)..."
67-
i=$((i + 1))
68108
done
69-
70-
if [ $i -eq $RETRIES ]; then
71-
log_fail "Failed to bring up $IFACE after $RETRIES attempts"
72-
echo "FAIL $TESTNAME" > "$res_file"
109+
110+
log_info "---- Ethernet Interface Test Summary ----"
111+
cat "$summary_file"
112+
113+
if [ "$iface_passed" -gt 0 ]; then
114+
echo "$TESTNAME PASS" > "$res_file"
115+
exit 0
116+
elif [ "$any_tested" -eq 1 ]; then
117+
echo "$TESTNAME FAIL" > "$res_file"
73118
exit 1
119+
else
120+
echo "$TESTNAME SKIP" > "$res_file"
121+
exit 0
74122
fi
75-
76-
# Ping test with retries
77-
log_info "Running ping test to 8.8.8.8 via $IFACE..."
78-
i=0
79-
while [ $i -lt $RETRIES ]; do
80-
if ping -I "$IFACE" -c 4 -W 2 8.8.8.8 >/dev/null 2>&1; then
81-
log_pass "Ethernet connectivity verified via ping"
82-
echo "PASS $TESTNAME" > "$res_file"
83-
exit 0
84-
fi
85-
log_warn "Ping failed (attempt $((i + 1))/$RETRIES)... retrying"
86-
sleep "$SLEEP_SEC"
87-
i=$((i + 1))
88-
done
89-
90-
log_fail "Ping test failed after $RETRIES attempts"
91-
echo "FAIL $TESTNAME" > "$res_file"
92-
exit 1

Runner/utils/functestlib.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
44
# SPDX-License-Identifier: BSD-3-Clause-Clear
55

6+
# Suppress 'Broken pipe' errors globally in this shell (put this at the very top once)
7+
trap '' PIPE
8+
69
# --- Logging helpers ---
710
log() {
811
level=$1
@@ -391,3 +394,104 @@ weston_start() {
391394
fi
392395
}
393396

397+
# Returns true (0) if interface is administratively and physically up
398+
is_interface_up() {
399+
iface="$1"
400+
if [ -f "/sys/class/net/$iface/operstate" ]; then
401+
[ "$(cat "/sys/class/net/$iface/operstate")" = "up" ]
402+
elif command -v ip >/dev/null 2>&1; then
403+
ip link show "$iface" 2>/dev/null | grep -qw "state UP"
404+
elif command -v ifconfig >/dev/null 2>&1; then
405+
ifconfig "$iface" 2>/dev/null | grep -qw "UP"
406+
else
407+
return 1
408+
fi
409+
}
410+
411+
# Returns true (0) if physical link/carrier is detected (cable plugged in)
412+
is_link_up() {
413+
iface="$1"
414+
[ -f "/sys/class/net/$iface/carrier" ] && [ "$(cat "/sys/class/net/$iface/carrier")" = "1" ]
415+
}
416+
417+
# Returns true (0) if interface is Ethernet type (type 1 in sysfs)
418+
is_ethernet_interface() {
419+
iface="$1"
420+
[ -f "/sys/class/net/$iface/type" ] && [ "$(cat "/sys/class/net/$iface/type")" = "1" ]
421+
}
422+
423+
# Get all Ethernet interfaces (excluding common virtual types)
424+
get_ethernet_interfaces() {
425+
for path in /sys/class/net/*; do
426+
iface=$(basename "$path")
427+
case "$iface" in
428+
lo|docker*|br-*|veth*|virbr*|tap*|tun*|wl*) continue ;;
429+
esac
430+
if is_ethernet_interface "$iface"; then
431+
echo "$iface"
432+
fi
433+
done
434+
}
435+
436+
# Bring up interface with retries, immediately succeed if already UP
437+
bringup_interface() {
438+
iface="$1"
439+
retries="$2"
440+
sleep_sec="$3"
441+
i=0
442+
while [ $i -lt "$retries" ]; do
443+
# If already UP, we're good
444+
if is_interface_up "$iface"; then
445+
return 0
446+
fi
447+
# Bring down/up
448+
if command -v ip >/dev/null 2>&1; then
449+
ip link set "$iface" down
450+
sleep 1
451+
ip link set "$iface" up
452+
sleep "$sleep_sec"
453+
elif command -v ifconfig >/dev/null 2>&1; then
454+
ifconfig "$iface" down
455+
sleep 1
456+
ifconfig "$iface" up
457+
sleep "$sleep_sec"
458+
fi
459+
# Check again
460+
if is_interface_up "$iface"; then
461+
return 0
462+
fi
463+
i=$((i + 1))
464+
done
465+
return 1
466+
}
467+
468+
# Wait for a valid IPv4 address on the given interface, up to a timeout (default 30s)
469+
wait_for_ip_address() {
470+
iface="$1"
471+
timeout="${2:-30}"
472+
elapsed=0
473+
while [ "$elapsed" -lt "$timeout" ]; do
474+
ip_addr=$(get_ip_address "$iface")
475+
if [ -n "$ip_addr" ]; then
476+
if echo "$ip_addr" | grep -q '^169\.254'; then
477+
echo "$ip_addr"
478+
return 2
479+
fi
480+
echo "$ip_addr"
481+
return 0
482+
fi
483+
sleep 1
484+
elapsed=$((elapsed + 1))
485+
done
486+
return 1
487+
}
488+
489+
# Get the IPv4 address for a given interface
490+
get_ip_address() {
491+
iface="$1"
492+
if command -v ip >/dev/null 2>&1; then
493+
ip -4 -o addr show "$iface" | awk '{print $4}' | cut -d/ -f1 | head -n1
494+
elif command -v ifconfig >/dev/null 2>&1; then
495+
ifconfig "$iface" 2>/dev/null | awk '/inet / {print $2}' | head -n1
496+
fi
497+
}

0 commit comments

Comments
 (0)