Improve mocking in HeatTestCase

As the mock lib is going to be used more actively, there is a need
to improve our boilerplate code in respect to mock.

Changes in this patch include:
* `patchobject` now accepts kwargs and passes them to `mock.patch.object`;
* added `patch` method that uses mock.patch (similar to `patchobject`)
  - NOTE: this shadows `testtools.TestCase.patch` method that uses
    simple monkey-patching in favor of mock-based patching,
    relevant comment added in the code;
* test code changed accordingly where needed.

Change-Id: I779cab63249d5eb1962e9ec388cd31f3df4a4189
This commit is contained in:
Pavlo Shchelokovskyy 2014-07-11 23:42:21 +03:00
parent f1080b6225
commit 3d65dba163
7 changed files with 32 additions and 50 deletions

View File

@ -204,8 +204,8 @@ class ScalingGroupTest(HeatTestCase):
for res_name, res_class in auto_scale.resource_mapping().items():
resource._register_class(res_name, res_class)
self.fake_auto_scale = FakeAutoScale()
self.patch(clients.OpenStackClients,
'auto_scale', lambda resource: self.fake_auto_scale)
self.patchobject(clients.OpenStackClients, 'auto_scale',
return_value=self.fake_auto_scale, create=True)
def _setup_test_stack(self):
self.stack = utils.parse_stack(self.group_template)
@ -382,7 +382,7 @@ Resources:
if count < 3:
raise auto_scale.Forbidden("Not empty!")
self.patch(self.fake_auto_scale, 'delete', delete)
self.patchobject(self.fake_auto_scale, 'delete', new=delete)
resource = self.stack['my_group']
scheduler.TaskRunner(resource.delete)()
# It really called delete until it succeeded:
@ -398,7 +398,7 @@ Resources:
def delete(group_id):
1 / 0
self.patch(self.fake_auto_scale, 'delete', delete)
self.patchobject(self.fake_auto_scale, 'delete', new=delete)
resource = self.stack['my_group']
err = self.assertRaises(
exception.ResourceFailure, scheduler.TaskRunner(resource.delete))
@ -426,8 +426,8 @@ class PolicyTest(HeatTestCase):
for res_name, res_class in auto_scale.resource_mapping().items():
resource._register_class(res_name, res_class)
self.fake_auto_scale = FakeAutoScale()
self.patch(clients.OpenStackClients,
'auto_scale', lambda resource: self.fake_auto_scale)
self.patchobject(clients.OpenStackClients, 'auto_scale',
return_value=self.fake_auto_scale, create=True)
def _setup_test_stack(self, template):
self.stack = utils.parse_stack(template)
@ -570,8 +570,8 @@ class WebHookTest(HeatTestCase):
for res_name, res_class in auto_scale.resource_mapping().items():
resource._register_class(res_name, res_class)
self.fake_auto_scale = FakeAutoScale()
self.patch(clients.OpenStackClients,
'auto_scale', lambda resource: self.fake_auto_scale)
self.patchobject(clients.OpenStackClients, 'auto_scale',
return_value=self.fake_auto_scale, create=True)
def _setup_test_stack(self, template):
self.stack = utils.parse_stack(template)

View File

