Skip to content

Commit c6ced29

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 c6ced29

File tree

4 files changed

+240
-0
lines changed

4 files changed

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