From eed1fa2cb05c3d94bc3475837dcb0e7a47fc58a9 Mon Sep 17 00:00:00 2001 From: Lorow Date: Sun, 30 Nov 2025 01:02:32 +0100 Subject: [PATCH] Implement tests for changing device mode and MDNS name --- tests/.env.example | 2 +- tests/conftest.py | 6 ++-- tests/test_basic_functionality.py | 52 ++++++++++++++++++++++++++++++- tests/utils.py | 16 ++++++++++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/tests/.env.example b/tests/.env.example index 7d2acea..632a783 100644 --- a/tests/.env.example +++ b/tests/.env.example @@ -1,3 +1,3 @@ WIFI_SSID= WIFI_PASS= -SWITCH_MODE_REBOOT_TIME=10 \ No newline at end of file +SWITCH_MODE_REBOOT_TIME=5 \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 5dc04cb..542f734 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -114,9 +114,9 @@ def openiris_device_manager(board_connection, config): @pytest.fixture() -def get_openiris_device(openiris_device_manager): - def func(): - return openiris_device_manager.get_device() +def get_openiris_device(openiris_device_manager, config): + def func(port: str | None = None, _config: dict | None = None): + return openiris_device_manager.get_device(port, config or _config) return func diff --git a/tests/test_basic_functionality.py b/tests/test_basic_functionality.py index 677671a..c81a25b 100644 --- a/tests/test_basic_functionality.py +++ b/tests/test_basic_functionality.py @@ -1,4 +1,5 @@ -from tests.utils import has_command_failed +import time +from tests.utils import has_command_failed, DetectPortChange import pytest @@ -9,3 +10,52 @@ def test_ping_wired(get_openiris_device, ensure_board_in_mode): command_result = device.send_command("ping") assert not has_command_failed(command_result) + + +@pytest.mark.has_capability("wired") +def test_changing_mode_to_wired(get_openiris_device, ensure_board_in_mode): + device = get_openiris_device() + + # let's make sure we're in the wireless mode first, if we're going to try changing it + device = ensure_board_in_mode("wifi", device) + command_result = device.send_command("switch_mode", {"mode": "uvc"}) + assert not has_command_failed(command_result) + + # to avoid any issues, let's restart the board + with DetectPortChange() as port_selector: + device.send_command("restart_device") + time.sleep(3) + + # and since we've changed the ports + device = get_openiris_device(port_selector.get_new_port()) + # initial read to flush the logs first + device.send_command("get_device_mode") + result = device.send_command("get_device_mode") + assert not has_command_failed(result) + + +@pytest.mark.has_capability("wireless") +def test_setting_mdns_name(get_openiris_device, ensure_board_in_mode): + + def check_mdns_name(name: str): + command_result = device.send_command("get_mdns_name") + assert not has_command_failed(command_result) + assert command_result["results"][0]["result"]["data"]["hostname"] == name + + device = get_openiris_device() + device = ensure_board_in_mode("wifi", device) + first_name = "testname1" + second_name = "testname2" + # try setting the test mdns name first, just so we know the commands pass + command_result = device.send_command("set_mdns", {"hostname": first_name}) + assert not has_command_failed(command_result) + + check_mdns_name(first_name) + + command_result = device.send_command("set_mdns", {"hostname": second_name}) + assert not has_command_failed(command_result) + + device.send_command("restart_device") + # let the board boot, wait till it connects + time.sleep(3) + check_mdns_name(second_name) diff --git a/tests/utils.py b/tests/utils.py index b02c21d..b578b52 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -47,3 +47,19 @@ def get_current_ports() -> list[str]: def get_new_port(old_ports, new_ports) -> str: return list(set(new_ports) - set(old_ports))[0] + + +class DetectPortChange: + def __init__(self): + self.old_ports = [] + self.new_ports = [] + + def __enter__(self, *args, **kwargs): + self.old_ports = get_current_ports() + return self + + def __exit__(self, *args, **kwargs): + self.new_ports = get_current_ports() + + def get_new_port(self): + return get_new_port(self.old_ports, self.new_ports) \ No newline at end of file