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:
@@ -16,6 +16,7 @@
|
||||
|
||||
import fixtures
|
||||
from oslotest import base
|
||||
from oslotest import mockpatch
|
||||
import six
|
||||
try:
|
||||
from six.moves import mock
|
||||
@@ -189,25 +190,23 @@ class TestCase(base.BaseTestCase):
|
||||
self.assertThat(seq2, matcher)
|
||||
|
||||
|
||||
class MockTestCase(base.BaseTestCase):
|
||||
class MockTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(MockTestCase, self).setUp()
|
||||
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."""
|
||||
patcher = mock.patch(target, autospec=autospec, **kwargs)
|
||||
mocked = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
f = self.useFixture(mockpatch.Patch(target,
|
||||
autospec=autospec, **kwargs))
|
||||
mocked = f.mock
|
||||
attach_as = kwargs.pop('attach_as', None)
|
||||
if attach_as is not None:
|
||||
self.master_mock.attach_mock(mocked, attach_as)
|
||||
|
||||
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.
|
||||
|
||||
This will create a class instance mock (using the provided name to
|
||||
@@ -219,9 +218,9 @@ class MockTestCase(base.BaseTestCase):
|
||||
else:
|
||||
instance_mock = mock.Mock()
|
||||
|
||||
patcher = mock.patch.object(module, name, autospec=autospec)
|
||||
class_mock = patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
f = self.useFixture(mockpatch.PatchObject(module, name,
|
||||
autospec=autospec))
|
||||
class_mock = f.mock
|
||||
class_mock.return_value = instance_mock
|
||||
|
||||
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(instance_mock, attach_instance_as)
|
||||
|
||||
return class_mock, instance_mock
|
||||
|
||||
def _reset_master_mock(self):
|
||||
def resetMasterMock(self):
|
||||
self.master_mock.reset_mock()
|
||||
|
||||
@@ -34,7 +34,7 @@ def mock_acked_message(ack_ok=True, **kwargs):
|
||||
return msg
|
||||
|
||||
|
||||
class TestDispatcher(test.MockTestCase):
|
||||
class TestDispatcher(test.TestCase):
|
||||
def test_creation(self):
|
||||
on_hello = mock.MagicMock()
|
||||
handlers = {'hello': on_hello}
|
||||
|
||||
@@ -31,7 +31,7 @@ class TestWorkerBasedActionEngine(test.MockTestCase):
|
||||
self.topics = ['test-topic1', 'test-topic2']
|
||||
|
||||
# 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')
|
||||
|
||||
def test_creation_default(self):
|
||||
|
||||
@@ -45,9 +45,9 @@ class TestWorkerTaskExecutor(test.MockTestCase):
|
||||
self.proxy_started_event = threading.Event()
|
||||
|
||||
# patch classes
|
||||
self.proxy_mock, self.proxy_inst_mock = self._patch_class(
|
||||
self.proxy_mock, self.proxy_inst_mock = self.patchClass(
|
||||
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)
|
||||
|
||||
# other mocking
|
||||
@@ -56,7 +56,7 @@ class TestWorkerTaskExecutor(test.MockTestCase):
|
||||
self.request_inst_mock.uuid = self.task_uuid
|
||||
self.request_inst_mock.expired = False
|
||||
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')
|
||||
self.message_mock = mock.MagicMock(name='message')
|
||||
self.message_mock.properties = {'correlation_id': self.task_uuid,
|
||||
@@ -78,7 +78,7 @@ class TestWorkerTaskExecutor(test.MockTestCase):
|
||||
executor_kwargs.update(kwargs)
|
||||
ex = executor.WorkerTaskExecutor(**executor_kwargs)
|
||||
if reset_master_mock:
|
||||
self._reset_master_mock()
|
||||
self.resetMasterMock()
|
||||
return ex
|
||||
|
||||
def test_creation(self):
|
||||
|
||||
@@ -29,7 +29,7 @@ BARRIER_WAIT_TIMEOUT = 1.0
|
||||
POLLING_INTERVAL = 0.01
|
||||
|
||||
|
||||
class TestMessagePump(test.MockTestCase):
|
||||
class TestMessagePump(test.TestCase):
|
||||
def test_notify(self):
|
||||
barrier = threading.Event()
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ WAIT_TIMEOUT = 1.0
|
||||
POLLING_INTERVAL = 0.01
|
||||
|
||||
|
||||
class TestPipeline(test.MockTestCase):
|
||||
class TestPipeline(test.TestCase):
|
||||
def _fetch_server(self, task_classes):
|
||||
endpoints = []
|
||||
for cls in task_classes:
|
||||
|
||||
@@ -34,13 +34,13 @@ class TestProxy(test.MockTestCase):
|
||||
self.de_period = proxy.DRAIN_EVENTS_PERIOD
|
||||
|
||||
# patch classes
|
||||
self.conn_mock, self.conn_inst_mock = self._patch_class(
|
||||
self.conn_mock, self.conn_inst_mock = self.patchClass(
|
||||
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')
|
||||
self.queue_mock, self.queue_inst_mock = self._patch_class(
|
||||
self.queue_mock, self.queue_inst_mock = self.patchClass(
|
||||
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')
|
||||
|
||||
# connection mocking
|
||||
@@ -48,14 +48,14 @@ class TestProxy(test.MockTestCase):
|
||||
socket.timeout, socket.timeout, KeyboardInterrupt]
|
||||
|
||||
# connections mocking
|
||||
self.connections_mock = self._patch(
|
||||
self.connections_mock = self.patch(
|
||||
"taskflow.engines.worker_based.proxy.kombu.connections",
|
||||
attach_as='connections')
|
||||
self.connections_mock.__getitem__().acquire().__enter__.return_value =\
|
||||
self.conn_inst_mock
|
||||
|
||||
# producers mocking
|
||||
self.producers_mock = self._patch(
|
||||
self.producers_mock = self.patch(
|
||||
"taskflow.engines.worker_based.proxy.kombu.producers",
|
||||
attach_as='producers')
|
||||
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')
|
||||
|
||||
# reset master mock
|
||||
self._reset_master_mock()
|
||||
self.resetMasterMock()
|
||||
|
||||
def _queue_name(self, topic):
|
||||
return "%s_%s" % (self.exchange_name, topic)
|
||||
@@ -99,7 +99,7 @@ class TestProxy(test.MockTestCase):
|
||||
proxy_kwargs.update(kwargs)
|
||||
p = proxy.Proxy(**proxy_kwargs)
|
||||
if reset_master_mock:
|
||||
self._reset_master_mock()
|
||||
self.resetMasterMock()
|
||||
return p
|
||||
|
||||
def test_creation(self):
|
||||
|
||||
@@ -42,9 +42,9 @@ class TestServer(test.MockTestCase):
|
||||
ep.Endpoint(task_cls=utils.ProgressingTask)]
|
||||
|
||||
# patch classes
|
||||
self.proxy_mock, self.proxy_inst_mock = self._patch_class(
|
||||
self.proxy_mock, self.proxy_inst_mock = self.patchClass(
|
||||
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')
|
||||
|
||||
# other mocking
|
||||
@@ -66,7 +66,7 @@ class TestServer(test.MockTestCase):
|
||||
server_kwargs.update(kwargs)
|
||||
s = server.Server(**server_kwargs)
|
||||
if reset_master_mock:
|
||||
self._reset_master_mock()
|
||||
self.resetMasterMock()
|
||||
return s
|
||||
|
||||
def make_request(self, **kwargs):
|
||||
|
||||
@@ -35,13 +35,13 @@ class TestWorker(test.MockTestCase):
|
||||
self.endpoint_count = 21
|
||||
|
||||
# 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')
|
||||
self.server_mock, self.server_inst_mock = self._patch_class(
|
||||
self.server_mock, self.server_inst_mock = self.patchClass(
|
||||
worker.server, 'Server')
|
||||
|
||||
# other mocking
|
||||
self.threads_count_mock = self._patch(
|
||||
self.threads_count_mock = self.patch(
|
||||
'taskflow.engines.worker_based.worker.tu.get_optimal_thread_count')
|
||||
self.threads_count_mock.return_value = self.threads_count
|
||||
|
||||
@@ -53,7 +53,7 @@ class TestWorker(test.MockTestCase):
|
||||
worker_kwargs.update(kwargs)
|
||||
w = worker.Worker(**worker_kwargs)
|
||||
if reset_master_mock:
|
||||
self._reset_master_mock()
|
||||
self.resetMasterMock()
|
||||
return w
|
||||
|
||||
def test_creation(self):
|
||||
|
||||
Reference in New Issue
Block a user