conftest.py 2.3 KB

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