Merge "Removed 'watcher_messaging' to use oslo.messaging"
This commit is contained in:
@@ -69,13 +69,15 @@ CONF.import_opt('auth_uri', 'keystonemiddleware.auth_token',
|
||||
|
||||
|
||||
class ApplierManager(MessagingCore):
|
||||
API_VERSION = '1.0'
|
||||
# todo(jed) need workflow
|
||||
|
||||
def __init__(self):
|
||||
MessagingCore.__init__(self, CONF.watcher_applier.publisher_id,
|
||||
CONF.watcher_applier.topic_control,
|
||||
CONF.watcher_applier.topic_status)
|
||||
super(ApplierManager, self).__init__(
|
||||
CONF.watcher_applier.publisher_id,
|
||||
CONF.watcher_applier.topic_control,
|
||||
CONF.watcher_applier.topic_status,
|
||||
api_version=self.API_VERSION,
|
||||
)
|
||||
# shared executor of the workflow
|
||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
||||
self.handler = NotificationHandler(self.publisher_id)
|
||||
|
||||
@@ -24,13 +24,10 @@ import oslo_messaging as om
|
||||
from watcher.applier.framework.manager_applier import APPLIER_MANAGER_OPTS
|
||||
from watcher.applier.framework.manager_applier import opt_group
|
||||
from watcher.common import exception
|
||||
from watcher.common import utils
|
||||
|
||||
|
||||
from watcher.common.messaging.messaging_core import MessagingCore
|
||||
from watcher.common.messaging.notification_handler import NotificationHandler
|
||||
from watcher.common.messaging.utils.transport_url_builder import \
|
||||
TransportUrlBuilder
|
||||
from watcher.common import utils
|
||||
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
@@ -39,19 +36,23 @@ CONF.register_opts(APPLIER_MANAGER_OPTS, opt_group)
|
||||
|
||||
|
||||
class ApplierAPI(MessagingCore):
|
||||
MessagingCore.API_VERSION = '1.0'
|
||||
|
||||
def __init__(self):
|
||||
MessagingCore.__init__(self, CONF.watcher_applier.publisher_id,
|
||||
CONF.watcher_applier.topic_control,
|
||||
CONF.watcher_applier.topic_status)
|
||||
super(ApplierAPI, self).__init__(
|
||||
CONF.watcher_applier.publisher_id,
|
||||
CONF.watcher_applier.topic_control,
|
||||
CONF.watcher_applier.topic_status,
|
||||
api_version=self.API_VERSION,
|
||||
)
|
||||
self.handler = NotificationHandler(self.publisher_id)
|
||||
self.handler.register_observer(self)
|
||||
self.topic_status.add_endpoint(self.handler)
|
||||
transport = om.get_transport(CONF, TransportUrlBuilder().url)
|
||||
transport = om.get_transport(CONF)
|
||||
|
||||
target = om.Target(
|
||||
topic=CONF.watcher_applier.topic_control,
|
||||
version=MessagingCore.API_VERSION)
|
||||
version=self.API_VERSION,
|
||||
)
|
||||
|
||||
self.client = om.RPCClient(transport, target,
|
||||
serializer=self.serializer)
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log
|
||||
from watcher.common.messaging.events.event_dispatcher import \
|
||||
@@ -28,60 +27,33 @@ from watcher.objects.base import WatcherObjectSerializer
|
||||
LOG = log.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
||||
WATCHER_MESSAGING_OPTS = [
|
||||
cfg.StrOpt('notifier_driver',
|
||||
default='messaging', help='The name of the driver used by'
|
||||
' oslo messaging'),
|
||||
cfg.StrOpt('executor',
|
||||
default='blocking', help='The name of a message executor, for'
|
||||
'example: eventlet, blocking'),
|
||||
cfg.StrOpt('protocol',
|
||||
default='rabbit', help='The protocol used by the message'
|
||||
' broker, for example rabbit'),
|
||||
cfg.StrOpt('user',
|
||||
default='guest', help='The username used by the message '
|
||||
'broker'),
|
||||
cfg.StrOpt('password',
|
||||
default='guest', help='The password of user used by the '
|
||||
'message broker'),
|
||||
cfg.StrOpt('host',
|
||||
default='localhost', help='The host where the message broker'
|
||||
'is installed'),
|
||||
cfg.StrOpt('port',
|
||||
default='5672', help='The port used bythe message broker'),
|
||||
cfg.StrOpt('virtual_host',
|
||||
default='', help='The virtual host used by the message '
|
||||
'broker')
|
||||
]
|
||||
|
||||
CONF = cfg.CONF
|
||||
opt_group = cfg.OptGroup(name='watcher_messaging',
|
||||
title='Options for the messaging core')
|
||||
CONF.register_group(opt_group)
|
||||
CONF.register_opts(WATCHER_MESSAGING_OPTS, opt_group)
|
||||
|
||||
|
||||
class MessagingCore(EventDispatcher):
|
||||
|
||||
API_VERSION = '1.0'
|
||||
|
||||
def __init__(self, publisher_id, topic_control, topic_status):
|
||||
EventDispatcher.__init__(self)
|
||||
def __init__(self, publisher_id, topic_control, topic_status,
|
||||
api_version=API_VERSION):
|
||||
super(MessagingCore, self).__init__()
|
||||
self.serializer = RequestContextSerializer(WatcherObjectSerializer())
|
||||
self.publisher_id = publisher_id
|
||||
self.api_version = api_version
|
||||
self.topic_control = self.build_topic(topic_control)
|
||||
self.topic_status = self.build_topic(topic_status)
|
||||
|
||||
def build_topic(self, topic_name):
|
||||
return MessagingHandler(self.publisher_id, topic_name, self,
|
||||
self.API_VERSION, self.serializer)
|
||||
self.api_version, self.serializer)
|
||||
|
||||
def connect(self):
|
||||
LOG.debug("connecting to rabbitMQ broker")
|
||||
LOG.debug("Connecting to '%s' (%s)",
|
||||
CONF.transport_url, CONF.rpc_backend)
|
||||
self.topic_control.start()
|
||||
self.topic_status.start()
|
||||
|
||||
def disconnect(self):
|
||||
LOG.debug("Disconnect to rabbitMQ broker")
|
||||
LOG.debug("Disconnecting from '%s' (%s)",
|
||||
CONF.transport_url, CONF.rpc_backend)
|
||||
self.topic_control.stop()
|
||||
self.topic_status.stop()
|
||||
|
||||
@@ -92,12 +64,12 @@ class MessagingCore(EventDispatcher):
|
||||
return self.topic_status.publish_event(event, payload, request_id)
|
||||
|
||||
def get_version(self):
|
||||
return self.API_VERSION
|
||||
return self.api_version
|
||||
|
||||
def check_api_version(self, context):
|
||||
api_manager_version = self.client.call(
|
||||
context.to_dict(), 'check_api_version',
|
||||
api_version=self.API_VERSION)
|
||||
api_version=self.api_version)
|
||||
return api_manager_version
|
||||
|
||||
def response(self, evt, ctx, message):
|
||||
|
||||
@@ -14,35 +14,42 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import socket
|
||||
|
||||
import eventlet
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log
|
||||
import oslo_messaging as om
|
||||
from threading import Thread
|
||||
from watcher.common.messaging.utils.transport_url_builder import \
|
||||
TransportUrlBuilder
|
||||
from watcher.common.rpc import JsonPayloadSerializer
|
||||
from watcher.common.rpc import RequestContextSerializer
|
||||
|
||||
# NOTE:
|
||||
# Ubuntu 14.04 forces librabbitmq when kombu is used
|
||||
# Unfortunately it forces a version that has a crash
|
||||
# bug. Calling eventlet.monkey_patch() tells kombu
|
||||
# to use libamqp instead.
|
||||
eventlet.monkey_patch()
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class MessagingHandler(Thread):
|
||||
|
||||
def __init__(self, publisher_id, topic_watcher, endpoint, version,
|
||||
serializer=None):
|
||||
Thread.__init__(self)
|
||||
super(MessagingHandler, self).__init__()
|
||||
self.publisher_id = publisher_id
|
||||
self.topic_watcher = topic_watcher
|
||||
self.__endpoints = []
|
||||
self.__serializer = serializer
|
||||
self.__version = version
|
||||
|
||||
self.__server = None
|
||||
self.__notifier = None
|
||||
self.__endpoints = []
|
||||
self.__topics = []
|
||||
self._publisher_id = publisher_id
|
||||
self._topic_watcher = topic_watcher
|
||||
self.__endpoints.append(endpoint)
|
||||
self.__version = version
|
||||
self.__serializer = serializer
|
||||
self.__transport = None
|
||||
self.add_endpoint(endpoint)
|
||||
|
||||
def add_endpoint(self, endpoint):
|
||||
self.__endpoints.append(endpoint)
|
||||
@@ -51,47 +58,50 @@ class MessagingHandler(Thread):
|
||||
if endpoint in self.__endpoints:
|
||||
self.__endpoints.remove(endpoint)
|
||||
|
||||
@property
|
||||
def endpoints(self):
|
||||
return self.__endpoints
|
||||
|
||||
@property
|
||||
def transport(self):
|
||||
return self.__transport
|
||||
|
||||
def build_notifier(self):
|
||||
serializer = RequestContextSerializer(JsonPayloadSerializer())
|
||||
return om.Notifier(
|
||||
self.transport,
|
||||
driver=CONF.watcher_messaging.notifier_driver,
|
||||
publisher_id=self._publisher_id,
|
||||
topic=self._topic_watcher,
|
||||
serializer=serializer)
|
||||
self.__transport,
|
||||
publisher_id=self.publisher_id,
|
||||
topic=self.topic_watcher,
|
||||
serializer=serializer
|
||||
)
|
||||
|
||||
def build_server(self, targets):
|
||||
|
||||
return om.get_rpc_server(self.transport, targets,
|
||||
def build_server(self, target):
|
||||
return om.get_rpc_server(self.__transport, target,
|
||||
self.__endpoints,
|
||||
executor=CONF.
|
||||
watcher_messaging.executor,
|
||||
serializer=self.__serializer)
|
||||
|
||||
def __build_transport_url(self):
|
||||
return TransportUrlBuilder().url
|
||||
|
||||
def __config(self):
|
||||
def _configure(self):
|
||||
try:
|
||||
self.transport = om.get_transport(
|
||||
cfg.CONF,
|
||||
url=self.__build_transport_url())
|
||||
self.__transport = om.get_transport(CONF)
|
||||
self.__notifier = self.build_notifier()
|
||||
if 0 < len(self.__endpoints):
|
||||
targets = om.Target(
|
||||
topic=self._topic_watcher,
|
||||
server=CONF.watcher_messaging.host,
|
||||
version=self.__version)
|
||||
self.__server = self.build_server(targets)
|
||||
if len(self.__endpoints):
|
||||
target = om.Target(
|
||||
topic=self.topic_watcher,
|
||||
# For compatibility, we can override it with 'host' opt
|
||||
server=CONF.host or socket.getfqdn(),
|
||||
version=self.__version,
|
||||
)
|
||||
self.__server = self.build_server(target)
|
||||
else:
|
||||
LOG.warn("you have no defined endpoint, \
|
||||
so you can only publish events")
|
||||
LOG.warn("you have no defined endpoint, "
|
||||
"so you can only publish events")
|
||||
except Exception as e:
|
||||
LOG.exception(e)
|
||||
LOG.error("configure : %s" % str(e.message))
|
||||
|
||||
def run(self):
|
||||
LOG.debug("configure MessagingHandler for %s" % self._topic_watcher)
|
||||
self.__config()
|
||||
LOG.debug("configure MessagingHandler for %s" % self.topic_watcher)
|
||||
self._configure()
|
||||
if len(self.__endpoints) > 0:
|
||||
LOG.debug("Starting up server")
|
||||
self.__server.start()
|
||||
@@ -102,6 +112,8 @@ class MessagingHandler(Thread):
|
||||
self.__server.stop()
|
||||
|
||||
def publish_event(self, event_type, payload, request_id=None):
|
||||
self.__notifier.info({'version_api': self.__version,
|
||||
'request_id': request_id},
|
||||
{'event_id': event_type}, payload)
|
||||
self.__notifier.info(
|
||||
{'version_api': self.__version,
|
||||
'request_id': request_id},
|
||||
{'event_id': event_type}, payload
|
||||
)
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class TransportUrlBuilder(object):
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return "%s://%s:%s@%s:%s/%s" % (
|
||||
CONF.watcher_messaging.protocol,
|
||||
CONF.watcher_messaging.user,
|
||||
CONF.watcher_messaging.password,
|
||||
CONF.watcher_messaging.host,
|
||||
CONF.watcher_messaging.port,
|
||||
CONF.watcher_messaging.virtual_host
|
||||
)
|
||||
@@ -1,107 +0,0 @@
|
||||
# Copyright 2014 - Rackspace Hosting
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Common RPC service and API tools for Watcher."""
|
||||
|
||||
import eventlet
|
||||
from oslo_config import cfg
|
||||
import oslo_messaging as messaging
|
||||
|
||||
from watcher.common import context as watcher_context
|
||||
from watcher.common import rpc
|
||||
from watcher.objects import base as objects_base
|
||||
|
||||
|
||||
# NOTE(paulczar):
|
||||
# Ubuntu 14.04 forces librabbitmq when kombu is used
|
||||
# Unfortunately it forces a version that has a crash
|
||||
# bug. Calling eventlet.monkey_patch() tells kombu
|
||||
# to use libamqp instead.
|
||||
eventlet.monkey_patch()
|
||||
|
||||
# NOTE(asalkeld):
|
||||
# The watcher.openstack.common.rpc entries are for compatability
|
||||
# with devstack rpc_backend configuration values.
|
||||
TRANSPORT_ALIASES = {
|
||||
'watcher.openstack.common.rpc.impl_kombu': 'rabbit',
|
||||
'watcher.openstack.common.rpc.impl_qpid': 'qpid',
|
||||
'watcher.openstack.common.rpc.impl_zmq': 'zmq',
|
||||
}
|
||||
|
||||
|
||||
class RequestContextSerializer(messaging.Serializer):
|
||||
|
||||
def __init__(self, base):
|
||||
self._base = base
|
||||
|
||||
def serialize_entity(self, context, entity):
|
||||
if not self._base:
|
||||
return entity
|
||||
return self._base.serialize_entity(context, entity)
|
||||
|
||||
def deserialize_entity(self, context, entity):
|
||||
if not self._base:
|
||||
return entity
|
||||
return self._base.deserialize_entity(context, entity)
|
||||
|
||||
def serialize_context(self, context):
|
||||
return context.to_dict()
|
||||
|
||||
def deserialize_context(self, context):
|
||||
return watcher_context.RequestContext.from_dict(context)
|
||||
|
||||
|
||||
class Service(object):
|
||||
_server = None
|
||||
|
||||
def __init__(self, topic, server, handlers):
|
||||
serializer = RequestContextSerializer(
|
||||
objects_base.WatcherObjectSerializer())
|
||||
transport = messaging.get_transport(cfg.CONF,
|
||||
aliases=TRANSPORT_ALIASES)
|
||||
# TODO(asalkeld) add support for version='x.y'
|
||||
target = messaging.Target(topic=topic, server=server)
|
||||
self._server = messaging.get_rpc_server(transport, target, handlers,
|
||||
serializer=serializer)
|
||||
|
||||
def serve(self):
|
||||
self._server.start()
|
||||
self._server.wait()
|
||||
|
||||
|
||||
class API(object):
|
||||
def __init__(self, transport=None, context=None, topic=None):
|
||||
serializer = RequestContextSerializer(
|
||||
objects_base.WatcherObjectSerializer())
|
||||
if transport is None:
|
||||
exmods = rpc.get_allowed_exmods()
|
||||
transport = messaging.get_transport(cfg.CONF,
|
||||
allowed_remote_exmods=exmods,
|
||||
aliases=TRANSPORT_ALIASES)
|
||||
self._context = context
|
||||
if topic is None:
|
||||
topic = ''
|
||||
target = messaging.Target(topic=topic)
|
||||
self._client = messaging.RPCClient(transport, target,
|
||||
serializer=serializer)
|
||||
|
||||
def _call(self, method, *args, **kwargs):
|
||||
# import pdb; pdb.set_trace()
|
||||
return self._client.call(self._context, method, *args, **kwargs)
|
||||
|
||||
def _cast(self, method, *args, **kwargs):
|
||||
self._client.cast(self._context, method, *args, **kwargs)
|
||||
|
||||
def echo(self, message):
|
||||
self._cast('echo', message=message)
|
||||
@@ -57,12 +57,14 @@ CONF.register_opts(WATCHER_DECISION_ENGINE_OPTS, decision_engine_opt_group)
|
||||
|
||||
|
||||
class DecisionEngineManager(MessagingCore):
|
||||
API_VERSION = '1.0'
|
||||
|
||||
def __init__(self):
|
||||
MessagingCore.__init__(self, CONF.watcher_decision_engine.publisher_id,
|
||||
CONF.watcher_decision_engine.topic_control,
|
||||
CONF.watcher_decision_engine.topic_status)
|
||||
super(DecisionEngineManager, self).__init__(
|
||||
CONF.watcher_decision_engine.publisher_id,
|
||||
CONF.watcher_decision_engine.topic_control,
|
||||
CONF.watcher_decision_engine.topic_status,
|
||||
api_version=self.API_VERSION,
|
||||
)
|
||||
self.handler = NotificationHandler(self.publisher_id)
|
||||
self.handler.register_observer(self)
|
||||
self.add_event_listener(Events.ALL, self.event_receive)
|
||||
|
||||
@@ -17,19 +17,14 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log
|
||||
import oslo_messaging as om
|
||||
|
||||
from watcher.common import exception
|
||||
from watcher.common import utils
|
||||
|
||||
|
||||
from watcher.common.messaging.messaging_core import MessagingCore
|
||||
from watcher.common.messaging.notification_handler import NotificationHandler
|
||||
from watcher.common.messaging.utils.transport_url_builder import \
|
||||
TransportUrlBuilder
|
||||
from watcher.common import utils
|
||||
from watcher.decision_engine.event.consumer_factory import EventConsumerFactory
|
||||
from watcher.decision_engine.manager import decision_engine_opt_group
|
||||
from watcher.decision_engine.manager import WATCHER_DECISION_ENGINE_OPTS
|
||||
@@ -44,23 +39,24 @@ CONF.register_opts(WATCHER_DECISION_ENGINE_OPTS, decision_engine_opt_group)
|
||||
|
||||
|
||||
class DecisionEngineAPI(MessagingCore):
|
||||
# This must be in sync with manager.DecisionEngineManager's.
|
||||
MessagingCore.API_VERSION = '1.0'
|
||||
|
||||
def __init__(self):
|
||||
MessagingCore.__init__(self, CONF.watcher_decision_engine.publisher_id,
|
||||
CONF.watcher_decision_engine.topic_control,
|
||||
CONF.watcher_decision_engine.topic_status)
|
||||
super(DecisionEngineAPI, self).__init__(
|
||||
CONF.watcher_decision_engine.publisher_id,
|
||||
CONF.watcher_decision_engine.topic_control,
|
||||
CONF.watcher_decision_engine.topic_status,
|
||||
api_version=self.API_VERSION,
|
||||
)
|
||||
self.handler = NotificationHandler(self.publisher_id)
|
||||
self.handler.register_observer(self)
|
||||
self.add_event_listener(Events.ALL, self.event_receive)
|
||||
self.topic_status.add_endpoint(self.handler)
|
||||
|
||||
transport = om.get_transport(CONF, TransportUrlBuilder().url)
|
||||
transport = om.get_transport(CONF)
|
||||
target = om.Target(
|
||||
exchange='watcher',
|
||||
topic=CONF.watcher_decision_engine.topic_control,
|
||||
version=MessagingCore.API_VERSION)
|
||||
version=self.API_VERSION,
|
||||
)
|
||||
|
||||
self.client = om.RPCClient(transport, target,
|
||||
serializer=self.serializer)
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
|
||||
import watcher.api.app
|
||||
from watcher.applier.framework import manager_applier
|
||||
import watcher.common.messaging.messaging_core
|
||||
|
||||
from watcher.decision_engine import manager
|
||||
from watcher.decision_engine.strategy.selector import default \
|
||||
as strategy_selector
|
||||
@@ -27,8 +25,6 @@ from watcher.decision_engine.strategy.selector import default \
|
||||
def list_opts():
|
||||
return [
|
||||
('api', watcher.api.app.API_SERVICE_OPTS),
|
||||
('watcher_messaging',
|
||||
watcher.common.messaging.messaging_core.WATCHER_MESSAGING_OPTS),
|
||||
('watcher_goals', strategy_selector.WATCHER_GOALS_OPTS),
|
||||
('watcher_decision_engine',
|
||||
manager.WATCHER_DECISION_ENGINE_OPTS),
|
||||
|
||||
@@ -15,63 +15,74 @@
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
from mock import patch
|
||||
|
||||
from watcher.common.messaging.messaging_core import MessagingCore
|
||||
from watcher.common.messaging.messaging_handler import MessagingHandler
|
||||
from watcher.common.rpc import RequestContextSerializer
|
||||
from watcher.tests import base
|
||||
|
||||
CONF = cfg.CONF
|
||||
from watcher.tests.base import TestCase
|
||||
|
||||
|
||||
class TestMessagingCore(base.TestCase):
|
||||
messaging = MessagingCore("", "", "")
|
||||
class TestMessagingCore(TestCase):
|
||||
|
||||
def fake_topic_name(self):
|
||||
topic_name = "MyTopic"
|
||||
return topic_name
|
||||
def setUp(self):
|
||||
super(TestMessagingCore, self).setUp()
|
||||
|
||||
def test_build_topic(self):
|
||||
topic_name = self.fake_topic_name()
|
||||
messaging_handler = self.messaging.build_topic(topic_name)
|
||||
topic_name = "MyTopic"
|
||||
messaging = MessagingCore("", "", "")
|
||||
messaging_handler = messaging.build_topic(topic_name)
|
||||
self.assertIsNotNone(messaging_handler)
|
||||
|
||||
def test_init_messaging_core(self):
|
||||
self.assertIsInstance(self.messaging.serializer,
|
||||
messaging = MessagingCore("", "", "")
|
||||
self.assertIsInstance(messaging.serializer,
|
||||
RequestContextSerializer)
|
||||
self.assertIsInstance(self.messaging.topic_control, MessagingHandler)
|
||||
self.assertIsInstance(self.messaging.topic_status, MessagingHandler)
|
||||
self.assertIsInstance(messaging.topic_control, MessagingHandler)
|
||||
self.assertIsInstance(messaging.topic_status, MessagingHandler)
|
||||
|
||||
def test_publish_control(self):
|
||||
with mock.patch.object(MessagingCore, 'publish_control') as mock_call:
|
||||
payload = {
|
||||
"name": "value",
|
||||
}
|
||||
event = "MyEvent"
|
||||
self.messaging.publish_control(event, payload)
|
||||
mock_call.assert_called_once_with(event, payload)
|
||||
@patch.object(MessagingCore, 'publish_control')
|
||||
def test_publish_control(self, mock_call):
|
||||
payload = {
|
||||
"name": "value",
|
||||
}
|
||||
event = "MyEvent"
|
||||
messaging = MessagingCore("", "", "")
|
||||
messaging.publish_control(event, payload)
|
||||
mock_call.assert_called_once_with(event, payload)
|
||||
|
||||
def test_publish_status(self):
|
||||
with mock.patch.object(MessagingCore, 'publish_status') as mock_call:
|
||||
payload = {
|
||||
"name": "value",
|
||||
}
|
||||
event = "MyEvent"
|
||||
self.messaging.publish_status(event, payload)
|
||||
mock_call.assert_called_once_with(event, payload)
|
||||
@patch.object(MessagingCore, 'publish_status')
|
||||
def test_publish_status(self, mock_call):
|
||||
payload = {
|
||||
"name": "value",
|
||||
}
|
||||
event = "MyEvent"
|
||||
messaging = MessagingCore("", "", "")
|
||||
messaging.publish_status(event, payload)
|
||||
mock_call.assert_called_once_with(event, payload)
|
||||
|
||||
def test_response(self):
|
||||
with mock.patch.object(MessagingCore, 'publish_status') as mock_call:
|
||||
event = "My event"
|
||||
context = {'request_id': 12}
|
||||
message = "My Message"
|
||||
@patch.object(MessagingCore, 'publish_status')
|
||||
def test_response(self, mock_call):
|
||||
event = "My event"
|
||||
context = {'request_id': 12}
|
||||
message = "My Message"
|
||||
|
||||
self.messaging.response(event, context, message)
|
||||
messaging = MessagingCore("", "", "")
|
||||
messaging.response(event, context, message)
|
||||
|
||||
expected_payload = {
|
||||
'request_id': context['request_id'],
|
||||
'msg': message
|
||||
}
|
||||
mock_call.assert_called_once_with(event, expected_payload)
|
||||
expected_payload = {
|
||||
'request_id': context['request_id'],
|
||||
'msg': message
|
||||
}
|
||||
mock_call.assert_called_once_with(event, expected_payload)
|
||||
|
||||
def test_messaging_build_topic(self):
|
||||
messaging = MessagingCore("pub_id", "test_topic", "does not matter")
|
||||
topic = messaging.build_topic("test_topic")
|
||||
|
||||
self.assertIsInstance(topic, MessagingHandler)
|
||||
self.assertEqual(messaging.publisher_id, "pub_id")
|
||||
self.assertEqual(topic.publisher_id, "pub_id")
|
||||
|
||||
self.assertEqual(messaging.topic_control.topic_watcher, "test_topic")
|
||||
self.assertEqual(topic.topic_watcher, "test_topic")
|
||||
|
||||
78
watcher/tests/common/messaging/test_messaging_handler.py
Normal file
78
watcher/tests/common/messaging/test_messaging_handler.py
Normal file
@@ -0,0 +1,78 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from mock import Mock
|
||||
from mock import patch
|
||||
from oslo_config import cfg
|
||||
import oslo_messaging as messaging
|
||||
from watcher.common.messaging.messaging_handler import MessagingHandler
|
||||
from watcher.tests.base import TestCase
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class TestMessagingHandler(TestCase):
|
||||
|
||||
PUBLISHER_ID = 'TEST_API'
|
||||
TOPIC_WATCHER = 'TEST_TOPIC_WATCHER'
|
||||
ENDPOINT = 'http://fake-fqdn:1337'
|
||||
VERSION = "1.0"
|
||||
|
||||
def setUp(self):
|
||||
super(TestMessagingHandler, self).setUp()
|
||||
CONF.set_default('host', 'fake-fqdn')
|
||||
|
||||
@patch.object(messaging, "get_rpc_server")
|
||||
@patch.object(messaging, "Target")
|
||||
def test_setup_messaging_handler(self, m_target_cls, m_get_rpc_server):
|
||||
m_target = Mock()
|
||||
m_target_cls.return_value = m_target
|
||||
messaging_handler = MessagingHandler(
|
||||
publisher_id=self.PUBLISHER_ID,
|
||||
topic_watcher=self.TOPIC_WATCHER,
|
||||
endpoint=self.ENDPOINT,
|
||||
version=self.VERSION,
|
||||
serializer=None,
|
||||
)
|
||||
|
||||
messaging_handler.run()
|
||||
|
||||
m_target_cls.assert_called_once_with(
|
||||
server="fake-fqdn",
|
||||
topic="TEST_TOPIC_WATCHER",
|
||||
version="1.0",
|
||||
)
|
||||
m_get_rpc_server.assert_called_once_with(
|
||||
messaging_handler.transport,
|
||||
m_target,
|
||||
[self.ENDPOINT],
|
||||
serializer=None,
|
||||
)
|
||||
|
||||
def test_messaging_handler_remove_endpoint(self):
|
||||
messaging_handler = MessagingHandler(
|
||||
publisher_id=self.PUBLISHER_ID,
|
||||
topic_watcher=self.TOPIC_WATCHER,
|
||||
endpoint=self.ENDPOINT,
|
||||
version=self.VERSION,
|
||||
serializer=None,
|
||||
)
|
||||
|
||||
self.assertEqual(messaging_handler.endpoints, [self.ENDPOINT])
|
||||
|
||||
messaging_handler.remove_endpoint(self.ENDPOINT)
|
||||
|
||||
self.assertEqual(messaging_handler.endpoints, [])
|
||||
@@ -1,46 +0,0 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from oslo_config import cfg
|
||||
import re
|
||||
from watcher.common.messaging.utils.transport_url_builder import \
|
||||
TransportUrlBuilder
|
||||
from watcher.tests import base
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class TestTransportUrlBuilder(base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestTransportUrlBuilder, self).setUp()
|
||||
|
||||
def test_transport_url_not_none(self):
|
||||
url = TransportUrlBuilder().url
|
||||
self.assertIsNotNone(url, "The transport url must not be none")
|
||||
|
||||
def test_transport_url_valid_pattern(self):
|
||||
url = TransportUrlBuilder().url
|
||||
url_pattern = r'(\D+)://(\D+):(\D+)@(\D+):(\d+)'
|
||||
pattern = re.compile(url_pattern)
|
||||
match = re.search(url_pattern, url)
|
||||
self.assertEqual('rabbit', match.group(1))
|
||||
self.assertEqual('guest', match.group(2))
|
||||
self.assertEqual('guest', match.group(3))
|
||||
self.assertEqual('localhost', match.group(4))
|
||||
self.assertEqual('5672', match.group(5))
|
||||
self.assertIsNotNone(pattern.match(url))
|
||||
@@ -34,7 +34,6 @@ class ConfFixture(fixtures.Fixture):
|
||||
def setUp(self):
|
||||
super(ConfFixture, self).setUp()
|
||||
|
||||
self.conf.set_default('host', 'fake-mini')
|
||||
self.conf.set_default('connection', "sqlite://", group='database')
|
||||
self.conf.set_default('sqlite_synchronous', False, group='database')
|
||||
self.conf.set_default('verbose', True)
|
||||
|
||||
@@ -40,7 +40,7 @@ class TestDecisionEngineAPI(base.TestCase):
|
||||
mock_call.assert_called_once_with(
|
||||
expected_context.to_dict(),
|
||||
'check_api_version',
|
||||
api_version=DecisionEngineAPI().API_VERSION)
|
||||
api_version=DecisionEngineAPI().api_version)
|
||||
|
||||
def test_execute_audit_throw_exception(self):
|
||||
audit_uuid = "uuid"
|
||||
|
||||
Reference in New Issue
Block a user