Add exception coverage for get, get_size, delete

This patch adds coverage for the various exceptions handled and
raised in get, get_size and delete methods.
It also corrects the behavior of _test_cinder_get_size method where
client.volumes returned dictionary instead of a MagicMock and it
worked due to the existing method "get" in dictionary which has
same name as "get" method in cinderclient. The dictionary object
is replaced with the appropriate MagicMock object in this patch.

Change-Id: If63a6c810b5aab992e54857bc81f5052c2c593c4
This commit is contained in:
whoami-rajat 2022-03-16 15:40:04 +05:30
parent e5a3c9eefa
commit 4bd0304efb
3 changed files with 71 additions and 28 deletions

View File

@ -437,11 +437,42 @@ class TestCinderStoreBase(object):
self.assertEqual(expected_num_chunks, num_chunks)
self.assertEqual(expected_file_contents, data)
def _test_cinder_volume_not_found(self, method_call, mock_method):
fake_volume_uuid = str(uuid.uuid4())
loc = mock.MagicMock(volume_id=fake_volume_uuid)
mock_not_found = {mock_method: mock.MagicMock(
side_effect=cinder.cinder_exception.NotFound(code=404))}
fake_volumes = mock.MagicMock(**mock_not_found)
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(volumes=fake_volumes)
self.assertRaises(exceptions.NotFound, method_call, loc,
context=self.context)
def test_cinder_get_volume_not_found(self):
self._test_cinder_volume_not_found(self.store.get, 'get')
def test_cinder_get_size_volume_not_found(self):
self._test_cinder_volume_not_found(self.store.get_size, 'get')
def test_cinder_delete_volume_not_found(self):
self._test_cinder_volume_not_found(self.store.delete, 'delete')
def test_cinder_get_client_exception(self):
fake_volume_uuid = str(uuid.uuid4())
loc = mock.MagicMock(volume_id=fake_volume_uuid)
with mock.patch.object(cinder.Store, 'get_cinderclient') as mock_cc:
mock_cc.side_effect = (
cinder.cinder_exception.ClientException(code=500))
self.assertRaises(exceptions.BackendException, self.store.get, loc,
context=self.context)
def _test_cinder_get_size(self, is_multi_store=False):
fake_client = mock.MagicMock(auth_token=None, management_url=None)
fake_volume_uuid = str(uuid.uuid4())
fake_volume = mock.MagicMock(size=5, metadata={})
fake_volumes = {fake_volume_uuid: fake_volume}
fake_volumes = mock.MagicMock(get=lambda fake_volume_uuid: fake_volume)
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(client=fake_client,
@ -471,6 +502,17 @@ class TestCinderStoreBase(object):
image_size = self.store.get_size(loc, context=self.context)
self.assertEqual(expected_image_size, image_size)
def test_cinder_get_size_generic_exception(self):
fake_volume_uuid = str(uuid.uuid4())
loc = mock.MagicMock(volume_id=fake_volume_uuid)
fake_volumes = mock.MagicMock(
get=mock.MagicMock(side_effect=Exception()))
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(volumes=fake_volumes)
image_size = self.store.get_size(loc, context=self.context)
self.assertEqual(0, image_size)
def _test_cinder_add(self, fake_volume, volume_file, size_kb=5,
verifier=None, backend='glance_store',
fail_resize=False, is_multi_store=False):
@ -528,6 +570,32 @@ class TestCinderStoreBase(object):
if is_multi_store:
self.assertEqual(backend, metadata["store"])
def _test_cinder_delete(self, is_multi_store=False):
fake_client = mock.MagicMock(auth_token=None, management_url=None)
fake_volume_uuid = str(uuid.uuid4())
fake_volumes = mock.MagicMock(delete=mock.Mock())
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(client=fake_client,
volumes=fake_volumes)
loc = self._get_uri_loc(fake_volume_uuid,
is_multi_store=is_multi_store)
self.store.delete(loc, context=self.context)
fake_volumes.delete.assert_called_once_with(fake_volume_uuid)
def test_cinder_delete_client_exception(self):
fake_volume_uuid = str(uuid.uuid4())
loc = mock.MagicMock(volume_id=fake_volume_uuid)
fake_volumes = mock.MagicMock(delete=mock.MagicMock(
side_effect=cinder.cinder_exception.ClientException(code=500)))
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(volumes=fake_volumes)
self.assertRaises(exceptions.BackendException, self.store.delete,
loc, context=self.context)
def test__get_device_size(self):
fake_data = b"fake binary data"
fake_len = int(math.ceil(float(len(fake_data)) / units.Gi))

View File

@ -23,7 +23,6 @@ import uuid
from oslo_utils import units
from glance_store import exceptions
from glance_store import location
from glance_store.tests import base
from glance_store.tests.unit import test_cinder_base
from glance_store.tests.unit import test_store_capabilities
@ -147,18 +146,7 @@ class TestCinderStore(base.StoreBaseTest,
fake_volume.delete.assert_called_once()
def test_cinder_delete(self):
fake_client = mock.MagicMock(auth_token=None, management_url=None)
fake_volume_uuid = str(uuid.uuid4())
fake_volumes = mock.MagicMock(delete=mock.Mock())
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(client=fake_client,
volumes=fake_volumes)
uri = 'cinder://%s' % fake_volume_uuid
loc = location.get_location_from_uri(uri, conf=self.conf)
self.store.delete(loc, context=self.context)
fake_volumes.delete.assert_called_once_with(fake_volume_uuid)
self._test_cinder_delete()
def test_set_url_prefix(self):
self.assertEqual('cinder://', self.store._url_prefix)

View File

@ -284,20 +284,7 @@ class TestMultiCinderStore(base.MultiStoreBaseTest,
fake_volume.delete.assert_called_once()
def test_cinder_delete(self):
fake_client = mock.MagicMock(auth_token=None, management_url=None)
fake_volume_uuid = str(uuid.uuid4())
fake_volumes = mock.MagicMock(delete=mock.Mock())
with mock.patch.object(cinder.Store, 'get_cinderclient') as mocked_cc:
mocked_cc.return_value = mock.MagicMock(client=fake_client,
volumes=fake_volumes)
uri = 'cinder://cinder1/%s' % fake_volume_uuid
loc = location.get_location_from_uri_and_backend(uri,
"cinder1",
conf=self.conf)
self.store.delete(loc, context=self.context)
fake_volumes.delete.assert_called_once_with(fake_volume_uuid)
self._test_cinder_delete(is_multi_store=True)
def test_set_url_prefix(self):
self.assertEqual('cinder://cinder1', self.store._url_prefix)