From 2c22199f637e2675b6022fc6460a45bb86f7f825 Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Thu, 18 May 2017 16:38:04 +0200 Subject: [PATCH] Fix py27 gate - Remove Tooz usage for tests The gate is failing and it looks related to the TooZ library calls. This patch mocks the tooz calls for anything in the tests that doesn't actually require them. Change-Id: If0c669417d882c1d8d6e667e357806aa8342cd0f Closes-Bug: #1691488 --- cinder/test.py | 6 + cinder/tests/unit/test_coordination.py | 2 + cinder/tests/unit/volume/test_volume.py | 200 ++++++++++++------------ 3 files changed, 110 insertions(+), 98 deletions(-) diff --git a/cinder/test.py b/cinder/test.py index 1454668f2bb..0f8f129e427 100644 --- a/cinder/test.py +++ b/cinder/test.py @@ -93,6 +93,7 @@ class TestCase(testtools.TestCase): POLICY_PATH = 'cinder/tests/unit/policy.json' MOCK_WORKER = True + MOCK_TOOZ = True def _get_joined_notifier(self, *args, **kwargs): # We create a new fake notifier but we join the notifications with @@ -119,6 +120,11 @@ class TestCase(testtools.TestCase): for method in ('create_worker', 'set_worker', 'unset_worker'): self.patch(clean_path % method, return_value=None) + if self.MOCK_TOOZ: + self.patch('cinder.coordination.Coordinator.start') + self.patch('cinder.coordination.Coordinator.stop') + self.patch('cinder.coordination.Coordinator.get_lock') + # Unit tests do not need to use lazy gettext i18n.enable_lazy(False) diff --git a/cinder/tests/unit/test_coordination.py b/cinder/tests/unit/test_coordination.py index 7b2d43a87b4..9e07e2ad967 100644 --- a/cinder/tests/unit/test_coordination.py +++ b/cinder/tests/unit/test_coordination.py @@ -50,6 +50,8 @@ class MockToozLock(tooz.locking.Lock): @mock.patch('tooz.coordination.get_coordinator') @mock.patch('random.uniform', lambda _a, _b: 0) class CoordinatorTestCase(test.TestCase): + MOCK_TOOZ = False + def test_coordinator_start(self, get_coordinator, heartbeat): crd = get_coordinator.return_value diff --git a/cinder/tests/unit/volume/test_volume.py b/cinder/tests/unit/volume/test_volume.py index 001761c5eca..84674230567 100644 --- a/cinder/tests/unit/volume/test_volume.py +++ b/cinder/tests/unit/volume/test_volume.py @@ -1264,49 +1264,6 @@ class VolumeTestCase(base.BaseVolumeTestCase): self.volume.delete_volume(self.context, src_vol) mock_lock.assert_called_with('%s-delete_volume' % src_vol_id) - def test_create_volume_from_volume_delete_lock_taken(self): - # create source volume - src_vol = tests_utils.create_volume(self.context, **self.volume_params) - src_vol_id = src_vol['id'] - - # no lock - self.volume.create_volume(self.context, src_vol) - - dst_vol = tests_utils.create_volume(self.context, - source_volid=src_vol_id, - **self.volume_params) - - orig_elevated = self.context.elevated - - gthreads = [] - - def mock_elevated(*args, **kwargs): - # unset mock so it is only called once - self.mock_object(self.context, 'elevated', orig_elevated) - - # we expect this to block and then fail - t = eventlet.spawn(self.volume.create_volume, - self.context, - volume=dst_vol, - request_spec={'source_volid': src_vol_id}) - gthreads.append(t) - - return orig_elevated(*args, **kwargs) - - # mock something from early on in the delete operation and within the - # lock so that when we do the create we expect it to block. - self.mock_object(self.context, 'elevated', mock_elevated) - - # locked - self.volume.delete_volume(self.context, src_vol) - - # we expect the volume create to fail with the following err since the - # source volume was deleted while the create was locked. Note that the - # volume is still in the db since it was created by the test prior to - # calling manager.create_volume. - with mock.patch('sys.stderr', new=six.StringIO()): - self.assertRaises(exception.VolumeNotFound, gthreads[0].wait) - def _raise_metadata_copy_failure(self, method, dst_vol): # MetadataCopyFailure exception will be raised if DB service is Down # while copying the volume glance metadata @@ -1508,61 +1465,6 @@ class VolumeTestCase(base.BaseVolumeTestCase): db.volume_destroy(self.context, volume_dst['id']) db.volume_destroy(self.context, volume_src['id']) - def test_create_volume_from_snapshot_delete_lock_taken(self): - # create source volume - src_vol = tests_utils.create_volume(self.context, **self.volume_params) - - # no lock - self.volume.create_volume(self.context, src_vol) - - # create snapshot - snap_id = create_snapshot(src_vol.id, - size=src_vol['size'])['id'] - snapshot_obj = objects.Snapshot.get_by_id(self.context, snap_id) - # no lock - self.volume.create_snapshot(self.context, snapshot_obj) - - # create vol from snapshot... - dst_vol = tests_utils.create_volume(self.context, - snapshot_id=snap_id, - source_volid=src_vol.id, - **self.volume_params) - - orig_elevated = self.context.elevated - - gthreads = [] - - def mock_elevated(*args, **kwargs): - # unset mock so it is only called once - self.mock_object(self.context, 'elevated', orig_elevated) - - # We expect this to block and then fail - t = eventlet.spawn(self.volume.create_volume, self.context, - volume=dst_vol, - request_spec={'snapshot_id': snap_id}) - gthreads.append(t) - - return orig_elevated(*args, **kwargs) - - # mock something from early on in the delete operation and within the - # lock so that when we do the create we expect it to block. - self.mock_object(self.context, 'elevated', mock_elevated) - - # locked - self.volume.delete_snapshot(self.context, snapshot_obj) - - # we expect the volume create to fail with the following err since the - # snapshot was deleted while the create was locked. Note that the - # volume is still in the db since it was created by the test prior to - # calling manager.create_volume. - with mock.patch('sys.stderr', new=six.StringIO()): - self.assertRaises(exception.SnapshotNotFound, gthreads[0].wait) - # locked - self.volume.delete_volume(self.context, src_vol) - # make sure it is gone - self.assertRaises(exception.VolumeNotFound, db.volume_get, - self.context, src_vol.id) - @mock.patch.object(key_manager, 'API', fake_keymgr.fake_api) def test_create_volume_from_snapshot_with_encryption(self): """Test volume can be created from a snapshot of an encrypted volume""" @@ -2652,3 +2554,105 @@ class VolumeTestCase(base.BaseVolumeTestCase): with mock.patch.object(volume, 'save') as save_mock: manager._set_resource_host(volume) save_mock.assert_not_called() + + +class VolumeTestCaseLocks(base.BaseVolumeTestCase): + MOCK_TOOZ = False + + def test_create_volume_from_volume_delete_lock_taken(self): + # create source volume + src_vol = tests_utils.create_volume(self.context, **self.volume_params) + src_vol_id = src_vol['id'] + + # no lock + self.volume.create_volume(self.context, src_vol) + + dst_vol = tests_utils.create_volume(self.context, + source_volid=src_vol_id, + **self.volume_params) + + orig_elevated = self.context.elevated + + gthreads = [] + + def mock_elevated(*args, **kwargs): + # unset mock so it is only called once + self.mock_object(self.context, 'elevated', orig_elevated) + + # we expect this to block and then fail + t = eventlet.spawn(self.volume.create_volume, + self.context, + volume=dst_vol, + request_spec={'source_volid': src_vol_id}) + gthreads.append(t) + + return orig_elevated(*args, **kwargs) + + # mock something from early on in the delete operation and within the + # lock so that when we do the create we expect it to block. + self.mock_object(self.context, 'elevated', mock_elevated) + + # locked + self.volume.delete_volume(self.context, src_vol) + + # we expect the volume create to fail with the following err since the + # source volume was deleted while the create was locked. Note that the + # volume is still in the db since it was created by the test prior to + # calling manager.create_volume. + with mock.patch('sys.stderr', new=six.StringIO()): + self.assertRaises(exception.VolumeNotFound, gthreads[0].wait) + + def test_create_volume_from_snapshot_delete_lock_taken(self): + # create source volume + src_vol = tests_utils.create_volume(self.context, **self.volume_params) + + # no lock + self.volume.create_volume(self.context, src_vol) + + # create snapshot + snap_id = create_snapshot(src_vol.id, + size=src_vol['size'])['id'] + snapshot_obj = objects.Snapshot.get_by_id(self.context, snap_id) + # no lock + self.volume.create_snapshot(self.context, snapshot_obj) + + # create vol from snapshot... + dst_vol = tests_utils.create_volume(self.context, + snapshot_id=snap_id, + source_volid=src_vol.id, + **self.volume_params) + + orig_elevated = self.context.elevated + + gthreads = [] + + def mock_elevated(*args, **kwargs): + # unset mock so it is only called once + self.mock_object(self.context, 'elevated', orig_elevated) + + # We expect this to block and then fail + t = eventlet.spawn(self.volume.create_volume, self.context, + volume=dst_vol, + request_spec={'snapshot_id': snap_id}) + gthreads.append(t) + + return orig_elevated(*args, **kwargs) + + # mock something from early on in the delete operation and within the + # lock so that when we do the create we expect it to block. + self.mock_object(self.context, 'elevated', mock_elevated) + + # locked + self.volume.delete_snapshot(self.context, snapshot_obj) + + # we expect the volume create to fail with the following err since the + # snapshot was deleted while the create was locked. Note that the + # volume is still in the db since it was created by the test prior to + # calling manager.create_volume. + with mock.patch('sys.stderr', new=six.StringIO()): + self.assertRaises(exception.SnapshotNotFound, gthreads[0].wait) + # locked + self.volume.delete_volume(self.context, src_vol) + # make sure it is gone + self.assertRaises(exception.VolumeNotFound, db.volume_get, + self.context, src_vol.id)