diff --git a/oslo_messaging/tests/notify/test_notifier.py b/oslo_messaging/tests/notify/test_notifier.py old mode 100644 new mode 100755 index 92e8f6435..854ce5954 --- a/oslo_messaging/tests/notify/test_notifier.py +++ b/oslo_messaging/tests/notify/test_notifier.py @@ -39,6 +39,14 @@ from six.moves import mock load_tests = testscenarios.load_tests_apply_scenarios +class JsonMessageMatcher(object): + def __init__(self, message): + self.message = message + + def __eq__(self, other): + return self.message == jsonutils.loads(other) + + class _FakeTransport(object): def __init__(self, conf): @@ -176,11 +184,10 @@ class TestMessagingNotifier(test_utils.BaseTestCase): if prepare_kwds: notifier = notifier.prepare(**prepare_kwds) - self.mox.StubOutWithMock(transport, '_send_notification') + transport._send_notification = mock.Mock() message_id = uuid.uuid4() - self.mox.StubOutWithMock(uuid, 'uuid4') - uuid.uuid4().AndReturn(message_id) + uuid.uuid4 = mock.Mock(return_value=message_id) mock_utcnow.return_value = datetime.datetime.utcnow() @@ -199,6 +206,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase): if self.v2: sends.append(dict(version=2.0)) + calls = [] for send_kwargs in sends: for topic in self.topics: if hasattr(self, 'retry'): @@ -207,14 +215,16 @@ class TestMessagingNotifier(test_utils.BaseTestCase): send_kwargs['retry'] = None target = oslo_messaging.Target(topic='%s.%s' % (topic, self.priority)) - transport._send_notification(target, self.ctxt, message, - **send_kwargs).InAnyOrder() - - self.mox.ReplayAll() + calls.append(mock.call(target, + self.ctxt, + message, + **send_kwargs)) method = getattr(notifier, self.priority) method(self.ctxt, 'test.notify', self.payload) + uuid.uuid4.assert_called_once_with() + transport._send_notification.assert_has_calls(calls, any_order=True) TestMessagingNotifier.generate_scenarios() @@ -238,18 +248,15 @@ class TestSerializer(test_utils.BaseTestCase): serializer=serializer) message_id = uuid.uuid4() - self.mox.StubOutWithMock(uuid, 'uuid4') - uuid.uuid4().AndReturn(message_id) + uuid.uuid4 = mock.Mock(return_value=message_id) mock_utcnow.return_value = datetime.datetime.utcnow() - self.mox.StubOutWithMock(serializer, 'serialize_context') - self.mox.StubOutWithMock(serializer, 'serialize_entity') - serializer.serialize_context(dict(user='bob')).\ - AndReturn(dict(user='alice')) - serializer.serialize_entity(dict(user='bob'), 'bar').AndReturn('sbar') + serializer.serialize_context = mock.Mock() + serializer.serialize_context.return_value = dict(user='alice') - self.mox.ReplayAll() + serializer.serialize_entity = mock.Mock() + serializer.serialize_entity.return_value = 'sbar' notifier.info(dict(user='bob'), 'test.notify', 'bar') @@ -265,6 +272,11 @@ class TestSerializer(test_utils.BaseTestCase): self.assertEqual([(dict(user='alice'), message, 'INFO', None)], _impl_test.NOTIFICATIONS) + uuid.uuid4.assert_called_once_with() + serializer.serialize_context.assert_called_once_with(dict(user='bob')) + serializer.serialize_entity.assert_called_once_with(dict(user='bob'), + 'bar') + class TestNotifierTopics(test_utils.BaseTestCase): @@ -303,8 +315,8 @@ class TestLogNotifier(test_utils.BaseTestCase): notifier = oslo_messaging.Notifier(transport, 'test.localhost') message_id = uuid.uuid4() - self.mox.StubOutWithMock(uuid, 'uuid4') - uuid.uuid4().AndReturn(message_id) + uuid.uuid4 = mock.Mock() + uuid.uuid4.return_value = message_id mock_utcnow.return_value = datetime.datetime.utcnow() @@ -317,34 +329,34 @@ class TestLogNotifier(test_utils.BaseTestCase): 'timestamp': str(timeutils.utcnow()), } - logger = self.mox.CreateMockAnything() - - self.mox.StubOutWithMock(logging, 'getLogger') - logging.getLogger('oslo.messaging.notification.test.notify').\ - AndReturn(logger) - - logger.info(jsonutils.dumps(message)) - - self.mox.ReplayAll() + logger = mock.Mock() + logging.getLogger = mock.Mock() + logging.getLogger.return_value = logger notifier.info({}, 'test.notify', 'bar') + uuid.uuid4.assert_called_once_with() + logging.getLogger.assert_called_once_with('oslo.messaging.' + 'notification.test.notify') + logger.info.assert_called_once_with(JsonMessageMatcher(message)) + def test_sample_priority(self): # Ensure logger drops sample-level notifications. driver = _impl_log.LogDriver(None, None, None) - logger = self.mox.CreateMock( - logging.getLogger('oslo.messaging.notification.foo')) + logger = mock.Mock(spec=logging.getLogger('oslo.messaging.' + 'notification.foo')) logger.sample = None - self.mox.StubOutWithMock(logging, 'getLogger') - logging.getLogger('oslo.messaging.notification.foo').\ - AndReturn(logger) - self.mox.ReplayAll() + logging.getLogger = mock.Mock() + logging.getLogger.return_value = logger msg = {'event_type': 'foo'} driver.notify(None, msg, "sample", None) + logging.getLogger.assert_called_once_with('oslo.messaging.' + 'notification.foo') + def test_mask_passwords(self): # Ensure that passwords are masked with notifications driver = _impl_log.LogDriver(None, None, None) diff --git a/oslo_messaging/tests/rpc/test_client.py b/oslo_messaging/tests/rpc/test_client.py old mode 100644 new mode 100755 index 4a527bcd5..16bf42749 --- a/oslo_messaging/tests/rpc/test_client.py +++ b/oslo_messaging/tests/rpc/test_client.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +import mock + from oslo_config import cfg import testscenarios @@ -54,7 +56,7 @@ class TestCastCall(test_utils.BaseTestCase): transport = _FakeTransport(self.conf) client = oslo_messaging.RPCClient(transport, oslo_messaging.Target()) - self.mox.StubOutWithMock(transport, '_send') + transport._send = mock.Mock() msg = dict(method='foo', args=self.args) kwargs = {'retry': None} @@ -62,12 +64,14 @@ class TestCastCall(test_utils.BaseTestCase): kwargs['wait_for_reply'] = True kwargs['timeout'] = None - transport._send(oslo_messaging.Target(), self.ctxt, msg, **kwargs) - self.mox.ReplayAll() - method = client.call if self.call else client.cast method(self.ctxt, 'foo', **self.args) + transport._send.assert_called_once_with(oslo_messaging.Target(), + self.ctxt, + msg, + **kwargs) + class TestCastToTarget(test_utils.BaseTestCase): @@ -191,16 +195,13 @@ class TestCastToTarget(test_utils.BaseTestCase): transport = _FakeTransport(self.conf) client = oslo_messaging.RPCClient(transport, target) - self.mox.StubOutWithMock(transport, '_send') + transport._send = mock.Mock() msg = dict(method='foo', args={}) if 'namespace' in self.expect: msg['namespace'] = self.expect['namespace'] if 'version' in self.expect: msg['version'] = self.expect['version'] - transport._send(expect_target, {}, msg, retry=None) - - self.mox.ReplayAll() if self.prepare: client = client.prepare(**self.prepare) @@ -208,6 +209,11 @@ class TestCastToTarget(test_utils.BaseTestCase): client = client.prepare(**self.prepare) client.cast({}, 'foo') + transport._send.assert_called_once_with(expect_target, + {}, + msg, + retry=None) + TestCastToTarget.generate_scenarios() @@ -241,18 +247,20 @@ class TestCallTimeout(test_utils.BaseTestCase): client = oslo_messaging.RPCClient(transport, oslo_messaging.Target(), timeout=self.ctor) - self.mox.StubOutWithMock(transport, '_send') + transport._send = mock.Mock() msg = dict(method='foo', args={}) kwargs = dict(wait_for_reply=True, timeout=self.expect, retry=None) - transport._send(oslo_messaging.Target(), {}, msg, **kwargs) - - self.mox.ReplayAll() if self.prepare is not _notset: client = client.prepare(timeout=self.prepare) client.call({}, 'foo') + transport._send.assert_called_once_with(oslo_messaging.Target(), + {}, + msg, + **kwargs) + class TestCallRetry(test_utils.BaseTestCase): @@ -270,19 +278,21 @@ class TestCallRetry(test_utils.BaseTestCase): client = oslo_messaging.RPCClient(transport, oslo_messaging.Target(), retry=self.ctor) - self.mox.StubOutWithMock(transport, '_send') + transport._send = mock.Mock() msg = dict(method='foo', args={}) kwargs = dict(wait_for_reply=True, timeout=60, retry=self.expect) - transport._send(oslo_messaging.Target(), {}, msg, **kwargs) - - self.mox.ReplayAll() if self.prepare is not _notset: client = client.prepare(retry=self.prepare) client.call({}, 'foo') + transport._send.assert_called_once_with(oslo_messaging.Target(), + {}, + msg, + **kwargs) + class TestCallFanout(test_utils.BaseTestCase): @@ -328,37 +338,46 @@ class TestSerializer(test_utils.BaseTestCase): client = oslo_messaging.RPCClient(transport, oslo_messaging.Target(), serializer=serializer) - self.mox.StubOutWithMock(transport, '_send') + transport._send = mock.Mock() msg = dict(method='foo', args=dict([(k, 's' + v) for k, v in self.args.items()])) kwargs = dict(wait_for_reply=True, timeout=None) if self.call else {} kwargs['retry'] = None - transport._send(oslo_messaging.Target(), - dict(user='alice'), - msg, - **kwargs).AndReturn(self.retval) - self.mox.StubOutWithMock(serializer, 'serialize_entity') - self.mox.StubOutWithMock(serializer, 'deserialize_entity') - self.mox.StubOutWithMock(serializer, 'serialize_context') + transport._send.return_value = self.retval + serializer.serialize_entity = mock.Mock() + serializer.deserialize_entity = mock.Mock() + serializer.serialize_context = mock.Mock() + + expected_side_effect = [] for arg in self.args: - serializer.serialize_entity(self.ctxt, arg).AndReturn('s' + arg) + expected_side_effect.append('s' + arg) + serializer.serialize_entity.side_effect = expected_side_effect if self.call: - serializer.deserialize_entity(self.ctxt, self.retval).\ - AndReturn('d' + self.retval) + serializer.deserialize_entity.return_value = 'd' + self.retval - serializer.serialize_context(self.ctxt).AndReturn(dict(user='alice')) - - self.mox.ReplayAll() + serializer.serialize_context.return_value = dict(user='alice') method = client.call if self.call else client.cast retval = method(self.ctxt, 'foo', **self.args) if self.retval is not None: self.assertEqual('d' + self.retval, retval) + transport._send.assert_called_once_with(oslo_messaging.Target(), + dict(user='alice'), + msg, + **kwargs) + expected_calls = [mock.call(self.ctxt, arg) for arg in self.args] + self.assertEqual(expected_calls, + serializer.serialize_entity.mock_calls) + if self.call: + serializer.deserialize_entity.assert_called_once_with(self.ctxt, + self.retval) + serializer.serialize_context.assert_called_once_with(self.ctxt) + class TestVersionCap(test_utils.BaseTestCase): @@ -418,7 +437,7 @@ class TestVersionCap(test_utils.BaseTestCase): version_cap=self.cap) if self.success: - self.mox.StubOutWithMock(transport, '_send') + transport._send = mock.Mock() if self.prepare_version is not _notset: target = target(version=self.prepare_version) @@ -432,10 +451,6 @@ class TestVersionCap(test_utils.BaseTestCase): kwargs['wait_for_reply'] = True kwargs['timeout'] = None - transport._send(target, {}, msg, **kwargs) - - self.mox.ReplayAll() - prep_kwargs = {} if self.prepare_cap is not _notset: prep_kwargs['version_cap'] = self.prepare_cap @@ -452,7 +467,7 @@ class TestVersionCap(test_utils.BaseTestCase): self.assertFalse(self.success) else: self.assertTrue(self.success) - + transport._send.assert_called_once_with(target, {}, msg, **kwargs) TestVersionCap.generate_scenarios() diff --git a/oslo_messaging/tests/rpc/test_dispatcher.py b/oslo_messaging/tests/rpc/test_dispatcher.py old mode 100644 new mode 100755 index 23747303d..00b36e718 --- a/oslo_messaging/tests/rpc/test_dispatcher.py +++ b/oslo_messaging/tests/rpc/test_dispatcher.py @@ -231,23 +231,23 @@ class TestSerializer(test_utils.BaseTestCase): serializer = msg_serializer.NoOpSerializer() dispatcher = oslo_messaging.RPCDispatcher([endpoint], serializer) - self.mox.StubOutWithMock(endpoint, 'foo') + endpoint.foo = mock.Mock() + args = dict([(k, 'd' + v) for k, v in self.args.items()]) - endpoint.foo(self.dctxt, **args).AndReturn(self.retval) + endpoint.foo.return_value = self.retval - self.mox.StubOutWithMock(serializer, 'serialize_entity') - self.mox.StubOutWithMock(serializer, 'deserialize_entity') - self.mox.StubOutWithMock(serializer, 'deserialize_context') + serializer.serialize_entity = mock.Mock() + serializer.deserialize_entity = mock.Mock() + serializer.deserialize_context = mock.Mock() - serializer.deserialize_context(self.ctxt).AndReturn(self.dctxt) + serializer.deserialize_context.return_value = self.dctxt - for arg in self.args: - serializer.deserialize_entity(self.dctxt, arg).AndReturn('d' + arg) + expected_side_effect = ['d' + arg for arg in self.args] + serializer.deserialize_entity.side_effect = expected_side_effect - serializer.serialize_entity(self.dctxt, self.retval). \ - AndReturn('s' + self.retval if self.retval else None) - - self.mox.ReplayAll() + serializer.serialize_entity.return_value = None + if self.retval: + serializer.serialize_entity.return_value = 's' + self.retval incoming = mock.Mock() incoming.ctxt = self.ctxt @@ -255,3 +255,13 @@ class TestSerializer(test_utils.BaseTestCase): retval = dispatcher.dispatch(incoming) if self.retval is not None: self.assertEqual('s' + self.retval, retval) + + endpoint.foo.assert_called_once_with(self.dctxt, **args) + serializer.deserialize_context.assert_called_once_with(self.ctxt) + + expected_calls = [mock.call(self.dctxt, arg) for arg in self.args] + self.assertEqual(expected_calls, + serializer.deserialize_entity.mock_calls) + + serializer.serialize_entity.assert_called_once_with(self.dctxt, + self.retval) diff --git a/oslo_messaging/tests/test_transport.py b/oslo_messaging/tests/test_transport.py old mode 100644 new mode 100755 index ee969cf69..1c75da50d --- a/oslo_messaging/tests/test_transport.py +++ b/oslo_messaging/tests/test_transport.py @@ -15,7 +15,6 @@ import fixtures import mock -from mox3 import mox from oslo_config import cfg import six from stevedore import driver @@ -119,7 +118,7 @@ class GetTransportTestCase(test_utils.BaseTestCase): control_exchange=self.control_exchange, transport_url=self.transport_url) - self.mox.StubOutWithMock(driver, 'DriverManager') + driver.DriverManager = mock.Mock() invoke_args = [self.conf, oslo_messaging.TransportURL.parse(self.conf, @@ -128,14 +127,8 @@ class GetTransportTestCase(test_utils.BaseTestCase): allowed_remote_exmods=self.expect['allowed']) drvr = _FakeDriver(self.conf) - driver.DriverManager('oslo.messaging.drivers', - self.expect['backend'], - invoke_on_load=True, - invoke_args=invoke_args, - invoke_kwds=invoke_kwds).\ - AndReturn(_FakeManager(drvr)) - self.mox.ReplayAll() + driver.DriverManager.return_value = _FakeManager(drvr) kwargs = dict(url=self.url) if self.allowed is not None: @@ -156,6 +149,12 @@ class GetTransportTestCase(test_utils.BaseTestCase): self.assertIs(transport_.conf, self.conf) self.assertIs(transport_._driver, drvr) + driver.DriverManager.assert_called_once_with('oslo.messaging.drivers', + self.expect['backend'], + invoke_on_load=True, + invoke_args=invoke_args, + invoke_kwds=invoke_kwds) + class GetTransportSadPathTestCase(test_utils.BaseTestCase): @@ -182,7 +181,7 @@ class GetTransportSadPathTestCase(test_utils.BaseTestCase): transport_url=self.transport_url) if self.rpc_backend: - self.mox.StubOutWithMock(driver, 'DriverManager') + driver.DriverManager = mock.Mock() invoke_args = [self.conf, oslo_messaging.TransportURL.parse(self.conf, @@ -190,18 +189,17 @@ class GetTransportSadPathTestCase(test_utils.BaseTestCase): invoke_kwds = dict(default_exchange='openstack', allowed_remote_exmods=[]) - driver.DriverManager('oslo.messaging.drivers', - self.rpc_backend, - invoke_on_load=True, - invoke_args=invoke_args, - invoke_kwds=invoke_kwds).\ - AndRaise(RuntimeError()) - - self.mox.ReplayAll() - + driver.DriverManager.side_effect = RuntimeError() try: oslo_messaging.get_transport(self.conf, url=self.url) self.assertFalse(True) + + driver.DriverManager.\ + assert_called_once_with('oslo.messaging.drivers', + self.rpc_backend, + invoke_on_load=True, + invoke_args=invoke_args, + invoke_kwds=invoke_kwds) except Exception as ex: ex_cls = self.ex.pop('cls') ex_msg_contains = self.ex.pop('msg_contains') @@ -258,18 +256,20 @@ class TestSetDefaults(test_utils.BaseTestCase): def test_set_default_control_exchange(self): oslo_messaging.set_transport_defaults(control_exchange='foo') - self.mox.StubOutWithMock(driver, 'DriverManager') - invoke_kwds = mox.ContainsKeyValue('default_exchange', 'foo') - driver.DriverManager(mox.IgnoreArg(), - mox.IgnoreArg(), - invoke_on_load=mox.IgnoreArg(), - invoke_args=mox.IgnoreArg(), - invoke_kwds=invoke_kwds).\ - AndReturn(_FakeManager(_FakeDriver(self.conf))) - self.mox.ReplayAll() + driver.DriverManager = mock.Mock() + invoke_kwds = dict(default_exchange='foo', allowed_remote_exmods=[]) + + driver.DriverManager.return_value = \ + _FakeManager(_FakeDriver(self.conf)) oslo_messaging.get_transport(self.conf) + driver.DriverManager.assert_called_once_with(mock.ANY, + mock.ANY, + invoke_on_load=mock.ANY, + invoke_args=mock.ANY, + invoke_kwds=invoke_kwds) + class TestTransportMethodArgs(test_utils.BaseTestCase): @@ -278,57 +278,70 @@ class TestTransportMethodArgs(test_utils.BaseTestCase): def test_send_defaults(self): t = transport.Transport(_FakeDriver(cfg.CONF)) - self.mox.StubOutWithMock(t._driver, 'send') - t._driver.send(self._target, 'ctxt', 'message', - wait_for_reply=None, - timeout=None, retry=None) - self.mox.ReplayAll() + t._driver.send = mock.Mock() t._send(self._target, 'ctxt', 'message') + t._driver.send.assert_called_once_with(self._target, + 'ctxt', + 'message', + wait_for_reply=None, + timeout=None, + retry=None) + def test_send_all_args(self): t = transport.Transport(_FakeDriver(cfg.CONF)) - self.mox.StubOutWithMock(t._driver, 'send') - t._driver.send(self._target, 'ctxt', 'message', - wait_for_reply='wait_for_reply', - timeout='timeout', retry='retry') - self.mox.ReplayAll() + t._driver.send = mock.Mock() t._send(self._target, 'ctxt', 'message', wait_for_reply='wait_for_reply', timeout='timeout', retry='retry') + t._driver.send.\ + assert_called_once_with(self._target, + 'ctxt', + 'message', + wait_for_reply='wait_for_reply', + timeout='timeout', + retry='retry') + def test_send_notification(self): t = transport.Transport(_FakeDriver(cfg.CONF)) - self.mox.StubOutWithMock(t._driver, 'send_notification') - t._driver.send_notification(self._target, 'ctxt', 'message', 1.0, - retry=None) - self.mox.ReplayAll() + t._driver.send_notification = mock.Mock() t._send_notification(self._target, 'ctxt', 'message', version=1.0) + t._driver.send_notification.assert_called_once_with(self._target, + 'ctxt', + 'message', + 1.0, + retry=None) + def test_send_notification_all_args(self): t = transport.Transport(_FakeDriver(cfg.CONF)) - self.mox.StubOutWithMock(t._driver, 'send_notification') - t._driver.send_notification(self._target, 'ctxt', 'message', 1.0, - retry=5) - self.mox.ReplayAll() + t._driver.send_notification = mock.Mock() t._send_notification(self._target, 'ctxt', 'message', version=1.0, retry=5) + t._driver.send_notification.assert_called_once_with(self._target, + 'ctxt', + 'message', + 1.0, + retry=5) + def test_listen(self): t = transport.Transport(_FakeDriver(cfg.CONF)) - self.mox.StubOutWithMock(t._driver, 'listen') - t._driver.listen(self._target, 1, None) - self.mox.ReplayAll() + t._driver.listen = mock.Mock() t._listen(self._target, 1, None) + t._driver.listen.assert_called_once_with(self._target, 1, None) + class TestTransportUrlCustomisation(test_utils.BaseTestCase): def setUp(self):