def test_events_real(self, context, monitor): # make sure that the module is unloaded initially pytest.unload_dummy() monitor.filter_by('net') monitor.start() self.prepare_test(monitor) # setup signal handlers event_callback = mock.Mock(side_effect=self.stop_when_done) added_callback = mock.Mock(side_effect=self.stop_when_done) removed_callback = mock.Mock(side_effect=self.stop_when_done) self.connect_signal(event_callback) self.connect_signal(added_callback, action='add') self.connect_signal(removed_callback, action='remove') # test add event self.start_event_loop(pytest.load_dummy) device = Device.from_path(context, '/devices/virtual/net/dummy0') event_callback.assert_called_with('add', device) added_callback.assert_called_with(device) assert not removed_callback.called for callback in (event_callback, added_callback, removed_callback): callback.reset_mock() self.start_event_loop(pytest.unload_dummy) event_callback.assert_called_with('remove', device) assert not added_callback.called removed_callback.assert_called_with(device)
def test_events_real(self, context, monitor): # make sure that the module is unloaded initially pytest.unload_dummy() monitor.filter_by('net') monitor.start() self.prepare_test(monitor) # setup signal handlers event_callback = Mock(side_effect=self.stop_when_done) added_callback = Mock(side_effect=self.stop_when_done) removed_callback = Mock(side_effect=self.stop_when_done) self.connect_signal(event_callback) self.connect_signal(added_callback, action='add') self.connect_signal(removed_callback, action='remove') # test add event self.start_event_loop(pytest.load_dummy) device = Device.from_path(context, '/devices/virtual/net/dummy0') event_callback.assert_called_with('add', device) added_callback.assert_called_with(device) assert not removed_callback.called for mock in (event_callback, added_callback, removed_callback): mock.reset_mock() self.start_event_loop(pytest.unload_dummy) event_callback.assert_called_with('remove', device) assert not added_callback.called removed_callback.assert_called_with(device)
def test_asstring(self, a_context, device_datum): """ Test that string value agrees with cli value and is unicode. """ device = Device.from_path(a_context, device_datum.device_path) for key, value in non_volatile_attributes(device_datum.attributes): assert is_unicode_string(device.attributes.asstring(key)) assert device.attributes.asstring(key) == value
def test_device_ordering(self, context, operator): try: device = Device.from_path(context, "/devices/platform") except DeviceNotFoundAtPathError: pytest.skip("device not found") with pytest.raises(TypeError) as exc_info: operator(device, device) assert str(exc_info.value) == "Device not orderable"
def test_device_ordering(self, context, operator): try: device = Device.from_path(context, '/devices/platform') except DeviceNotFoundAtPathError: pytest.skip('device not found') with pytest.raises(TypeError) as exc_info: operator(device, device) assert str(exc_info.value) == 'Device not orderable'
def test_match_parent(self, context, device_data): device = Device.from_path(context, device_data.device_path) parent = device.parent if parent is None: pytest.skip('Device {0!r} has no parent'.format(device)) else: children = list(context.list_devices().match_parent(parent)) assert device in children assert parent in children
def test_asint(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) for key, value in self.non_volatile_items(device_datum.attributes): try: value = int(value) except ValueError: with pytest.raises(ValueError): device.attributes.asint(key) else: assert device.attributes.asint(key) == value
def test_getitem(self, a_context, device_datum): """ Test that attribute value is the same as datum attribute value and is instance of bytes. """ device = Device.from_path(a_context, device_datum.device_path) for key, value in non_volatile_attributes(device_datum.attributes): raw_value = value.encode(sys.getfilesystemencoding()) assert isinstance(device.attributes.get(key), bytes) assert device.attributes.get(key) == raw_value
def test_asbool(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) for key, value in self.non_volatile_items(device_datum.attributes): if value == '1': assert device.attributes.asbool(key) elif value == '0': assert not device.attributes.asbool(key) else: with pytest.raises(ValueError) as exc_info: device.attributes.asbool(key) message = 'Not a boolean value:' assert str(exc_info.value).startswith(message)
def test_asint(self, a_context, device_datum): """ Test that integer result is an int or ValueError raised. """ device = Device.from_path(a_context, device_datum.device_path) for key, value in non_volatile_attributes(device_datum.attributes): try: value = int(value) except ValueError: with pytest.raises(ValueError): device.attributes.asint(key) else: assert device.attributes.asint(key) == value
def test_events_real(self, context, monitor): # make sure that the module is unloaded initially pytest.unload_dummy() monitor.filter_by('net') monitor.start() self.prepare_test(monitor) # setup signal handlers event_callback = mock.Mock( side_effect=lambda *args: self.stop_event_loop()) self.connect_signal(event_callback) # test add event self.start_event_loop(pytest.load_dummy) device = Device.from_path(context, '/devices/virtual/net/dummy0') event_callback.assert_called_with(device) event_callback.reset_mock() self.start_event_loop(pytest.unload_dummy) event_callback.assert_called_with(device)
def test_getitem(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) for key, value in self.non_volatile_items(device_datum.attributes): raw_value = value.encode(sys.getfilesystemencoding()) assert isinstance(device.attributes.get(key), bytes) assert device.attributes.get(key) == raw_value
def pytest_funcarg__fake_monitor_device(request): context = request.getfuncargvalue('context') try: return Device.from_path(context, '/devices/platform') except DeviceNotFoundAtPathError: pytest.skip('device not found')
from __future__ import division from __future__ import print_function from __future__ import unicode_literals from hypothesis import strategies import pytest from pyudev import Context from pyudev import Device from .utils import udev _CONTEXT = Context() _DEVICE_DATA = list(udev.get_device_sample(udev.DeviceDatabase.db())) _DEVICES = [Device.from_path(_CONTEXT, d.device_path) for d in _DEVICE_DATA] _CONTEXT_STRATEGY = strategies.just(_CONTEXT) _UDEV_VERSION = int(udev.UDevAdm.adm().query_udev_version()) def _UDEV_TEST(version, node=None): # pylint: disable=invalid-name fmt_str = "%s: udev version must be at least %s, is %s" return pytest.mark.skipif( _UDEV_VERSION < version, reason=fmt_str % (node, version, _UDEV_VERSION) ) _VOLATILE_ATTRIBUTES = ('energy_uj', 'power_on_acct') def non_volatile_attributes(attributes): """
def test_from_path_strips_leading_slash(self, context): assert Device.from_path(context, 'devices/platform') == \ Device.from_path(context, '/devices/platform')
def pytest_funcarg__device(request): device_data = getattr(request, 'param', None) or \ request.getfuncargvalue('device_data') context = request.getfuncargvalue('context') return Device.from_path(context, device_data.device_path)
def test_iteration(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) assert set(device.tags) == set(device_datum.tags) for tag in device.tags: assert is_unicode_string(tag)
def test_from_path_strips_leading_slash(self, context): try: assert Device.from_path(context, "devices/platform") == Device.from_path(context, "/devices/platform") except DeviceNotFoundAtPathError: pytest.skip("device not found")
def pytest_funcarg__device(request): device_data = getattr(request, "param", None) or request.getfuncargvalue("device_data") context = request.getfuncargvalue("context") return Device.from_path(context, device_data.device_path)
def poll_udev(): battery = Device.from_path(self.context, self.battery.device_path) on_udev_event(battery) GLib.timeout_add(interval, poll_udev)
def test_asstring(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) for key, value in self.non_volatile_items(device_datum.attributes): assert is_unicode_string(device.attributes.asstring(key)) assert device.attributes.asstring(key) == value
def test_device_ordering(self, context, operator): device = Device.from_path(context, '/devices/platform') with pytest.raises(TypeError) as exc_info: operator(device, device) assert str(exc_info.value) == 'Device not orderable'
def fake_monitor_device(request): context = request.getfuncargvalue('context') device = get_device_sample(DeviceDatabase.db(), sample_size=1)[0] return Device.from_path(context, device.device_path)
def test_asstring(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) for key, value in self.non_volatile_items(device_datum.attributes): assert is_unicode_string( device.attributes.asstring(key)) assert device.attributes.asstring(key) == value
def test_from_path(self, context, device_data): device = Device.from_path(context, device_data.device_path) assert device is not None assert device == Device.from_sys_path(context, device_data.sys_path) assert device == Device.from_path(context, device_data.sys_path)
def pytest_funcarg__fake_monitor_device(request): context = request.getfuncargvalue('context') return Device.from_path(context, '/devices/platform')
def test_contains(self, a_context, device_datum): device = Device.from_path(a_context, device_datum.device_path) for tag in device_datum.tags: assert tag in device.tags
def test_from_path_strips_leading_slash(self, context): try: assert Device.from_path(context, 'devices/platform') == \ Device.from_path(context, '/devices/platform') except DeviceNotFoundAtPathError: pytest.skip('device not found')