Verified Commit 66981910 authored by Karel Koci's avatar Karel Koci 🤘 Committed by Karel Koci
Browse files

WIP

parent ac47a771
......@@ -13,6 +13,7 @@ __pycache__
## Configuration
targets.ini
wifi.ini
## Logs
*.log
......
#!/bin/bash
# nsfarm:client
##################################################################################
# Common image for clients on LAN
##################################################################################
set -e
wait4network
# Add openssh client to access router shell
apk add wpa_supplicant wpa_supplicant-openrc
# Configure LAN1 interface for static local network
cat >> /etc/network/interfaces <<EOF
auto wifi
iface lan inet dhcp
EOF
"""NSFarm configuration classes.
"""NSFarm target configuration classes.
"""
import pathlib
import configparser
......@@ -7,8 +7,8 @@ import typing
import collections.abc
TARGET_CONFS = (
"/etc/nsfarm_targets.ini",
"~/.nsfarm_targets.ini",
"/etc/nsfarm/targets.ini",
"~/.nsfarm/targets.ini",
"./targets.ini"
)
......
"""NSFarm wifi access and configuration.
"""
import pathlib
import configparser
import typing
import collections.abc
from .lxd.device import NetInterface as _LXD_NetInterface
WIFI_CONFS = (
"/etc/nsfarm/wifi.ini",
"~/.nsfarm/wifi.ini",
"./wifi.ini"
)
class Wifi:
"""Wifi configuraiton handler.
"""
def __init__(self, interface, conf):
self._interface = interface
self._conf = conf
def present(self) -> bool:
"""Verify if target is present on system. This means if resources it specifies are all available.
"""
return True
return (pathlib.Path("/sys/class/net") / self._interface).exists()
def lxd_netinterface(self, name="wifi"):
"""Returns instance of lxd.device.NetInterface.
"""
return _LXD_NetInterface(name, self._interface)
@property
def name(self):
"""WiFi interface name.
"""
return self._interface
@property
def band24(self):
"""If WiFi support 2.4GHz
"""
return self._conf.getboolean("band24", fallback=True)
@property
def band5(self):
"""If WiFi support 5GHz
"""
return self._conf.getboolean("band5", fallback=False)
@property
def ap_mode(self):
"""If WiFi support access point mode.
"""
return self._conf.getboolean("ap", fallback=False)
def __str__(self):
return self._interface
class WifiDevices(collections.abc.Mapping):
"""All available Wifi devices.
"""
def __init__(self, rootdir: str = "."):
self._rootdir = pathlib.Path(rootdir)
self._conf = configparser.ConfigParser()
# Load configuration
for file in WIFI_CONFS:
path = pathlib.Path(file).expanduser()
if not path.is_absolute():
path = self._rootdir / path
self._conf.read(path)
def filter(self, band24: typing.Optional[bool] = None, band5: typing.Optional[bool] = None):
"""Method for selection of available and valid target with given parameters.
Returns generator of available targets.
"""
for wifi in self:
instance = self[wifi]
if not instance.present():
continue
if band24 is not None and band24 != instance.band24:
continue
if band5 is not None and band5 != instance.band5:
continue
yield wifi
def __getitem__(self, key) -> Wifi:
return Wifi(key, self._conf[key])
def __iter__(self):
for target in self._conf:
if target == "DEFAULT":
continue
yield target
def __len__(self):
return len(self._conf)
......@@ -3,8 +3,8 @@
#
# NSFarm collects all targets from multiple locations and it does that in
# following order:
# /etc/nsfarm_targets.ini
# ~/.nsfarm_targets.ini
# /etc/nsfarm/targets.ini
# ~/.nsfarm/targets.ini
# $NSFARM_ROOT/targets.ini
# $-C_OPTION
# Where NSFARM_ROOT is root directory of NSFarm project and -C_OPTION is path
......
......@@ -8,6 +8,7 @@ import nsfarm.board
import nsfarm.cli
import nsfarm.lxd
import nsfarm.target
import nsfarm.wifi
def pytest_addoption(parser):
......@@ -88,8 +89,8 @@ def fixture_board(request):
"""
brd = nsfarm.board.get_board(request.config.target_config)
brd.power(True)
request.addfinalizer(lambda: brd.power(False))
return brd
yield brd
brd.power(False)
@pytest.fixture(scope="session", name="lxd")
......@@ -113,6 +114,28 @@ def fixture_lan1(request):
return nsfarm.lxd.NetInterface("lan", request.config.target_config.lan1)
########################################################################################################################
# Dynamic fixtures #####################################################################################################
def _fixture_wifi(metafunc, devices, param, **kwargs):
if param not in metafunc.fixturenames:
return
dev_list = [devices[dev] for dev in devices.filter(**kwargs)]
if dev_list:
metafunc.parametrize(param, dev_list)
else:
metafunc.parametrize(param, [pytest.param("no-wifi", marks=pytest.mark.skip)])
def pytest_generate_tests(metafunc):
devices = nsfarm.wifi.WifiDevices()
_fixture_wifi(metafunc, devices, "wifi")
_fixture_wifi(metafunc, devices, "wifi24", band24=True)
_fixture_wifi(metafunc, devices, "wifi5", band5=True)
_fixture_wifi(metafunc, devices, "wifi24_ap", band24=True, ap=True)
_fixture_wifi(metafunc, devices, "wifi5_ap", band5=True, ap=True)
########################################################################################################################
# Boot and setup fixtures ##############################################################################################
......
# This is example configuration file for NSFarm Wi-Fi devices.
# Sections in this file define separate Wi-Fi adapters available for testing.
#
# NSFarm collects all Wi-Fi devices from multiple locations and it does that in
# following order:
# /etc/nsfarm/wifi.ini
# ~/.nsfarm/wifi.ini
# $NSFARM_ROOT/wifi.ini
# Where NSFARM_ROOT is root directory of NSFarm project.
# Note that options introduced later overwrite previous ones.
# This is example configuration for Wi-Fi device with network interface wlp1s0.
# Section name is name of network interface associated with Wi-Fi device.
[wlp1s0]
# This is boolean value specifying that Wi-Fi device supports 2.4GHz band. In
# default if not specified the support is expected.
band24 = true
# This is boolean value specifying that Wi-Fi device supports 5GHz band. In
# default if not specified the support is not expected.
band5 = true
# This is boolean value specifying that Wi-Fi is capable of Access Point mode thus
# can be used not only as client. In default if not specified the AP capability is
# not expected.
ap = false
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment