示例#1
0
class Application(object):
    def __init__(self,
                 application_config,
                 entry_points=['zaf.addons', 'zaf.local_addons'],
                 signalhandler=None):
        root_logger = logging.getLogger()
        # Default config for rootlogger to not spam until logger is correctly configured
        root_logger.setLevel(logging.INFO)

        self.app_config = application_config
        self.signalhandler = signalhandler
        self.entry_points = entry_points
        self.extension_manager = ExtensionManager()
        self.component_manager = ComponentManager()
        self.component_factory = Factory(self.component_manager)
        self.session_scope = self.component_factory.enter_scope('session')
        self.messagebus = MessageBus(self.component_factory,
                                     self.session_scope)
        self.messagebus.define_endpoints_and_messages(
            {APPLICATION_ENDPOINT: [BEFORE_COMMAND, AFTER_COMMAND]})
        self.config = ConfigManager()
        self.command = None
        self._exit_code = 1
        self.app_config.apply_configuration(self.config)

        @component(name='MessageBus', scope='session')
        def messagebus():
            """
            Access the message bus.

            The message bus can be used to register dispatchers to specific
            endpoints, message_ids and entities and to send requests and events
            to the registered dispatchers.
            """
            return self.messagebus

        @component(name='ComponentFactory', scope='session')
        def component_factory():
            """
            Access the component factory.

            The component factory can be used to call callables inside a scope
            and have the correct components instantiated in the scope.
            """
            return self.component_factory

        @component(name='ComponentManager', scope='session')
        def component_manager():
            """
            Access the component manager.

            The component manager can be used to find out what components are
            available.
            """
            return self.component_manager

        @component(name='Config', scope='session')
        def config():
            """
            Access the Config Manager.

            The Config components gives full access to all the config.
            """
            return self.config

        @component(name='ExtensionManager', scope='session')
        def extension_manager():
            """
            Access the extension manager.

            The extension manager can be used to find out what extensions are
            loaded.
            """
            return self.extension_manager

    def run(self):
        with self as instance:
            instance._exit_code = instance.execute_command()
        return instance._exit_code

    def setup(self):
        application_config_options = [
            ConfigOption(MESSAGEBUS_TIMEOUT, required=False),
            ConfigOption(CWD, required=True),
        ]
        loader = ExtensionLoader(self.extension_manager, self.config,
                                 self.messagebus, application_config_options,
                                 self.component_manager)
        self.command = loader.load_extensions(self.entry_points)

    def teardown(self):
        try:
            self.messagebus.wait_for_not_active(
                timeout=self.config.get(MESSAGEBUS_TIMEOUT))
        except MessageBusTimeout as e:
            logger.critical(
                'Waiting for messagebus to be inactive timed out,'
                ' shutting down anyway. State: {state}'.format(state=e))
        finally:
            logger.debug(
                'Dispatchers still registered to the messagebus: {dispatchers}'
                .format(dispatchers=self.messagebus.get_dispatchers()))
            self.extension_manager.destroy()
            for th in threading.enumerate():
                try:
                    if th.name != 'MainThread':
                        logger.debug(th)
                        logger.debug('\n'.join(
                            traceback.format_stack(
                                sys._current_frames()[th.ident])))
                except KeyError:
                    logger.debug(th)

    def execute_command(self):
        logger.debug(
            'Executing command {command} for application {application} with version {version}'
            .format(command=self.command.name,
                    application=self.app_config.name,
                    version=self.app_config.version))
        self._activate_signalhandler()
        self.messagebus.trigger_event(BEFORE_COMMAND,
                                      APPLICATION_ENDPOINT,
                                      data=self.command.name)
        result = 0
        try:
            result = self.component_factory.call(self.command.callable,
                                                 self.session_scope, self)
            return result if result is not None else 0
        finally:
            logger.debug(
                'Command {command} exited with exit code {result}'.format(
                    command=self.command.name,
                    result=result).format(command=self.command.name,
                                          application=self.app_config.name,
                                          version=self.app_config.version))

            self.component_factory.exit_scope(self.session_scope)
            self.messagebus.trigger_event(AFTER_COMMAND,
                                          APPLICATION_ENDPOINT,
                                          data=self.command.name)

    def gather_metadata(self, metadata_filter=None):
        return ZafMetadata(self.extension_manager.all_extensions,
                           self.component_manager.get_components_info(),
                           self.config, self.app_config.entrypoint,
                           self.extension_manager, metadata_filter)

    def __enter__(self):
        try:
            self.setup()
        except Exception:
            self.__exit__(*sys.exc_info())
            raise
        return self

    def __exit__(self, *args):
        self.teardown()

    @property
    def exit_code(self):
        return self._exit_code

    def _activate_signalhandler(self):
        if self.signalhandler is not None:
            self.signalhandler.activate(self.messagebus)

    def _deactivate_signalhandler(self):
        if self.signalhandler is not None:
            self.signalhandler.deactivate()
