Merge "Removed 'watcher_messaging' to use oslo.messaging"

This commit is contained in:
Jenkins
2015-12-02 11:16:53 +00:00
committed by Gerrit Code Review
17 changed files with 622 additions and 383 deletions

View File

@@ -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")

View 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, [])

View File

@@ -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))