Refactored DE and Applier to use oslo.service

In this PS, I have refactored the Decision Engine and the Applier
to use the oslo service utility.

Change-Id: If29158cc9b5e5e50f6c69d67c232cceeb07084f2
Closes-Bug: #1541850
This commit is contained in:
Vincent Françoise
2016-03-18 10:22:46 +01:00
parent a9e7251d0d
commit eb861f86ab
16 changed files with 286 additions and 286 deletions

View File

@@ -18,22 +18,22 @@
#
from mock import patch
from threading import Thread
from watcher.applier.manager import ApplierManager
from watcher.common.messaging.messaging_core import MessagingCore
from watcher.applier import manager as applier_manager
from watcher.common.messaging import messaging_handler
from watcher.common import service
from watcher.tests import base
class TestApplierManager(base.TestCase):
def setUp(self):
super(TestApplierManager, self).setUp()
self.applier = ApplierManager()
self.applier = service.Service(applier_manager.ApplierManager)
@patch.object(MessagingCore, "connect")
@patch.object(Thread, "join")
def test_connect(self, m_messaging, m_thread):
self.applier.connect()
self.applier.join()
self.assertEqual(2, m_messaging.call_count)
self.assertEqual(1, m_thread.call_count)
@patch.object(messaging_handler.MessagingHandler, "stop")
@patch.object(messaging_handler.MessagingHandler, "start")
def test_start(self, m_messaging_start, m_messaging_stop):
self.applier.start()
self.applier.stop()
self.assertEqual(2, m_messaging_start.call_count)
self.assertEqual(2, m_messaging_stop.call_count)

View File

@@ -19,9 +19,10 @@ from __future__ import unicode_literals
import types
from mock import patch
import mock
from oslo_config import cfg
from watcher.applier.manager import ApplierManager
from oslo_service import service
from watcher.cmd import applier
from watcher.tests.base import BaseTestCase
@@ -43,9 +44,7 @@ class TestApplier(BaseTestCase):
super(TestApplier, self).tearDown()
self.conf._parse_cli_opts = self._parse_cli_opts
@patch.object(ApplierManager, "connect")
@patch.object(ApplierManager, "join")
def test_run_applier_app(self, m_connect, m_join):
@mock.patch.object(service, "launch")
def test_run_applier_app(self, m_launch):
applier.main()
self.assertEqual(1, m_connect.call_count)
self.assertEqual(1, m_join.call_count)
self.assertEqual(1, m_launch.call_count)

View File

@@ -19,15 +19,15 @@ from __future__ import unicode_literals
import types
from mock import patch
import mock
from oslo_config import cfg
from watcher.decision_engine.manager import DecisionEngineManager
from watcher.tests.base import BaseTestCase
from oslo_service import service
from watcher.cmd import decisionengine
from watcher.tests import base
class TestDecisionEngine(BaseTestCase):
class TestDecisionEngine(base.BaseTestCase):
def setUp(self):
super(TestDecisionEngine, self).setUp()
@@ -45,9 +45,7 @@ class TestDecisionEngine(BaseTestCase):
super(TestDecisionEngine, self).tearDown()
self.conf._parse_cli_opts = self._parse_cli_opts
@patch.object(DecisionEngineManager, "connect")
@patch.object(DecisionEngineManager, "join")
def test_run_de_app(self, m_connect, m_join):
@mock.patch.object(service, "launch")
def test_run_de_app(self, m_launch):
decisionengine.main()
self.assertEqual(1, m_connect.call_count)
self.assertEqual(1, m_join.call_count)
self.assertEqual(1, m_launch.call_count)

View File

@@ -42,7 +42,7 @@ class TestMessagingHandler(base.TestCase):
handler = messaging_handler.MessagingHandler(
publisher_id=self.PUBLISHER_ID,
topic_name=self.TOPIC_WATCHER,
endpoint=self.ENDPOINT,
endpoints=[self.ENDPOINT],
version=self.VERSION,
serializer=None,
)
@@ -65,7 +65,7 @@ class TestMessagingHandler(base.TestCase):
handler = messaging_handler.MessagingHandler(
publisher_id=self.PUBLISHER_ID,
topic_name=self.TOPIC_WATCHER,
endpoint=self.ENDPOINT,
endpoints=[self.ENDPOINT],
version=self.VERSION,
serializer=None,
)

View File

