2024-02-16 05:38:42 +08:00
|
|
|
import contextlib
|
2024-02-08 00:35:32 +08:00
|
|
|
import gc
|
2023-10-05 02:51:44 +08:00
|
|
|
import os
|
2023-09-06 09:52:40 +08:00
|
|
|
import pytest
|
2023-11-09 05:43:32 +08:00
|
|
|
import random
|
2023-09-06 09:52:40 +08:00
|
|
|
|
|
|
|
from openpilot.common.prefix import OpenpilotPrefix
|
2024-05-26 03:41:17 +08:00
|
|
|
from openpilot.system.manager import manager
|
2024-02-03 06:18:01 +08:00
|
|
|
from openpilot.system.hardware import TICI, HARDWARE
|
2023-09-06 09:52:40 +08:00
|
|
|
|
2024-10-04 14:39:13 +08:00
|
|
|
# TODO: pytest-cpp doesn't support FAIL, and we need to create test translations in sessionstart
|
|
|
|
# pending https://github.com/pytest-dev/pytest-cpp/pull/147
|
|
|
|
collect_ignore = [
|
|
|
|
"selfdrive/ui/tests/test_translations",
|
|
|
|
"selfdrive/test/process_replay/test_processes.py",
|
|
|
|
"selfdrive/test/process_replay/test_regen.py",
|
|
|
|
"selfdrive/test/test_time_to_onroad.py",
|
|
|
|
]
|
|
|
|
collect_ignore_glob = [
|
|
|
|
"selfdrive/debug/*.py",
|
|
|
|
"selfdrive/modeld/*.py",
|
|
|
|
]
|
|
|
|
|
2023-09-06 09:52:40 +08:00
|
|
|
|
2023-12-10 06:28:32 +08:00
|
|
|
def pytest_sessionstart(session):
|
|
|
|
# TODO: fix tests and enable test order randomization
|
|
|
|
if session.config.pluginmanager.hasplugin('randomly'):
|
|
|
|
session.config.option.randomly_reorganize = False
|
|
|
|
|
|
|
|
|
2023-12-19 17:18:54 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True, trylast=True)
|
|
|
|
def pytest_runtest_call(item):
|
|
|
|
# ensure we run as a hook after capturemanager's
|
|
|
|
if item.get_closest_marker("nocapture") is not None:
|
|
|
|
capmanager = item.config.pluginmanager.getplugin("capturemanager")
|
|
|
|
with capmanager.global_and_fixture_disabled():
|
|
|
|
yield
|
|
|
|
else:
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def clean_env():
|
2023-10-05 02:51:44 +08:00
|
|
|
starting_env = dict(os.environ)
|
2024-02-16 05:38:42 +08:00
|
|
|
yield
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(starting_env)
|
2023-10-05 02:51:44 +08:00
|
|
|
|
2023-11-09 05:43:32 +08:00
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
|
|
|
def openpilot_function_fixture(request):
|
|
|
|
random.seed(0)
|
2023-10-24 08:41:19 +08:00
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
with clean_env():
|
|
|
|
# setup a clean environment for each test
|
|
|
|
with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix:
|
|
|
|
prefix = os.environ["OPENPILOT_PREFIX"]
|
2023-10-05 02:51:44 +08:00
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
yield
|
2023-10-24 08:41:19 +08:00
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
# ensure the test doesn't change the prefix
|
|
|
|
assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"]
|
2023-10-05 02:51:44 +08:00
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
# cleanup any started processes
|
|
|
|
manager.manager_cleanup()
|
2023-10-05 02:51:44 +08:00
|
|
|
|
2024-02-16 05:38:42 +08:00
|
|
|
# some processes disable gc for performance, re-enable here
|
|
|
|
if not gc.isenabled():
|
|
|
|
gc.enable()
|
|
|
|
gc.collect()
|
2024-02-08 00:35:32 +08:00
|
|
|
|
2023-10-05 02:51:44 +08:00
|
|
|
# If you use setUpClass, the environment variables won't be cleared properly,
|
|
|
|
# so we need to hook both the function and class pytest fixtures
|
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def openpilot_class_fixture():
|
2024-02-16 05:38:42 +08:00
|
|
|
with clean_env():
|
|
|
|
yield
|
2023-11-08 09:35:44 +08:00
|
|
|
|
|
|
|
|
2024-02-09 05:32:40 +08:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def tici_setup_fixture(openpilot_function_fixture):
|
|
|
|
"""Ensure a consistent state for tests on-device. Needs the openpilot function fixture to run first."""
|
2024-02-03 06:18:01 +08:00
|
|
|
HARDWARE.initialize_hardware()
|
|
|
|
HARDWARE.set_power_save(False)
|
|
|
|
os.system("pkill -9 -f athena")
|
|
|
|
|
|
|
|
|
2023-11-15 09:33:47 +08:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
2023-11-08 09:35:44 +08:00
|
|
|
def pytest_collection_modifyitems(config, items):
|
|
|
|
skipper = pytest.mark.skip(reason="Skipping tici test on PC")
|
|
|
|
for item in items:
|
2024-02-03 06:18:01 +08:00
|
|
|
if "tici" in item.keywords:
|
|
|
|
if not TICI:
|
|
|
|
item.add_marker(skipper)
|
|
|
|
else:
|
|
|
|
item.fixturenames.append('tici_setup_fixture')
|
2023-11-15 09:33:47 +08:00
|
|
|
|
|
|
|
if "xdist_group_class_property" in item.keywords:
|
2023-12-07 08:00:59 +08:00
|
|
|
class_property_name = item.get_closest_marker('xdist_group_class_property').args[0]
|
|
|
|
class_property_value = getattr(item.cls, class_property_name)
|
|
|
|
item.add_marker(pytest.mark.xdist_group(class_property_value))
|
2023-11-15 09:33:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.hookimpl(trylast=True)
|
|
|
|
def pytest_configure(config):
|
2023-12-19 17:18:54 +08:00
|
|
|
config_line = "xdist_group_class_property: group tests by a property of the class that contains them"
|
|
|
|
config.addinivalue_line("markers", config_line)
|
|
|
|
|
|
|
|
config_line = "nocapture: don't capture test output"
|
2023-12-10 05:24:18 +08:00
|
|
|
config.addinivalue_line("markers", config_line)
|
2024-01-23 08:30:59 +08:00
|
|
|
|
|
|
|
config_line = "shared_download_cache: share download cache between tests"
|
|
|
|
config.addinivalue_line("markers", config_line)
|