From 516340473e3988eea2d192d5de0a9370192d7f18 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Thu, 17 Oct 2024 14:22:51 +0200 Subject: [PATCH] tests/py: Enable umockdev based tests We re-exec the test case in pytest_configure if the LD_PRELOAD doesn't contain libumockdev-preload.so. Even if the system doesn't have it, it will show up in the environment and we can continue. It also sets the UMOCKDEV_DIR env for the portal frontend. In theory dbusmock should already do this for us but it doesn't seem to be working. --- tests/__init__.py | 7 ++++++- tests/conftest.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/__init__.py b/tests/__init__.py index e0c9c338c..33817bdb0 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -336,7 +336,8 @@ def __init__( self, dbus_test_case, portal_name: str, - app_id: str = "org.example.App" + app_id: str = "org.example.App", + umockdev = None, ): self.dbus_test_case = dbus_test_case self.portal_name = portal_name @@ -346,6 +347,7 @@ def __init__( self.portal_interfaces: Dict[str, dbus.Interface] = {} self.app_id = app_id self.busses = {dbusmock.BusType.SYSTEM: {}, dbusmock.BusType.SESSION: {}} + self.umockdev = umockdev @property def interface_name(self) -> str: @@ -443,6 +445,9 @@ def start_xdp(self): env["XDG_CURRENT_DESKTOP"] = "test" env["XDG_DESKTOP_PORTAL_TEST_APP_ID"] = self.app_id + if self.umockdev: + env["UMOCKDEV_DIR"] = self.umockdev.get_root_dir() + self.start_dbus_monitor() self.start_portal_frontend(env) self.start_permission_store(env) diff --git a/tests/conftest.py b/tests/conftest.py index b9f0728ba..37e361762 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,15 +7,25 @@ import pytest import dbusmock import os +import sys import tempfile from tests import PortalMock def pytest_configure(): + ensure_umockdev_loaded() create_test_dirs() +def ensure_umockdev_loaded(): + umockdev_preload = "libumockdev-preload.so" + preload = os.environ.get("LD_PRELOAD", "") + if umockdev_preload not in preload: + os.environ["LD_PRELOAD"] = f"{umockdev_preload}:{preload}" + os.execv(sys.executable, [sys.executable] + sys.argv) + + def create_test_dirs(): test_root = tempfile.TemporaryDirectory( prefix='xdp-testroot-', @@ -124,6 +134,14 @@ def app_id(): return "org.example.App" +@pytest.fixture +def umockdev(): + """ + Default fixture providing a umockdev testbed + """ + return None + + @pytest.fixture def portal_mock( dbus_test_case, @@ -131,6 +149,7 @@ def portal_mock( required_templates, template_params, app_id, + umockdev, ) -> PortalMock: """ Fixture yielding a PortalMock object with the impl started, if applicable. @@ -139,6 +158,7 @@ def portal_mock( dbus_test_case, portal_name, app_id=app_id, + umockdev=umockdev, ) for template, params in required_templates.items():