diff --git a/contrib/rackspace/rackspace/tests/test_auto_scale.py b/contrib/rackspace/rackspace/tests/test_auto_scale.py index 216a2a4f2..601e2f2c0 100644 --- a/contrib/rackspace/rackspace/tests/test_auto_scale.py +++ b/contrib/rackspace/rackspace/tests/test_auto_scale.py @@ -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) diff --git a/heat/tests/common.py b/heat/tests/common.py index 512a360dc..17979d633 100644 --- a/heat/tests/common.py +++ b/heat/tests/common.py @@ -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): diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index c8f65a0a7..4fbb64247 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -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() diff --git a/heat/tests/test_heat_autoscaling_group.py b/heat/tests/test_heat_autoscaling_group.py index 754df11b2..aa522fab9 100644 --- a/heat/tests/test_heat_autoscaling_group.py +++ b/heat/tests/test_heat_autoscaling_group.py @@ -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' diff --git a/heat/tests/test_metadata_refresh.py b/heat/tests/test_metadata_refresh.py index 07a75758f..ff4cd10af 100644 --- a/heat/tests/test_metadata_refresh.py +++ b/heat/tests/test_metadata_refresh.py @@ -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', diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index d7527b761..817fb4e1b 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -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()) diff --git a/heat/tests/test_validate.py b/heat/tests/test_validate.py index df56aedca..8ad7b3844 100644 --- a/heat/tests/test_validate.py +++ b/heat/tests/test_validate.py @@ -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')