Beispiel #1
0
    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)
Beispiel #2
0
 def test_fail_when_no_serial_connection_is_registered_to_send_command(self):
     messagebus = MessageBus(Factory(ComponentManager(COMPONENT_REGISTRY)))
     messagebus.define_endpoints_and_messages({SERIAL_ENDPOINT: [LOG_LINE_RECEIVED]})
     client = SerialClient(messagebus, 'entity')
     with self.assertRaises(SerialError) as error:
         client.send_line('line', timeout=1)
         assert 'No serial connection' in error.msg
Beispiel #3
0
 def setUp(self):
     self.messagebus = MessageBus(Factory(ComponentManager({})))
     self.endpoint = MOCK_ENDPOINT
     self.sut = Mock()
     self.sut.entity = 'entity'
     self.config = Mock()
     self.config.get = MagicMock(side_effect=config_get_log_sources)
     self.messagebus.define_endpoints_and_messages(
         {self.endpoint: [LOG_LINE_RECEIVED]})
Beispiel #4
0
    def setUp(self):
        self.messagebus = MessageBus(Factory(ComponentManager({})))
        self.sut = MagicMock()
        self.sut.entity = 'entity1'
        config = Mock()

        self.messagebus.define_endpoints_and_messages({
            MOCK_ENDPOINT: [SUT_RESET_EXPECTED],
            SUTEVENTSCOMPONENT_ENDPOINT: [SUT_RESET_EXPECTED, SUT_RESET_DONE]
        })
        self.sut_events = SutEvents(self.messagebus, config, self.sut)
Beispiel #5
0
def messages(core):
    """
    List messages and the endpoints that are defined for the messages.

    Example to show all messages and endpoints for the run command::

        zaf messages --target-command run
    """
    messagebus = MessageBus(component_factory=None)
    messagebus.define_endpoints_and_messages(
        core.extension_manager.get_endpoints_and_messages(
            core.config.get(TARGET_COMMAND)))
    messages_with_endpoints = messagebus.get_defined_messages_and_endpoints()
    print(format_result(messages_with_endpoints))
    return 0
Beispiel #6
0
class TestAwaitSutMessage(unittest.TestCase):
    def setUp(self):
        self.messagebus = MessageBus(Factory(ComponentManager({})))
        self.sut = MagicMock()
        self.sut.entity = 'entity1'
        config = Mock()

        self.messagebus.define_endpoints_and_messages({
            MOCK_ENDPOINT: [SUT_RESET_EXPECTED],
            SUTEVENTSCOMPONENT_ENDPOINT: [SUT_RESET_EXPECTED, SUT_RESET_DONE]
        })
        self.sut_events = SutEvents(self.messagebus, config, self.sut)

    def test_that_await_message_returns_future_with_message(self):
        with self.sut_events.await_sut_message(SUT_RESET_EXPECTED,
                                               timeout=1) as future:
            self.messagebus.trigger_event(SUT_RESET_EXPECTED,
                                          MOCK_ENDPOINT,
                                          entity=self.sut.entity)
        message = future.result(timeout=0)
        self.assertEqual(message.message_id, SUT_RESET_EXPECTED)

    def test_that_await_message_blocks_raises_exception_when_timing_out(self):
        with self.assertRaises(SutEventTimeout):
            with self.sut_events.await_sut_message(SUT_RESET_EXPECTED,
                                                   timeout=0):
                pass

    def test_that_await_reset_done_returns_future_with_reset_done_message(
            self):
        with self.sut_events.await_sut_reset_done(timeout=1) as future:
            self.messagebus.trigger_event(SUT_RESET_DONE,
                                          SUTEVENTSCOMPONENT_ENDPOINT,
                                          entity=self.sut.entity)
        message = future.result(timeout=0)
        self.assertEqual(message.message_id, SUT_RESET_DONE)
Beispiel #7
0
    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
