summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/hid/tests/conftest.py
blob: 1361ec981db6f79a58cf91e8732dcd7c05c47d38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/env python3
# SPDX-License-Identifier: GPL-2.0
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Benjamin Tissoires <benjamin.tissoires@gmail.com>
# Copyright (c) 2017 Red Hat, Inc.

import platform
import pytest
import re
import resource
import subprocess
from .base import HIDTestUdevRule
from pathlib import Path


# See the comment in HIDTestUdevRule, this doesn't set up but it will clean
# up once the last test exited.
@pytest.fixture(autouse=True, scope="session")
def udev_rules_session_setup():
    with HIDTestUdevRule.instance():
        yield


@pytest.fixture(autouse=True, scope="session")
def setup_rlimit():
    resource.setrlimit(resource.RLIMIT_CORE, (0, 0))


@pytest.fixture(autouse=True, scope="session")
def start_udevd(pytestconfig):
    if pytestconfig.getoption("udevd"):
        import subprocess

        with subprocess.Popen("/usr/lib/systemd/systemd-udevd") as proc:
            yield
            proc.kill()
    else:
        yield


def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "skip_if_uhdev(condition, message): mark test to skip if the condition on the uhdev device is met",
    )


# Generate the list of modules and modaliases
# for the tests that need to be parametrized with those
def pytest_generate_tests(metafunc):
    if "usbVidPid" in metafunc.fixturenames:
        modules = (
            Path("/lib/modules/")
            / platform.uname().release
            / "kernel"
            / "drivers"
            / "hid"
        )

        modalias_re = re.compile(r"alias:\s+hid:b0003g.*v([0-9a-fA-F]+)p([0-9a-fA-F]+)")

        params = []
        ids = []
        for module in modules.glob("*.ko"):
            p = subprocess.run(
                ["modinfo", module], capture_output=True, check=True, encoding="utf-8"
            )
            for line in p.stdout.split("\n"):
                m = modalias_re.match(line)
                if m is not None:
                    vid, pid = m.groups()
                    vid = int(vid, 16)
                    pid = int(pid, 16)
                    params.append([module.name.replace(".ko", ""), vid, pid])
                    ids.append(f"{module.name} {vid:04x}:{pid:04x}")
        metafunc.parametrize("usbVidPid", params, ids=ids)


def pytest_addoption(parser):
    parser.addoption("--udevd", action="store_true", default=False)