Path: blob/master/tools/testing/selftests/hid/tests/conftest.py
49255 views
#!/bin/env python31# SPDX-License-Identifier: GPL-2.02# -*- coding: utf-8 -*-3#4# Copyright (c) 2017 Benjamin Tissoires <[email protected]>5# Copyright (c) 2017 Red Hat, Inc.67from packaging.version import Version8import platform9import pytest10import re11import resource12import subprocess13from .base import HIDTestUdevRule14from pathlib import Path151617@pytest.fixture(autouse=True)18def hidtools_version_check():19HIDTOOLS_VERSION = "0.12"20try:21import hidtools2223version = hidtools.__version__ # type: ignore24if Version(version) < Version(HIDTOOLS_VERSION):25pytest.skip(reason=f"have hidtools {version}, require >={HIDTOOLS_VERSION}")26except Exception:27pytest.skip(reason=f"hidtools >={HIDTOOLS_VERSION} required")282930# See the comment in HIDTestUdevRule, this doesn't set up but it will clean31# up once the last test exited.32@pytest.fixture(autouse=True, scope="session")33def udev_rules_session_setup():34with HIDTestUdevRule.instance():35yield363738@pytest.fixture(autouse=True, scope="session")39def setup_rlimit():40resource.setrlimit(resource.RLIMIT_CORE, (0, 0))414243@pytest.fixture(autouse=True, scope="session")44def start_udevd(pytestconfig):45if pytestconfig.getoption("udevd"):46import subprocess4748with subprocess.Popen("/usr/lib/systemd/systemd-udevd") as proc:49yield50proc.kill()51else:52yield535455def pytest_configure(config):56config.addinivalue_line(57"markers",58"skip_if_uhdev(condition, message): mark test to skip if the condition on the uhdev device is met",59)606162# Generate the list of modules and modaliases63# for the tests that need to be parametrized with those64def pytest_generate_tests(metafunc):65if "usbVidPid" in metafunc.fixturenames:66modules = (67Path("/lib/modules/")68/ platform.uname().release69/ "kernel"70/ "drivers"71/ "hid"72)7374modalias_re = re.compile(r"alias:\s+hid:b0003g.*v([0-9a-fA-F]+)p([0-9a-fA-F]+)")7576params = []77ids = []78for module in modules.glob("*.ko"):79p = subprocess.run(80["modinfo", module], capture_output=True, check=True, encoding="utf-8"81)82for line in p.stdout.split("\n"):83m = modalias_re.match(line)84if m is not None:85vid, pid = m.groups()86vid = int(vid, 16)87pid = int(pid, 16)88params.append([module.name.replace(".ko", ""), vid, pid])89ids.append(f"{module.name} {vid:04x}:{pid:04x}")90metafunc.parametrize("usbVidPid", params, ids=ids)919293def pytest_addoption(parser):94parser.addoption("--udevd", action="store_true", default=False)959697