def patch_filter_by(type): add_match = 'udev_monitor_filter_add_match_{0}'.format(type) filter_update = 'udev_monitor_filter_update' with pytest.patch_libudev(add_match) as add_match: add_match.return_value = 0 with pytest.patch_libudev(filter_update) as filter_update: filter_update.return_value = 0 yield add_match, filter_update
def test_find_parent_no_devtype_mock(self, device): get_parent = 'udev_device_get_parent_with_subsystem_devtype' ref = 'udev_device_ref' with pytest.nested(pytest.patch_libudev(get_parent), pytest.patch_libudev(ref)) as (get_parent, ref): get_parent.return_value = mock.sentinel.device ref.return_value = mock.sentinel.referenced_device parent = device.find_parent('subsystem') get_parent.assert_called_with(device, b'subsystem', None) ref.assert_called_with(mock.sentinel.device) assert isinstance(get_parent.call_args[0][1], bytes) assert isinstance(parent, Device) assert parent._as_parameter_ is mock.sentinel.referenced_device
def test_receive_device_mock(self, monitor): receive_device = 'udev_monitor_receive_device' get_action = 'udev_device_get_action' with pytest.nested(pytest.patch_libudev(receive_device), pytest.patch_libudev(get_action)) as (receive_device, get_action): receive_device.return_value = mock.sentinel.pointer get_action.return_value = b'action' action, device = monitor.receive_device() assert action == 'action' assert pytest.is_unicode_string(action) assert isinstance(device, Device) assert device.context is monitor.context assert device._as_parameter_ is mock.sentinel.pointer get_action.assert_called_with(mock.sentinel.pointer) receive_device.assert_called_with(monitor)
def test_is_initialized(self, device): assert isinstance(device.is_initialized, bool) get_is_initialized = 'udev_device_get_is_initialized' with pytest.patch_libudev(get_is_initialized) as get_is_initialized: get_is_initialized.return_value = True assert device.is_initialized get_is_initialized.assert_called_with(device)
def patch_libudev_list(items, list_func): linked_list = LinkedList.from_sequence(items) get_next = 'udev_list_entry_get_next' get_name = 'udev_list_entry_get_name' get_value = 'udev_list_entry_get_value' with pytest.nested(pytest.patch_libudev(get_next), pytest.patch_libudev(get_name), pytest.patch_libudev(get_value), pytest.patch_libudev(list_func)) as (get_next, get_name, get_value, list_func): list_func.return_value = linked_list.first get_name.side_effect = lambda e: e.name get_value.side_effect = lambda e: e.value get_next.side_effect = lambda e: e.next yield list_func
def test_time_since_initialized(self, device): assert isinstance(device.time_since_initialized, timedelta) usec_since_init = 'udev_device_get_usec_since_initialized' with pytest.patch_libudev(usec_since_init) as usec_since_init: usec_since_init.return_value = 100 assert device.time_since_initialized.microseconds == 100 usec_since_init.assert_called_with(device)
def test_contains_mock(self, device): """ Test that ``udev_device_has_tag`` is called if available. """ has_tag = 'udev_device_has_tag' with pytest.patch_libudev(has_tag) as has_tag: has_tag.return_value = 1 assert 'foo' in device.tags has_tag.assert_called_with(device, b'foo')
def test_match_tag_mock(self, context): add_match_tag = 'udev_enumerate_add_match_tag' enumerator = context.list_devices() with pytest.patch_libudev(add_match_tag) as add_match_tag: retval = enumerator.match_tag('spam') assert retval is enumerator add_match_tag.assert_called_with(enumerator, b'spam') args, _ = add_match_tag.call_args assert isinstance(args[1], bytes)
def test_pytest_filter_by_tag_mock(self, monitor): match_tag = 'udev_monitor_filter_add_match_tag' with pytest.patch_libudev(match_tag) as match_tag: match_tag.return_value = 0 monitor.filter_by_tag(b'spam') match_tag.assert_called_with(monitor, b'spam') monitor.filter_by_tag('eggs') match_tag.assert_called_with(monitor, b'eggs') assert isinstance(match_tag.call_args[0][1], bytes)
def _assert_from_netlink_called(context, *args): new_from_netlink = 'udev_monitor_new_from_netlink' with pytest.patch_libudev(new_from_netlink) as new_from_netlink: source = args[0].encode('ascii') if args else b'udev' new_from_netlink.return_value = mock.sentinel.pointer monitor = Monitor.from_netlink(context, *args) new_from_netlink.assert_called_with(context, source) assert isinstance(new_from_netlink.call_args[0][1], bytes) assert monitor._as_parameter_ is mock.sentinel.pointer
def test_filter_by_subsystem_dev_type_mock(self, monitor): add_match = 'udev_monitor_filter_add_match_subsystem_devtype' with pytest.patch_libudev(add_match) as add_match: add_match.return_value = 0 monitor.filter_by(b'input', b'usb_interface') add_match.assert_called_with(monitor, b'input', b'usb_interface') monitor.filter_by('input', 'usb_interface') add_match.assert_called_with(monitor, b'input', b'usb_interface') assert isinstance(add_match.call_args[0][2], bytes)
def test_from_socket_mock(self, context, socket_path): socket_path = str(socket_path) new_from_socket = 'udev_monitor_new_from_socket' with pytest.patch_libudev(new_from_socket) as new_from_socket: new_from_socket.return_value = mock.sentinel.pointer monitor = Monitor.from_socket(context, socket_path) new_from_socket.assert_called_with( context, socket_path.encode(sys.getfilesystemencoding())) assert monitor._as_parameter_ is mock.sentinel.pointer Monitor.from_socket(context, 'foobar') new_from_socket.assert_called_with(context, b'foobar') assert isinstance(new_from_socket.call_args[0][1], bytes)
def test_log_priority_set_mock(self, context): set_prio = 'udev_set_log_priority' with pytest.patch_libudev(set_prio) as set_prio: context.log_priority = mock.sentinel.log_priority set_prio.assert_called_with(context, mock.sentinel.log_priority)
def test_fileno_mock(self, monitor): get_fd = 'udev_monitor_get_fd' with pytest.patch_libudev(get_fd) as get_fd: get_fd.return_value = mock.sentinel.fileno assert monitor.fileno() is mock.sentinel.fileno get_fd.assert_called_with(monitor)
def test_log_priority_get_mock(self, context): get_prio = 'udev_get_log_priority' with pytest.patch_libudev(get_prio) as get_prio: get_prio.return_value = mock.sentinel.log_priority assert context.log_priority is mock.sentinel.log_priority get_prio.assert_called_with(context)
def test_set_receive_buffer_size_mock(self, monitor): set_receive_buffer_size = 'udev_monitor_set_receive_buffer_size' with pytest.patch_libudev(set_receive_buffer_size) as func: func.return_value = 0 monitor.set_receive_buffer_size(1000) func.assert_called_with(monitor, 1000)
def test_enable_receiving_mock(self, monitor): with pytest.patch_libudev('udev_monitor_enable_receiving') as func: func.return_value = 0 monitor.enable_receiving() func.assert_called_with(monitor)
def test_match_is_initialized(self, context): match_is_initialized = 'udev_enumerate_add_match_is_initialized' with pytest.patch_libudev(match_is_initialized) as match_is_initialized: context.list_devices().match_is_initialized() assert match_is_initialized.called