Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1#!/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3# -*- coding: utf-8 -*-
4#
5# Copyright (c) 2017 Benjamin Tissoires <benjamin.tissoires@gmail.com>
6# Copyright (c) 2017 Red Hat, Inc.
7
8from packaging.version import Version
9import platform
10import pytest
11import re
12import resource
13import subprocess
14from .base import HIDTestUdevRule
15from pathlib import Path
16
17
18@pytest.fixture(autouse=True)
19def hidtools_version_check():
20 HIDTOOLS_VERSION = "0.12"
21 try:
22 import hidtools
23
24 version = hidtools.__version__ # type: ignore
25 if Version(version) < Version(HIDTOOLS_VERSION):
26 pytest.skip(reason=f"have hidtools {version}, require >={HIDTOOLS_VERSION}")
27 except Exception:
28 pytest.skip(reason=f"hidtools >={HIDTOOLS_VERSION} required")
29
30
31# See the comment in HIDTestUdevRule, this doesn't set up but it will clean
32# up once the last test exited.
33@pytest.fixture(autouse=True, scope="session")
34def udev_rules_session_setup():
35 with HIDTestUdevRule.instance():
36 yield
37
38
39@pytest.fixture(autouse=True, scope="session")
40def setup_rlimit():
41 resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
42
43
44@pytest.fixture(autouse=True, scope="session")
45def start_udevd(pytestconfig):
46 if pytestconfig.getoption("udevd"):
47 import subprocess
48
49 with subprocess.Popen("/usr/lib/systemd/systemd-udevd") as proc:
50 yield
51 proc.kill()
52 else:
53 yield
54
55
56def pytest_configure(config):
57 config.addinivalue_line(
58 "markers",
59 "skip_if_uhdev(condition, message): mark test to skip if the condition on the uhdev device is met",
60 )
61
62
63# Generate the list of modules and modaliases
64# for the tests that need to be parametrized with those
65def pytest_generate_tests(metafunc):
66 if "usbVidPid" in metafunc.fixturenames:
67 modules = (
68 Path("/lib/modules/")
69 / platform.uname().release
70 / "kernel"
71 / "drivers"
72 / "hid"
73 )
74
75 modalias_re = re.compile(r"alias:\s+hid:b0003g.*v([0-9a-fA-F]+)p([0-9a-fA-F]+)")
76
77 params = []
78 ids = []
79 for module in modules.glob("*.ko"):
80 p = subprocess.run(
81 ["modinfo", module], capture_output=True, check=True, encoding="utf-8"
82 )
83 for line in p.stdout.split("\n"):
84 m = modalias_re.match(line)
85 if m is not None:
86 vid, pid = m.groups()
87 vid = int(vid, 16)
88 pid = int(pid, 16)
89 params.append([module.name.replace(".ko", ""), vid, pid])
90 ids.append(f"{module.name} {vid:04x}:{pid:04x}")
91 metafunc.parametrize("usbVidPid", params, ids=ids)
92
93
94def pytest_addoption(parser):
95 parser.addoption("--udevd", action="store_true", default=False)