|
18 | 18 | Handles linux-specific functionality for running test cases
|
19 | 19 | """
|
20 | 20 |
|
| 21 | +import glob |
21 | 22 | import logging
|
22 | 23 | import os
|
| 24 | +import shutil |
23 | 25 | import subprocess
|
24 | 26 | import sys
|
25 | 27 | import time
|
| 28 | +from collections import namedtuple |
| 29 | +from time import sleep |
| 30 | +from typing import Optional |
26 | 31 |
|
27 | 32 | from .test_definition import ApplicationPaths
|
28 | 33 |
|
29 | 34 | test_environ = os.environ.copy()
|
| 35 | +PW_PROJECT_ROOT = os.environ.get("PW_PROJECT_ROOT") |
| 36 | +QEMU_CONFIG_FILES = "integrations/docker/images/stage-2/chip-build-linux-qemu/files" |
30 | 37 |
|
31 | 38 |
|
32 | 39 | def EnsureNetworkNamespaceAvailability():
|
@@ -169,6 +176,174 @@ def ShutdownNamespaceForTestExecution():
|
169 | 176 | RemoveNamespaceForAppTest()
|
170 | 177 |
|
171 | 178 |
|
| 179 | +class DbusTest: |
| 180 | + DBUS_SYSTEM_BUS_ADDRESS = "unix:path=/tmp/chip-dbus-test" |
| 181 | + |
| 182 | + def __init__(self): |
| 183 | + self._dbus = None |
| 184 | + self._dbus_proxy = None |
| 185 | + |
| 186 | + def start(self): |
| 187 | + original_env = os.environ.copy() |
| 188 | + os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = DbusTest.DBUS_SYSTEM_BUS_ADDRESS |
| 189 | + dbus = shutil.which("dbus-daemon") |
| 190 | + self._dbus = subprocess.Popen( |
| 191 | + [dbus, "--session", "--address", self.DBUS_SYSTEM_BUS_ADDRESS]) |
| 192 | + |
| 193 | + self._dbus_proxy = subprocess.Popen( |
| 194 | + ["python3", f"{PW_PROJECT_ROOT}/scripts/tools/dbus-proxy-bluez.py", "--bus-proxy", DbusTest.DBUS_SYSTEM_BUS_ADDRESS], env=original_env) |
| 195 | + |
| 196 | + def stop(self): |
| 197 | + if self._dbus: |
| 198 | + self._dbus_proxy.terminate() |
| 199 | + self._dbus.terminate() |
| 200 | + self._dbus.wait() |
| 201 | + |
| 202 | + |
| 203 | +class VirtualWifi: |
| 204 | + def __init__(self, hostapd_path: str, dnsmasq_path: str, wpa_supplicant_path: str, wlan_app: Optional[str] = None, wlan_tool: Optional[str] = None): |
| 205 | + self._hostapd_path = hostapd_path |
| 206 | + self._dnsmasq_path = dnsmasq_path |
| 207 | + self._wpa_supplicant_path = wpa_supplicant_path |
| 208 | + self._hostapd_conf = os.path.join( |
| 209 | + PW_PROJECT_ROOT, QEMU_CONFIG_FILES, "wifi/hostapd.conf") |
| 210 | + self._dnsmasq_conf = os.path.join( |
| 211 | + PW_PROJECT_ROOT, QEMU_CONFIG_FILES, "wifi/dnsmasq.conf") |
| 212 | + self._wpa_supplicant_conf = os.path.join( |
| 213 | + PW_PROJECT_ROOT, QEMU_CONFIG_FILES, "wifi/wpa_supplicant.conf") |
| 214 | + |
| 215 | + if wlan_app is None or wlan_tool is None: |
| 216 | + wlans = glob.glob( |
| 217 | + "/sys/devices/virtual/mac80211_hwsim/hwsim*/net/*") |
| 218 | + if len(wlans) < 2: |
| 219 | + raise RuntimeError("Not enough wlan devices found") |
| 220 | + |
| 221 | + self._wlan_app = os.path.basename(wlans[0]) |
| 222 | + self._wlan_tool = os.path.basename(wlans[1]) |
| 223 | + else: |
| 224 | + self._wlan_app = wlan_app |
| 225 | + self._wlan_tool = wlan_tool |
| 226 | + self._hostapd = None |
| 227 | + self._dnsmasq = None |
| 228 | + self._wpa_supplicant = None |
| 229 | + |
| 230 | + @staticmethod |
| 231 | + def _get_phy(dev: str) -> str: |
| 232 | + output = subprocess.check_output(['iw', 'dev', dev, 'info']) |
| 233 | + for line in output.split(b'\n'): |
| 234 | + if b'wiphy' in line: |
| 235 | + wiphy = int(line.split(b' ')[1]) |
| 236 | + return f"phy{wiphy}" |
| 237 | + raise ValueError(f'No wiphy found for {dev}') |
| 238 | + |
| 239 | + @staticmethod |
| 240 | + def _move_phy_to_netns(phy: str, netns: str): |
| 241 | + subprocess.check_call( |
| 242 | + ["iw", "phy", phy, "set", "netns", "name", netns]) |
| 243 | + |
| 244 | + @staticmethod |
| 245 | + def _set_interface_ip_in_netns(netns: str, dev: str, ip: str): |
| 246 | + subprocess.check_call( |
| 247 | + ["ip", "netns", "exec", netns, "ip", "link", "set", "dev", dev, "up"]) |
| 248 | + subprocess.check_call( |
| 249 | + ["ip", "netns", "exec", netns, "ip", "addr", "add", ip, "dev", dev]) |
| 250 | + |
| 251 | + def start(self): |
| 252 | + self._move_phy_to_netns(self._get_phy(self._wlan_app), 'app') |
| 253 | + self._move_phy_to_netns(self._get_phy(self._wlan_tool), 'tool') |
| 254 | + self._set_interface_ip_in_netns( |
| 255 | + 'tool', self._wlan_tool, '192.168.200.1/24') |
| 256 | + |
| 257 | + self._hostapd = subprocess.Popen(["ip", "netns", "exec", "tool", self._hostapd_path, |
| 258 | + self._hostapd_conf], stdout=subprocess.DEVNULL) |
| 259 | + self._dnsmasq = subprocess.Popen(["ip", "netns", "exec", "tool", self._dnsmasq_path, |
| 260 | + '-d', '-C', self._dnsmasq_conf], stdout=subprocess.DEVNULL) |
| 261 | + self._wpa_supplicant = subprocess.Popen( |
| 262 | + ["ip", "netns", "exec", "app", self._wpa_supplicant_path, "-u", '-s', '-c', self._wpa_supplicant_conf], stdout=subprocess.DEVNULL) |
| 263 | + |
| 264 | + def stop(self): |
| 265 | + if self._hostapd: |
| 266 | + self._hostapd.terminate() |
| 267 | + self._hostapd.wait() |
| 268 | + if self._dnsmasq: |
| 269 | + self._dnsmasq.terminate() |
| 270 | + self._dnsmasq.wait() |
| 271 | + if self._wpa_supplicant: |
| 272 | + self._wpa_supplicant.terminate() |
| 273 | + self._wpa_supplicant.wait() |
| 274 | + |
| 275 | + |
| 276 | +class VirtualBle: |
| 277 | + BleDevice = namedtuple('BleDevice', ['hci', 'mac', 'index']) |
| 278 | + |
| 279 | + def __init__(self, btvirt_path: str, bluetoothctl_path: str): |
| 280 | + self._btvirt_path = btvirt_path |
| 281 | + self._bluetoothctl_path = bluetoothctl_path |
| 282 | + self._btvirt = None |
| 283 | + self._bluetoothctl = None |
| 284 | + self._ble_app = None |
| 285 | + self._ble_tool = None |
| 286 | + |
| 287 | + @property |
| 288 | + def ble_app(self) -> Optional[BleDevice]: |
| 289 | + if not self._ble_app: |
| 290 | + raise RuntimeError("Bluetooth not started") |
| 291 | + return self._ble_app |
| 292 | + |
| 293 | + @property |
| 294 | + def ble_tool(self) -> Optional[BleDevice]: |
| 295 | + if not self._ble_tool: |
| 296 | + raise RuntimeError("Bluetooth not started") |
| 297 | + return self._ble_tool |
| 298 | + |
| 299 | + def bletoothctl_cmd(self, cmd): |
| 300 | + self._bluetoothctl.stdin.write(cmd) |
| 301 | + self._bluetoothctl.stdin.flush() |
| 302 | + |
| 303 | + def _get_mac_address(self, hci_name): |
| 304 | + result = subprocess.run( |
| 305 | + ['hcitool', 'dev'], capture_output=True, text=True) |
| 306 | + lines = result.stdout.splitlines() |
| 307 | + |
| 308 | + for line in lines: |
| 309 | + if hci_name in line: |
| 310 | + mac_address = line.split()[1] |
| 311 | + return mac_address |
| 312 | + |
| 313 | + raise RuntimeError(f"No MAC address found for device {hci_name}") |
| 314 | + |
| 315 | + def _get_ble_info(self): |
| 316 | + ble_dev_paths = glob.glob("/sys/devices/virtual/bluetooth/hci*") |
| 317 | + hci = [os.path.basename(path) for path in ble_dev_paths] |
| 318 | + if len(hci) < 2: |
| 319 | + raise RuntimeError("Not enough BLE devices found") |
| 320 | + self._ble_app = self.BleDevice( |
| 321 | + hci=hci[0], mac=self._get_mac_address(hci[0]), index=int(hci[0].replace('hci', ''))) |
| 322 | + self._ble_tool = self.BleDevice( |
| 323 | + hci=hci[1], mac=self._get_mac_address(hci[1]), index=int(hci[1].replace('hci', ''))) |
| 324 | + |
| 325 | + def _run_bluetoothctl(self): |
| 326 | + self._bluetoothctl = subprocess.Popen([self._bluetoothctl_path], text=True, |
| 327 | + stdin=subprocess.PIPE, stdout=subprocess.DEVNULL) |
| 328 | + self.bletoothctl_cmd(f"select {self.ble_app.mac}\n") |
| 329 | + self.bletoothctl_cmd("power on\n") |
| 330 | + self.bletoothctl_cmd(f"select {self.ble_tool.mac}\n") |
| 331 | + self.bletoothctl_cmd("power on\n") |
| 332 | + self.bletoothctl_cmd("quit\n") |
| 333 | + self._bluetoothctl.wait() |
| 334 | + |
| 335 | + def start(self): |
| 336 | + self._btvirt = subprocess.Popen([self._btvirt_path, '-l2']) |
| 337 | + sleep(1) |
| 338 | + self._get_ble_info() |
| 339 | + self._run_bluetoothctl() |
| 340 | + |
| 341 | + def stop(self): |
| 342 | + if self._btvirt: |
| 343 | + self._btvirt.terminate() |
| 344 | + self._btvirt.wait() |
| 345 | + |
| 346 | + |
172 | 347 | def PathsWithNetworkNamespaces(paths: ApplicationPaths) -> ApplicationPaths:
|
173 | 348 | """
|
174 | 349 | Returns a copy of paths with updated command arrays to invoke the
|
|
0 commit comments