initial version
Change-Id: I699e0ab082657880998d8618fe29eb7f56c6c661
This commit is contained in:
0
watcher/tests/common/__init__.py
Normal file
0
watcher/tests/common/__init__.py
Normal file
0
watcher/tests/common/messaging/__init__.py
Normal file
0
watcher/tests/common/messaging/__init__.py
Normal file
1
watcher/tests/common/messaging/event/__init__.py
Normal file
1
watcher/tests/common/messaging/event/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
__author__ = 'bcom'
|
||||
@@ -0,0 +1,80 @@
|
||||
# -*- 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 call
|
||||
from mock import MagicMock
|
||||
from watcher.common.messaging.events.event import Event
|
||||
from watcher.common.messaging.events.event_dispatcher import EventDispatcher
|
||||
from watcher.decision_engine.framework.messaging.events import Events
|
||||
from watcher.tests import base
|
||||
|
||||
|
||||
class TestEventDispatcher(base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestEventDispatcher, self).setUp()
|
||||
self.event_dispatcher = EventDispatcher()
|
||||
|
||||
def fake_listener(self):
|
||||
return MagicMock()
|
||||
|
||||
def fake_event(self, event_type):
|
||||
event = Event()
|
||||
event.set_type(event_type)
|
||||
return event
|
||||
|
||||
def test_add_listener(self):
|
||||
listener = self.fake_listener()
|
||||
self.event_dispatcher.add_event_listener(Events.ALL,
|
||||
listener)
|
||||
|
||||
self.assertEqual(True, self.event_dispatcher.has_listener(
|
||||
Events.ALL, listener))
|
||||
|
||||
def test_remove_listener(self):
|
||||
listener = self.fake_listener()
|
||||
self.event_dispatcher.add_event_listener(Events.ALL,
|
||||
listener)
|
||||
self.event_dispatcher.remove_event_listener(Events.ALL, listener)
|
||||
|
||||
self.assertEqual(False, self.event_dispatcher.has_listener(
|
||||
Events.TRIGGER_AUDIT, listener))
|
||||
|
||||
def test_dispatch_event(self):
|
||||
listener = self.fake_listener()
|
||||
event = self.fake_event(Events.TRIGGER_AUDIT)
|
||||
self.event_dispatcher.add_event_listener(Events.TRIGGER_AUDIT,
|
||||
listener)
|
||||
|
||||
self.event_dispatcher.dispatch_event(event)
|
||||
listener.assert_has_calls(call(event))
|
||||
|
||||
def test_dispatch_event_to_all_listener(self):
|
||||
event = self.fake_event(Events.ACTION_PLAN)
|
||||
listener_all = self.fake_listener()
|
||||
listener_action_plan = self.fake_listener()
|
||||
listener_trigger_audit = self.fake_listener()
|
||||
|
||||
self.event_dispatcher.add_event_listener(Events.ALL, listener_all)
|
||||
self.event_dispatcher.add_event_listener(Events.ACTION_PLAN,
|
||||
listener_action_plan)
|
||||
|
||||
self.event_dispatcher.add_event_listener(Events.TRIGGER_AUDIT,
|
||||
listener_trigger_audit)
|
||||
|
||||
self.event_dispatcher.dispatch_event(event)
|
||||
listener_all.assert_has_calls(call(event))
|
||||
listener_action_plan.assert_has_calls(call(event))
|
||||
listener_trigger_audit.assert_has_calls([])
|
||||
77
watcher/tests/common/messaging/test_messaging_core.py
Normal file
77
watcher/tests/common/messaging/test_messaging_core.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# -*- 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.
|
||||
|
||||
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
|
||||
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
|
||||
|
||||
|
||||
class TestMessagingCore(base.TestCase):
|
||||
messaging = MessagingCore("", "", "")
|
||||
|
||||
def fake_topic_name(self):
|
||||
topic_name = "MyTopic"
|
||||
return topic_name
|
||||
|
||||
def test_build_topic(self):
|
||||
topic_name = self.fake_topic_name()
|
||||
messaging_handler = self.messaging.build_topic(topic_name)
|
||||
self.assertIsNotNone(messaging_handler)
|
||||
|
||||
def test_init_messaging_core(self):
|
||||
self.assertIsInstance(self.messaging.serializer,
|
||||
RequestContextSerializer)
|
||||
self.assertIsInstance(self.messaging.topic_control, MessagingHandler)
|
||||
self.assertIsInstance(self.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)
|
||||
|
||||
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)
|
||||
|
||||
def test_response(self):
|
||||
with mock.patch.object(MessagingCore, 'publish_status') as mock_call:
|
||||
event = "My event"
|
||||
context = {'request_id': 12}
|
||||
message = "My Message"
|
||||
|
||||
self.messaging.response(event, context, message)
|
||||
|
||||
expected_payload = {
|
||||
'request_id': context['request_id'],
|
||||
'msg': message
|
||||
}
|
||||
mock_call.assert_called_once_with(event, expected_payload)
|
||||
55
watcher/tests/common/messaging/test_notification_handler.py
Normal file
55
watcher/tests/common/messaging/test_notification_handler.py
Normal file
@@ -0,0 +1,55 @@
|
||||
# -*- 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.
|
||||
|
||||
|
||||
import mock
|
||||
from oslo import messaging
|
||||
from watcher.common.messaging.notification_handler import NotificationHandler
|
||||
from watcher.common.messaging.utils.observable import Observable
|
||||
from watcher.tests import base
|
||||
|
||||
PUBLISHER_ID = 'TEST_API'
|
||||
|
||||
|
||||
class TestNotificationHandler(base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestNotificationHandler, self).setUp()
|
||||
self.notification_handler = NotificationHandler(PUBLISHER_ID)
|
||||
|
||||
def _test_notify(self, level_to_call):
|
||||
ctx = {}
|
||||
publisher_id = PUBLISHER_ID
|
||||
event_type = 'Test'
|
||||
payload = {}
|
||||
metadata = {}
|
||||
|
||||
with mock.patch.object(Observable, 'notify') as mock_call:
|
||||
notification_result = level_to_call(ctx, publisher_id, event_type,
|
||||
payload, metadata)
|
||||
self.assertEqual(messaging.NotificationResult.HANDLED,
|
||||
notification_result)
|
||||
mock_call.assert_called_once_with(ctx, publisher_id, event_type,
|
||||
metadata, payload)
|
||||
|
||||
def test_notify_info(self):
|
||||
self._test_notify(self.notification_handler.info)
|
||||
|
||||
def test_notify_warn(self):
|
||||
self._test_notify(self.notification_handler.warn)
|
||||
|
||||
def test_notify_error(self):
|
||||
self._test_notify(self.notification_handler.error)
|
||||
0
watcher/tests/common/messaging/utils/__init__.py
Normal file
0
watcher/tests/common/messaging/utils/__init__.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# -*- 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
|
||||
print(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))
|
||||
Reference in New Issue
Block a user