Exemple #1
0
def _check_device(device):
    """
    Check that device exists by getting it.
    """
    try:
        Devices.from_path(_CONTEXT, device.sys_path)
        return True
    except DeviceNotFoundError:
        return False
    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 = Devices.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)
Exemple #3
0
 def test_iteration_and_contains(self, a_context, device_datum):
     """
     Test that iteration yields all tags and contains checks them.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     assert frozenset(device.tags) == frozenset(device_datum.tags)
     assert all(is_unicode_string(tag) for tag in device.tags)
Exemple #4
0
 def test_getitem_devname(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     data_devname = os.path.join(
         a_context.device_path, device_datum.properties['DEVNAME'])
     device_devname = \
        os.path.join(a_context.device_path, device.properties['DEVNAME'])
     assert device_devname == data_devname
Exemple #5
0
 def test_key_subset(self, a_context, device_datum):
     """
     Verify that the device contains all the keys in the device datum.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     assert frozenset(device_datum.properties.keys()) <= \
        frozenset(device.properties.keys())
Exemple #6
0
 def test_length(self, a_context, device_datum):
     """
     Verify that the keys in the device and in the datum are equal.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     assert frozenset(device_datum.properties.keys()) == \
        frozenset(device.properties.keys())
Exemple #7
0
 def test_asstring(self, a_context, device_datum):
     """
     Test that attribute exists for actual device and is unicode.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     assert all(is_unicode_string(device.attributes.asstring(key)) \
        for key in device_datum.attributes.keys())
Exemple #8
0
 def test_getitem(self, a_context, device_datum):
     """
     Test that attribute value exists and is instance of bytes.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     assert all(isinstance(device.attributes.get(key), bytes) \
        for key in device_datum.attributes.keys())
Exemple #9
0
 def test_iteration(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for property in device:
         assert is_unicode_string(property)
     # test that iteration really yields all properties
     device_properties = set(device)
     for property in device_datum.properties:
         assert property in device_properties
Exemple #10
0
 def test_asint(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for property, value in device_datum.properties.items():
         try:
             value = int(value)
         except ValueError:
             with pytest.raises(ValueError):
                 device.asint(property)
         else:
             assert device.asint(property) == value
Exemple #11
0
 def test_getitem(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for prop in device_datum.properties:
         if prop == 'DEVLINKS':
             assert sorted(device.properties[prop].split(),) == \
                sorted(device_datum.properties[prop].split(),)
         elif prop == 'TAGS':
             assert sorted(device.properties[prop].split(':'),) == \
                sorted(device_datum.properties[prop].split(':'),)
         else:
             assert device.properties[prop] == device_datum.properties[prop]
Exemple #12
0
 def test_asbool(self, a_context, device_datum):
     """
     Test that bool result is a bool or ValueError raised.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     for key, value in device_datum.attributes.items():
         if value in ('0', '1'):
             assert device.attributes.asbool(key) in (False, True)
         else:
             with pytest.raises(ValueError):
                 device.attributes.asbool(key)
Exemple #13
0
 def test_asint(self, a_context, device_datum):
     """
     Test that integer result is an int or ValueError raised.
     """
     device = Devices.from_path(a_context, device_datum.device_path)
     for key, value in device_datum.attributes.items():
         try:
             value = int(value)
         except ValueError:
             with pytest.raises(ValueError):
                 device.attributes.asint(key)
