Skip to content

Commit 369f3a2

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 369f3a2

File tree

2 files changed

+92
-30
lines changed

2 files changed

+92
-30
lines changed

Runner/suites/Connectivity/Ethernet/run.sh

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,53 @@ 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+
# Detect Ethernet interface dynamically using helper
47+
IFACE=$(get_ethernet_interface)
48+
if [ -z "$IFACE" ]; then
49+
log_fail "No Ethernet interface found!"
50+
echo "$TESTNAME SKIP" > "$res_file"
51+
exit 0
52+
fi
53+
log_info "Detected Ethernet interface: $IFACE"
54+
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+
58+
# Bring up the interface with retries (always brings down first)
5759
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
65-
fi
66-
log_warn "$IFACE is still DOWN (attempt $((i + 1))/$RETRIES)..."
67-
i=$((i + 1))
68-
done
69-
70-
if [ $i -eq $RETRIES ]; then
60+
if ! bringup_interface "$IFACE" "$RETRIES" "$SLEEP_SEC"; then
7161
log_fail "Failed to bring up $IFACE after $RETRIES attempts"
72-
echo "FAIL $TESTNAME" > "$res_file"
62+
echo "$TESTNAME FAIL" > "$res_file"
7363
exit 1
7464
fi
75-
65+
log_pass "$IFACE is UP"
66+
67+
# Log the current IP address (if any)
68+
IPADDR=$(get_ip_address "$IFACE")
69+
if [ -n "$IPADDR" ]; then
70+
log_info "IP Address for $IFACE: $IPADDR"
71+
else
72+
log_warn "Could not retrieve IP address for $IFACE"
73+
fi
74+
7675
# Ping test with retries
7776
log_info "Running ping test to 8.8.8.8 via $IFACE..."
7877
i=0
7978
while [ $i -lt $RETRIES ]; do
8079
if ping -I "$IFACE" -c 4 -W 2 8.8.8.8 >/dev/null 2>&1; then
8180
log_pass "Ethernet connectivity verified via ping"
82-
echo "PASS $TESTNAME" > "$res_file"
81+
echo "$TESTNAME PASS" > "$res_file"
8382
exit 0
8483
fi
8584
log_warn "Ping failed (attempt $((i + 1))/$RETRIES)... retrying"
8685
sleep "$SLEEP_SEC"
8786
i=$((i + 1))
8887
done
89-
88+
9089
log_fail "Ping test failed after $RETRIES attempts"
91-
echo "FAIL $TESTNAME" > "$res_file"
90+
echo "$TESTNAME FAIL" > "$res_file"
9291
exit 1

Runner/utils/functestlib.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,66 @@ weston_start() {
391391
fi
392392
}
393393

394+
# Get the IPv4 address for a given interface
395+
get_ip_address() {
396+
iface="$1"
397+
if command -v ip >/dev/null 2>&1; then
398+
ip -4 -o addr show "$iface" | awk '{print $4}' | cut -d/ -f1 | head -n1
399+
elif command -v ifconfig >/dev/null 2>&1; then
400+
ifconfig "$iface" 2>/dev/null | awk '/inet / {print $2}' | head -n1
401+
fi
402+
}
403+
404+
# Returns true (0) if interface is Ethernet type (type 1 in sysfs), false otherwise.
405+
is_ethernet_interface() {
406+
iface="$1"
407+
[ -f "/sys/class/net/$iface/type" ] && [ "$(cat "/sys/class/net/$iface/type")" = "1" ]
408+
}
409+
410+
# Returns the names of all detected Ethernet interfaces (excluding loopback and common virtual types)
411+
get_ethernet_interfaces() {
412+
for path in /sys/class/net/*; do
413+
iface=$(basename "$path")
414+
case "$iface" in
415+
lo) continue ;; # Loopback
416+
docker*|br-*|veth*|virbr*|tap*|tun*|wlan*) continue ;; # Common virtual & WiFi interfaces
417+
esac
418+
if is_ethernet_interface "$iface"; then
419+
echo "$iface"
420+
fi
421+
done
422+
}
423+
424+
# Returns the first detected Ethernet interface (or nothing if none found)
425+
get_ethernet_interface() {
426+
get_ethernet_interfaces | head -n1
427+
}
428+
429+
# Bring up interface with retries: always down before up
430+
bringup_interface() {
431+
iface="$1"
432+
retries="$2"
433+
sleep_sec="$3"
434+
i=0
435+
while [ $i -lt "$retries" ]; do
436+
if command -v ip >/dev/null 2>&1; then
437+
ip link set "$iface" down
438+
sleep 1
439+
ip link set "$iface" up
440+
sleep "$sleep_sec"
441+
if ip link show "$iface" | grep -q "state UP"; then
442+
return 0
443+
fi
444+
elif command -v ifconfig >/dev/null 2>&1; then
445+
ifconfig "$iface" down
446+
sleep 1
447+
ifconfig "$iface" up
448+
sleep "$sleep_sec"
449+
if ifconfig "$iface" | grep -q "UP"; then
450+
return 0
451+
fi
452+
fi
453+
i=$((i + 1))
454+
done
455+
return 1
456+
}

0 commit comments

Comments
 (0)