示例#2
0
class ExtensionTestHarness(object):
    """
    Class that helps testing extensions by creating and configuration the surroundings.

    It can be used as a context manager and helps with multi threaded tests.

    .. code-block:: python

        with ExtensionTestHarness(
                MyExtension(), {endpoint: [message1, message2]}, {option1: value1, option2: value2}) as harness:
            with harness.patch('module.to.patch') as m:
                harness.trigger_event(message1, endpoint, data=MyData())
                m.wait_for_call() # Waits for the call to module.to.patch. Needed due to multi threading.
                m.assert_called_with('the wanted value')

    """
    def __init__(self,
                 constructor,
                 entity=None,
                 endpoints_and_messages=None,
                 config=None,
                 components=None):
        """
        Initialize a new Harness for an extension and defines endpoints_and_messages in the messagebus.

        :param constructor: the constructor of the extension to test
        :param entity: If not given and the extension is instantiated on an entity the first entity
                       in the config will be used. Otherwise this can be used to set the wanted entity.
        :param endpoints_and_messages: endpoints and messages that the extensions wants to subscribe to
        :param config: Instance of ConfigManager with the wanted config for the extension.
        :param components: The components to insert into the component registry. List of ComponentMock
        """
        self._constructor = constructor

        self.config = ConfigManager() if config is None else config
        self._all_dispatchers = []
        self._instances = {}
        self._entity = None
        self._entity_option_id = None
        self.init_component_handling([] if components is None else components)

        if not isinstance(self._constructor, type):
            raise Exception(
                'Extension should be a constructor of an extension, not an instance.'
            )

        if endpoints_and_messages:
            self.messagebus.define_endpoints_and_messages(
                endpoints_and_messages)

        config_options = self._constructor.config_options
        config_option_ids = [c.option_id for c in config_options]
        self.config.set_default_values(config_option_ids)
        self.init_entity(entity, config_options)

        self.assert_required_options(config_options, self._entity_option_id)

        self._config_view = ConfigView(self.config, config_option_ids,
                                       self._entity_option_id, self._entity)
        self._active = _is_extension_active(self._config_view, constructor)

    def init_entity(self, entity, config_options):
        self._entity_option_id = None

        entity_option_ids = [
            c.option_id for c in config_options if c.instantiate_on
        ]
        if entity_option_ids:
            self._entity_option_id = entity_option_ids[0]
            entities = self.config.get(self._entity_option_id)
            if entity:
                if entity not in entities:
                    AssertionError(
                        "Entity '{entity}' is not a valid entity for '{option_id}', "
                        "valid options are '{entities}'".format(
                            entity=entity,
                            option_id=self._entity_option_id.key,
                            entities=entities))
                self._entity = entity
            else:
                self._entity = entities[0]

            self._instances[self._entity_option_id] = self._entity

    def init_component_handling(self, components):
        self.component_registry = create_registry()
        self.component_manager = ComponentManager(self.component_registry)
        self.component_factory = Factory(self.component_manager)
        self.messagebus = MessageBus(self.component_factory, Scope('session'))

        components.append(
            ComponentMock(name='ComponentFactory',
                          mock=self.component_factory))
        components.append(
            ComponentMock(name='MessageBus', mock=self.messagebus))
        components.append(ComponentMock(name='Config', mock=self.config))
        components.append(
            ComponentMock(name='ComponentManager',
                          mock=self.component_manager))
        components.append(
            ComponentMock(name='ExtensionManager', mock=MagicMock()))
        for comp in components:
            if type(comp) == ComponentMock:
                component(name=comp.name,
                          scope=comp.scope,
                          can=comp.can,
                          provided_by_extension=comp.provided_by_extension)(
                              comp, self.component_manager)
            else:
                self.component_manager.register_component(comp)

    def assert_required_options(self, config_options, entity_option_id):
        def assert_in_config(option_id, entity=None):
            if self.config.get(option_id, entity=entity) is None:
                raise AssertionError(
                    'Missing required option {option}{for_entity}'.format(
                        option=option.option_id.key,
                        for_entity='for entity {entity}'.format(
                            entity=entity) if entity else ''))

        for option in config_options:
            if option.required and not option.option_id.has_default:
                if option.option_id.at is not None and option.option_id == entity_option_id:
                    assert_in_config(option.option_id, entity=self._entity)
                elif option.option_id.at is not None:
                    entities = self.config.get(option.option_id.at)
                    if not option.option_id.at.multiple:
                        entities = [entities]

                    for entity in entities:
                        assert_in_config(option.option_id, entity=entity)
                else:
                    assert_in_config(option.option_id)

    @property
    def extension(self):
        return self._instance

    def __enter__(self):
        self._instance = self._constructor(self._config_view, self._instances)
        self.messagebus.define_endpoints_and_messages(
            self._instance.endpoints_and_messages)

        if hasattr(self._instance, 'register_components'):
            self._instance.register_components(self.component_manager)

        for method, descriptor in get_dispatcher_descriptors(self._instance):
            dispatcher = descriptor.dispatcher_constructor(
                self.messagebus, method)
            entities = None
            if descriptor.entity_option_id is not None:
                entities = [self._instances[descriptor.entity_option_id]]
            if self._active:
                dispatcher.register(descriptor.message_ids,
                                    endpoint_ids=descriptor.endpoint_ids,
                                    entities=entities)
            self._all_dispatchers.append(dispatcher)

        self._instance.register_dispatchers(self.messagebus)
        return self

    def __exit__(self, *exc_info):
        for dispatcher in self._all_dispatchers:
            if self._active:
                dispatcher.destroy()
        self._instance.destroy()

    def trigger_event(self,
                      message_id,
                      sender_endpoint_id,
                      entity=None,
                      data=None):
        self.messagebus.trigger_event(message_id, sender_endpoint_id, entity,
                                      data)

    def send_request(self,
                     message_id,
                     receiver_endpoint_id=None,
                     entity=None,
                     data=None):
        return self.messagebus.send_request(message_id, receiver_endpoint_id,
                                            entity, data)

    def any_registered_dispatchers(self,
                                   message_id,
                                   endpoint_id=None,
                                   entity=None):
        return self.messagebus.has_registered_dispatchers(
            message_id, endpoint_id, entity)

    @contextmanager
    def message_queue(self,
                      message_ids,
                      endpoint_ids=None,
                      entities=None,
                      match=None,
                      priority=0):
        with LocalMessageQueue(self.messagebus, message_ids, endpoint_ids,
                               entities, match, priority) as q:
            yield q

    def patch(self, target, *patch_args, **patch_kwargs):
        """
        Patch with a special multi threaded wait_for_call method.

        The returned mock stores the arguments when it's called in a queue and then
        wait_for_call can be used to get the a tuple of *args* and *kwargs* that the
        mock was called with.
        The mock is a normal mock so after waiting for a call the normal mock asserts
        can be called.

        .. code-block:: python

            harness = ExtensionTestHarness(...)

            with harness.patch('function.to.patch') as mock:
                trigger_something()
                mock_args, mock_kwargs = mock.wait_for_call(timeout=4)
                mock.assert_called_once()
                self.assertEqual(mock_args[0], 'expected_value')

        :param target: the target
        :param patch_args: args sent to normal patch
        :param patch_kwargs: kwargs sent to normal patch
        :return: a mock with the extra wait_for_call method
        """

        return patch(target, *patch_args, new=SyncMock(), **patch_kwargs)