예제 #1
0
    def start_service(self, config_params):
        """Create/start an AP service with provided configurations.

        @param config_params dictionary of configuration parameters.
        @return string object path of the newly created service.

        """
        service = self._get_dbus_object(
            self.DBUS_SERVICE_INTERFACE,
            dbus_util.dbus2primitive(self._manager.CreateService()))
        # Get configuration object for the service.
        service_config = self._get_dbus_object(
            self.DBUS_CONFIG_INTERFACE,
            self._get_dbus_property(service, self.DBUS_SERVICE_INTERFACE,
                                    self.SERVICE_PROPERTY_CONFIG))
        # Set configuration properties.
        for name, value in config_params.iteritems():
            if name in self.CONFIG_PROPERTY_DBUS_TYPE_MAPPING:
                func = self.CONFIG_PROPERTY_DBUS_TYPE_MAPPING[name]
                self._set_dbus_property(service_config,
                                        self.DBUS_CONFIG_INTERFACE, name,
                                        func(value, variant_level=1))
            else:
                raise ApmanagerProxyError(
                    'Unknown configuration parameter [%s]' % name)

        # Start AP service.
        service.Start()
        return service.object_path
예제 #2
0
    def register_with_server(self):
        """Make buffet register with the cloud server.

        This includes the whole registration flow and ends with buffet
        obtained an access token for future interactions. The status
        is guaranteed to be STATUS_CONNECTED when this
        method returns.

        @return string: the device_id obtained during registration.

        """
        ticket = self._registration_client.create_registration_ticket()
        logging.info('Created ticket: %r', ticket)
        buffet = buffet_dbus_helper.BuffetDBusHelper()
        buffet.manager.UpdateDeviceInfo(dbus.String(TEST_NAME),
                                        dbus.String(TEST_DESCRIPTION),
                                        dbus.String(TEST_LOCATION))
        device_id = dbus_util.dbus2primitive(
            buffet.manager.RegisterDevice(dbus.String(ticket['id'])))
        # Confirm that registration has populated some fields.
        device_resource = self._device_client.get_device(device_id)
        logging.debug('Got device resource=%r', device_resource)
        _assert_has(device_resource, 'name', TEST_NAME, 'device resource')
        _assert_has(device_resource, 'modelManifestId', 'AATST',
                    'device resource')
        logging.info('Registration successful')
        self.check_buffet_status_is(STATUS_CONNECTED,
                                    expected_device_id=device_id,
                                    timeout_seconds=5)
        return device_id
예제 #3
0
    def dbus2primitive(cls, value):
        """Typecast values from dbus types to python types.

        @param value: dbus object to convert to a primitive.

        """
        return dbus_util.dbus2primitive(value)
예제 #4
0
    def check_buffet_is_polling(self, device_id, timeout_seconds=30):
        """Assert that buffet is polling for new commands.

        @param device_id: string device id created during registration.
        @param timeout_seconds: number of seconds to wait for polling
                to start.

        """
        new_command_message = ('This is message %d' %
                               len(self._expected_messages))
        command_resource = {
            'name': '%s.%s' % (TEST_COMMAND_CATEGORY, TEST_COMMAND_NAME),
            'deviceId': device_id,
            'parameters': {
                TEST_COMMAND_PARAM: new_command_message
            }
        }
        self._expected_messages.append(new_command_message)
        self._command_client.create_command(device_id, command_resource)
        # Confirm that the command eventually appears on buffet.
        buffet = buffet_dbus_helper.BuffetDBusHelper()
        polling_interval_seconds = 0.5
        start_time = time.time()
        while time.time() - start_time < timeout_seconds:
            objects = dbus_util.dbus2primitive(
                buffet.object_manager.GetManagedObjects())
            cmds = [
                interfaces[buffet_dbus_helper.COMMAND_INTERFACE]
                for path, interfaces in objects.iteritems()
                if buffet_dbus_helper.COMMAND_INTERFACE in interfaces
            ]
            messages = [
                cmd['Parameters'][TEST_COMMAND_PARAM] for cmd in cmds
                if (cmd['Name'] == '%s.%s' %
                    (TEST_COMMAND_CATEGORY, TEST_COMMAND_NAME))
            ]
            # |cmds| is a list of property sets
            if len(messages) != len(self._expected_messages):
                # Still waiting for our pending command to show up.
                time.sleep(polling_interval_seconds)
                continue
            logging.debug(
                'Finally saw the right number of commands over '
                'DBus: %r', cmds)
            if sorted(messages) != sorted(self._expected_messages):
                raise error.TestFail(
                    'Expected commands with messages=%r but got %r.' %
                    (self._expected_messages, messages))
            logging.info(
                'Buffet has DBus proxies for commands with '
                'messages: %r', self._expected_messages)
            return
        raise error.TestFail('Timed out waiting for Buffet to expose '
                             'pending commands with messages: %r' %
                             self._expected_messages)
