def test_proxy_instance_catches_unknown_service_exception(self):
        query = xpathselect.Query.root('foo')
        e = DBusException(name='org.freedesktop.DBus.Error.ServiceUnknown')
        fake_dbus_address = Mock()
        fake_dbus_address.introspection_iface.GetState.side_effect = e
        backend = backends.Backend(fake_dbus_address)

        self.assertRaises(RuntimeError, backend.execute_query_get_data, query)
    def test_proxy_instance_raises_uncaught_exceptions(self):
        query = xpathselect.Query.root('foo')
        e = Exception()
        fake_dbus_address = Mock()
        fake_dbus_address.introspection_iface.GetState.side_effect = e
        backend = backends.Backend(fake_dbus_address)

        self.assertRaises(Exception, backend.execute_query_get_data, query)
    def test_proxy_instances_returns_list(self, mio):
        query = xpathselect.Query.root('foo')
        fake_dbus_address = Mock()
        fake_dbus_address.introspection_iface.GetState.return_value = [
            (b'/root/path', {}) for i in range(1)
        ]
        backend = backends.Backend(fake_dbus_address)

        self.assertThat(backend.execute_query_get_proxy_instances(query, 0),
                        Equals([None]))
 def test_unknown_service_exception_gives_correct_msg(self):
     query = xpathselect.Query.root('foo')
     e = DBusException(name='org.freedesktop.DBus.Error.ServiceUnknown')
     fake_dbus_address = Mock()
     fake_dbus_address.introspection_iface.GetState.side_effect = e
     backend = backends.Backend(fake_dbus_address)
     try:
         backend.execute_query_get_data(query)
     except RuntimeError as e:
         msg = ("Lost dbus backend communication. It appears the "
                "application under test exited before the test "
                "finished!")
         self.assertEqual(str(e), msg)
    def test_small_query_returns_dont_log_warnings(self, mock_logger):
        """Queries that return small numbers of items must not log a warning.

        'small' is defined as 15 or fewer.

        """
        query = xpathselect.Query.root('foo')
        fake_dbus_address = Mock()
        fake_dbus_address.introspection_iface.GetState.return_value = \
            [(b'/root/path', {}) for i in range(15)]
        backend = backends.Backend(fake_dbus_address)
        backend.execute_query_get_data(query, )

        self.assertThat(mock_logger.warning.called, Equals(False))
Exemple #6
0
 def build_proxy(introspection_xml, cls_name, path, cls_state):
     # Figure out if the backend has any extension methods, and return
     # classes that understand how to use each of those extensions:
     extension_classes = _get_proxy_bases_from_introspection_xml(
         introspection_xml)
     # Register those base classes for everything that will derive from this
     # emulator base class.
     _object_registry.register_extension_classes_for_proxy_base(
         emulator_base,
         extension_classes,
     )
     proxy_class = _object_registry._get_proxy_object_class(
         emulator_base._id, path, cls_state)
     reply_handler(
         proxy_class(cls_state, path, backends.Backend(data_source)))
    def test_proxy_instances_with_clientside_filtering_returns_list(self, mio):
        query = xpathselect.Query.root('foo')
        query.needs_client_side_filtering = Mock(return_value=True)
        fake_dbus_address = Mock()
        fake_dbus_address.introspection_iface.GetState.return_value = [
            (b'/root/path', {}) for i in range(1)
        ]
        backend = backends.Backend(fake_dbus_address)

        with patch.object(backends,
                          '_object_passes_filters',
                          return_value=True):
            self.assertThat(
                backend.execute_query_get_proxy_instances(query, 0),
                Equals([None]))
    def test_large_query_returns_log_warnings(self, mock_logger):
        """Queries that return large numbers of items must cause a log warning.

        'large' is defined as more than 15.

        """
        query = xpathselect.Query.root('foo')
        fake_dbus_address = Mock()
        fake_dbus_address.introspection_iface.GetState.return_value = \
            [(b'/root/path', {}) for i in range(16)]
        backend = backends.Backend(fake_dbus_address)
        backend.execute_query_get_data(query, )

        mock_logger.warning.assert_called_once_with(
            "Your query '%r' returned a lot of data (%d items). This "
            "is likely to be slow. You may want to consider optimising"
            " your query to return fewer items.", query, 16)
Exemple #9
0
def _make_proxy_object(dbus_address, emulator_base):
    """Returns a root proxy object given a DBus service name.

    :param dbus_address: The DBusAddress object we're querying.
    :param emulator_base: The emulator base object (or None), as provided by
        the user.
    """
    # make sure we always have an emulator base. Either use the one the user
    # gave us, or make one:
    emulator_base = emulator_base or _make_default_emulator_base()
    _raise_if_base_class_not_actually_base(emulator_base)

    # Get the dbus introspection Xml for the backend.
    intro_xml = _get_introspection_xml_from_backend(dbus_address)
    try:
        # Figure out if the backend has any extension methods, and return
        # classes that understand how to use each of those extensions:
        extension_classes = _get_proxy_bases_from_introspection_xml(intro_xml)

        # Register those base classes for everything that will derive from this
        # emulator base class.
        _object_registry.register_extension_classes_for_proxy_base(
            emulator_base,
            extension_classes,
        )
    except RuntimeError as e:
        e.args = ("Could not find Autopilot interface on dbus address '%s'." %
                  dbus_address, )
        raise e

    cls_name, path, cls_state = _get_proxy_object_class_name_and_state(
        dbus_address)

    proxy_class = _object_registry._get_proxy_object_class(
        emulator_base._id, path, cls_state)
    # For this object only, add the ApplicationProxy class, since it's the
    # root of the tree. Ideally this would be nicer...
    if ApplicationProxyObject not in proxy_class.__bases__:
        proxy_class.__bases__ += (ApplicationProxyObject, )
    return proxy_class(cls_state, path, backends.Backend(dbus_address))