Skip to content

Commit 31cebf9

Browse files
committed
WiFi Test: Add cleanup logic and improve hybrid connection handling
- Register a cleanup() function to terminate background wpa_supplicant processes - Ensure WPA config is temporary and removed post test - Improve robustness of fallback to wpa_supplicant + udhcpc if nmcli fails - Ensure IP and internet connectivity checks are logged - Preserve original argument/env/file logic for SSID/password - Maintain logging, result output, and functestlib usage Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 791d3d4 commit 31cebf9

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# WiFi Connectivity Validation
2+
3+
## 📋 Overview
4+
5+
This test validates WiFi functionality by:
6+
7+
- Connecting to an access point (AP) using either `nmcli` or `wpa_supplicant`.
8+
- Verifying IP acquisition via DHCP.
9+
- Checking internet connectivity with a `ping` test.
10+
- Handling systemd network service status.
11+
- Supporting flexible SSID/password input via arguments, environment, or file.
12+
13+
## ✅ SSID/PASSWORD Input Priority (Hybrid Approach)
14+
15+
1. **Command-line arguments**:
16+
```sh
17+
./run.sh "MySSID" "MyPassword"
18+
```
19+
20+
2. **Environment variables**:
21+
```sh
22+
SSID_ENV=MySSID PASSWORD_ENV=MyPassword ./run.sh
23+
```
24+
25+
3. **Fallback to `ssid_list.txt` file** (if above not set):
26+
```txt
27+
MySSID MyPassword
28+
```
29+
30+
## ⚙️ Supported Tools
31+
32+
- Primary: `nmcli`
33+
- Fallback: `wpa_supplicant`, `udhcpc`, `ifconfig`
34+
35+
Ensure these tools are available in the system before running the test. Missing tools are detected and logged as skipped/failure.
36+
37+
## 🧪 Test Flow
38+
39+
1. **Dependency check** – verifies necessary binaries are present.
40+
2. **Systemd services check** – attempts to start network services if inactive.
41+
3. **WiFi connect (nmcli or wpa_supplicant)** – based on tool availability.
42+
4. **IP assignment check** – validates `ifconfig wlan0` output.
43+
5. **Internet test** – pings `8.8.8.8` to confirm outbound reachability.
44+
6. **Result logging** – writes `.res` file and logs all actions.
45+
46+
## 🧾 Output
47+
48+
- `WiFi_Connectivity.res`: Contains `WiFi_Connectivity PASS` or `FAIL`.
49+
- Logs are printed using `log_info`, `log_pass`, and `log_fail` from `functestlib.sh`.
50+
51+
## 📂 Directory Structure
52+
53+
```
54+
WiFi/
55+
├── run.sh
56+
├── ssid_list.txt (optional)
57+
├── README.md
58+
```
59+
60+
## 🌐 Integration (meta-qcom_PreMerge.yaml)
61+
62+
Add this test with SSID parameters as follows:
63+
64+
```yaml
65+
- name: WiFi_Connectivity
66+
path: Runner/suites/Connectivity/WiFi
67+
timeout:
68+
minutes: 5
69+
params:
70+
SSID_ENV: "xxxx"
71+
PASSWORD_ENV: "xxxx"
72+
```
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
4+
# SPDX-License-Identifier: BSD-3-Clause-Clear
5+
6+
# Robustly find and source init_env
7+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8+
INIT_ENV=""
9+
SEARCH="$SCRIPT_DIR"
10+
while [ "$SEARCH" != "/" ]; do
11+
if [ -f "$SEARCH/init_env" ]; then
12+
INIT_ENV="$SEARCH/init_env"
13+
break
14+
fi
15+
SEARCH=$(dirname "$SEARCH")
16+
done
17+
18+
if [ -z "$INIT_ENV" ]; then
19+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
20+
exit 1
21+
fi
22+
23+
# shellcheck disable=SC1090
24+
if [ -z "$__INIT_ENV_LOADED" ]; then
25+
. "$INIT_ENV"
26+
fi
27+
28+
# shellcheck disable=SC1090,SC1091
29+
. "$TOOLS/functestlib.sh"
30+
31+
TESTNAME="WiFi"
32+
res_file="./$TESTNAME.res"
33+
test_path=$(find_test_case_by_name "$TESTNAME")
34+
cd "$test_path" || exit 1
35+
36+
log_info "-------------------------------------------------------------"
37+
log_info "------------------- Starting $TESTNAME Test -----------------"
38+
39+
cleanup() {
40+
log_info "Cleaning up WiFi test environment..."
41+
pkill -f "wpa_supplicant -i wlan0" 2>/dev/null
42+
rm -f /tmp/wpa_supplicant.conf
43+
ifconfig wlan0 0.0.0.0 2>/dev/null
44+
}
45+
trap cleanup EXIT
46+
47+
# Parse SSID and PASSWORD
48+
SSID="$1"
49+
PASSWORD="$2"
50+
51+
if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then
52+
SSID="${SSID:-$SSID_ENV}"
53+
PASSWORD="${PASSWORD:-$PASSWORD_ENV}"
54+
fi
55+
56+
if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then
57+
if [ -f "./ssid_list.txt" ]; then
58+
SSID=$(awk 'NR==1 {print $1}' ./ssid_list.txt)
59+
PASSWORD=$(awk 'NR==1 {print $2}' ./ssid_list.txt)
60+
log_info "Using SSID and password from ssid_list.txt"
61+
else
62+
log_fail "SSID and password not provided."
63+
echo "$TESTNAME FAIL" > "$res_file"
64+
exit 1
65+
fi
66+
fi
67+
68+
check_dependencies "ifconfig" || exit 1
69+
check_dependencies "ping" || exit 1
70+
check_systemd_services systemd-networkd.service || exit 1
71+
72+
# Try nmcli
73+
if check_dependencies "nmcli"; then
74+
log_info "Trying to connect using nmcli..."
75+
if nmcli dev wifi connect "$SSID" password "$PASSWORD"; then
76+
log_pass "Connected to $SSID using nmcli"
77+
IP=$(ifconfig wlan0 | awk '/inet / {print $2}')
78+
log_info "IP Address: $IP"
79+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
80+
log_pass "Internet connectivity verified via ping"
81+
echo "$TESTNAME PASS" > "$res_file"
82+
exit 0
83+
else
84+
log_fail "Ping test failed after nmcli connection"
85+
fi
86+
else
87+
log_info "nmcli connection failed, trying fallback..."
88+
fi
89+
fi
90+
91+
# Fallback to wpa_supplicant + udhcpc
92+
if check_dependencies "wpa_supplicant" && check_dependencies "udhcpc"; then
93+
log_info "Trying fallback with wpa_supplicant + udhcpc"
94+
WPA_CONF="/tmp/wpa_supplicant.conf"
95+
{
96+
echo "ctrl_interface=/var/run/wpa_supplicant"
97+
echo "network={"
98+
echo " ssid=\"$SSID\""
99+
echo " key_mgmt=WPA-PSK"
100+
echo " pairwise=CCMP TKIP"
101+
echo " group=CCMP TKIP"
102+
echo " psk=\"$PASSWORD\""
103+
echo "}"
104+
} > "$WPA_CONF"
105+
106+
wpa_supplicant -B -i wlan0 -D nl80211 -c "$WPA_CONF"
107+
sleep 4
108+
udhcpc -i wlan0 >/dev/null 2>&1
109+
sleep 2
110+
111+
IP=$(ifconfig wlan0 | awk '/inet / {print $2}')
112+
if [ -n "$IP" ]; then
113+
log_pass "Got IP via fallback: $IP"
114+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
115+
log_pass "Ping successful via fallback"
116+
echo "$TESTNAME PASS" > "$res_file"
117+
exit 0
118+
else
119+
log_fail "Ping failed after fallback connection"
120+
fi
121+
else
122+
log_fail "Failed to get IP via fallback"
123+
fi
124+
else
125+
log_error "Neither nmcli nor wpa_supplicant fallback available"
126+
fi
127+
128+
log_fail "$TESTNAME : Test Failed"
129+
echo "$TESTNAME FAIL" > "$res_file"
130+
exit 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<SSID> <PASSWORD>
2+
<SSID_1> <PASSWORD_1>

Runner/utils/functestlib.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,27 @@ weston_start() {
382382
fi
383383
}
384384

385+
# Get systemd service status
386+
check_systemd_services() {
387+
for service in "$@"; do
388+
if systemctl is-enabled "$service" >/dev/null 2>&1; then
389+
if ! systemctl is-active --quiet "$service"; then
390+
log_warn "$service is not running, attempting to start..."
391+
systemctl start "$service"
392+
sleep 2
393+
if systemctl is-active --quiet "$service"; then
394+
log_pass "$service started successfully."
395+
else
396+
log_fail "$service failed to start."
397+
return 1
398+
fi
399+
else
400+
log_info "$service is already active."
401+
fi
402+
else
403+
log_warn "$service is not enabled or not found"
404+
fi
405+
done
406+
return 0
407+
}
408+

0 commit comments

Comments
 (0)