| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
Tests methods belonging to Devices class. |
| 19 |
|
| 20 |
.. moduleauthor:: mulhern <[email protected]> |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
import os |
| 25 |
import stat |
| 26 |
|
| 27 |
|
| 28 |
import pytest |
| 29 |
from hypothesis import assume, given, settings |
| 30 |
|
| 31 |
|
| 32 |
from pyudev import ( |
| 33 |
DeviceNotFoundAtPathError, |
| 34 |
DeviceNotFoundByFileError, |
| 35 |
DeviceNotFoundByNameError, |
| 36 |
DeviceNotFoundByNumberError, |
| 37 |
DeviceNotFoundInEnvironmentError, |
| 38 |
Devices, |
| 39 |
) |
| 40 |
|
| 41 |
from .._constants import ( |
| 42 |
_CONTEXT, |
| 43 |
_CONTEXT_STRATEGY, |
| 44 |
_SUBSYSTEM_STRATEGY, |
| 45 |
_UDEV_TEST, |
| 46 |
device_strategy, |
| 47 |
) |
| 48 |
from ..utils import failed_health_check_wrapper |
| 49 |
|
| 50 |
|
| 51 |
class TestDevices: |
| 52 |
""" |
| 53 |
Test ``Devices`` methods. |
| 54 |
""" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@given(_CONTEXT_STRATEGY, device_strategy()) |
| 59 |
@settings(max_examples=5) |
| 60 |
def test_from_path(self, a_context, a_device): |
| 61 |
""" |
| 62 |
from_path() method yields correct device. |
| 63 |
""" |
| 64 |
assert a_device == Devices.from_path(a_context, a_device.device_path) |
| 65 |
|
| 66 |
@given(_CONTEXT_STRATEGY, device_strategy()) |
| 67 |
@settings(max_examples=5) |
| 68 |
def test_from_path_strips_leading_slash(self, a_context, a_device): |
| 69 |
""" |
| 70 |
from_path() yields the same value, even if initial '/' is missing. |
| 71 |
""" |
| 72 |
path = a_device.device_path |
| 73 |
assert Devices.from_path(a_context, path[1:]) == Devices.from_path( |
| 74 |
a_context, path |
| 75 |
) |
| 76 |
|
| 77 |
@given(_CONTEXT_STRATEGY, device_strategy()) |
| 78 |
@settings(max_examples=5) |
| 79 |
def test_from_sys_path(self, a_context, a_device): |
| 80 |
""" |
| 81 |
from_sys_path() yields the correct device. |
| 82 |
""" |
| 83 |
assert a_device == Devices.from_sys_path(a_context, a_device.sys_path) |
| 84 |
|
| 85 |
def test_from_sys_path_device_not_found(self): |
| 86 |
""" |
| 87 |
Verify that a non-existant sys_path causes an exception. |
| 88 |
""" |
| 89 |
sys_path = "there_will_not_be_such_a_device" |
| 90 |
with pytest.raises(DeviceNotFoundAtPathError) as exc_info: |
| 91 |
Devices.from_sys_path(_CONTEXT, sys_path) |
| 92 |
error = exc_info.value |
| 93 |
assert error.sys_path == sys_path |
| 94 |
|
| 95 |
@given(_CONTEXT_STRATEGY, device_strategy()) |
| 96 |
@settings(max_examples=5) |
| 97 |
def test_from_name(self, a_context, a_device): |
| 98 |
""" |
| 99 |
Test that getting a new device based on the name and subsystem |
| 100 |
yields an equivalent device. |
| 101 |
""" |
| 102 |
new_device = Devices.from_name(a_context, a_device.subsystem, a_device.sys_name) |
| 103 |
assert new_device == a_device |
| 104 |
|
| 105 |
@given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY) |
| 106 |
@settings(max_examples=5) |
| 107 |
def test_from_name_no_device_in_existing_subsystem(self, a_context, subsys): |
| 108 |
""" |
| 109 |
Verify that a real subsystem and non-existant name causes an |
| 110 |
exception to be raised. |
| 111 |
""" |
| 112 |
with pytest.raises(DeviceNotFoundByNameError) as exc_info: |
| 113 |
Devices.from_name(a_context, subsys, "foobar") |
| 114 |
error = exc_info.value |
| 115 |
assert error.subsystem == subsys |
| 116 |
assert error.sys_name == "foobar" |
| 117 |
|
| 118 |
def test_from_name_nonexisting_subsystem(self): |
| 119 |
""" |
| 120 |
Verify that a non-existant subsystem causes an exception. |
| 121 |
""" |
| 122 |
with pytest.raises(DeviceNotFoundByNameError) as exc_info: |
| 123 |
Devices.from_name(_CONTEXT, "no_such_subsystem", "foobar") |
| 124 |
error = exc_info.value |
| 125 |
assert error.subsystem == "no_such_subsystem" |
| 126 |
assert error.sys_name == "foobar" |
| 127 |
|
| 128 |
@failed_health_check_wrapper |
| 129 |
@given( |
| 130 |
_CONTEXT_STRATEGY, |
| 131 |
device_strategy(filter_func=lambda x: x.device_node is not None), |
| 132 |
) |
| 133 |
@settings(max_examples=5) |
| 134 |
def test_from_device_number(self, a_context, a_device): |
| 135 |
""" |
| 136 |
Verify that from_device_number() yields the correct device. |
| 137 |
""" |
| 138 |
mode = os.stat(a_device.device_node).st_mode |
| 139 |
typ = "block" if stat.S_ISBLK(mode) else "char" |
| 140 |
device = Devices.from_device_number(a_context, typ, a_device.device_number) |
| 141 |
assert a_device == device |
| 142 |
|
| 143 |
@failed_health_check_wrapper |
| 144 |
@given( |
| 145 |
_CONTEXT_STRATEGY, |
| 146 |
device_strategy(filter_func=lambda x: x.device_node is not None), |
| 147 |
) |
| 148 |
@settings(max_examples=5) |
| 149 |
def test_from_device_number_wrong_type(self, a_context, a_device): |
| 150 |
""" |
| 151 |
Verify appropriate behavior on real device number but swapped |
| 152 |
subsystems. |
| 153 |
""" |
| 154 |
mode = os.stat(a_device.device_node).st_mode |
| 155 |
|
| 156 |
|
| 157 |
typ = "char" if stat.S_ISBLK(mode) else "block" |
| 158 |
try: |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
device = Devices.from_device_number(a_context, typ, a_device.device_number) |
| 163 |
|
| 164 |
|
| 165 |
assert device != a_device |
| 166 |
except DeviceNotFoundByNumberError as error: |
| 167 |
|
| 168 |
assert error.device_type == typ |
| 169 |
assert error.device_number == a_device.device_number |
| 170 |
|
| 171 |
def test_from_device_number_invalid_type(self): |
| 172 |
""" |
| 173 |
Verify that a non-existant subsystem always results in an exception. |
| 174 |
""" |
| 175 |
with pytest.raises(DeviceNotFoundByNumberError): |
| 176 |
Devices.from_device_number(_CONTEXT, "foobar", 100) |
| 177 |
|
| 178 |
@failed_health_check_wrapper |
| 179 |
@given( |
| 180 |
_CONTEXT_STRATEGY, |
| 181 |
device_strategy(filter_func=lambda x: x.device_node is not None), |
| 182 |
) |
| 183 |
@settings(max_examples=5) |
| 184 |
def test_from_device_file(self, a_context, a_device): |
| 185 |
""" |
| 186 |
Verify that from_device_file() yields correct device. |
| 187 |
""" |
| 188 |
device = Devices.from_device_file(a_context, a_device.device_node) |
| 189 |
assert a_device == device |
| 190 |
|
| 191 |
def test_from_device_file_no_device_file(self, tmpdir): |
| 192 |
""" |
| 193 |
Verify that a file that is not a device file will cause an exception |
| 194 |
to be raised. |
| 195 |
""" |
| 196 |
filename = tmpdir.join("test") |
| 197 |
filename.ensure(file=True) |
| 198 |
with pytest.raises(DeviceNotFoundByFileError): |
| 199 |
Devices.from_device_file(_CONTEXT, str(filename)) |
| 200 |
|
| 201 |
def test_from_device_file_non_existing(self, tmpdir): |
| 202 |
""" |
| 203 |
Test that an OSError is raised when constructing a ``Device`` from |
| 204 |
a file that does not actually exist. |
| 205 |
""" |
| 206 |
filename = tmpdir.join("test_from_device_file_non_existing") |
| 207 |
assert not tmpdir.check(file=True) |
| 208 |
with pytest.raises(DeviceNotFoundByFileError): |
| 209 |
Devices.from_device_file(_CONTEXT, str(filename)) |
| 210 |
|
| 211 |
@_UDEV_TEST(152, "test_from_environment") |
| 212 |
def test_from_environment(self): |
| 213 |
""" |
| 214 |
there is no device in a standard environment |
| 215 |
""" |
| 216 |
with pytest.raises(DeviceNotFoundInEnvironmentError): |
| 217 |
Devices.from_environment(_CONTEXT) |