From 65c1528b1c7d038778840d298a3da33f220c466e Mon Sep 17 00:00:00 2001 From: git-harry Date: Tue, 25 Nov 2014 14:20:26 +0000 Subject: [PATCH] Match mock.patch decorator with appropriate param mock.patch and mock.patch.object can be used as decorators for mocking within the scope of the function they decorate. When there are multiple decorators it is important the function parameters relate to the corresponding patch objects i.e. that the parameter order matches the decorator order. It is easiest to explain this with an example: @mock.patch.object(Foo, 'bar') @mock.patch.object(SomeClass, 'some_method', some_function) @mock.patch.object(AClass, 'a_method') def test_some_stuff(self, mock_a_method, mock_bar): pass So the decorator closest to the function definition must correspond to the first (left-most) patch parameter. Note, if the decorator is given a third argument, the kwarg new, then the decorated function is not passed an extra argument by that decorator. Change-Id: I035d71cb3b81f0c8bfd83ed81d8426cb0df31c90 --- cinder/tests/test_nimble.py | 4 ++-- cinder/tests/test_volume.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cinder/tests/test_nimble.py b/cinder/tests/test_nimble.py index 9096c9ac3..925eea147 100644 --- a/cinder/tests/test_nimble.py +++ b/cinder/tests/test_nimble.py @@ -277,11 +277,11 @@ class NimbleDriverVolumeTestCase(NimbleDriverBaseTestCase): 'name': 'testvolume', 'sid': 'a9b9aba7'}) - @mock.patch(NIMBLE_RANDOM) @mock.patch(NIMBLE_URLLIB2) @mock.patch(NIMBLE_CLIENT) @NimbleDriverBaseTestCase.client_mock_decorator(create_configuration( 'nimble', 'nimble_pass', '10.18.108.55', 'default', '*', False)) + @mock.patch(NIMBLE_RANDOM) def test_create_cloned_volume(self, mock_random): mock_random.sample.return_value = 'abcdefghijkl' self.mock_client_service.service.snapVol.return_value = \ @@ -471,11 +471,11 @@ class NimbleDriverConnectionTestCase(NimbleDriverBaseTestCase): self.mock_client_service.method_calls, expected_call_list) - @mock.patch(NIMBLE_RANDOM) @mock.patch(NIMBLE_URLLIB2) @mock.patch(NIMBLE_CLIENT) @NimbleDriverBaseTestCase.client_mock_decorator(create_configuration( 'nimble', 'nimble_pass', '10.18.108.55', 'default', '*')) + @mock.patch(NIMBLE_RANDOM) def test_initialize_connection_igroup_not_exist(self, mock_random): mock_random.sample.return_value = 'abcdefghijkl' self.mock_client_service.service.getInitiatorGrpList.return_value = \ diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index a858668c2..5af42eef5 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -323,9 +323,9 @@ class VolumeTestCase(BaseVolumeTestCase): self.assertEqual(volume.status, "error") db.volume_destroy(context.get_admin_context(), volume_id) - @mock.patch.object(QUOTAS, 'reserve') - @mock.patch.object(QUOTAS, 'commit') @mock.patch.object(QUOTAS, 'rollback') + @mock.patch.object(QUOTAS, 'commit') + @mock.patch.object(QUOTAS, 'reserve') def test_delete_driver_not_initialized(self, reserve, commit, rollback): # NOTE(flaper87): Set initialized to False self.volume.driver._initialized = False @@ -1368,8 +1368,8 @@ class VolumeTestCase(BaseVolumeTestCase): @mock.patch.object(db, 'volume_get') @mock.patch.object(db, 'volume_update') def test_initialize_connection_export_failure(self, - _mock_volume_get, _mock_volume_update, + _mock_volume_get, _mock_create_export): """Test exception path for create_export failure.""" _fake_admin_meta = {'fake-key': 'fake-value'} -- 2.45.2