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
This commit is contained in:
Brant Knudson 2017-02-21 14:22:16 -06:00 committed by ChangBo Guo(gcb)
parent e2b5577266
commit 4531490b05
6 changed files with 31 additions and 19 deletions

View File

@ -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,

View File

@ -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):

View File

@ -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',

View File

@ -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):

View File

@ -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)

View File

@ -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.