Beispiel #8
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()
Beispiel #9
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)
Beispiel #10
0
class TestWaitForLine(unittest.TestCase):
    def setUp(self):
        self.messagebus = MessageBus(Factory(ComponentManager({})))
        self.endpoint = MOCK_ENDPOINT
        self.sut = Mock()
        self.sut.entity = 'entity'
        self.config = Mock()
        self.config.get = MagicMock(side_effect=config_get_log_sources)
        self.messagebus.define_endpoints_and_messages(
            {self.endpoint: [LOG_LINE_RECEIVED]})

    def test_wait_for_line_times_out(self):
        sut_events = SutEvents(self.messagebus, self.config, self.sut)
        with sut_events.wait_for_log_line(r'not matching') as lines:
            self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                          self.endpoint,
                                          entity='log-entity',
                                          data='line')
            with self.assertRaises(NoMatchingLogLine):
                lines.get(timeout=0)

    def test_wait_for_line_matches_first_line(self):
        [match] = list(self.wait_for_lines(r'li\w+', ['line'], 1))
        self.assertEqual(match.string, 'line')

    def test_wait_for_line_matches_some_lines(self):
        [match1, match2] = list(
            self.wait_for_lines(r'match\d+',
                                ['match1', 'no match', 'match2', 'no match'],
                                2))
        self.assertEqual(match1.string, 'match1')
        self.assertEqual(match2.string, 'match2')

    def test_wait_for_line_matches_with_match_groups(self):
        [match] = list(self.wait_for_lines(r'(\S+) (\S+)', ['first second'],
                                           1))
        self.assertEqual(match.group(1), 'first')
        self.assertEqual(match.group(2), 'second')

    def test_wait_for_line_matches_with_named_match_groups(self):
        [match] = list(
            self.wait_for_lines(r'(?P<first>\S+) (?P<second>\S+)',
                                ['first second'], 1))
        self.assertEqual(match.group('first'), 'first')
        self.assertEqual(match.group('second'), 'second')

    def test_wait_for_line_matches_when_match_is_not_at_start_of_line(self):
        [match] = list(
            self.wait_for_lines(r'(?P<second>second)', ['first second'], 1))
        self.assertEqual(match.string, 'first second')
        self.assertEqual(match.group('second'), 'second')

    def wait_for_lines(self, log_line_regex, lines, expected_matches):
        sut_events = SutEvents(self.messagebus, self.config, self.sut)
        with sut_events.wait_for_log_line(log_line_regex) as received_lines:
            for line in lines:
                self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                              self.endpoint,
                                              entity='log-entity',
                                              data=line)

            return [
                received_lines.get(timeout=0)
                for _ in range(0, expected_matches)
            ]

    def test_get_all_lines(self):
        lines = ['first A', 'second B', 'third A', 'fourth B']
        regex = r'A'
        matching_lines = ['first A', 'third A']
        sut_events = SutEvents(self.messagebus, self.config, self.sut)
        with sut_events.wait_for_log_line(regex) as queue:
            for line in lines:
                self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                              self.endpoint,
                                              entity='log-entity',
                                              data=line)
            self.messagebus.wait_for_not_active()
            matches = queue.get_all()
        strings = [match.string for match in matches]
        self.assertEqual(strings, matching_lines)

    def test_wait_for_line_raises_no_log_sources_error_if_log_source_is_unavailable(
            self):
        sut_events = SutEvents(self.messagebus, self.config, self.sut)
        with self.assertRaises(NoLogSources):
            sut_events.wait_for_log_line(
                r'not matching',
                log_sources='log-source-unavailable').__enter__()

    def test_wait_for_line_raises_no_log_sources_error_if_no_log_sources_defined_for_sut(
            self):
        config = Mock()
        config.get = MagicMock(return_value=[])
        sut_events = SutEvents(self.messagebus, config, self.sut)
        with self.assertRaises(NoLogSources):
            sut_events.wait_for_log_line(
                r'not matching', log_sources='not-a-log-source').__enter__()

    def test_wait_for_line_matches_only_one_log_source(self):
        lines = ['first A', 'second B', 'third A', 'fourth B']
        regex = r'B'
        matching_lines = ['second B', 'fourth B']
        config = Mock()
        config.get = MagicMock(return_value=['log-source-A', 'log-source-B'])
        sut_events = SutEvents(self.messagebus, config, self.sut)
        with sut_events.wait_for_log_line(regex,
                                          log_sources='log-source-B') as queue:
            self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                          self.endpoint,
                                          entity='log-source-A',
                                          data=lines[0])
            self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                          self.endpoint,
                                          entity='log-source-B',
                                          data=lines[1])
            self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                          self.endpoint,
                                          entity='log-source-A',
                                          data=lines[2])
            self.messagebus.trigger_event(LOG_LINE_RECEIVED,
                                          self.endpoint,
                                          entity='log-source-B',
                                          data=lines[3])
            self.messagebus.wait_for_not_active()
            matches = queue.get_all()
        strings = [match.string for match in matches]
        self.assertEqual(strings, matching_lines)