From 82dfeaa4e5e51dea46c691d3331f61e34e3ff88d Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Thu, 5 Dec 2024 18:16:20 +0100 Subject: [PATCH] tests/py: Add helpers for figuring out if tests run in CI or containers Which is useful for skipping test with the @pytest.mark.skipif decorator. --- tests/__init__.py | 12 ++++++++++++ tests/test_usb.py | 12 +----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index e0831299f..2f12b44ac 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -9,6 +9,7 @@ from itertools import count from typing import Any, Dict, Optional, NamedTuple, Callable +import os import dbus import dbus.proxies import dbusmock @@ -27,6 +28,17 @@ logger = logging.getLogger("tests") +def is_in_ci() -> bool: + return os.environ.get("XDP_TEST_IN_CI") is not None + + +def is_in_container() -> bool: + return is_in_ci() or ( + "container" in os.environ + and (os.environ["container"] == "docker" or os.environ["container"] == "podman") + ) + + def wait(ms: int): """ Waits for the specified amount of milliseconds. diff --git a/tests/test_usb.py b/tests/test_usb.py index d6f930bc1..a0d4f45e9 100644 --- a/tests/test_usb.py +++ b/tests/test_usb.py @@ -188,17 +188,7 @@ def cb_device_events(session_handle, events): ) @pytest.mark.parametrize("usb_queries", ["vnd:04a9", None]) - @pytest.mark.skipif( - ("GITHUB_ACTIONS" in os.environ and os.environ["GITHUB_ACTIONS"] == "true") - or ( - "container" in os.environ - and ( - os.environ["container"] == "docker" - or os.environ["container"] == "podman" - ) - ), - reason="Test fail in containers", - ) + @pytest.mark.skipif(xdp.is_in_container(), reason="Test fail in containers") def test_device_remove(self, portals, dbus_con, app_id, usb_queries, umockdev): usb_intf = xdp.get_portal_iface(dbus_con, "Usb")