Skip to content

Commit 2762cd7

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 d75bcd4 commit 2762cd7

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
# Parse SSID and PASSWORD from args/env/file
40+
SSID="$1"
41+
PASSWORD="$2"
42+
43+
if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then
44+
SSID="${SSID:-$SSID_ENV}"
45+
PASSWORD="${PASSWORD:-$PASSWORD_ENV}"
46+
fi
47+
48+
if [ -z "$SSID" ] || [ -z "$PASSWORD" ]; then
49+
if [ -f "./ssid_list.txt" ]; then
50+
SSID=$(awk 'NR==1 {print $1}' ./ssid_list.txt)
51+
PASSWORD=$(awk 'NR==1 {print $2}' ./ssid_list.txt)
52+
log_info "Using SSID and password from ssid_list.txt"
53+
else
54+
log_fail "SSID and password not provided via argument, env, or file."
55+
echo "$TESTNAME FAIL" > "$res_file"
56+
exit 1
57+
fi
58+
fi
59+
60+
# Define cleanup function
61+
cleanup() {
62+
log_info "Cleaning up WiFi test environment..."
63+
pkill -f "wpa_supplicant -i wlan0" 2>/dev/null
64+
rm -f /tmp/wpa_supplicant.conf
65+
ifconfig wlan0 0.0.0.0 >/dev/null 2>&1
66+
}
67+
68+
# Check required dependencies
69+
check_dependencies ifconfig ping
70+
check_systemd_services systemd-networkd.service || {
71+
log_error "Network services check failed"
72+
echo "$TESTNAME FAIL" > "$res_file"
73+
exit 1
74+
}
75+
76+
# Try nmcli first
77+
if command -v nmcli >/dev/null 2>&1; then
78+
log_info "Trying to connect using nmcli..."
79+
nmcli dev wifi connect "$SSID" password "$PASSWORD" 2>&1 | tee nmcli.log && {
80+
log_pass "Connected to $SSID using nmcli"
81+
IP=$(ifconfig wlan0 | awk '/inet / {print $2}')
82+
log_info "IP Address: $IP"
83+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
84+
log_pass "Internet connectivity verified via ping"
85+
echo "$TESTNAME PASS" > "$res_file"
86+
cleanup
87+
exit 0
88+
else
89+
log_fail "Ping test failed after nmcli connection"
90+
fi
91+
}
92+
fi
93+
94+
# Fall back to wpa_supplicant + udhcpc
95+
if command -v wpa_supplicant >/dev/null 2>&1 && command -v udhcpc >/dev/null 2>&1; then
96+
log_info "Falling back to wpa_supplicant + udhcpc"
97+
WPA_CONF="/tmp/wpa_supplicant.conf"
98+
{
99+
echo "ctrl_interface=/var/run/wpa_supplicant"
100+
echo "network={"
101+
echo " ssid=\"$SSID\""
102+
echo " key_mgmt=WPA-PSK"
103+
echo " pairwise=CCMP TKIP"
104+
echo " group=CCMP TKIP"
105+
echo " psk=\"$PASSWORD\""
106+
echo "}"
107+
} > "$WPA_CONF"
108+
109+
killall wpa_supplicant 2>/dev/null
110+
wpa_supplicant -B -i wlan0 -D nl80211 -c "$WPA_CONF" 2>&1 | tee wpa.log
111+
sleep 4
112+
udhcpc -i wlan0 >/dev/null 2>&1
113+
sleep 2
114+
115+
IP=$(ifconfig wlan0 | awk '/inet / {print $2}')
116+
if [ -n "$IP" ]; then
117+
log_pass "Got IP via udhcpc: $IP"
118+
if ping -c 3 -W 2 8.8.8.8 >/dev/null 2>&1; then
119+
log_pass "Internet connectivity verified via ping"
120+
echo "$TESTNAME PASS" > "$res_file"
121+
cleanup
122+
exit 0
123+
else
124+
log_fail "Ping test failed after wpa_supplicant connection"
125+
fi
126+
else
127+
log_fail "Failed to acquire IP via udhcpc"
128+
fi
129+
else
130+
log_error "Neither nmcli nor wpa_supplicant+udhcpc available"
131+
fi
132+
133+
log_fail "$TESTNAME : Test Failed"
134+
echo "$TESTNAME FAIL" > "$res_file"
135+
cleanup
136+
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)