| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
pyudev.tests._device_tests |
| 19 |
========================== |
| 20 |
|
| 21 |
Tests for devices. |
| 22 |
|
| 23 |
.. moduleauthor:: mulhern <[email protected]> |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
import pytest |
| 28 |
from hypothesis import strategies |
| 29 |
|
| 30 |
|
| 31 |
from pyudev import Context, DeviceNotFoundError, Devices |
| 32 |
|
| 33 |
from .utils import udev |
| 34 |
|
| 35 |
_CONTEXT = Context() |
| 36 |
|
| 37 |
|
| 38 |
def _check_device(device): |
| 39 |
""" |
| 40 |
Check that device exists by getting it. |
| 41 |
""" |
| 42 |
try: |
| 43 |
Devices.from_path(_CONTEXT, device.sys_path) |
| 44 |
return True |
| 45 |
except DeviceNotFoundError: |
| 46 |
return False |
| 47 |
|
| 48 |
|
| 49 |
_DEVICE_DATA = [d for d in udev.DeviceDatabase.db()] |
| 50 |
_DEVICES = [Devices.from_path(_CONTEXT, d.device_path) for d in _DEVICE_DATA] |
| 51 |
|
| 52 |
|
| 53 |
def device_strategy(require_existing=True, filter_func=lambda x: True): |
| 54 |
""" |
| 55 |
Strategy that yields filtered devices. |
| 56 |
|
| 57 |
The devices are filtered before being sampled to reduce the number |
| 58 |
of health failures. |
| 59 |
|
| 60 |
:param bool require_existing: at the very last, verify existance |
| 61 |
:param filter_func: a function to be used as a filter |
| 62 |
:type filter_func: Device -> bool |
| 63 |
""" |
| 64 |
strategy = strategies.sampled_from( |
| 65 |
[x for x in _CONTEXT.list_devices() if filter_func(x)] |
| 66 |
) |
| 67 |
|
| 68 |
if require_existing: |
| 69 |
strategy = strategy.filter(_check_device) |
| 70 |
|
| 71 |
return strategy |
| 72 |
|
| 73 |
|
| 74 |
_CONTEXT_STRATEGY = strategies.just(_CONTEXT) |
| 75 |
|
| 76 |
_UDEV_VERSION = int(udev.UDevAdm.adm().query_udev_version()) |
| 77 |
|
| 78 |
_SUBSYSTEM_STRATEGY = device_strategy().map(lambda x: x.subsystem) |
| 79 |
|
| 80 |
|
| 81 |
_SUBSYSTEM_STRATEGY = _SUBSYSTEM_STRATEGY.filter(lambda s: s != "i2c") |
| 82 |
|
| 83 |
_SYSNAME_STRATEGY = device_strategy().map(lambda x: x.sys_name) |
| 84 |
|
| 85 |
_PROPERTY_STRATEGY = device_strategy().flatmap( |
| 86 |
lambda d: strategies.sampled_from([p for p in d.properties.items()]) |
| 87 |
) |
| 88 |
|
| 89 |
_MATCH_PROPERTY_STRATEGY = _PROPERTY_STRATEGY.filter(lambda p: p[0][-4:] != "_ENC") |
| 90 |
|
| 91 |
if _UDEV_VERSION <= 230: |
| 92 |
_MATCH_PROPERTY_STRATEGY = _MATCH_PROPERTY_STRATEGY.filter( |
| 93 |
lambda p: "[" not in p[1] |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
_ATTRIBUTES_STRATEGY = device_strategy().map(lambda d: d.attributes) |
| 98 |
|
| 99 |
|
| 100 |
_ATTRIBUTE_STRATEGY = _ATTRIBUTES_STRATEGY.flatmap( |
| 101 |
lambda attrs: strategies.sampled_from([a for a in attrs.available_attributes]).map( |
| 102 |
lambda key: (key, attrs.get(key)) |
| 103 |
) |
| 104 |
) |
| 105 |
|
| 106 |
_ATTRIBUTE_STRATEGY = _ATTRIBUTE_STRATEGY.filter(lambda p: p[1] is not None) |
| 107 |
|
| 108 |
if _UDEV_VERSION <= 230: |
| 109 |
_ATTRIBUTE_STRATEGY = _ATTRIBUTE_STRATEGY.filter( |
| 110 |
lambda p: not p[1].startswith(b"\\") |
| 111 |
and not p[1][-1:] == b" " |
| 112 |
and not p[1].startswith(b"[") |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
_TAGS_STRATEGY = device_strategy().map(lambda d: [t for t in d.tags]) |
| 117 |
|
| 118 |
|
| 119 |
_TAG_STRATEGY = _TAGS_STRATEGY.filter(lambda t: t != []).flatmap( |
| 120 |
strategies.sampled_from |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
def _UDEV_TEST(version, node=None): |
| 125 |
fmt_str = "%s: udev version must be at least %s, is %s" |
| 126 |
return pytest.mark.skipif( |
| 127 |
_UDEV_VERSION < version, reason=fmt_str % (node, version, _UDEV_VERSION) |
| 128 |
) |