Skip to content

Commit 47d28b3

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 47d28b3

File tree

2 files changed

+131
-44
lines changed

2 files changed

+131
-44
lines changed

Runner/suites/Connectivity/Ethernet/run.sh

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,58 @@ 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+
46+
# Get all Ethernet interfaces (excluding loopback, wifi, virtual, etc)
47+
IFACES=$(get_ethernet_interfaces)
48+
if [ -z "$IFACES" ]; then
49+
log_skip "No valid Ethernet interfaces detected."
50+
echo "$TESTNAME SKIP" > "$res_file"
51+
exit 0
52+
fi
53+
54+
SUCCESS=0
4655
RETRIES=3
4756
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
57+
58+
for IFACE in $IFACES; do
59+
log_info "Trying Ethernet interface: $IFACE"
60+
if ! bringup_interface "$IFACE" "$RETRIES" "$SLEEP_SEC"; then
61+
log_warn "Could not bring up $IFACE after $RETRIES retries. Trying next."
62+
continue
63+
fi
64+
log_pass "$IFACE is UP, waiting for IP address..."
65+
66+
IPADDR=$(wait_for_ip_address "$IFACE" 20)
67+
if [ -z "$IPADDR" ]; then
68+
log_warn "$IFACE: No IP address assigned after timeout."
69+
continue
6570
fi
66-
log_warn "$IFACE is still DOWN (attempt $((i + 1))/$RETRIES)..."
67-
i=$((i + 1))
71+
log_pass "$IFACE has IP: $IPADDR"
72+
73+
log_info "Pinging 8.8.8.8 via $IFACE..."
74+
i=0
75+
while [ $i -lt $RETRIES ]; do
76+
if ping -I "$IFACE" -c 4 -W 2 8.8.8.8 >/dev/null 2>&1; then
77+
log_pass "Ethernet connectivity verified via ping on $IFACE"
78+
echo "$TESTNAME PASS" > "$res_file"
79+
SUCCESS=1
80+
break
81+
fi
82+
log_warn "$IFACE: Ping failed (attempt $((i + 1))/$RETRIES)..."
83+
sleep "$SLEEP_SEC"
84+
i=$((i + 1))
85+
done
86+
87+
[ $SUCCESS -eq 1 ] && break
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+
if [ $SUCCESS -eq 1 ]; then
91+
exit 0
92+
else
93+
log_fail "No Ethernet interface passed connectivity tests"
94+
echo "$TESTNAME FAIL" > "$res_file"
7395
exit 1
7496
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: 83 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,83 @@ 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+
# Bring up interface with retries: always down before up
450+
bringup_interface() {
451+
iface="$1"
452+
retries="$2"
453+
sleep_sec="$3"
454+
i=0
455+
while [ $i -lt "$retries" ]; do
456+
if command -v ip >/dev/null 2>&1; then
457+
ip link set "$iface" down
458+
sleep 1
459+
ip link set "$iface" up
460+
sleep "$sleep_sec"
461+
if ip link show "$iface" | grep -q "state UP"; then
462+
return 0
463+
fi
464+
elif command -v ifconfig >/dev/null 2>&1; then
465+
ifconfig "$iface" down
466+
sleep 1
467+
ifconfig "$iface" up
468+
sleep "$sleep_sec"
469+
if ifconfig "$iface" | grep -q "UP"; then
470+
return 0
471+
fi
472+
fi
473+
i=$((i + 1))
474+
done
475+
return 1
476+
}

0 commit comments

Comments
 (0)