Skip to content

Commit 0eb4e7e

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 0eb4e7e

File tree

3 files changed

+221
-46
lines changed

3 files changed

+221
-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: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,57 +36,86 @@ 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+
if [ -n "$user_iface" ]; then
52+
IFACES="$user_iface"
53+
log_info "User specified interface: $user_iface"
54+
else
55+
IFACES=$(get_ethernet_interfaces)
56+
log_info "Auto-detected Ethernet interfaces: $IFACES"
5457
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
58+
59+
iface_passed=0
60+
iface_failed=0
61+
iface_skipped=0
62+
63+
for iface in $IFACES; do
64+
log_info "---- Testing interface: $iface ----"
65+
66+
# Check if interface is up
67+
if ! is_interface_up "$iface"; then
68+
log_warn "$iface is DOWN, skipping"
69+
echo "$iface: SKIP (down/no cable)" >> "$summary_file"
70+
iface_skipped=$((iface_skipped+1))
71+
continue
72+
fi
73+
74+
ip_addr="$(get_ip_address "$iface")"
75+
76+
if [ -z "$ip_addr" ]; then
77+
log_info "$iface has no IP, attempting DHCP"
78+
run_dhcp_client "$iface" 10
79+
sleep 2
80+
ip_addr="$(get_ip_address "$iface")"
81+
fi
82+
83+
if [ -z "$ip_addr" ]; then
84+
log_warn "$iface has no IP assigned, skipping"
85+
echo "$iface: SKIP (no IP assigned)" >> "$summary_file"
86+
iface_skipped=$((iface_skipped+1))
87+
continue
88+
elif echo "$ip_addr" | grep -q '^169\.254'; then
89+
log_warn "$iface got only link-local IP ($ip_addr), skipping"
90+
echo "$iface: SKIP (link-local IP: $ip_addr)" >> "$summary_file"
91+
iface_skipped=$((iface_skipped+1))
92+
continue
93+
else
94+
log_info "$iface got IP: $ip_addr"
95+
fi
96+
97+
# Ping test
98+
if ping -I "$iface" -c 4 -W 2 8.8.8.8 >/dev/null 2>&1; then
99+
log_pass "$iface connectivity verified via ping"
100+
echo "$iface: PASS" >> "$summary_file"
101+
iface_passed=$((iface_passed+1))
102+
else
103+
log_fail "Ping test failed for $iface"
104+
echo "$iface: FAIL (ping failed)" >> "$summary_file"
105+
iface_failed=$((iface_failed+1))
65106
fi
66-
log_warn "$IFACE is still DOWN (attempt $((i + 1))/$RETRIES)..."
67-
i=$((i + 1))
68107
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"
108+
109+
log_info "---- Ethernet Interface Test Summary ----"
110+
cat "$summary_file"
111+
112+
if [ "$iface_passed" -gt 0 ]; then
113+
echo "$TESTNAME PASS" > "$res_file"
114+
exit 0
115+
elif [ "$iface_failed" -gt 0 ]; then
116+
echo "$TESTNAME FAIL" > "$res_file"
73117
exit 1
118+
else
119+
echo "$TESTNAME SKIP" > "$res_file"
120+
exit 2
74121
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: 135 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,135 @@ 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 (down before up).
437+
bringup_interface() {
438+
iface="$1"; retries="${2:-3}"; sleep_sec="${3:-2}"; i=0
439+
while [ $i -lt "$retries" ]; do
440+
if command -v ip >/dev/null 2>&1; then
441+
ip link set "$iface" down
442+
sleep 1
443+
ip link set "$iface" up
444+
sleep "$sleep_sec"
445+
ip link show "$iface" | grep -q "state UP" && return 0
446+
elif command -v ifconfig >/dev/null 2>&1; then
447+
ifconfig "$iface" down
448+
sleep 1
449+
ifconfig "$iface" up
450+
sleep "$sleep_sec"
451+
ifconfig "$iface" | grep -q "UP" && return 0
452+
fi
453+
i=$((i + 1))
454+
done
455+
return 1
456+
}
457+
458+
# Wait for a valid IPv4 address on the given interface, up to a timeout (default 30s)
459+
wait_for_ip_address() {
460+
iface="$1"
461+
timeout="${2:-30}"
462+
elapsed=0
463+
while [ "$elapsed" -lt "$timeout" ]; do
464+
ip_addr=$(get_ip_address "$iface")
465+
if [ -n "$ip_addr" ]; then
466+
if echo "$ip_addr" | grep -q '^169\.254'; then
467+
echo "$ip_addr"
468+
return 2
469+
fi
470+
echo "$ip_addr"
471+
return 0
472+
fi
473+
sleep 1
474+
elapsed=$((elapsed + 1))
475+
done
476+
return 1
477+
}
478+
479+
# Get the IPv4 address for a given interface.
480+
get_ip_address() {
481+
iface="$1"
482+
if command -v ip >/dev/null 2>&1; then
483+
ip -4 -o addr show "$iface" | awk '{print $4}' | cut -d/ -f1 | head -n1
484+
elif command -v ifconfig >/dev/null 2>&1; then
485+
ifconfig "$iface" 2>/dev/null | awk '/inet / {print $2}' | head -n1
486+
fi
487+
}
488+
489+
# Run a command with a timeout (in seconds)
490+
run_with_timeout() {
491+
timeout="$1"; shift
492+
( "$@" ) &
493+
pid=$!
494+
( sleep "$timeout"; kill "$pid" 2>/dev/null ) &
495+
watcher=$!
496+
wait $pid 2>/dev/null
497+
status=$?
498+
kill $watcher 2>/dev/null
499+
return $status
500+
}
501+
502+
# DHCP client logic (dhclient and udhcpc with timeouts)
503+
run_dhcp_client() {
504+
iface="$1"
505+
timeout="${2:-10}"
506+
ip_addr=""
507+
log_info "Attempting DHCP on $iface (timeout ${timeout}s)..."
508+
if command -v dhclient >/dev/null 2>&1; then
509+
log_info "Trying dhclient for $iface"
510+
run_with_timeout "$timeout" dhclient "$iface"
511+
ip_addr=$(wait_for_ip_address "$iface" 5)
512+
if [ -n "$ip_addr" ]; then
513+
echo "$ip_addr"
514+
return 0
515+
fi
516+
fi
517+
if command -v udhcpc >/dev/null 2>&1; then
518+
log_info "Trying udhcpc for $iface"
519+
run_with_timeout "$timeout" udhcpc -i "$iface" -T 3 -t 3
520+
ip_addr=$(wait_for_ip_address "$iface" 5)
521+
if [ -n "$ip_addr" ]; then
522+
echo "$ip_addr"
523+
return 0
524+
fi
525+
fi
526+
log_warn "DHCP failed for $iface"
527+
return 1
528+
}

0 commit comments

Comments
 (0)