1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
# -*- coding: utf-8 -*-
# Copyright (C) 2010, 2011, 2012 Sebastian Wiesner <lunaryorn@gmail.com>
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import (print_function, division, unicode_literals,
absolute_import)
import pytest
import mock
from hypothesis import given
from hypothesis import settings
from hypothesis import strategies
from pyudev import Enumerator
from ._constants import _ATTRIBUTE_STRATEGY
from ._constants import _CONTEXT_STRATEGY
from ._constants import _DEVICE_STRATEGY
from ._constants import _MATCH_PROPERTY_STRATEGY
from ._constants import _SUBSYSTEM_STRATEGY
from ._constants import _SYSNAME_STRATEGY
from ._constants import _TAG_STRATEGY
from ._constants import _UDEV_TEST
from ._constants import _UDEV_VERSION
from .utils import failed_health_check_wrapper
def _is_int(value):
try:
int(value)
return True
except (TypeError, ValueError):
return False
def _is_bool(value):
try:
return int(value) in (0, 1)
except (TypeError, ValueError):
return False
def _test_direct_and_complement(context, devices, func):
"""
Test that results are correct and that complement holds.
:param Context context: the libudev context
:param devices: the devices that match
:type devices: frozenset of Device
:param func: the property to test
:type func: device -> bool
"""
assert [device for device in devices if not func(device)] == []
complement = frozenset(context.list_devices()) - devices
assert [device for device in complement if func(device)] == []
def _test_intersection_and_union(context, matches, nomatches):
"""
Test that intersection is empty and union is all of devices.
:param matches: the matching devices
:type matches: frozenset of Device
:param nomatches: the non-matching devices
:type nomatches: frozenset of Device
"""
assert matches & nomatches == frozenset()
assert matches | nomatches == frozenset(context.list_devices())
class TestEnumerator(object):
"""
Test the Enumerator class.
"""
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
@settings(max_examples=50)
def test_match_subsystem(self, context, subsystem):
"""
Subsystem match matches devices w/ correct subsystem.
"""
_test_direct_and_complement(
context,
frozenset(context.list_devices().match_subsystem(subsystem)),
lambda d: d.subsystem == subsystem
)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
@settings(max_examples=5)
def test_match_subsystem_nomatch(self, context, subsystem):
"""
Subsystem no match gets no subsystem with subsystem.
"""
_test_direct_and_complement(
context,
frozenset(
context.list_devices().match_subsystem(subsystem, nomatch=True)
),
lambda d: d.subsystem != subsystem
)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
@settings(max_examples=5)
def test_match_subsystem_nomatch_unfulfillable(self, context, subsystem):
"""
Combining match and no match should give an empty result.
"""
devices = context.list_devices()
devices.match_subsystem(subsystem)
devices.match_subsystem(subsystem, nomatch=True)
assert not list(devices)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _SUBSYSTEM_STRATEGY)
@settings(max_examples=5)
def test_match_subsystem_nomatch_complete(self, context, subsystem):
"""
Test that w/ respect to the universe of devices returned by
list_devices() a match and its inverse are complements of each other.
Note that list_devices() omits devices that have no subsystem,
so with respect to the whole universe of devices, the two are
not complements of each other.
"""
m_devices = frozenset(context.list_devices().match_subsystem(subsystem))
nm_devices = frozenset(
context.list_devices().match_subsystem(subsystem, nomatch=True)
)
_test_intersection_and_union(context, m_devices, nm_devices)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _SYSNAME_STRATEGY)
@settings(max_examples=5)
def test_match_sys_name(self, context, sysname):
"""
A sysname lookup only gives devices with that sysname.
"""
_test_direct_and_complement(
context,
frozenset(context.list_devices().match_sys_name(sysname)),
lambda d: d.sys_name == sysname
)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _MATCH_PROPERTY_STRATEGY)
@settings(max_examples=50)
def test_match_property_string(self, context, pair):
"""
Match property only gets devices with that property.
"""
key, value = pair
_test_direct_and_complement(
context,
frozenset(context.list_devices().match_property(key, value)),
lambda d: d.properties.get(key) == value
)
@failed_health_check_wrapper
@given(
_CONTEXT_STRATEGY,
_MATCH_PROPERTY_STRATEGY.filter(lambda x: _is_int(x[1]))
)
@settings(max_examples=50)
def test_match_property_int(self, context, pair):
"""
For a property that might plausibly have an integer value, search
using the integer value and verify that the result all match.
"""
key, value = pair
devices = context.list_devices().match_property(key, int(value))
assert all(
device.properties[key] == value and \
device.properties.asint(key) == int(value) \
for device in devices
)
@failed_health_check_wrapper
@given(
_CONTEXT_STRATEGY,
_MATCH_PROPERTY_STRATEGY.filter(lambda x: _is_bool(x[1]))
)
@settings(max_examples=10)
def test_match_property_bool(self, context, pair):
"""
Verify that a probably boolean property lookup works.
"""
key, value = pair
bool_value = True if int(value) == 1 else False
devices = context.list_devices().match_property(key, bool_value)
assert all(
device.properties[key] == value and \
device.properties.asbool(key) == bool_value \
for device in devices
)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _ATTRIBUTE_STRATEGY)
def test_match_attribute_match(self, context, pair):
"""
Test match returns matching devices.
"""
key, value = pair
_test_direct_and_complement(
context,
frozenset(context.list_devices().match_attribute(key, value)),
lambda d: d.attributes.get(key) == value
)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _ATTRIBUTE_STRATEGY)
def test_match_attribute_nomatch(self, context, pair):
"""
Test that nomatch returns no devices with attribute value match.
"""
key, value = pair
_test_direct_and_complement(
context,
frozenset(
context.list_devices().match_attribute(key, value, nomatch=True)
),
lambda d: d.attributes.get(key) != value
)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _ATTRIBUTE_STRATEGY)
@settings(max_examples=50)
def test_match_attribute_nomatch_unfulfillable(self, context, pair):
"""
Match and no match for a key/value gives empty set.
"""
key, value = pair
devices = context.list_devices()
devices.match_attribute(key, value)
devices.match_attribute(key, value, nomatch=True)
assert not list(devices)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _ATTRIBUTE_STRATEGY)
@settings(max_examples=50)
def test_match_attribute_nomatch_complete(self, context, pair):
"""
Test that w/ respect to the universe of devices returned by
list_devices() a match and its inverse are complements of each other.
"""
key, value = pair
m_devices = frozenset(
context.list_devices().match_attribute(key, value)
)
nm_devices = frozenset(
context.list_devices().match_attribute(key, value, nomatch=True)
)
_test_intersection_and_union(context, m_devices, nm_devices)
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _ATTRIBUTE_STRATEGY)
@settings(max_examples=50)
def test_match_attribute_string(self, context, pair):
"""
Test that matching attribute as string works.
"""
key, value = pair
devices = context.list_devices().match_attribute(key, value)
assert all(device.attributes.get(key) == value for device in devices)
@failed_health_check_wrapper
@given(
_CONTEXT_STRATEGY,
_ATTRIBUTE_STRATEGY.filter(lambda x: _is_int(x[1]))
)
@settings(max_examples=50)
def test_match_attribute_int(self, context, pair):
"""
Test matching integer attribute.
"""
key, value = pair
int_value = int(value)
devices = context.list_devices().match_attribute(key, int_value)
for device in devices:
attributes = device.attributes
assert attributes.get(key) == value
assert device.attributes.asint(key) == int_value
@failed_health_check_wrapper
@given(
_CONTEXT_STRATEGY,
_ATTRIBUTE_STRATEGY.filter(lambda x: _is_bool(x[1]))
)
@settings(max_examples=50)
def test_match_attribute_bool(self, context, pair):
"""
Test matching boolean attribute.
"""
key, value = pair
bool_value = True if int(value) == 1 else False
devices = context.list_devices().match_attribute(key, bool_value)
for device in devices:
attributes = device.attributes
assert attributes.get(key) == value
assert attributes.asbool(key) == bool_value
@_UDEV_TEST(154, "test_match_tag")
@failed_health_check_wrapper
@given(_CONTEXT_STRATEGY, _TAG_STRATEGY)
@settings(max_examples=50)
def test_match_tag(self, context, tag):
"""
Test that matches returned for tag actually have tag.
"""
_test_direct_and_complement(
context,
frozenset(context.list_devices().match_tag(tag)),
lambda d: tag in d.tags
)
@failed_health_check_wrapper
@given(
_CONTEXT_STRATEGY,
_DEVICE_STRATEGY.filter(lambda d: d.parent is not None)
)
@settings(max_examples=5)
def test_match_parent(self, context, device):
"""
For a given device, verify that it is in its parent's children.
Verify that the parent is also among the device's children,
unless the parent lacks a subsystem
See: rhbz#1255191
"""
parent = device.parent
children = list(context.list_devices().match_parent(parent))
assert device in children
if _UDEV_VERSION <= 175:
assert parent in children
else:
if parent.subsystem is not None:
assert parent in children
else:
assert parent not in children
class TestEnumeratorMatchCombinations(object):
"""
Test combinations of matches.
"""
@given(
_CONTEXT_STRATEGY,
strategies.lists(
elements=_MATCH_PROPERTY_STRATEGY,
min_size=2,
max_size=3,
unique_by=lambda p: p[0]
)
)
@settings(max_examples=20)
def test_combined_property_matches(self, context, ppairs):
"""
Test for behaviour as observed in #1
If matching multiple properties, then the result is the union of
the matching sets, i.e., the resulting filter is a disjunction.
"""
enumeration = context.list_devices()
for key, value in ppairs:
enumeration.match_property(key, value)
_test_direct_and_complement(
context,
frozenset(enumeration),
lambda d: any(
d.properties.get(key) == value for key, value in ppairs
)
)
@given(
_CONTEXT_STRATEGY,
strategies.lists(
elements=_ATTRIBUTE_STRATEGY,
min_size=2,
max_size=3,
unique_by=lambda p: p[0]
)
)
@settings(max_examples=20)
def test_combined_attribute_matches(self, context, apairs):
"""
Test for conjunction of attributes.
If matching multiple attributes, then the result is the intersection of
the matching sets, i.e., the resulting filter is a conjunction.
"""
enumeration = context.list_devices()
for key, value in apairs:
enumeration.match_attribute(key, value)
_test_direct_and_complement(
context,
frozenset(enumeration),
lambda d: all(
d.attributes.get(key) == value for key, value in apairs
)
)
@given(
_CONTEXT_STRATEGY,
strategies.lists(
elements=_MATCH_PROPERTY_STRATEGY,
min_size=1,
max_size=2,
unique_by=lambda p: p[0]
),
strategies.lists(
elements=_ATTRIBUTE_STRATEGY,
min_size=1,
max_size=2,
unique_by=lambda p: p[0]
)
)
@settings(max_examples=20)
def test_combined_matches_of_different_types(self, context, ppairs, apairs):
"""
Require that properties and attributes have a conjunction.
"""
enumeration = context.list_devices()
for key, value in ppairs:
enumeration.match_property(key, value)
for key, value in apairs:
enumeration.match_attribute(key, value)
_test_direct_and_complement(
context,
frozenset(enumeration),
lambda d: all(
d.attributes.get(key) == value for key, value in apairs
) and any(
d.properties.get(key) == value for key, value in ppairs
)
)
@given(
_CONTEXT_STRATEGY,
_SUBSYSTEM_STRATEGY,
_SYSNAME_STRATEGY,
_MATCH_PROPERTY_STRATEGY
)
@settings(max_examples=10)
def test_match(self, context, subsystem, sysname, ppair):
"""
Test that matches from different categories are a conjunction.
"""
prop_name, prop_value = ppair
kwargs = {prop_name: prop_value}
devices = frozenset(
context.list_devices().match(
subsystem=subsystem,
sys_name=sysname,
**kwargs
)
)
_test_direct_and_complement(
context,
devices,
lambda d: d.subsystem == subsystem and d.sys_name == sysname and \
d.properties.get(prop_name) == prop_value
)
class TestEnumeratorMatchMethod(object):
"""
Test the behavior of Enumerator.match.
Only methods that test behavior of this method by patching the Enumerator
object with the methods that match() should invoke belong here.
"""
_ENUMERATOR_STRATEGY = _CONTEXT_STRATEGY.map(lambda x: x.list_devices())
@given(_ENUMERATOR_STRATEGY)
@settings(max_examples=1)
def test_match_passthrough_subsystem(self, enumerator):
"""
Test that special keyword subsystem results in a match_subsystem call.
"""
with mock.patch.object(enumerator, 'match_subsystem',
autospec=True) as match_subsystem:
enumerator.match(subsystem=mock.sentinel.subsystem)
match_subsystem.assert_called_with(mock.sentinel.subsystem)
@given(_ENUMERATOR_STRATEGY)
@settings(max_examples=1)
def test_match_passthrough_sys_name(self, enumerator):
"""
Test that special keyword sys_name results in a match_sys_name call.
"""
with mock.patch.object(enumerator, 'match_sys_name',
autospec=True) as match_sys_name:
enumerator.match(sys_name=mock.sentinel.sys_name)
match_sys_name.assert_called_with(mock.sentinel.sys_name)
@given(_ENUMERATOR_STRATEGY)
@settings(max_examples=1)
def test_match_passthrough_tag(self, enumerator):
"""
Test that special keyword tag results in a match_tag call.
"""
with mock.patch.object(enumerator, 'match_tag',
autospec=True) as match_tag:
enumerator.match(tag=mock.sentinel.tag)
match_tag.assert_called_with(mock.sentinel.tag)
@_UDEV_TEST(172, "test_match_passthrough_parent")
@given(_ENUMERATOR_STRATEGY)
@settings(max_examples=1)
def test_match_passthrough_parent(self, enumerator):
"""
Test that special keyword 'parent' results in a match parent call.
"""
with mock.patch.object(enumerator, 'match_parent',
autospec=True) as match_parent:
enumerator.match(parent=mock.sentinel.parent)
match_parent.assert_called_with(mock.sentinel.parent)
@given(_ENUMERATOR_STRATEGY)
@settings(max_examples=1)
def test_match_passthrough_property(self, enumerator):
"""
Test that non-special keyword args are treated as properties.
"""
with mock.patch.object(enumerator, 'match_property',
autospec=True) as match_property:
enumerator.match(eggs=mock.sentinel.eggs, spam=mock.sentinel.spam)
assert match_property.call_count == 2
posargs = [args for args, _ in match_property.call_args_list]
assert ('spam', mock.sentinel.spam) in posargs
assert ('eggs', mock.sentinel.eggs) in posargs
|