pytest: add marker for sharing the download cache (#31082)

* fix cache

* with a marker
old-commit-hash: 3846130d8e
chrysler-long2
Justin Newberry 1 year ago committed by GitHub
parent b2983029d9
commit 4080bec11d
  1. 10
      common/prefix.py
  2. 7
      conftest.py
  3. 4
      selfdrive/car/tests/test_models.py
  4. 1
      selfdrive/locationd/test/test_locationd_scenarios.py
  5. 6
      system/hardware/hw.py

@ -6,12 +6,14 @@ from typing import Optional
from openpilot.common.params import Params from openpilot.common.params import Params
from openpilot.system.hardware.hw import Paths from openpilot.system.hardware.hw import Paths
from openpilot.system.hardware.hw import DEFAULT_DOWNLOAD_CACHE_ROOT
class OpenpilotPrefix: class OpenpilotPrefix:
def __init__(self, prefix: Optional[str] = None, clean_dirs_on_exit: bool = True): def __init__(self, prefix: Optional[str] = None, clean_dirs_on_exit: bool = True, shared_download_cache: bool = False):
self.prefix = prefix if prefix else str(uuid.uuid4().hex[0:15]) self.prefix = prefix if prefix else str(uuid.uuid4().hex[0:15])
self.msgq_path = os.path.join('/dev/shm', self.prefix) self.msgq_path = os.path.join('/dev/shm', self.prefix)
self.clean_dirs_on_exit = clean_dirs_on_exit self.clean_dirs_on_exit = clean_dirs_on_exit
self.shared_download_cache = shared_download_cache
def __enter__(self): def __enter__(self):
self.original_prefix = os.environ.get('OPENPILOT_PREFIX', None) self.original_prefix = os.environ.get('OPENPILOT_PREFIX', None)
@ -22,6 +24,9 @@ class OpenpilotPrefix:
pass pass
os.makedirs(Paths.log_root(), exist_ok=True) os.makedirs(Paths.log_root(), exist_ok=True)
if self.shared_download_cache:
os.environ["COMMA_CACHE"] = DEFAULT_DOWNLOAD_CACHE_ROOT
return self return self
def __exit__(self, exc_type, exc_obj, exc_tb): def __exit__(self, exc_type, exc_obj, exc_tb):
@ -42,5 +47,6 @@ class OpenpilotPrefix:
os.remove(symlink_path) os.remove(symlink_path)
shutil.rmtree(self.msgq_path, ignore_errors=True) shutil.rmtree(self.msgq_path, ignore_errors=True)
shutil.rmtree(Paths.log_root(), ignore_errors=True) shutil.rmtree(Paths.log_root(), ignore_errors=True)
shutil.rmtree(Paths.download_cache_root(), ignore_errors=True) if not os.environ.get("COMMA_CACHE", False):
shutil.rmtree(Paths.download_cache_root(), ignore_errors=True)
shutil.rmtree(Paths.comma_home(), ignore_errors=True) shutil.rmtree(Paths.comma_home(), ignore_errors=True)

@ -25,13 +25,13 @@ def pytest_runtest_call(item):
@pytest.fixture(scope="function", autouse=True) @pytest.fixture(scope="function", autouse=True)
def openpilot_function_fixture(): def openpilot_function_fixture(request):
starting_env = dict(os.environ) starting_env = dict(os.environ)
random.seed(0) random.seed(0)
# setup a clean environment for each test # setup a clean environment for each test
with OpenpilotPrefix(): with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix:
prefix = os.environ["OPENPILOT_PREFIX"] prefix = os.environ["OPENPILOT_PREFIX"]
yield yield
@ -77,3 +77,6 @@ def pytest_configure(config):
config_line = "nocapture: don't capture test output" config_line = "nocapture: don't capture test output"
config.addinivalue_line("markers", config_line) config.addinivalue_line("markers", config_line)
config_line = "shared_download_cache: share download cache between tests"
config.addinivalue_line("markers", config_line)

@ -22,6 +22,7 @@ from openpilot.selfdrive.car.honda.values import CAR as HONDA, HONDA_BOSCH
from openpilot.selfdrive.car.tests.routes import non_tested_cars, routes, CarTestRoute from openpilot.selfdrive.car.tests.routes import non_tested_cars, routes, CarTestRoute
from openpilot.selfdrive.controls.controlsd import Controls from openpilot.selfdrive.controls.controlsd import Controls
from openpilot.selfdrive.test.helpers import read_segment_list from openpilot.selfdrive.test.helpers import read_segment_list
from openpilot.system.hardware.hw import DEFAULT_DOWNLOAD_CACHE_ROOT
from openpilot.tools.lib.openpilotci import get_url from openpilot.tools.lib.openpilotci import get_url
from openpilot.tools.lib.logreader import LogReader from openpilot.tools.lib.logreader import LogReader
from openpilot.tools.lib.sanitizer import sanitize from openpilot.tools.lib.sanitizer import sanitize
@ -64,6 +65,7 @@ def get_test_cases() -> List[Tuple[str, Optional[CarTestRoute]]]:
@pytest.mark.slow @pytest.mark.slow
@pytest.mark.shared_download_cache
class TestCarModelBase(unittest.TestCase): class TestCarModelBase(unittest.TestCase):
car_model: Optional[str] = None car_model: Optional[str] = None
test_route: Optional[CarTestRoute] = None test_route: Optional[CarTestRoute] = None
@ -183,6 +185,8 @@ class TestCarModelBase(unittest.TestCase):
assert cls.CP assert cls.CP
assert cls.CP.carFingerprint == cls.car_model assert cls.CP.carFingerprint == cls.car_model
os.environ["COMMA_CACHE"] = DEFAULT_DOWNLOAD_CACHE_ROOT
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
del cls.can_msgs del cls.can_msgs

@ -97,6 +97,7 @@ def run_scenarios(scenario, logs):
@pytest.mark.xdist_group("test_locationd_scenarios") @pytest.mark.xdist_group("test_locationd_scenarios")
@pytest.mark.shared_download_cache
class TestLocationdScenarios(unittest.TestCase): class TestLocationdScenarios(unittest.TestCase):
""" """
Test locationd with different scenarios. In all these scenarios, we expect the following: Test locationd with different scenarios. In all these scenarios, we expect the following:

@ -3,6 +3,8 @@ from pathlib import Path
from openpilot.system.hardware import PC from openpilot.system.hardware import PC
DEFAULT_DOWNLOAD_CACHE_ROOT = "/tmp/comma_download_cache"
class Paths: class Paths:
@staticmethod @staticmethod
def comma_home() -> str: def comma_home() -> str:
@ -31,8 +33,8 @@ class Paths:
@staticmethod @staticmethod
def download_cache_root() -> str: def download_cache_root() -> str:
if os.environ.get('COMMA_CACHE', False): if os.environ.get('COMMA_CACHE', False):
return os.environ['COMMA_CACHE'] return os.environ['COMMA_CACHE'] + "/"
return "/tmp/comma_download_cache" + os.environ.get("OPENPILOT_PREFIX", "") + "/" return DEFAULT_DOWNLOAD_CACHE_ROOT + os.environ.get("OPENPILOT_PREFIX", "") + "/"
@staticmethod @staticmethod
def persist_root() -> str: def persist_root() -> str:

Loading…
Cancel
Save