@ -110,8 +110,15 @@ class HeatTestCase(testscenarios.WithScenarios,
self.m.StubOutWithMock(scheduler, 'wallclock')
scheduler.wallclock = fake_wallclock
def patchobject(self, obj, attr):
mockfixture = self.useFixture(mockpatch.PatchObject(obj, attr))
def patchobject(self, obj, attr, **kwargs):
mockfixture = self.useFixture(mockpatch.PatchObject(obj, attr,
**kwargs))
return mockfixture.mock
# NOTE(pshchelo): this overrides the testtools.TestCase.patch method
# that does simple monkey-patching in favor of mock's patching
def patch(self, target, **kwargs):
mockfixture = self.useFixture(mockpatch.Patch(target, **kwargs))
return mockfixture.mock
def stub_keystoneclient(self, fake_client=None, **kwargs):

View File

@ -24,7 +24,6 @@ from oslo.config import cfg
from oslo import messaging
from oslo.messaging.rpc import client as rpc_client
from oslo.messaging.rpc import dispatcher
from oslotest import mockpatch
import six
from heat.common import exception
@ -175,12 +174,6 @@ resources:
'''
def mock_warnings(test):
mock_warnings = mock.patch('heat.engine.service.warnings')
mock_warnings.start()
test.addCleanup(mock_warnings.stop)
def get_wordpress_stack(stack_name, ctx):
t = template_format.parse(wp_template)
template = parser.Template(t)
@ -432,7 +425,7 @@ class StackServiceCreateUpdateDeleteTest(HeatTestCase):
def setUp(self):
super(StackServiceCreateUpdateDeleteTest, self).setUp()
self.ctx = utils.dummy_context()
mock_warnings(self)
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
self.man.create_periodic_tasks()
@ -1257,7 +1250,7 @@ class StackServiceUpdateSuspendedNotSupportedTest(HeatTestCase):
def setUp(self):
super(StackServiceUpdateSuspendedNotSupportedTest, self).setUp()
self.ctx = utils.dummy_context()
mock_warnings(self)
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
def test_stack_update_suspended(self):
@ -1291,7 +1284,7 @@ class StackServiceSuspendResumeTest(HeatTestCase):
def setUp(self):
super(StackServiceSuspendResumeTest, self).setUp()
self.ctx = utils.dummy_context()
mock_warnings(self)
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
self.man.create_periodic_tasks()
@ -1365,7 +1358,7 @@ class StackServiceAuthorizeTest(HeatTestCase):
super(StackServiceAuthorizeTest, self).setUp()
self.ctx = utils.dummy_context(tenant_id='stack_service_test_tenant')
mock_warnings(self)
self.patch('heat.engine.service.warnings')
self.eng = service.EngineService('a-host', 'a-topic')
self.eng.engine_id = 'engine-fake-uuid'
cfg.CONF.set_default('heat_stack_user_role', 'stack_user_role')
@ -1460,7 +1453,7 @@ class StackServiceTest(HeatTestCase):
super(StackServiceTest, self).setUp()
self.ctx = utils.dummy_context(tenant_id='stack_service_test_tenant')
mock_warnings(self)
self.patch('heat.engine.service.warnings')
self.eng = service.EngineService('a-host', 'a-topic')
self.eng.create_periodic_tasks()
self.eng.engine_id = 'engine-fake-uuid'
@ -2722,7 +2715,7 @@ class SoftwareConfigServiceTest(HeatTestCase):
def setUp(self):
super(SoftwareConfigServiceTest, self).setUp()
self.ctx = utils.dummy_context()
mock_warnings(self)
self.patch('heat.engine.service.warnings')
self.engine = service.EngineService('a-host', 'a-topic')
def _create_software_config(
@ -3052,15 +3045,12 @@ class ThreadGroupManagerTest(HeatTestCase):
self.engine_id = 'engine_id'
self.stack = mock.Mock()
self.lock_mock = mock.Mock()
self.stlock_mock = self.useFixture(
mockpatch.Patch('heat.engine.service.stack_lock')).mock
self.stlock_mock = self.patch('heat.engine.service.stack_lock')
self.stlock_mock.StackLock.return_value = self.lock_mock
self.tg_mock = mock.Mock()
self.thg_mock = self.useFixture(
mockpatch.Patch('heat.engine.service.threadgroup')).mock
self.thg_mock = self.patch('heat.engine.service.threadgroup')
self.thg_mock.ThreadGroup.return_value = self.tg_mock
self.cfg_mock = self.useFixture(
mockpatch.Patch('heat.engine.service.cfg')).mock
self.cfg_mock = self.patch('heat.engine.service.cfg')
def test_tgm_start_with_lock(self):
thm = service.ThreadGroupManager()

View File

@ -13,7 +13,6 @@
import copy
import datetime
import itertools
import mock
from oslo.config import cfg
@ -436,11 +435,9 @@ class RollingUpdatesTest(HeatTestCase):
templates = [definitions[name] for name in created_order]
batches.append(templates)
patcher = mock.patch.object(
self.patchobject(
stack_resource.StackResource, 'update_with_template',
side_effect=update_with_template, wraps=rsrc.update_with_template)
patcher.start()
self.addCleanup(patcher.stop)
props = copy.deepcopy(rsrc.properties.data)
props['resource']['properties']['Foo'] = 'Hi'

View File

@ -12,7 +12,6 @@
# under the License.
import mock
import mox
from oslo.config import cfg
@ -218,9 +217,7 @@ class WaitCondMetadataUpdateTest(HeatTestCase):
def setUp(self):
super(WaitCondMetadataUpdateTest, self).setUp()
self.stub_keystoneclient()
self.mock_warnings = mock.patch('heat.engine.service.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
self.patch('heat.engine.service.warnings')
self.man = service.EngineService('a-host', 'a-topic')
cfg.CONF.set_default('heat_waitcondition_server_url',

View File

@ -54,9 +54,7 @@ class ResourceTest(HeatTestCase):
self.stack = parser.Stack(utils.dummy_context(), 'test_stack',
parser.Template(empty_template), env=env,
stack_id=str(uuid.uuid4()))
self.mock_warnings = mock.patch('heat.engine.resource.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
self.patch('heat.engine.resource.warnings')
def test_get_class_ok(self):
cls = resource.get_class('GenericResourceType')
@ -926,9 +924,7 @@ class ResourceAdoptTest(HeatTestCase):
super(ResourceAdoptTest, self).setUp()
resource._register_class('GenericResourceType',
generic_rsrc.GenericResource)
self.mock_warnings = mock.patch('heat.engine.resource.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
self.patch('heat.engine.resource.warnings')
def test_adopt_resource_success(self):
adopt_data = '{}'
@ -1504,9 +1500,7 @@ class MetadataTest(HeatTestCase):
scheduler.TaskRunner(self.res.create)()
self.addCleanup(self.stack.delete)
self.mock_warnings = mock.patch('heat.engine.resource.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
self.patch('heat.engine.resource.warnings')
def test_read_initial(self):
self.assertEqual({'Test': 'Initial metadata'}, self.res.metadata_get())

View File

@ -12,7 +12,6 @@
# under the License.
from glanceclient import exc as glance_exceptions
import mock
import six
from heat.common import exception
@ -812,9 +811,7 @@ class validateTest(HeatTestCase):
self.gc = fakes.FakeClient()
resources.initialise()
self.ctx = utils.dummy_context()
self.mock_warnings = mock.patch('heat.engine.service.warnings')
self.mock_warnings.start()
self.addCleanup(self.mock_warnings.stop)
self.patch('heat.engine.service.warnings')
def _mock_get_image_id_success(self, imageId_input, imageId):
self.m.StubOutWithMock(glance.GlanceClientPlugin, 'get_image_id')