conftest.py 2.8 KB

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