summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--osprofiler/drivers/messaging.py16
-rw-r--r--osprofiler/initializer.py9
-rw-r--r--osprofiler/tests/unit/drivers/test_base.py6
-rw-r--r--osprofiler/tests/unit/drivers/test_messaging.py33
-rw-r--r--osprofiler/tests/unit/test_initializer.py43
-rw-r--r--requirements.txt1
6 files changed, 80 insertions, 28 deletions
diff --git a/osprofiler/drivers/messaging.py b/osprofiler/drivers/messaging.py
index 47a8a81..4acf4fb 100644
--- a/osprofiler/drivers/messaging.py
+++ b/osprofiler/drivers/messaging.py
@@ -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)
diff --git a/osprofiler/initializer.py b/osprofiler/initializer.py
index 84e56d8..6f66e3b 100644
--- a/osprofiler/initializer.py
+++ b/osprofiler/initializer.py
@@ -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)
diff --git a/osprofiler/tests/unit/drivers/test_base.py b/osprofiler/tests/unit/drivers/test_base.py
index 462559b..4bb5674 100644
--- a/osprofiler/tests/unit/drivers/test_base.py
+++ b/osprofiler/tests/unit/drivers/test_base.py
@@ -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
diff --git a/osprofiler/tests/unit/drivers/test_messaging.py b/osprofiler/tests/unit/drivers/test_messaging.py
index ad59c73..9a2c0a3 100644
--- a/osprofiler/tests/unit/drivers/test_messaging.py
+++ b/osprofiler/tests/unit/drivers/test_messaging.py
@@ -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"
+ # emulate dynamic load of oslo.messaging library
+ oslo_messaging_mock = mock.Mock()
+ try_import_mock.return_value = oslo_messaging_mock
+
+ # 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://", messaging, context, transport,
- project, service, host).notify
+ "messaging://", project=project, service=service,
+ context=context, host=host).notify
- messaging.Notifier.assert_called_once_with(
+ 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)
diff --git a/osprofiler/tests/unit/test_initializer.py b/osprofiler/tests/unit/test_initializer.py
new file mode 100644
index 0000000..abb69d3
--- /dev/null
+++ b/osprofiler/tests/unit/test_initializer.py
@@ -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")
diff --git a/requirements.txt b/requirements.txt
index e38a9d8..dcb5bc8 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -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