Example #1
0
    def test_filters(self):
        notification_filter = oslo_messaging.NotificationFilter(
            **self.filter_rule)
        endpoint = mock.Mock(spec=['info'], filter_rule=notification_filter)

        targets = [oslo_messaging.Target(topic='notifications')]
        dispatcher = notify_dispatcher.NotificationDispatcher(
            targets, [endpoint], serializer=None, allow_requeue=True)
        message = {
            'payload': {
                'state': 'active'
            },
            'priority': 'info',
            'publisher_id': self.publisher_id,
            'event_type': self.event_type,
            'timestamp': '2014-03-03 18:21:04.369234',
            'message_id': '99863dda-97f0-443a-a0c1-6ed317b7fd45'
        }
        incoming = mock.Mock(ctxt=self.context, message=message)
        callback = dispatcher(incoming)
        callback.run()
        callback.done()

        if self.match:
            self.assertEqual(1, endpoint.info.call_count)
        else:
            self.assertEqual(0, endpoint.info.call_count)
    def test_filters(self):
        notification_filter = oslo_messaging.NotificationFilter(
            **self.filter_rule)
        endpoint = mock.Mock(spec=['info'], filter_rule=notification_filter)

        dispatcher = notify_dispatcher.NotificationDispatcher([endpoint],
                                                              serializer=None)
        message = {
            'payload': {
                'state': 'active',
                'virtual_size': None
            },
            'priority': 'info',
            'publisher_id': self.publisher_id,
            'event_type': self.event_type,
            'timestamp': '2014-03-03 18:21:04.369234',
            'message_id': '99863dda-97f0-443a-a0c1-6ed317b7fd45'
        }
        incoming = mock.Mock(ctxt=self.context, message=message)
        dispatcher.dispatch(incoming)

        if self.match:
            self.assertEqual(1, endpoint.info.call_count)
        else:
            self.assertEqual(0, endpoint.info.call_count)
Example #3
0
def get_notification_listener(transport, targets, endpoints,
                              executor='blocking', serializer=None,
                              allow_requeue=False, pool=None):
    """Construct a notification listener

    The executor parameter controls how incoming messages will be received and
    dispatched. By default, the most simple executor is used - the blocking
    executor.

    If the eventlet executor is used, the threading and time library need to be
    monkeypatched.

    :param transport: the messaging transport
    :type transport: Transport
    :param targets: the exchanges and topics to listen on
    :type targets: list of Target
    :param endpoints: a list of endpoint objects
    :type endpoints: list
    :param executor: name of a message executor - for example
                     'eventlet', 'blocking'
    :type executor: str
    :param serializer: an optional entity serializer
    :type serializer: Serializer
    :param allow_requeue: whether NotificationResult.REQUEUE support is needed
    :type allow_requeue: bool
    :param pool: the pool name
    :type pool: str
    :raises: NotImplementedError
    """
    dispatcher = notify_dispatcher.NotificationDispatcher(targets, endpoints,
                                                          serializer,
                                                          allow_requeue, pool)
    return msg_server.MessageHandlingServer(transport, dispatcher, executor)
Example #4
0
def _prepare_notification_service(server_id):
    endpoints = [report_notification, track_instance, untrack_instance]

    transport = messaging.get_transport(CONF)
    s_target = target.Target(topic='murano', server=server_id)
    dispatcher = oslo_dispatcher.NotificationDispatcher(
        [s_target], endpoints, None, True)
    return messaging.MessageHandlingServer(transport, dispatcher, 'eventlet')
 def test_dispatcher_unknown_prio(self, mylog):
     msg = notification_msg.copy()
     msg['priority'] = 'what???'
     dispatcher = notify_dispatcher.NotificationDispatcher([mock.Mock()],
                                                           None)
     res = dispatcher.dispatch(mock.Mock(ctxt={}, message=msg))
     self.assertIsNone(res)
     mylog.warning.assert_called_once_with('Unknown priority "%s"',
                                           'what???')
Example #6
0
 def test_dispatcher_unknown_prio(self, mylog):
     msg = notification_msg.copy()
     msg['priority'] = 'what???'
     dispatcher = notify_dispatcher.NotificationDispatcher(
         [mock.Mock()], [mock.Mock()], None, allow_requeue=True, pool=None)
     with dispatcher(mock.Mock(ctxt={}, message=msg)) as callback:
         callback()
     mylog.warning.assert_called_once_with('Unknown priority "%s"',
                                           'what???')
