From 4531490b05528c245658f81af77319b07c638a99 Mon Sep 17 00:00:00 2001 From: Brant Knudson <bknudson@us.ibm.com> Date: Tue, 21 Feb 2017 14:22:16 -0600 Subject: [PATCH] Remove use of mox stubs OpenStack projects should be using mock rather than mox. oslo.messaging was using mox's stubs via oslotest rather than mock.patch.object. The code is converted to use mock.patch.object via fixtures.MockPatchObject. Change-Id: I19490b4e8211c35b237ebfd38bf2f8b8b44cbf61 --- .../tests/drivers/test_impl_rabbit.py | 3 ++- oslo_messaging/tests/drivers/test_pool.py | 19 ++++++++++++------- .../tests/notify/test_log_handler.py | 5 ++++- oslo_messaging/tests/notify/test_notifier.py | 6 ++++-- oslo_messaging/tests/rpc/test_server.py | 13 +++++++++---- oslo_messaging/tests/utils.py | 4 ---- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/oslo_messaging/tests/drivers/test_impl_rabbit.py b/oslo_messaging/tests/drivers/test_impl_rabbit.py index 8a8b96a60..e9e42b1c3 100644 --- a/oslo_messaging/tests/drivers/test_impl_rabbit.py +++ b/oslo_messaging/tests/drivers/test_impl_rabbit.py @@ -588,7 +588,8 @@ class TestRacyWaitForReply(test_utils.BaseTestCase): cond.wait() return orig_reply_waiter(self, msg_id, timeout) - self.stubs.Set(amqpdriver.ReplyWaiter, 'wait', reply_waiter) + self.useFixture(fixtures.MockPatchObject( + amqpdriver.ReplyWaiter, 'wait', reply_waiter)) def send_and_wait_for_reply(i, wait_for_reply): replies.append(driver.send(target, diff --git a/oslo_messaging/tests/drivers/test_pool.py b/oslo_messaging/tests/drivers/test_pool.py index a0b6ab47c..d5c642014 100644 --- a/oslo_messaging/tests/drivers/test_pool.py +++ b/oslo_messaging/tests/drivers/test_pool.py @@ -16,6 +16,7 @@ import threading import uuid +import fixtures import testscenarios from oslo_messaging._drivers import pool @@ -54,9 +55,9 @@ class PoolTestCase(test_utils.BaseTestCase): has been called by each thread. """ - def __init__(self, cond, n_threads, stubs): + def __init__(self, cond, n_threads, test): self.cond = cond - self.stubs = stubs + self.test = test self.n_threads = n_threads self.n_waits = 0 self.orig_wait = cond.wait @@ -64,12 +65,14 @@ class PoolTestCase(test_utils.BaseTestCase): def count_waits(**kwargs): self.n_waits += 1 self.orig_wait(**kwargs) - self.stubs.Set(self.cond, 'wait', count_waits) + self.test.useFixture(fixtures.MockPatchObject( + self.cond, 'wait', count_waits)) def wait(self): while self.n_waits < self.n_threads: pass - self.stubs.Set(self.cond, 'wait', self.orig_wait) + self.test.useFixture(fixtures.MockPatchObject( + self.cond, 'wait', self.orig_wait)) def test_pool(self): kwargs = {} @@ -82,9 +85,11 @@ class PoolTestCase(test_utils.BaseTestCase): def create_error(): raise RuntimeError orig_create = p.create - self.stubs.Set(p, 'create', create_error) + self.useFixture(fixtures.MockPatchObject( + p, 'create', create_error)) self.assertRaises(RuntimeError, p.get) - self.stubs.Set(p, 'create', orig_create) + self.useFixture(fixtures.MockPatchObject( + p, 'create', orig_create)) objs = [] for i in range(self.n_iters): @@ -95,7 +100,7 @@ class PoolTestCase(test_utils.BaseTestCase): o = p.get() self.assertIn(o, objs) - waiter = self.ThreadWaitWaiter(p._cond, self.n_iters, self.stubs) + waiter = self.ThreadWaitWaiter(p._cond, self.n_iters, self) threads = [] for i in range(self.n_iters): diff --git a/oslo_messaging/tests/notify/test_log_handler.py b/oslo_messaging/tests/notify/test_log_handler.py index 1851321dd..6ca5f683a 100644 --- a/oslo_messaging/tests/notify/test_log_handler.py +++ b/oslo_messaging/tests/notify/test_log_handler.py @@ -12,6 +12,8 @@ import logging +import fixtures + import oslo_messaging from oslo_messaging.notify import log_handler from oslo_messaging.tests.notify import test_notifier @@ -38,7 +40,8 @@ class PublishErrorsHandlerTestCase(test_utils.BaseTestCase): def fake_notifier(*args, **kwargs): self.stub_flg = False - self.stubs.Set(notifier, 'error', fake_notifier) + self.useFixture(fixtures.MockPatchObject( + notifier, 'error', fake_notifier)) logrecord = logging.LogRecord(name='name', level='WARN', pathname='/tmp', lineno=1, msg='Message', diff --git a/oslo_messaging/tests/notify/test_notifier.py b/oslo_messaging/tests/notify/test_notifier.py index 7484311f9..b6ee8ff8a 100755 --- a/oslo_messaging/tests/notify/test_notifier.py +++ b/oslo_messaging/tests/notify/test_notifier.py @@ -153,8 +153,10 @@ class TestMessagingNotifier(test_utils.BaseTestCase): super(TestMessagingNotifier, self).setUp() self.logger = self.useFixture(_ReRaiseLoggedExceptionsFixture()).logger - self.stubs.Set(messaging, 'LOG', self.logger) - self.stubs.Set(msg_notifier, '_LOG', self.logger) + self.useFixture(fixtures.MockPatchObject( + messaging, 'LOG', self.logger)) + self.useFixture(fixtures.MockPatchObject( + msg_notifier, '_LOG', self.logger)) @mock.patch('oslo_utils.timeutils.utcnow') def test_notifier(self, mock_utcnow): diff --git a/oslo_messaging/tests/rpc/test_server.py b/oslo_messaging/tests/rpc/test_server.py index 56b5bd1f5..f44cd8b91 100644 --- a/oslo_messaging/tests/rpc/test_server.py +++ b/oslo_messaging/tests/rpc/test_server.py @@ -17,6 +17,7 @@ import threading import warnings import eventlet +import fixtures from oslo_config import cfg from six.moves import mock import testscenarios @@ -362,8 +363,10 @@ class TestRPCServer(test_utils.BaseTestCase, ServerSetupMixin): a = a[0] errors.append(str(msg) % a) - self.stubs.Set(rpc_server_module.LOG, 'debug', stub_debug) - self.stubs.Set(rpc_server_module.LOG, 'error', stub_error) + self.useFixture(fixtures.MockPatchObject( + rpc_server_module.LOG, 'debug', stub_debug)) + self.useFixture(fixtures.MockPatchObject( + rpc_server_module.LOG, 'error', stub_error)) server_thread = self._setup_server(transport, TestEndpoint()) client = self._setup_client(transport) @@ -396,8 +399,10 @@ class TestRPCServer(test_utils.BaseTestCase, ServerSetupMixin): a = a[0] errors.append(str(msg) % a) - self.stubs.Set(rpc_server_module.LOG, 'debug', stub_debug) - self.stubs.Set(rpc_server_module.LOG, 'error', stub_error) + self.useFixture(fixtures.MockPatchObject( + rpc_server_module.LOG, 'debug', stub_debug)) + self.useFixture(fixtures.MockPatchObject( + rpc_server_module.LOG, 'error', stub_error)) class TestEndpoint(object): @oslo_messaging.expected_exceptions(ValueError) diff --git a/oslo_messaging/tests/utils.py b/oslo_messaging/tests/utils.py index 8dcdede8f..451b9551b 100644 --- a/oslo_messaging/tests/utils.py +++ b/oslo_messaging/tests/utils.py @@ -23,7 +23,6 @@ import threading from oslo_config import cfg from oslotest import base -from oslotest import moxstubout TRUE_VALUES = ('true', '1', 'yes') @@ -42,9 +41,6 @@ class BaseTestCase(base.BaseTestCase): self.conf.project = 'project' self.conf.prog = 'prog' - moxfixture = self.useFixture(moxstubout.MoxStubout()) - self.stubs = moxfixture.stubs - def config(self, **kw): """Override some configuration values.