Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
 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"
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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'
Ejemplo n.º 7
0
 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
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
 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
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
0
 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
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 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)
Ejemplo n.º 15
0
 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
Ejemplo n.º 16
0
 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
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
 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
Ejemplo n.º 20
0
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')
Ejemplo n.º 21
0
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):
    """
Ejemplo n.º 22
0
 def test_from_path_strips_leading_slash(self, context):
     assert Device.from_path(context, 'devices/platform') == \
            Device.from_path(context, '/devices/platform')
Ejemplo n.º 23
0
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)
Ejemplo n.º 24
0
 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)
Ejemplo n.º 25
0
 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")
Ejemplo n.º 26
0
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)
Ejemplo n.º 27
0
 def poll_udev():
     battery = Device.from_path(self.context, self.battery.device_path)
     on_udev_event(battery)
     GLib.timeout_add(interval, poll_udev)
Ejemplo n.º 28
0
 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
Ejemplo n.º 29
0
 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'
Ejemplo n.º 30
0
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)
Ejemplo n.º 31
0
 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
Ejemplo n.º 32
0
 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'
Ejemplo n.º 33
0
 def test_from_path_strips_leading_slash(self, context):
     assert Device.from_path(context, 'devices/platform') == \
            Device.from_path(context, '/devices/platform')
Ejemplo n.º 34
0
 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)
Ejemplo n.º 35
0
def pytest_funcarg__fake_monitor_device(request):
    context = request.getfuncargvalue('context')
    return Device.from_path(context, '/devices/platform')
Ejemplo n.º 36
0
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)
Ejemplo n.º 37
0
 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
Ejemplo n.º 38
0
 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
Ejemplo n.º 39
0
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)
Ejemplo n.º 40
0
 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)
Ejemplo n.º 41
0
 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')
Ejemplo n.º 42
0
def pytest_funcarg__fake_monitor_device(request):
    context = request.getfuncargvalue('context')
    return Device.from_path(context, '/devices/platform')
Ejemplo n.º 43
0
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')