Merge "Make dependency on oslo.messaging runtime only"

This commit is contained in:
Jenkins 2017-08-29 05:21:11 +00:00 committed by Gerrit Code Review
commit de34c0d29e
6 changed files with 81 additions and 29 deletions

View File

@ -13,22 +13,28 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_utils import importutils
from osprofiler.drivers import base
class Messaging(base.Driver):
def __init__(self, connection_str, messaging=None, context=None,
transport=None, project=None, service=None,
host=None, **kwargs):
def __init__(self, connection_str, project=None, service=None, host=None,
context=None, conf=None, **kwargs):
"""Driver sending notifications via message queues."""
oslo_messaging = importutils.try_import("oslo_messaging")
if not oslo_messaging:
raise ValueError("Oslo.messaging library is required for "
"messaging driver")
super(Messaging, self).__init__(connection_str, project=project,
service=service, host=host)
self.messaging = messaging
self.context = context
self.client = messaging.Notifier(
transport = oslo_messaging.get_notification_transport(conf)
self.client = oslo_messaging.Notifier(
transport, publisher_id=self.host, driver="messaging",
topics=["profiler"], retry=0)

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import oslo_messaging
from osprofiler import notifier
from osprofiler import web
@ -30,17 +28,12 @@ def init_from_conf(conf, context, project, service, host):
running on.
"""
connection_str = conf.profiler.connection_string
kwargs = {}
if connection_str.startswith("messaging"):
kwargs = {"messaging": oslo_messaging,
"transport": oslo_messaging.get_notification_transport(conf)}
_notifier = notifier.create(
connection_str,
context=context,
project=project,
service=service,
host=host,
conf=conf,
**kwargs)
conf=conf)
notifier.set(_notifier)
web.enable(conf.profiler.hmac_keys)

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from osprofiler.drivers import base
from osprofiler.tests import test
@ -55,10 +53,6 @@ class NotifierBaseTestCase(test.TestCase):
"Driver not found for connection string: "
"nonexisting://")
def test_plugins_are_imported(self):
base.get_driver("messaging://", mock.MagicMock(), "context",
"transport", "host")
def test_build_empty_tree(self):
class C(base.Driver):
@classmethod

View File

@ -21,20 +21,37 @@ from osprofiler.tests import test
class MessagingTestCase(test.TestCase):
def test_init_and_notify(self):
@mock.patch("oslo_utils.importutils.try_import")
def test_init_no_oslo_messaging(self, try_import_mock):
try_import_mock.return_value = None
messaging = mock.MagicMock()
self.assertRaises(
ValueError, base.get_driver,
"messaging://", project="project", service="service",
host="host", context={})
@mock.patch("oslo_utils.importutils.try_import")
def test_init_and_notify(self, try_import_mock):
context = "context"
transport = "transport"
project = "project"
service = "service"
host = "host"
notify_func = base.get_driver(
"messaging://", messaging, context, transport,
project, service, host).notify
# emulate dynamic load of oslo.messaging library
oslo_messaging_mock = mock.Mock()
try_import_mock.return_value = oslo_messaging_mock
messaging.Notifier.assert_called_once_with(
# mock oslo.messaging APIs
notifier_mock = mock.Mock()
oslo_messaging_mock.Notifier.return_value = notifier_mock
oslo_messaging_mock.get_notification_transport.return_value = transport
notify_func = base.get_driver(
"messaging://", project=project, service=service,
context=context, host=host).notify
oslo_messaging_mock.Notifier.assert_called_once_with(
transport, publisher_id=host, driver="messaging",
topics=["profiler"], retry=0)
@ -46,10 +63,10 @@ class MessagingTestCase(test.TestCase):
}
notify_func(info)
messaging.Notifier().info.assert_called_once_with(
notifier_mock.info.assert_called_once_with(
context, "profiler.service", info)
messaging.reset_mock()
notifier_mock.reset_mock()
notify_func(info, context="my_context")
messaging.Notifier().info.assert_called_once_with(
notifier_mock.info.assert_called_once_with(
"my_context", "profiler.service", info)

View File

@ -0,0 +1,43 @@
# 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
import testtools
from osprofiler import initializer
class InitializerTestCase(testtools.TestCase):
@mock.patch("osprofiler.notifier.set")
@mock.patch("osprofiler.notifier.create")
@mock.patch("osprofiler.web.enable")
def test_initializer(self, web_enable_mock, notifier_create_mock,
notifier_set_mock):
conf = mock.Mock()
conf.profiler.connection_string = "driver://"
conf.profiler.hmac_keys = "hmac_keys"
context = {}
project = "my-project"
service = "my-service"
host = "my-host"
notifier_mock = mock.Mock()
notifier_create_mock.return_value = notifier_mock
initializer.init_from_conf(conf, context, project, service, host)
notifier_create_mock.assert_called_once_with(
"driver://", context=context, project=project, service=service,
host=host, conf=conf)
notifier_set_mock.assert_called_once_with(notifier_mock)
web_enable_mock.assert_called_once_with("hmac_keys")

View File

@ -1,5 +1,4 @@
six>=1.9.0 # MIT
oslo.messaging>=5.2.0 # Apache-2.0
oslo.log>=3.11.0 # Apache-2.0
oslo.utils>=3.16.0 # Apache-2.0
WebOb>=1.6.0 # MIT