Use the features that the oslotest mock base class provides

Instead of having our own mock subclass that has similar functions
as the oslotest base mocking class just use the base class functions
where we can and add on our own customizations as we choose to.

This change moves to using the base classes fixtures and also adjusts
the customized subclass method names to match closer to the rest of
the unittest classes method name style (camel-case not underscores).

Change-Id: If24530c0381d7fb99797acaa582d3be1d7054185
This commit is contained in:
Joshua Harlow
2014-09-10 16:39:30 -07:00
parent ce620c399a
commit 5780a5d77e
9 changed files with 34 additions and 36 deletions

View File

@@ -16,6 +16,7 @@
import fixtures import fixtures
from oslotest import base from oslotest import base
from oslotest import mockpatch
import six import six
try: try:
from six.moves import mock from six.moves import mock
@@ -189,25 +190,23 @@ class TestCase(base.BaseTestCase):
self.assertThat(seq2, matcher) self.assertThat(seq2, matcher)
class MockTestCase(base.BaseTestCase): class MockTestCase(TestCase):
def setUp(self): def setUp(self):
super(MockTestCase, self).setUp() super(MockTestCase, self).setUp()
self.master_mock = mock.Mock(name='master_mock') self.master_mock = mock.Mock(name='master_mock')
def _patch(self, target, autospec=True, **kwargs): def patch(self, target, autospec=True, **kwargs):
"""Patch target and attach it to the master mock.""" """Patch target and attach it to the master mock."""
patcher = mock.patch(target, autospec=autospec, **kwargs) f = self.useFixture(mockpatch.Patch(target,
mocked = patcher.start() autospec=autospec, **kwargs))
self.addCleanup(patcher.stop) mocked = f.mock
attach_as = kwargs.pop('attach_as', None) attach_as = kwargs.pop('attach_as', None)
if attach_as is not None: if attach_as is not None:
self.master_mock.attach_mock(mocked, attach_as) self.master_mock.attach_mock(mocked, attach_as)
return mocked return mocked
def _patch_class(self, module, name, autospec=True, attach_as=None): def patchClass(self, module, name, autospec=True, attach_as=None):
"""Patches a modules class. """Patches a modules class.
This will create a class instance mock (using the provided name to This will create a class instance mock (using the provided name to
@@ -219,9 +218,9 @@ class MockTestCase(base.BaseTestCase):
else: else:
instance_mock = mock.Mock() instance_mock = mock.Mock()
patcher = mock.patch.object(module, name, autospec=autospec) f = self.useFixture(mockpatch.PatchObject(module, name,
class_mock = patcher.start() autospec=autospec))
self.addCleanup(patcher.stop) class_mock = f.mock
class_mock.return_value = instance_mock class_mock.return_value = instance_mock
if attach_as is None: if attach_as is None:
@@ -233,8 +232,7 @@ class MockTestCase(base.BaseTestCase):
self.master_mock.attach_mock(class_mock, attach_class_as) self.master_mock.attach_mock(class_mock, attach_class_as)
self.master_mock.attach_mock(instance_mock, attach_instance_as) self.master_mock.attach_mock(instance_mock, attach_instance_as)
return class_mock, instance_mock return class_mock, instance_mock
def _reset_master_mock(self): def resetMasterMock(self):
self.master_mock.reset_mock() self.master_mock.reset_mock()

View File

@@ -34,7 +34,7 @@ def mock_acked_message(ack_ok=True, **kwargs):
return msg return msg
class TestDispatcher(test.MockTestCase): class TestDispatcher(test.TestCase):
def test_creation(self): def test_creation(self):
on_hello = mock.MagicMock() on_hello = mock.MagicMock()
handlers = {'hello': on_hello} handlers = {'hello': on_hello}

View File

@@ -31,7 +31,7 @@ class TestWorkerBasedActionEngine(test.MockTestCase):
self.topics = ['test-topic1', 'test-topic2'] self.topics = ['test-topic1', 'test-topic2']
# patch classes # patch classes
self.executor_mock, self.executor_inst_mock = self._patch_class( self.executor_mock, self.executor_inst_mock = self.patchClass(
engine.executor, 'WorkerTaskExecutor', attach_as='executor') engine.executor, 'WorkerTaskExecutor', attach_as='executor')
def test_creation_default(self): def test_creation_default(self):

View File

@@ -45,9 +45,9 @@ class TestWorkerTaskExecutor(test.MockTestCase):
self.proxy_started_event = threading.Event() self.proxy_started_event = threading.Event()
# patch classes # patch classes
self.proxy_mock, self.proxy_inst_mock = self._patch_class( self.proxy_mock, self.proxy_inst_mock = self.patchClass(
executor.proxy, 'Proxy') executor.proxy, 'Proxy')
self.request_mock, self.request_inst_mock = self._patch_class( self.request_mock, self.request_inst_mock = self.patchClass(
executor.pr, 'Request', autospec=False) executor.pr, 'Request', autospec=False)
# other mocking # other mocking
@@ -56,7 +56,7 @@ class TestWorkerTaskExecutor(test.MockTestCase):
self.request_inst_mock.uuid = self.task_uuid self.request_inst_mock.uuid = self.task_uuid
self.request_inst_mock.expired = False self.request_inst_mock.expired = False
self.request_inst_mock.task_cls = self.task.name self.request_inst_mock.task_cls = self.task.name
self.wait_for_any_mock = self._patch( self.wait_for_any_mock = self.patch(
'taskflow.engines.worker_based.executor.async_utils.wait_for_any') 'taskflow.engines.worker_based.executor.async_utils.wait_for_any')
self.message_mock = mock.MagicMock(name='message') self.message_mock = mock.MagicMock(name='message')
self.message_mock.properties = {'correlation_id': self.task_uuid, self.message_mock.properties = {'correlation_id': self.task_uuid,
@@ -78,7 +78,7 @@ class TestWorkerTaskExecutor(test.MockTestCase):
executor_kwargs.update(kwargs) executor_kwargs.update(kwargs)
ex = executor.WorkerTaskExecutor(**executor_kwargs) ex = executor.WorkerTaskExecutor(**executor_kwargs)
if reset_master_mock: if reset_master_mock:
self._reset_master_mock() self.resetMasterMock()
return ex return ex
def test_creation(self): def test_creation(self):

View File

@@ -29,7 +29,7 @@ BARRIER_WAIT_TIMEOUT = 1.0
POLLING_INTERVAL = 0.01 POLLING_INTERVAL = 0.01
class TestMessagePump(test.MockTestCase): class TestMessagePump(test.TestCase):
def test_notify(self): def test_notify(self):
barrier = threading.Event() barrier = threading.Event()

View File

@@ -32,7 +32,7 @@ WAIT_TIMEOUT = 1.0
POLLING_INTERVAL = 0.01 POLLING_INTERVAL = 0.01
class TestPipeline(test.MockTestCase): class TestPipeline(test.TestCase):
def _fetch_server(self, task_classes): def _fetch_server(self, task_classes):
endpoints = [] endpoints = []
for cls in task_classes: for cls in task_classes:

View File

@@ -34,13 +34,13 @@ class TestProxy(test.MockTestCase):
self.de_period = proxy.DRAIN_EVENTS_PERIOD self.de_period = proxy.DRAIN_EVENTS_PERIOD
# patch classes # patch classes
self.conn_mock, self.conn_inst_mock = self._patch_class( self.conn_mock, self.conn_inst_mock = self.patchClass(
proxy.kombu, 'Connection') proxy.kombu, 'Connection')
self.exchange_mock, self.exchange_inst_mock = self._patch_class( self.exchange_mock, self.exchange_inst_mock = self.patchClass(
proxy.kombu, 'Exchange') proxy.kombu, 'Exchange')
self.queue_mock, self.queue_inst_mock = self._patch_class( self.queue_mock, self.queue_inst_mock = self.patchClass(
proxy.kombu, 'Queue') proxy.kombu, 'Queue')
self.producer_mock, self.producer_inst_mock = self._patch_class( self.producer_mock, self.producer_inst_mock = self.patchClass(
proxy.kombu, 'Producer') proxy.kombu, 'Producer')
# connection mocking # connection mocking
@@ -48,14 +48,14 @@ class TestProxy(test.MockTestCase):
socket.timeout, socket.timeout, KeyboardInterrupt] socket.timeout, socket.timeout, KeyboardInterrupt]
# connections mocking # connections mocking
self.connections_mock = self._patch( self.connections_mock = self.patch(
"taskflow.engines.worker_based.proxy.kombu.connections", "taskflow.engines.worker_based.proxy.kombu.connections",
attach_as='connections') attach_as='connections')
self.connections_mock.__getitem__().acquire().__enter__.return_value =\ self.connections_mock.__getitem__().acquire().__enter__.return_value =\
self.conn_inst_mock self.conn_inst_mock
# producers mocking # producers mocking
self.producers_mock = self._patch( self.producers_mock = self.patch(
"taskflow.engines.worker_based.proxy.kombu.producers", "taskflow.engines.worker_based.proxy.kombu.producers",
attach_as='producers') attach_as='producers')
self.producers_mock.__getitem__().acquire().__enter__.return_value =\ self.producers_mock.__getitem__().acquire().__enter__.return_value =\
@@ -70,7 +70,7 @@ class TestProxy(test.MockTestCase):
self.master_mock.attach_mock(self.on_wait_mock, 'on_wait') self.master_mock.attach_mock(self.on_wait_mock, 'on_wait')
# reset master mock # reset master mock
self._reset_master_mock() self.resetMasterMock()
def _queue_name(self, topic): def _queue_name(self, topic):
return "%s_%s" % (self.exchange_name, topic) return "%s_%s" % (self.exchange_name, topic)
@@ -99,7 +99,7 @@ class TestProxy(test.MockTestCase):
proxy_kwargs.update(kwargs) proxy_kwargs.update(kwargs)
p = proxy.Proxy(**proxy_kwargs) p = proxy.Proxy(**proxy_kwargs)
if reset_master_mock: if reset_master_mock:
self._reset_master_mock() self.resetMasterMock()
return p return p
def test_creation(self): def test_creation(self):

View File

@@ -42,9 +42,9 @@ class TestServer(test.MockTestCase):
ep.Endpoint(task_cls=utils.ProgressingTask)] ep.Endpoint(task_cls=utils.ProgressingTask)]
# patch classes # patch classes
self.proxy_mock, self.proxy_inst_mock = self._patch_class( self.proxy_mock, self.proxy_inst_mock = self.patchClass(
server.proxy, 'Proxy') server.proxy, 'Proxy')
self.response_mock, self.response_inst_mock = self._patch_class( self.response_mock, self.response_inst_mock = self.patchClass(
server.pr, 'Response') server.pr, 'Response')
# other mocking # other mocking
@@ -66,7 +66,7 @@ class TestServer(test.MockTestCase):
server_kwargs.update(kwargs) server_kwargs.update(kwargs)
s = server.Server(**server_kwargs) s = server.Server(**server_kwargs)
if reset_master_mock: if reset_master_mock:
self._reset_master_mock() self.resetMasterMock()
return s return s
def make_request(self, **kwargs): def make_request(self, **kwargs):

