| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
tests.test_discover |
| 19 |
=================== |
| 20 |
|
| 21 |
Tests discovering what device is meant by somewhat unspecific information. |
| 22 |
|
| 23 |
.. moduleauthor:: mulhern <[email protected]> |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
import os |
| 28 |
|
| 29 |
|
| 30 |
import pytest |
| 31 |
from hypothesis import assume, given, settings, strategies |
| 32 |
|
| 33 |
|
| 34 |
import pyudev |
| 35 |
from pyudev import ( |
| 36 |
DeviceFileHypothesis, |
| 37 |
DeviceNameHypothesis, |
| 38 |
DeviceNumberHypothesis, |
| 39 |
DevicePathHypothesis, |
| 40 |
Discovery, |
| 41 |
) |
| 42 |
|
| 43 |
_CONTEXT = pyudev.Context() |
| 44 |
_DEVICES = [d for d in _CONTEXT.list_devices()] |
| 45 |
|
| 46 |
NUM_TESTS = 5 |
| 47 |
|
| 48 |
|
| 49 |
class TestUtilities: |
| 50 |
""" |
| 51 |
Some utilities used by the actual tests. |
| 52 |
""" |
| 53 |
|
| 54 |
@staticmethod |
| 55 |
def get_device_numbers(a_device, a_string): |
| 56 |
""" |
| 57 |
Get the device number from the device in a few formats. |
| 58 |
|
| 59 |
:param :class:`Device` a_device: the device |
| 60 |
:param str a_string: a likely string between the major and minor |
| 61 |
:returns: a tuple of device numbers |
| 62 |
:rtype: tuple of str |
| 63 |
""" |
| 64 |
device_number = a_device.device_number |
| 65 |
major_number = os.major(device_number) |
| 66 |
minor_number = os.minor(device_number) |
| 67 |
pair_number = "%s%s%s" % (major_number, a_string, minor_number) |
| 68 |
return (str(device_number), pair_number) |
| 69 |
|
| 70 |
@staticmethod |
| 71 |
def get_paths(a_device): |
| 72 |
""" |
| 73 |
Get some variants on a device path. |
| 74 |
|
| 75 |
:param :class:`Device` a_device: the device |
| 76 |
:returns: a tuple of paths in sysfs |
| 77 |
:rtype: tuple of str |
| 78 |
""" |
| 79 |
sys_path = a_device.sys_path |
| 80 |
(_, _, truncated_path) = sys_path[1:].partition("/") |
| 81 |
return (sys_path, truncated_path, a_device.device_path) |
| 82 |
|
| 83 |
@staticmethod |
| 84 |
def get_files(a_device): |
| 85 |
""" |
| 86 |
Get a bunch of files, including device nodes and links. |
| 87 |
|
| 88 |
:param :class:`Device` a_device: the device |
| 89 |
:returns: a list of files |
| 90 |
:rtype: list of str |
| 91 |
""" |
| 92 |
links = list(a_device.device_links) |
| 93 |
names = [os.path.basename(l) for l in links] |
| 94 |
links.extend(names) |
| 95 |
|
| 96 |
device_node = a_device.device_node |
| 97 |
if device_node: |
| 98 |
links.append(device_node) |
| 99 |
return links |
| 100 |
|
| 101 |
|
| 102 |
class TestDiscovery: |
| 103 |
""" |
| 104 |
Test discovery of an object from limited bits of its description. |
| 105 |
""" |
| 106 |
|
| 107 |
_CONTEXT = pyudev.Context() |
| 108 |
_DISCOVER = Discovery() |
| 109 |
_DISCOVER.setup(_CONTEXT) |
| 110 |
|
| 111 |
@given( |
| 112 |
strategies.sampled_from(_DEVICES).filter(lambda x: x.device_number), |
| 113 |
strategies.text(":, -/+=").filter(lambda x: x), |
| 114 |
) |
| 115 |
@settings(max_examples=NUM_TESTS) |
| 116 |
def test_device_number(self, a_device, a_string): |
| 117 |
""" |
| 118 |
Test lookup by a device number. |
| 119 |
|
| 120 |
Note that device number is per class, so there may be two |
| 121 |
devices with the same device number. |
| 122 |
""" |
| 123 |
for number in TestUtilities.get_device_numbers(a_device, a_string): |
| 124 |
res = DeviceNumberHypothesis.get_devices(self._CONTEXT, number) |
| 125 |
assert a_device in res |
| 126 |
|
| 127 |
@given(strategies.sampled_from(_DEVICES)) |
| 128 |
@settings(max_examples=NUM_TESTS) |
| 129 |
def test_path(self, a_device): |
| 130 |
""" |
| 131 |
Test lookup by path. |
| 132 |
""" |
| 133 |
for path in TestUtilities.get_paths(a_device): |
| 134 |
res = DevicePathHypothesis.get_devices(self._CONTEXT, path) |
| 135 |
assert res == set((a_device,)) |
| 136 |
|
| 137 |
@given(strategies.sampled_from(_DEVICES)) |
| 138 |
@settings(max_examples=NUM_TESTS) |
| 139 |
def test_name(self, a_device): |
| 140 |
""" |
| 141 |
Test lookup by device name. |
| 142 |
|
| 143 |
Note that there may be multiple devices corresponding to the name |
| 144 |
in different subsystems. |
| 145 |
""" |
| 146 |
name = a_device.sys_name |
| 147 |
res = DeviceNameHypothesis.get_devices(self._CONTEXT, name) |
| 148 |
assert a_device in res |
| 149 |
|
| 150 |
_devices = [d for d in _DEVICES if list(d.device_links)] |
| 151 |
|
| 152 |
@given( |
| 153 |
strategies.sampled_from(_DEVICES), |
| 154 |
strategies.text(":, -/+=").filter(lambda x: x), |
| 155 |
) |
| 156 |
@settings(max_examples=NUM_TESTS) |
| 157 |
def test_anything(self, a_device, a_string): |
| 158 |
""" |
| 159 |
Grab any of the likely candidates for looking up a device. |
| 160 |
""" |
| 161 |
assume(not "DM_MULTIPATH_TIMESTAMP" in a_device.properties) |
| 162 |
|
| 163 |
values = list(TestUtilities.get_device_numbers(a_device, a_string)) |
| 164 |
values.extend(TestUtilities.get_paths(a_device)) |
| 165 |
values.append(a_device.sys_name) |
| 166 |
values.extend(TestUtilities.get_files(a_device)) |
| 167 |
|
| 168 |
results = frozenset( |
| 169 |
d for v in values for d in self._DISCOVER.get_devices(self._CONTEXT, v) |
| 170 |
) |
| 171 |
|
| 172 |
assert a_device in results |