예제 #5
0
    def _on_name_owner_changed(self, service_id, owner):
        """Callback that removes a service when the owner disconnects from DBus.

        @param service_id: string service_id of service to remove.
        @param owner: dbus.String identifier of service owner.

        """
        owner = dbus_util.dbus2primitive(owner)
        logging.debug('Name owner for service=%s changed to "%s".', service_id,
                      owner)
        if not owner:
            self.RemoveExposedService(service_id)
예제 #6
0
 def _get_peers(self):
     object_manager = dbus.Interface(
         self._bus.get_object(SERVICE_NAME, DBUS_PATH_OBJECT_MANAGER),
         DBUS_INTERFACE_OBJECT_MANAGER)
     # |dbus_objects| is a map<object path,
     #                         map<interface name,
     #                             map<property name, value>>>
     dbus_objects = object_manager.GetManagedObjects()
     objects = dbus_util.dbus2primitive(dbus_objects)
     peer_objects = [(path, interfaces)
                     for path, interfaces in objects.iteritems()
                     if (path.startswith(PEER_PATH_PREFIX)
                         and DBUS_INTERFACE_PEER in interfaces)]
     peers = []
     for peer_path, interfaces in peer_objects:
         service_property_sets = [
             interfaces[DBUS_INTERFACE_SERVICE]
             for path, interfaces in objects.iteritems()
             if (path.startswith(peer_path + '/services/')
                 and DBUS_INTERFACE_SERVICE in interfaces)
         ]
         services = []
         for service_properties in service_property_sets:
             logging.debug('Found service with properties: %r',
                           service_properties)
             ip_addrs = [
                 ('.'.join(map(str, ip)), port)
                 for ip, port in service_properties[SERVICE_PROPERTY_IPS]
             ]
             services.append(
                 Service(
                     service_id=service_properties[SERVICE_PROPERTY_ID],
                     service_info=service_properties[SERVICE_PROPERTY_INFO],
                     service_ips=ip_addrs))
         peer_properties = interfaces[DBUS_INTERFACE_PEER]
         peer = Peer(uuid=peer_properties[PEER_PROPERTY_ID],
                     last_seen=peer_properties[PEER_PROPERTY_LAST_SEEN],
                     services=services)
         peers.append(peer)
     return peers
예제 #7
0
    def _get_dbus_property(self, dbus_object, interface_name, property_key):
        """get property on a dbus Interface

        @param dbus_object DBus object to read property from
        @param interface_name string name of the interface
        @param property_key string name of property on interface
        @return python typed object representing property value or None

        """
        # Get the property interface for the given DBus object.
        property_interface = self._get_dbus_object(
            self.DBUS_PROPERTY_INTERFACE, dbus_object.object_path)
        # Invoke Get method on the property interface.
        try:
            value = dbus_util.dbus2primitive(
                property_interface.Get(dbus.String(interface_name),
                                       dbus.String(property_key)))
        except dbus.exceptions.DBusException as e:
            raise ApmanagerProxyError(
                'Failed to get property %s on interface %s' %
                (property_key, interface_name))
        return value
 def __getattr__(self, name):
     components = name.split('_')
     name = ''.join(x.title() for x in components)
     dbus_value = self.properties.Get(MANAGER_INTERFACE, name)
     return dbus_util.dbus2primitive(dbus_value)