Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
d12e5a18fThis file is pinned to the selected release. Download the original to open it in your design tool.
"""Tests for setup script logic — IP validation, CONTROLLER.ini generation, multi-OWL config.
Priority: Field deployment — validates that setup scripts produce correct configs
for multi-OWL scenarios (2+ OWL units on one networked controller).
"""
import configparser
import os
import re
import pytest
# ---------------------------------------------------------------------------
# IP validation (Python port of setup.sh _validate_ip)
# ---------------------------------------------------------------------------
def _validate_ip(ip):
"""Python equivalent of setup.sh _validate_ip."""
if not re.match(r'^(\d{1,3}\.){3}\d{1,3}$', ip):
return False
octets = ip.split('.')
for o in octets:
if int(o) < 0 or int(o) > 255:
return False
return True
def _ip_base(ip):
"""Python equivalent of setup.sh _ip_base (first three octets)."""
parts = ip.split('.')
return '.'.join(parts[:3])
def _last_octet(ip):
"""Python equivalent of setup.sh _last_octet."""
return ip.split('.')[-1]
@pytest.mark.unit
class TestIPValidation:
"""Tests for _validate_ip logic used in both setup scripts."""
def test_valid_ip(self):
assert _validate_ip('192.168.1.1') is True
def test_valid_ip_zeros(self):
assert _validate_ip('0.0.0.0') is True
def test_valid_ip_255(self):
assert _validate_ip('255.255.255.255') is True
def test_invalid_octet_over_255(self):
assert _validate_ip('256.1.1.1') is False
def test_invalid_too_few_octets(self):
assert _validate_ip('192.168.1') is False
def test_invalid_too_many_octets(self):
assert _validate_ip('192.168.1.1.1') is False
def test_invalid_alpha(self):
assert _validate_ip('abc.def.ghi.jkl') is False
def test_invalid_empty(self):
assert _validate_ip('') is False
def test_invalid_negative(self):
# Regex won't match dash, so this returns False
assert _validate_ip('-1.0.0.0') is False
def test_ip_base_extraction(self):
assert _ip_base('192.168.1.42') == '192.168.1'
def test_last_octet_extraction(self):
assert _last_octet('192.168.1.42') == '42'
def test_subnet_consistency_same_base(self):
"""OWL, gateway, and controller must share the same /24 subnet."""
owl_ip = '192.168.1.11'
gateway = '192.168.1.1'
controller = '192.168.1.2'
assert _ip_base(owl_ip) == _ip_base(gateway)
assert _ip_base(owl_ip) == _ip_base(controller)
def test_subnet_mismatch_detected(self):
"""Different subnets should be caught."""
owl_ip = '192.168.1.11'
controller = '10.0.0.2'
assert _ip_base(owl_ip) != _ip_base(controller)
def test_reserved_octets_detected(self):
"""Host octets .0 and .255 should be flagged as reserved."""
assert _last_octet('192.168.1.0') == '0'
assert _last_octet('192.168.1.255') == '255'
# ---------------------------------------------------------------------------
# CONTROLLER.ini generation validation
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestControllerINIGeneration:
"""Tests for CONTROLLER.ini content as generated by setup scripts."""
def _write_standalone_ini(self, path, hostname='owl-1'):
"""Simulate standalone mode CONTROLLER.ini from setup.sh."""
content = f"""[MQTT]
enable = True
broker_ip = localhost
broker_port = 1883
device_id = {hostname}
[WebDashboard]
port = 8000
[Network]
mode = standalone
static_ip = 10.42.0.1
controller_ip = localhost
[GPS]
source = none
port = /dev/ttyUSB0
baudrate = 9600
enable = False
nmea_port = 8500
boom_width = 12.0
track_save_directory = tracks
[Actuation]
actuation_duration = 0.15
delay = 0.0
actuation_length_cm = 10
offset_cm = 30
speed_avg_window = 5.0
"""
with open(path, 'w') as f:
f.write(content)
def _write_networked_ini(self, path, hostname='owl-2', broker_ip='192.168.1.2',
static_ip='192.168.1.12'):
"""Simulate networked mode CONTROLLER.ini from setup.sh."""
content = f"""[MQTT]
enable = True
broker_ip = {broker_ip}
broker_port = 1883
device_id = {hostname}
[WebDashboard]
port = 8000
[Network]
mode = networked
static_ip = {static_ip}
controller_ip = {broker_ip}
[GPS]
source = none
port = /dev/ttyUSB0
baudrate = 9600
enable = False
nmea_port = 8500
boom_width = 12.0
track_save_directory = tracks
[Actuation]
actuation_duration = 0.15
delay = 0.0
actuation_length_cm = 10
offset_cm = 30
speed_avg_window = 5.0
"""
with open(path, 'w') as f:
f.write(content)
def _write_controller_ini(self, path, static_ip='192.168.1.2', gps_enable=False,
boom_width='12.0'):
"""Simulate in-cab controller CONTROLLER.ini from in-cab_controller_setup.sh."""
content = f"""[MQTT]
enable = True
broker_ip = localhost
broker_port = 1883
device_id = auto
[WebDashboard]
port = 8000
[Network]
mode = networked
static_ip = {static_ip}
controller_ip = localhost
[GPS]
source = none
port = /dev/ttyUSB0
baudrate = 9600
enable = {'True' if gps_enable else 'False'}
nmea_port = 8500
boom_width = {boom_width}
track_save_directory = tracks
[Actuation]
actuation_duration = 0.15
delay = 0.0
actuation_length_cm = 10
offset_cm = 30
speed_avg_window = 5.0
"""
with open(path, 'w') as f:
f.write(content)
def test_standalone_ini_has_localhost_broker(self, tmp_path):
ini_path = tmp_path / 'CONTROLLER.ini'
self._write_standalone_ini(str(ini_path))
config = configparser.ConfigParser()
config.read(str(ini_path))
assert config.get('MQTT', 'broker_ip') == 'localhost'
assert config.get('Network', 'mode') == 'standalone'
def test_networked_ini_points_to_controller(self, tmp_path):
ini_path = tmp_path / 'CONTROLLER.ini'
self._write_networked_ini(str(ini_path), broker_ip='192.168.1.2')
config = configparser.ConfigParser()
config.read(str(ini_path))
assert config.get('MQTT', 'broker_ip') == '192.168.1.2'
assert config.get('Network', 'mode') == 'networked'
assert config.get('Network', 'controller_ip') == '192.168.1.2'
def test_each_owl_has_unique_device_id(self, tmp_path):
"""Multi-OWL: each unit must have a unique device_id."""
owl1 = tmp_path / 'owl1.ini'
owl2 = tmp_path / 'owl2.ini'
self._write_networked_ini(str(owl1), hostname='owl-1', static_ip='192.168.1.11')
self._write_networked_ini(str(owl2), hostname='owl-2', static_ip='192.168.1.12')
c1 = configparser.ConfigParser()
c1.read(str(owl1))
c2 = configparser.ConfigParser()
c2.read(str(owl2))
assert c1.get('MQTT', 'device_id') == 'owl-1'
assert c2.get('MQTT', 'device_id') == 'owl-2'
assert c1.get('MQTT', 'device_id') != c2.get('MQTT', 'device_id')
def test_both_owls_point_to_same_broker(self, tmp_path):
"""Multi-OWL: both units should point to the same controller IP."""
owl1 = tmp_path / 'owl1.ini'
owl2 = tmp_path / 'owl2.ini'
self._write_networked_ini(str(owl1), hostname='owl-1', broker_ip='192.168.1.2')
self._write_networked_ini(str(owl2), hostname='owl-2', broker_ip='192.168.1.2')
c1 = configparser.ConfigParser()
c1.read(str(owl1))
c2 = configparser.ConfigParser()
c2.read(str(owl2))
assert c1.get('MQTT', 'broker_ip') == c2.get('MQTT', 'broker_ip')
def test_controller_ini_broker_is_localhost(self, tmp_path):
"""The in-cab controller IS the broker, so broker_ip = localhost."""
ini_path = tmp_path / 'CONTROLLER.ini'
self._write_controller_ini(str(ini_path))
config = configparser.ConfigParser()
config.read(str(ini_path))
assert config.get('MQTT', 'broker_ip') == 'localhost'
assert config.get('MQTT', 'device_id') == 'auto'
def test_controller_ini_has_all_required_sections(self, tmp_path):
"""CONTROLLER.ini must have all sections that networked.py expects."""
ini_path = tmp_path / 'CONTROLLER.ini'
self._write_controller_ini(str(ini_path))
config = configparser.ConfigParser()
config.read(str(ini_path))
required = ['MQTT', 'WebDashboard', 'Network', 'GPS', 'Actuation']
for section in required:
assert config.has_section(section), f"Missing section: {section}"
def test_gps_enabled_when_requested(self, tmp_path):
ini_path = tmp_path / 'CONTROLLER.ini'
self._write_controller_ini(str(ini_path), gps_enable=True, boom_width='18.0')
config = configparser.ConfigParser()
config.read(str(ini_path))
assert config.get('GPS', 'enable') == 'True'
assert config.get('GPS', 'boom_width') == '18.0'
def test_actuation_defaults_present(self, tmp_path):
"""Actuation section must have sane defaults."""
ini_path = tmp_path / 'CONTROLLER.ini'
self._write_controller_ini(str(ini_path))
config = configparser.ConfigParser()
config.read(str(ini_path))
assert config.getfloat('Actuation', 'actuation_duration') == 0.15
assert config.getfloat('Actuation', 'delay') == 0.0
assert config.getint('Actuation', 'actuation_length_cm') == 10
# ---------------------------------------------------------------------------
# Multi-OWL MQTT topic routing
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestMultiOWLTopicRouting:
"""Validate MQTT topic patterns match between setup and runtime."""
def test_owl_state_topic_includes_device_id(self):
"""OWL publishes to owl/{device_id}/state — controller subscribes to owl/+/state."""
device_id = 'owl-1'
topic = f'owl/{device_id}/state'
assert topic == 'owl/owl-1/state'
def test_owl_command_topic_includes_device_id(self):
"""Controller sends to owl/{device_id}/commands."""
device_id = 'owl-2'
topic = f'owl/{device_id}/commands'
assert topic == 'owl/owl-2/commands'
def test_two_owls_have_separate_topics(self):
"""Two OWLs must NOT share the same command topic."""
t1 = f'owl/owl-1/commands'
t2 = f'owl/owl-2/commands'
assert t1 != t2
def test_wildcard_subscription_matches_all_owls(self):
"""Controller subscribes to 'owl/+/state' which should match all OWLs."""
import re
wildcard = r'^owl/[^/]+/state$'
assert re.match(wildcard, 'owl/owl-1/state')
assert re.match(wildcard, 'owl/owl-2/state')
assert re.match(wildcard, 'owl/owl-99/state')
assert not re.match(wildcard, 'owl/state') # standalone topic
def test_topic_parsing_extracts_device_id(self):
"""_on_message parses topic_parts[1] as device_id."""
topic = 'owl/owl-pi-3/state'
parts = topic.split('/')
assert len(parts) == 3
assert parts[1] == 'owl-pi-3'
assert parts[2] == 'state'
# ---------------------------------------------------------------------------
# Nginx config validation
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestNginxConfig:
"""Validate nginx config patterns for multi-OWL video feed proxying."""
def test_standalone_nginx_proxies_dashboard_and_video(self):
"""Standalone nginx must proxy both / (dashboard) and /video_feed."""
# These are the proxy_pass targets from setup.sh standalone config
dashboard_proxy = 'http://127.0.0.1:8000'
video_proxy = 'http://127.0.0.1:8001/stream.mjpg'
assert '8000' in dashboard_proxy
assert '8001' in video_proxy
def test_networked_owl_nginx_only_serves_video(self):
"""Networked OWL nginx must NOT proxy a dashboard — video only."""
# From setup.sh networked nginx config:
# location = / returns static HTML (no proxy)
# location /video_feed proxies to 8001
# No location / with proxy_pass to 8000
pass # Structural validation — checked by reading setup.sh
def test_controller_nginx_has_client_max_body_size(self):
"""In-cab controller nginx must allow large model uploads."""
# From in-cab_controller_setup.sh line 498:
# client_max_body_size 250m
max_body = '250m'
assert max_body == '250m'
def test_controller_nginx_proxies_video_per_owl(self):
"""Controller nginx supports /video_feed/{device_id} for per-OWL streams."""
# From in-cab_controller_setup.sh nginx config:
# location ~ ^/video_feed/(.+)$ { proxy_pass ... }
# This allows the controller UI to show video from individual OWLs
pattern = r'^/video_feed/(.+)$'
assert re.match(pattern, '/video_feed/owl-1')
assert re.match(pattern, '/video_feed/owl-2')
assert not re.match(pattern, '/video_feed/') # empty device
# ---------------------------------------------------------------------------
# Full multi-OWL deployment scenario
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestMultiOWLDeploymentScenario:
"""End-to-end validation of a 2-OWL deployment configuration."""
def test_two_owl_setup_produces_valid_configs(self, tmp_path):
"""Simulate setting up 2 OWLs and a controller, verify consistency."""
# Controller
ctrl_ini = tmp_path / 'controller.ini'
ctrl_content = """[MQTT]
enable = True
broker_ip = localhost
broker_port = 1883
device_id = auto
[Network]
mode = networked
static_ip = 192.168.1.2
controller_ip = localhost
"""
ctrl_ini.write_text(ctrl_content)
# OWL 1
owl1_ini = tmp_path / 'owl1.ini'
owl1_content = """[MQTT]
enable = True
broker_ip = 192.168.1.2
broker_port = 1883
device_id = owl-1
[Network]
mode = networked
static_ip = 192.168.1.11
controller_ip = 192.168.1.2
"""
owl1_ini.write_text(owl1_content)
# OWL 2
owl2_ini = tmp_path / 'owl2.ini'
owl2_content = """[MQTT]
enable = True
broker_ip = 192.168.1.2
broker_port = 1883
device_id = owl-2
[Network]
mode = networked
static_ip = 192.168.1.12
controller_ip = 192.168.1.2
"""
owl2_ini.write_text(owl2_content)
# Parse all three
ctrl_cfg = configparser.ConfigParser()
ctrl_cfg.read(str(ctrl_ini))
owl1_cfg = configparser.ConfigParser()
owl1_cfg.read(str(owl1_ini))
owl2_cfg = configparser.ConfigParser()
owl2_cfg.read(str(owl2_ini))
# Verify: all MQTT enabled
assert ctrl_cfg.get('MQTT', 'enable') == 'True'
assert owl1_cfg.get('MQTT', 'enable') == 'True'
assert owl2_cfg.get('MQTT', 'enable') == 'True'
# Verify: both OWLs point to controller's static IP as broker
ctrl_ip = ctrl_cfg.get('Network', 'static_ip')
assert owl1_cfg.get('MQTT', 'broker_ip') == ctrl_ip
assert owl2_cfg.get('MQTT', 'broker_ip') == ctrl_ip
# Verify: unique device IDs
ids = {
owl1_cfg.get('MQTT', 'device_id'),
owl2_cfg.get('MQTT', 'device_id'),
}
assert len(ids) == 2 # both unique
# Verify: all on same subnet
ips = [
ctrl_cfg.get('Network', 'static_ip'),
owl1_cfg.get('Network', 'static_ip'),
owl2_cfg.get('Network', 'static_ip'),
]
bases = [_ip_base(ip) for ip in ips]
assert all(b == bases[0] for b in bases), f"Subnet mismatch: {bases}"
# Verify: same MQTT port
assert owl1_cfg.get('MQTT', 'broker_port') == '1883'
assert owl2_cfg.get('MQTT', 'broker_port') == '1883'
# ---------------------------------------------------------------------------
# Dependency single-source-of-truth — setup scripts must install from
# requirements files, never inline package lists (a package baked into a
# setup script is invisible to owl_update.sh and breaks remote updates)
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestRequirementsSingleSource:
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
def _pip_install_lines(self, script_relpath):
path = os.path.join(self.PROJECT_ROOT, script_relpath)
with open(path, encoding='utf-8') as f:
return [line for line in f
if 'pip install' in line and not line.strip().startswith('#')]
@pytest.mark.parametrize('script', [
'owl_setup.sh',
'controller/networked/in-cab_controller_setup.sh',
])
def test_setup_scripts_install_from_requirements_files(self, script):
offenders = []
for line in self._pip_install_lines(script):
# Allowed: pip install -r <file>, pip install --upgrade pip,
# version-pinned numpy workaround, opencv (installed separately
# by design — see requirements.txt header)
if re.search(r'pip install\s+(--no-input\s+)?(--upgrade pip|-r\s|"numpy|opencv)', line):
continue
offenders.append(line.strip())
assert not offenders, (
f"{script} installs packages outside requirements files "
f"(invisible to owl_update.sh): {offenders}"
)
def test_controller_requirements_file_exists(self):
path = os.path.join(self.PROJECT_ROOT, 'requirements-controller.txt')
assert os.path.exists(path), "requirements-controller.txt missing"
with open(path, encoding='utf-8') as f:
content = f.read()
# The controller stack and the agent dep must be pinned here
for pkg in ('flask', 'gunicorn', 'paho-mqtt', 'httpx'):
assert pkg in content, f"requirements-controller.txt missing {pkg}"
def test_owl_requirements_include_runtime_deps(self):
"""paho-mqtt etc. were previously only in owl_setup.sh — they must
stay in requirements.txt so remote updates can install them."""
path = os.path.join(self.PROJECT_ROOT, 'requirements.txt')
with open(path, encoding='utf-8') as f:
content = f.read()
for pkg in ('flask', 'gunicorn', 'paho-mqtt', 'psutil', 'boto3', 'pyserial'):
assert pkg in content, f"requirements.txt missing {pkg}"
# ---------------------------------------------------------------------------
# Software-update sudoers rule — the dashboard's /api/settings/update/start
# launches owl_update.sh via systemd-run; the argv is built by
# utils/software_update.update_argv and must match the rule literally.
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestUpdateSudoersRule:
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
def _read(self, relpath):
with open(os.path.join(self.PROJECT_ROOT, relpath), encoding='utf-8') as f:
return f.read()
def test_shared_setup_installs_the_update_rule(self):
content = self._read('controller/shared/setup.sh')
assert 'setup_update_permissions()' in content
assert '/etc/sudoers.d/96-owl-update' in content
# Called in the permissions block, after device settings
assert re.search(r'setup_device_settings_permissions\s*\nsetup_update_permissions',
content), 'setup_update_permissions is not called'
# Validated before install (a bad sudoers file locks sudo out)
assert 'visudo -cf' in content
def test_rule_matches_the_shared_argv(self):
"""Literal up to the script, then a single glob for its own args —
the same shape owl_cloud_provision.sh installs."""
from utils import software_update as swu
content = self._read('controller/shared/setup.sh')
match = re.search(r'Cmnd_Alias OWL_UPDATE_RUN = (.+)', content)
assert match, 'OWL_UPDATE_RUN alias missing'
rule = match.group(1)
expected_tail = (f'--unit={swu.UNIT_NAME} --collect '
f'--property=RuntimeMaxSec={swu.RUNTIME_MAX_S} '
'--uid=${CURRENT_USER} --gid=${CURRENT_USER} '
'/bin/bash ${REPO_DIR}/owl_update.sh *')
assert rule.endswith(expected_tail), rule
cloud = self._read('owl_cloud_provision.sh')
assert expected_tail in cloud, 'cloud provisioning rule drifted'
def test_dashboard_restart_is_permitted(self):
content = self._read('controller/shared/setup.sh')
assert 'restart owl-dash.service' in content
# ---------------------------------------------------------------------------
# Software-update sudoers rule: the dashboard's /api/settings/update/start
# launches owl_update.sh via systemd-run; the argv is built by
# utils/software_update.update_argv and must match the rule literally.
# ---------------------------------------------------------------------------
@pytest.mark.unit
class TestUpdateSudoersRule:
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
def _read(self, relpath):
with open(os.path.join(self.PROJECT_ROOT, relpath), encoding='utf-8') as f:
return f.read()
def test_shared_setup_installs_the_update_rule(self):
content = self._read('controller/shared/setup.sh')
assert 'setup_update_permissions()' in content
assert '/etc/sudoers.d/96-owl-update' in content
assert re.search(r'setup_device_settings_permissions\s*\nsetup_update_permissions',
content), 'setup_update_permissions is not called'
# Validated before install (a bad sudoers file locks sudo out)
assert 'visudo -cf' in content
def test_rule_matches_the_shared_argv(self):
"""Literal up to the script, then a single glob for its own args,
the same shape owl_cloud_provision.sh installs."""
from utils import software_update as swu
content = self._read('controller/shared/setup.sh')
match = re.search(r'Cmnd_Alias OWL_UPDATE_RUN = (.+)', content)
assert match, 'OWL_UPDATE_RUN alias missing'
rule = match.group(1)
expected_tail = ('--unit=%s --collect --property=RuntimeMaxSec=%d '
'--uid=${CURRENT_USER} --gid=${CURRENT_USER} '
'/bin/bash ${REPO_DIR}/owl_update.sh *'
% (swu.UNIT_NAME, swu.RUNTIME_MAX_S))
assert rule.endswith(expected_tail), rule
cloud = self._read('owl_cloud_provision.sh')
assert expected_tail in cloud, 'cloud provisioning rule drifted'
def test_dashboard_restart_is_permitted(self):
content = self._read('controller/shared/setup.sh')
assert 'restart owl-dash.service' in content