Skip to content

Commit edf9b55

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 edf9b55

File tree

3 files changed

+194
-45
lines changed

3 files changed

+194
-45
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: 88 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,57 +36,101 @@ 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"
47+
4648
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
49+
SLEEP_SEC=2
50+
51+
user_iface="$1"
52+
iface_list=""
53+
if [ -n "$user_iface" ]; then
54+
iface_list="$user_iface"
55+
log_info "User specified interface: $user_iface"
56+
else
57+
iface_list=$(get_ethernet_interfaces)
58+
log_info "Enumerating all detected Ethernet interfaces: $iface_list"
5459
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
60+
61+
pass_count=0
62+
fail_count=0
63+
skip_count=0
64+
65+
for iface in $iface_list; do
66+
log_info "---- Testing interface: $iface ----"
67+
if ! is_link_up "$iface"; then
68+
log_warn "No cable detected or carrier not present on $iface. Skipping."
69+
echo "$iface: SKIP (no cable)" >> "$summary_file"
70+
skip_count=$((skip_count + 1))
71+
continue
72+
fi
73+
74+
if ! bringup_interface "$iface" "$RETRIES" "$SLEEP_SEC"; then
75+
log_fail "Failed to bring up $iface after $RETRIES attempts"
76+
echo "$iface: FAIL (up failed)" >> "$summary_file"
77+
fail_count=$((fail_count + 1))
78+
continue
79+
fi
80+
log_pass "$iface is UP"
81+
82+
# Try DHCP client if present (optional)
83+
if ! wait_for_ip_address "$iface" 10 >/dev/null; then
84+
if command -v dhclient >/dev/null 2>&1; then
85+
log_info "Trying dhclient for $iface"
86+
dhclient "$iface"
87+
sleep 2
88+
elif command -v udhcpc >/dev/null 2>&1; then
89+
log_info "Trying udhcpc for $iface"
90+
udhcpc -i "$iface"
91+
sleep 2
92+
fi
93+
fi
94+
95+
ip_addr=$(wait_for_ip_address "$iface" 10)
96+
if [ -z "$ip_addr" ]; then
97+
log_fail "$iface did not get IP address"
98+
echo "$iface: FAIL (no IP)" >> "$summary_file"
99+
fail_count=$((fail_count + 1))
100+
continue
101+
fi
102+
log_pass "$iface got IP address: $ip_addr"
103+
104+
i=0
105+
ping_success=0
106+
while [ $i -lt $RETRIES ]; do
107+
if ping -I "$iface" -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
108+
log_pass "$iface: Ethernet connectivity verified via ping"
109+
echo "$iface: PASS" >> "$summary_file"
110+
pass_count=$((pass_count + 1))
111+
ping_success=1
112+
break
113+
fi
114+
log_warn "$iface: Ping failed (attempt $((i + 1))/$RETRIES)... retrying"
115+
sleep "$SLEEP_SEC"
116+
i=$((i + 1))
117+
done
118+
if [ $ping_success -eq 0 ]; then
119+
log_fail "$iface: Ping test failed after $RETRIES attempts"
120+
echo "$iface: FAIL (ping)" >> "$summary_file"
121+
fail_count=$((fail_count + 1))
65122
fi
66-
log_warn "$IFACE is still DOWN (attempt $((i + 1))/$RETRIES)..."
67-
i=$((i + 1))
68123
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"
124+
125+
log_info "---- Ethernet Interface Test Summary ----"
126+
cat "$summary_file"
127+
128+
if [ $pass_count -gt 0 ]; then
129+
log_pass "At least one interface PASS; overall PASS"
130+
echo "$TESTNAME PASS" > "$res_file"
131+
exit 0
132+
else
133+
log_fail "No interfaces passed connectivity test"
134+
echo "$TESTNAME FAIL" > "$res_file"
73135
exit 1
74136
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: 94 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,94 @@ 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: always down before 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 command -v ip >/dev/null 2>&1; then
444+
ip link set "$iface" down
445+
sleep 1
446+
ip link set "$iface" up
447+
sleep "$sleep_sec"
448+
if is_interface_up "$iface"; then return 0; fi
449+
elif command -v ifconfig >/dev/null 2>&1; then
450+
ifconfig "$iface" down
451+
sleep 1
452+
ifconfig "$iface" up
453+
sleep "$sleep_sec"
454+
if is_interface_up "$iface"; then return 0; fi
455+
fi
456+
i=$((i + 1))
457+
done
458+
return 1
459+
}
460+
461+
# Wait for a valid IPv4 address on the given interface, up to a timeout (default 30s)
462+
wait_for_ip_address() {
463+
iface="$1"
464+
timeout="${2:-30}"
465+
elapsed=0
466+
while [ "$elapsed" -lt "$timeout" ]; do
467+
ip_addr=$(get_ip_address "$iface")
468+
# Exclude empty and link-local addresses
469+
if [ -n "$ip_addr" ] && ! echo "$ip_addr" | grep -q '^169\.254'; then
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+
}

0 commit comments

Comments
 (0)