Skip to content

Commit ae82ec9

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 ae82ec9

File tree

2 files changed

+147
-44
lines changed

2 files changed

+147
-44
lines changed

Runner/suites/Connectivity/Ethernet/run.sh

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,61 @@ rm -f "$res_file"
3939

4040
log_info "--------------------------------------------------------------------------"
4141
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
42-
42+
43+
# Check for dependencies
4344
check_dependencies ip ping
44-
45-
IFACE="eth0"
45+
4646
RETRIES=3
4747
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
54-
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
48+
PASS_COUNT=0
49+
FAIL_COUNT=0
50+
51+
# Loop over all detected Ethernet interfaces
52+
for IFACE in $(get_ethernet_interfaces); do
53+
log_info "Testing interface: $IFACE"
54+
55+
# Bring up interface with retries
56+
if ! bringup_interface "$IFACE" $RETRIES $SLEEP_SEC; then
57+
log_warn "Failed to bring up $IFACE after $RETRIES attempts. Skipping..."
58+
FAIL_COUNT=$((FAIL_COUNT+1))
59+
continue
60+
fi
61+
62+
# Wait for IP address (optional, but recommended for DHCP setups)
63+
IP=$(wait_for_ip_address "$IFACE" 15)
64+
if [ -z "$IP" ]; then
65+
log_warn "No IP address assigned to $IFACE after 15 seconds. Skipping..."
66+
FAIL_COUNT=$((FAIL_COUNT+1))
67+
continue
68+
fi
69+
log_info "Interface $IFACE has IP: $IP"
70+
71+
# Ping test with retries
72+
i=0
73+
while [ $i -lt $RETRIES ]; do
74+
if ping -I "$IFACE" -c 4 -W 2 8.8.8.8 >/dev/null 2>&1; then
75+
log_pass "Ethernet connectivity verified via ping on $IFACE"
76+
PASS_COUNT=$((PASS_COUNT+1))
77+
break
78+
fi
79+
log_warn "Ping via $IFACE failed (attempt $((i + 1))/$RETRIES)..."
80+
sleep "$SLEEP_SEC"
81+
i=$((i + 1))
82+
done
83+
84+
if [ $i -eq $RETRIES ]; then
85+
log_fail "Ping test failed on $IFACE after $RETRIES attempts"
86+
FAIL_COUNT=$((FAIL_COUNT+1))
6587
fi
66-
log_warn "$IFACE is still DOWN (attempt $((i + 1))/$RETRIES)..."
67-
i=$((i + 1))
6888
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"
89+
90+
# Final result: pass if at least one interface passed
91+
if [ $PASS_COUNT -gt 0 ]; then
92+
echo "$TESTNAME PASS" > "$res_file"
93+
log_pass "$TESTNAME : PASS ($PASS_COUNT interface(s) passed, $FAIL_COUNT failed)"
94+
exit 0
95+
else
96+
echo "$TESTNAME FAIL" > "$res_file"
97+
log_fail "$TESTNAME : FAIL (No interface passed)"
7398
exit 1
7499
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: 96 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,96 @@ weston_start() {
391394
fi
392395
}
393396

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

0 commit comments

Comments
 (0)