Example #7
0
    def test_dispatcher(self):
        endpoints = []
        for endpoint_methods in self.endpoints:
            e = mock.Mock(spec=endpoint_methods)
            endpoints.append(e)
            for m in endpoint_methods:
                method = getattr(e, m)
                if self.ex:
                    method.side_effect = self.ex()
                else:
                    method.return_value = self.return_value

        msg = notification_msg.copy()
        msg['priority'] = self.priority

        targets = [oslo_messaging.Target(topic='notifications')]
        dispatcher = notify_dispatcher.NotificationDispatcher(
            targets, endpoints, None, allow_requeue=True, pool=None)

        # check it listen on wanted topics
        self.assertEqual(
            sorted(
                set((targets[0], prio) for prio in
                    itertools.chain.from_iterable(self.endpoints))),
            sorted(dispatcher._targets_priorities))

        incoming = mock.Mock(ctxt={}, message=msg)
        callback = dispatcher(incoming)
        callback.run()
        callback.done()

        # check endpoint callbacks are called or not
        for i, endpoint_methods in enumerate(self.endpoints):
            for m in endpoint_methods:
                if m == self.endpoints_expect_calls[i]:
                    method = getattr(endpoints[i], m)
                    method.assert_called_once_with({}, msg['publisher_id'],
                                                   msg['event_type'],
                                                   msg['payload'], {
                                                       'timestamp': mock.ANY,
                                                       'message_id': mock.ANY
                                                   })
                else:
                    self.assertEqual(0, endpoints[i].call_count)

        if self.ex:
            self.assertEqual(1, incoming.acknowledge.call_count)
            self.assertEqual(0, incoming.requeue.call_count)
        elif self.return_value == oslo_messaging.NotificationResult.HANDLED \
                or self.return_value is None:
            self.assertEqual(1, incoming.acknowledge.call_count)
            self.assertEqual(0, incoming.requeue.call_count)
        elif self.return_value == oslo_messaging.NotificationResult.REQUEUE:
            self.assertEqual(0, incoming.acknowledge.call_count)
            self.assertEqual(1, incoming.requeue.call_count)
Example #8
0
def get_notification_server(targets, endpoints, serializer=None):
    """Retrieve notification server

    This Notification server uses same transport configuration as used by
    other barbican functionality like async order processing.

    Assumption is that messaging infrastructure is going to be shared (same)
    among different barbican features.
    """
    allow_requeue = getattr(getattr(CONF, KS_NOTIFICATIONS_GRP_NAME),
                            'allow_requeue')
    TRANSPORT._require_driver_features(requeue=allow_requeue)
    dispatcher = notfiy_dispatcher.NotificationDispatcher(
        targets, endpoints, serializer, allow_requeue)
    # we don't want blocking executor so use eventlet as executor choice
    return msg_server.MessageHandlingServer(TRANSPORT,
                                            dispatcher,
                                            executor='eventlet')
Example #9
0
 def test_delete_snapshot(self, delete, find):
     endpoints = get_endpoints_for_sp('default')
     dispatcher = notify_dispatcher.NotificationDispatcher(endpoints,
                                                           serializer=None)
     MESSAGE = {
         'payload': {
             'snapshot_id': "1232123212321",
             'tenant_id': "abdbabdbabdba"
         },
         'priority': 'info',
         'publisher_id': 'snapshot.node4',
         'event_type': 'snapshot.delete.end',
         'timestamp': '2014-03-03 18:21:04.369234',
         'message_id': '99863dda-97f0-443a-a0c1-6ed317b7fd45'
     }
     incoming = mock.Mock(ctxt={}, message=MESSAGE)
     dispatcher.dispatch(incoming)
     find.assert_called_with('snapshots', '1232123212321')
     delete.assert_called_with(35)
Example #10
0
 def test_create_volume(self, insert):
     endpoints = get_endpoints_for_sp('default')
     dispatcher = notify_dispatcher.NotificationDispatcher(endpoints,
                                                           serializer=None)
     MESSAGE = {
         'payload': {
             'volume_id': "1232123212321",
             'tenant_id': "abdbabdbabdba"
         },
         'priority': 'info',
         'publisher_id': 'volume.node4',
         'event_type': 'volume.create.start',
         'timestamp': '2014-03-03 18:21:04.369234',
         'message_id': '99863dda-97f0-443a-a0c1-6ed317b7fd45'
     }
     incoming = mock.Mock(ctxt={}, message=MESSAGE)
     dispatcher.dispatch(incoming)
     insert.assert_called_with(
         ResourceMapping('volumes', '1232123212321', 'abdbabdbabdba',
                         'default'))
    def test_dispatcher(self):
        endpoints = []
        for endpoint_methods in self.endpoints:
            e = mock.Mock(spec=endpoint_methods)
            endpoints.append(e)
            for m in endpoint_methods:
                method = getattr(e, m)
                if self.ex:
                    method.side_effect = self.ex()
                else:
                    method.return_value = self.return_value

        msg = notification_msg.copy()
        msg['priority'] = self.priority

        dispatcher = notify_dispatcher.NotificationDispatcher(endpoints, None)

        incoming = mock.Mock(ctxt={}, message=msg)

        res = dispatcher.dispatch(incoming)

        expected_res = (notify_dispatcher.NotificationResult.REQUEUE if (
            self.return_value == notify_dispatcher.NotificationResult.REQUEUE
            or self.ex is not None) else
                        notify_dispatcher.NotificationResult.HANDLED)

        self.assertEqual(expected_res, res)

        # check endpoint callbacks are called or not
        for i, endpoint_methods in enumerate(self.endpoints):
            for m in endpoint_methods:
                if m == self.endpoints_expect_calls[i]:
                    method = getattr(endpoints[i], m)
                    method.assert_called_once_with({}, msg['publisher_id'],
                                                   msg['event_type'],
                                                   msg['payload'], {
                                                       'timestamp': mock.ANY,
                                                       'message_id': mock.ANY
                                                   })
                else:
                    self.assertEqual(0, endpoints[i].call_count)