From fd799bc05afccd758bc21c4278280c09c2580ab9 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 2 Feb 2024 17:18:01 -0500 Subject: [PATCH] Pytest: consistent hardware state for tici tests (#31279) * consistent hardware * consistent hardware * moved * this too * ruff * s * duplicated --------- Co-authored-by: Comma Device old-commit-hash: 0277fc5548bc87a17ae34b914df0593d034e5e94 --- Jenkinsfile | 2 +- conftest.py | 18 +++++++++++++++--- selfdrive/test/test_onroad.py | 8 +------- system/hardware/tici/tests/test_hardware.py | 7 +------ system/hardware/tici/tests/test_power_draw.py | 5 +---- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 816c2971c0..d3b3444991 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -208,7 +208,7 @@ node { deviceStage("tici", "tici-common", ["UNSAFE=1"], [ ["build", "cd selfdrive/manager && ./build.py"], ["test pandad", "pytest selfdrive/boardd/tests/test_pandad.py", ["panda/", "selfdrive/boardd/"]], - ["test power draw", "./system/hardware/tici/tests/test_power_draw.py"], + ["test power draw", "pytest -s system/hardware/tici/tests/test_power_draw.py"], ["test encoder", "LD_LIBRARY_PATH=/usr/local/lib pytest system/loggerd/tests/test_encoder.py"], ["test pigeond", "pytest system/sensord/tests/test_pigeond.py"], ["test manager", "pytest selfdrive/manager/test/test_manager.py"], diff --git a/conftest.py b/conftest.py index c4eb259a35..e215bb8b4c 100644 --- a/conftest.py +++ b/conftest.py @@ -4,7 +4,7 @@ import random from openpilot.common.prefix import OpenpilotPrefix from openpilot.selfdrive.manager import manager -from openpilot.system.hardware import TICI +from openpilot.system.hardware import TICI, HARDWARE def pytest_sessionstart(session): @@ -57,12 +57,24 @@ def openpilot_class_fixture(): os.environ.update(starting_env) +@pytest.fixture(scope="class") +def tici_setup_fixture(): + """Ensure a consistent state for tests on-device""" + HARDWARE.initialize_hardware() + HARDWARE.set_power_save(False) + os.system("pkill -9 -f athena") + os.system("rm /dev/shm/*") + + @pytest.hookimpl(tryfirst=True) def pytest_collection_modifyitems(config, items): skipper = pytest.mark.skip(reason="Skipping tici test on PC") for item in items: - if not TICI and "tici" in item.keywords: - item.add_marker(skipper) + if "tici" in item.keywords: + if not TICI: + item.add_marker(skipper) + else: + item.fixturenames.append('tici_setup_fixture') if "xdist_group_class_property" in item.keywords: class_property_name = item.get_closest_marker('xdist_group_class_property').args[0] diff --git a/selfdrive/test/test_onroad.py b/selfdrive/test/test_onroad.py index 335da73232..036eacfa48 100755 --- a/selfdrive/test/test_onroad.py +++ b/selfdrive/test/test_onroad.py @@ -112,17 +112,11 @@ class TestOnroad(unittest.TestCase): # setup env params = Params() - if "CI" in os.environ: - params.clear_all() params.remove("CurrentRoute") set_params_enabled() os.environ['TESTING_CLOSET'] = '1' if os.path.exists(Paths.log_root()): shutil.rmtree(Paths.log_root()) - os.system("rm /dev/shm/*") - - # Make sure athena isn't running - os.system("pkill -9 -f athena") # start manager and run openpilot for a minute proc = None @@ -429,4 +423,4 @@ class TestOnroad(unittest.TestCase): if __name__ == "__main__": - unittest.main() + pytest.main() diff --git a/system/hardware/tici/tests/test_hardware.py b/system/hardware/tici/tests/test_hardware.py index 4abc86107b..0c436595ee 100755 --- a/system/hardware/tici/tests/test_hardware.py +++ b/system/hardware/tici/tests/test_hardware.py @@ -12,11 +12,6 @@ HARDWARE = Tici() @pytest.mark.tici class TestHardware(unittest.TestCase): - @classmethod - def setUpClass(cls): - HARDWARE.initialize_hardware() - HARDWARE.set_power_save(False) - def test_power_save_time(self): ts = [] for _ in range(5): @@ -30,4 +25,4 @@ class TestHardware(unittest.TestCase): if __name__ == "__main__": - unittest.main() + pytest.main() diff --git a/system/hardware/tici/tests/test_power_draw.py b/system/hardware/tici/tests/test_power_draw.py index 0518eec543..db75f79af5 100755 --- a/system/hardware/tici/tests/test_power_draw.py +++ b/system/hardware/tici/tests/test_power_draw.py @@ -11,7 +11,6 @@ import cereal.messaging as messaging from cereal.services import SERVICE_LIST from openpilot.common.mock import mock_messages from openpilot.selfdrive.car.car_helpers import write_car_param -from openpilot.system.hardware import HARDWARE from openpilot.system.hardware.tici.power_monitor import get_power from openpilot.selfdrive.manager.process_config import managed_processes from openpilot.selfdrive.manager.manager import manager_cleanup @@ -41,8 +40,6 @@ PROCS = [ class TestPowerDraw(unittest.TestCase): def setUp(self): - HARDWARE.initialize_hardware() - HARDWARE.set_power_save(False) write_car_param() # wait a bit for power save to disable @@ -88,4 +85,4 @@ class TestPowerDraw(unittest.TestCase): if __name__ == "__main__": - unittest.main() + pytest.main()