Exemple #14
0
 def test_asbool(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for prop, value in device_datum.properties.items():
         if value == '1':
             assert device.asbool(prop)
         elif value == '0':
             assert not device.asbool(prop)
         else:
             with pytest.raises(ValueError) as exc_info:
                 device.asbool(prop)
             message = 'Not a boolean value: {0!r}'
             assert str(exc_info.value) == message.format(value)
Exemple #15
0
 def test_getitem(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for prop in device_datum.properties:
         if prop == 'DEVLINKS':
             assert sorted(device.properties[prop].split(),) == \
                sorted(device_datum.properties[prop].split(),)
         elif prop == 'TAGS':
             assert sorted(device.properties[prop].split(':'),) == \
                sorted(device_datum.properties[prop].split(':'),)
         else:
             # Do not test equality of device properties with udevadm oracle.
             # https://bugzilla.redhat.com/show_bug.cgi?id=1787089
             pass
Exemple #16
0
    def test_asbool(self, a_context, device_datum):
        """
        Test that values of 1 and 0 get properly interpreted as bool
        and that all other values raise a ValueError.

        :param Context a_context: libudev context
        :param device_datum: a device datum
        """
        device = Devices.from_path(a_context, device_datum.device_path)
        for prop, value in device_datum.properties.items():
            if value == '1':
                assert device.properties.asbool(prop)
            elif value == '0':
                assert not device.properties.asbool(prop)
            else:
                with pytest.raises(ValueError) as exc_info:
                    device.properties.asbool(prop)
Exemple #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 = Devices.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)
Exemple #18
0
    def test_from_device_file_links(self, a_context, device_datum):
        """
        For each link in DEVLINKS, test that the constructed device's
        path matches the orginal devices path.

        This does not hold true in the case of multipath. In this case
        udevadm's DEVLINKS fields holds some links that do not actually
        point to the originating device.

        See: https://bugzilla.redhat.com/show_bug.cgi?id=1263441.
        """
        device = Devices.from_path(a_context, device_datum.device_path)
        assume(not 'DM_MULTIPATH_TIMESTAMP' in device.properties)

        for link in device_datum.device_links:
            link = os.path.join(a_context.device_path, link)

            device = Devices.from_device_file(a_context, link)
            assert device.device_path == device_datum.device_path
            assert link in device.device_links
Exemple #19
0
    def test_from_device_file_links(self, a_context, device_datum):
        """
        For each link in DEVLINKS, test that the constructed device's
        path matches the orginal devices path.

        This does not hold true in the case of multipath. In this case
        udevadm's DEVLINKS fields holds some links that do not actually
        point to the originating device.

        See: https://bugzilla.redhat.com/show_bug.cgi?id=1263441.
        """
        device = Devices.from_path(a_context, device_datum.device_path)
        assume(not 'DM_MULTIPATH_TIMESTAMP' in device)

        for link in device_datum.device_links:
            link = os.path.join(a_context.device_path, link)

            device = Devices.from_device_file(a_context, link)
            assert device.device_path == device_datum.device_path
            assert link in device.device_links
Exemple #20
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 = Devices.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)
Exemple #21
0
def fake_monitor_device(request):
    context = request.getfixturevalue("context")
    return Devices.from_path(context, "/devices/platform")
Exemple #22
0
 def test_sys_path(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.sys_path == device_datum.sys_path
     assert is_unicode_string(device.sys_path)
Exemple #23
0
 def test_driver(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.driver == device_datum.properties.get('DRIVER')
     if device.driver:
         assert is_unicode_string(device.driver)
Exemple #24
0
 def test_links(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert sorted(device.device_links) == sorted(device_datum.device_links)
     for link in device.device_links:
         assert is_unicode_string(link)
Exemple #25
0
 def test_device_node(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.device_node == device_datum.device_node
     if device.device_node:
         assert is_unicode_string(device.device_node)
Exemple #26
0
 def test_type(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.device_type == device_datum.properties.get('DEVTYPE')
     if device.device_type:
         assert is_unicode_string(device.device_type)
Exemple #27
0
 def test_device_number(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.device_number == device_datum.device_number
Exemple #28
0
 def test_from_path(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device is not None
     assert device == \
        Devices.from_sys_path(a_context, device_datum.sys_path)
     assert device == Devices.from_path(a_context, device_datum.sys_path)
Exemple #29
0
def fake_monitor_device(request):
    context = request.getfuncargvalue('context')
    device = random.choice(list(DeviceDatabase.db()))
    return Devices.from_path(context, device.device_path)
Exemple #30
0
 def test_getitem_devname(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     data_devname = os.path.join(
         a_context.device_path, device_datum.properties['DEVNAME'])
     device_devname = os.path.join(a_context.device_path, device['DEVNAME'])
     assert device_devname == data_devname
Exemple #31
0
 def test_getitem(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for prop in device_datum.properties:
         assert device[prop] == device_datum.properties[prop]
Exemple #32
0
 def test_length(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert len(device) == len(device_datum.properties)
Exemple #33
0
 def test_links(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert sorted(device.device_links) == sorted(device_datum.device_links)
     for link in device.device_links:
         assert is_unicode_string(link)
Exemple #34
0
def fake_monitor_device(request):
    context = request.getfixturevalue('context')
    device = random.choice(list(DeviceDatabase.db()))
    return Devices.from_path(context, device.device_path)
Exemple #35
0
 def test_sys_path(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.sys_path == device_datum.sys_path
     assert is_unicode_string(device.sys_path)
Exemple #36
0
 def test_device_node(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.device_node == device_datum.device_node
     if device.device_node:
         assert is_unicode_string(device.device_node)
Exemple #37
0
 def test_from_path_strips_leading_slash(self, a_context, device_datum):
     path = device_datum.device_path
     assert Devices.from_path(a_context, path[1:]) == \
            Devices.from_path(a_context, path)
Exemple #38
0
 def test_from_path(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device is not None
     assert device == \
        Devices.from_sys_path(a_context, device_datum.sys_path)
     assert device == Devices.from_path(a_context, device_datum.sys_path)
Exemple #39
0
 def test_subsystem(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.subsystem == device_datum.properties['SUBSYSTEM']
     assert is_unicode_string(device.subsystem)
Exemple #40
0
 def test_from_path_strips_leading_slash(self, a_context, device_datum):
     path = device_datum.device_path
     assert Devices.from_path(a_context, path[1:]) == \
            Devices.from_path(a_context, path)
Exemple #41
0
 def test_driver(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.driver == device_datum.properties.get('DRIVER')
     if device.driver:
         assert is_unicode_string(device.driver)
Exemple #42
0
_CONTEXT = Context()


def _check_device(device):
    """
    Check that device exists by getting it.
    """
    try:
        Devices.from_path(_CONTEXT, device.sys_path)
        return True
    except DeviceNotFoundError:
        return False


_DEVICE_DATA = udev.DeviceDatabase.db()
_DEVICES = [Devices.from_path(_CONTEXT, d.device_path) for d in _DEVICE_DATA]


def device_strategy(require_existing=True, filter_func=lambda x: True):
    """
    Strategy that yields filtered devices.

    The devices are filtered before being sampled to reduce the number
    of health failures.

    :param bool require_existing: at the very last, verify existance
    :param filter_func: a function to be used as a filter
    :type filter_func: Device -> bool
    """
    strategy = strategies.sampled_from(x for x in _CONTEXT.list_devices()
                                       if filter_func(x))
Exemple #43
0
 def test_device_number(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.device_number == device_datum.device_number
Exemple #44
0
 def test_length(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert len(device) == len(device_datum.properties)
Exemple #45
0
 def test_subsystem(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.subsystem == device_datum.properties['SUBSYSTEM']
     assert is_unicode_string(device.subsystem)
Exemple #46
0
 def test_type(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     assert device.device_type == device_datum.properties.get('DEVTYPE')
     if device.device_type:
         assert is_unicode_string(device.device_type)
Exemple #47
0
 def test_getitem(self, a_context, device_datum):
     device = Devices.from_path(a_context, device_datum.device_path)
     for prop in device_datum.properties:
         assert device[prop] == device_datum.properties[prop]
def fake_monitor_device(request):
    context = request.getfuncargvalue('context')
    return Devices.from_path(context, '/devices/platform')
Exemple #49
0
 def test_from_path(self, a_context, a_device):
     """
     from_path() method yields correct device.
     """
     assert a_device == Devices.from_path(a_context, a_device.device_path)
def fake_monitor_device(request):
    context = request.getfuncargvalue('context')
    return Devices.from_path(context, '/devices/platform')