@@ -17,44 +17,58 @@
import mock
from watcher.common.messaging import messaging_core
from watcher.common.messaging import messaging_handler
from watcher.common import rpc
from watcher.common import service
from watcher.tests import base
class TestMessagingCore(base.TestCase):
class DummyManager(object):
API_VERSION = '1.0'
conductor_endpoints = []
status_endpoints = []
def __init__(self):
self.publisher_id = "pub_id"
self.conductor_topic = "conductor_topic"
self.status_topic = "status_topic"
self.api_version = self.API_VERSION
class TestService(base.TestCase):
def setUp(self):
super(TestMessagingCore, self).setUp()
super(TestService, self).setUp()
@mock.patch.object(messaging_handler, "MessagingHandler")
def test_connect(self, m_handler):
messaging = messaging_core.MessagingCore("", "", "")
messaging.connect()
def test_start(self, m_handler):
dummy_service = service.Service(DummyManager)
dummy_service.start()
self.assertEqual(2, m_handler.call_count)
@mock.patch.object(messaging_handler, "MessagingHandler")
def test_disconnect(self, m_handler):
messaging = messaging_core.MessagingCore("", "", "")
messaging.disconnect()
def test_stop(self, m_handler):
dummy_service = service.Service(DummyManager)
dummy_service.stop()
self.assertEqual(2, m_handler.call_count)
def test_build_topic_handler(self):
topic_name = "MyTopic"
messaging = messaging_core.MessagingCore("", "", "")
handler = messaging.build_topic_handler(topic_name)
topic_name = "mytopic"
dummy_service = service.Service(DummyManager)
handler = dummy_service.build_topic_handler(topic_name)
self.assertIsNotNone(handler)
def test_init_messaging_core(self):
messaging = messaging_core.MessagingCore("", "", "")
self.assertIsInstance(messaging.serializer,
def test_init_service(self):
dummy_service = service.Service(DummyManager)
self.assertIsInstance(dummy_service.serializer,
rpc.RequestContextSerializer)
self.assertIsInstance(
messaging.conductor_topic_handler,
dummy_service.conductor_topic_handler,
messaging_handler.MessagingHandler)
self.assertIsInstance(
messaging.status_topic_handler,
dummy_service.status_topic_handler,
messaging_handler.MessagingHandler)
@mock.patch.object(messaging_handler, "MessagingHandler")
@@ -64,9 +78,9 @@ class TestMessagingCore(base.TestCase):
payload = {
"name": "value",
}
event = "MyEvent"
messaging = messaging_core.MessagingCore("", "", "")
messaging.publish_control(event, payload)
event = "myevent"
dummy_service = service.Service(DummyManager)
dummy_service.publish_control(event, payload)
m_handler.publish_event.assert_called_once_with(event, payload)
@mock.patch.object(messaging_handler, "MessagingHandler")
@@ -76,19 +90,19 @@ class TestMessagingCore(base.TestCase):
payload = {
"name": "value",
}
event = "MyEvent"
messaging = messaging_core.MessagingCore("", "", "")
messaging.publish_status(event, payload)
event = "myevent"
dummy_service = service.Service(DummyManager)
dummy_service.publish_status(event, payload)
m_handler.publish_event.assert_called_once_with(event, payload, None)
@mock.patch.object(messaging_core.MessagingCore, 'publish_status')
@mock.patch.object(service.Service, 'publish_status')
def test_response(self, mock_call):
event = "My event"
context = {'request_id': 12}
message = "My Message"
messaging = messaging_core.MessagingCore("", "", "")
messaging.response(event, context, message)
dummy_service = service.Service(DummyManager)
dummy_service.response(event, context, message)
expected_payload = {
'request_id': context['request_id'],
@@ -97,14 +111,13 @@ class TestMessagingCore(base.TestCase):
mock_call.assert_called_once_with(event, expected_payload)
def test_messaging_build_topic_handler(self):
messaging = messaging_core.MessagingCore(
"pub_id", "test_topic", "does not matter")
topic = messaging.build_topic_handler("test_topic")
dummy_service = service.Service(DummyManager)
topic = dummy_service.build_topic_handler("conductor_topic")
self.assertIsInstance(topic, messaging_handler.MessagingHandler)
self.assertEqual("pub_id", messaging.publisher_id)
self.assertEqual("pub_id", dummy_service.publisher_id)
self.assertEqual("pub_id", topic.publisher_id)
self.assertEqual("test_topic",
messaging.conductor_topic_handler.topic_name)
self.assertEqual("test_topic", topic.topic_name)
self.assertEqual("conductor_topic",
dummy_service.conductor_topic_handler.topic_name)
self.assertEqual("conductor_topic", topic.topic_name)

View File

@@ -13,6 +13,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
from watcher.common import utils
@@ -41,7 +42,7 @@ class TestAuditEndpoint(DbTestCase):
audit_uuid = utils.generate_uuid()
audit_handler = DefaultAuditHandler(mock.MagicMock())
endpoint = AuditEndpoint(audit_handler, max_workers=2)
endpoint = AuditEndpoint(audit_handler)
with mock.patch.object(DefaultAuditHandler, 'execute') as mock_call:
mock_call.return_value = 0
@@ -54,7 +55,7 @@ class TestAuditEndpoint(DbTestCase):
mock_collector.return_value = FakerModelCollector()
audit_uuid = utils.generate_uuid()
audit_handler = DefaultAuditHandler(mock.MagicMock())
endpoint = AuditEndpoint(audit_handler, max_workers=2)
endpoint = AuditEndpoint(audit_handler)
with mock.patch.object(DefaultAuditHandler, 'execute') \
as mock_call: