diff options
author | Jenkins <jenkins@review.openstack.org> | 2017-08-29 05:21:11 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2017-08-29 05:21:11 +0000 |
commit | de34c0d29e2a85396a9ff67dd022240043e1214e (patch) | |
tree | 337b439ba2371605cdb607646bc21efb3d560fd1 /osprofiler/tests | |
parent | c06046b6ba46ec2b2d4284ec4a010f906d4d9a97 (diff) | |
parent | 00eaa1096d94dbbde579ac01a9e78425490ec647 (diff) | |
download | osprofiler-1.12.0.tar.gz |
Merge "Make dependency on oslo.messaging runtime only"1.12.0
Diffstat (limited to 'osprofiler/tests')
-rw-r--r-- | osprofiler/tests/unit/drivers/test_base.py | 6 | ||||
-rw-r--r-- | osprofiler/tests/unit/drivers/test_messaging.py | 33 | ||||
-rw-r--r-- | osprofiler/tests/unit/test_initializer.py | 43 |
3 files changed, 68 insertions, 14 deletions
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") |