View File

@@ -35,13 +35,13 @@ class TestWorker(test.MockTestCase):
self.endpoint_count = 21 self.endpoint_count = 21
# patch classes # patch classes
self.executor_mock, self.executor_inst_mock = self._patch_class( self.executor_mock, self.executor_inst_mock = self.patchClass(
worker.futures, 'ThreadPoolExecutor', attach_as='executor') worker.futures, 'ThreadPoolExecutor', attach_as='executor')
self.server_mock, self.server_inst_mock = self._patch_class( self.server_mock, self.server_inst_mock = self.patchClass(
worker.server, 'Server') worker.server, 'Server')
# other mocking # other mocking
self.threads_count_mock = self._patch( self.threads_count_mock = self.patch(
'taskflow.engines.worker_based.worker.tu.get_optimal_thread_count') 'taskflow.engines.worker_based.worker.tu.get_optimal_thread_count')
self.threads_count_mock.return_value = self.threads_count self.threads_count_mock.return_value = self.threads_count
@@ -53,7 +53,7 @@ class TestWorker(test.MockTestCase):
worker_kwargs.update(kwargs) worker_kwargs.update(kwargs)
w = worker.Worker(**worker_kwargs) w = worker.Worker(**worker_kwargs)
if reset_master_mock: if reset_master_mock:
self._reset_master_mock() self.resetMasterMock()
return w return w
def test_creation(self): def test_creation(self):