Followup: Add try/finally block
This is a followup of 55cefedd16
where we fix the file pointer issue with glance cinder powerflex
driver.
It was suggested to use try/finally block to restore file pointer
if a call to the _get_device_size method is made which is done
in this patch.
Change-Id: I0da7815e8b928dbfc7b1d03c2558709214592c9d
This commit is contained in:
parent
d42e8cf8dd
commit
71a3650358
@ -38,14 +38,16 @@ class ScaleIOBrickConnector(base.BaseBrickConnectorInterface):
|
||||
# so we use the seek/tell logic.
|
||||
# Get the current position
|
||||
current_pos = device_file.tell()
|
||||
# Seek to the end of the file
|
||||
device_file.seek(0, os.SEEK_END)
|
||||
# Get the size of file (in bytes)
|
||||
device_size = device_file.tell()
|
||||
# Convert the bytes size into GB
|
||||
device_size = int(math.ceil(float(device_size) / units.Gi))
|
||||
# Restore the file pointer to original position
|
||||
device_file.seek(current_pos, os.SEEK_SET)
|
||||
try:
|
||||
# Seek to the end of the file
|
||||
device_file.seek(0, os.SEEK_END)
|
||||
# Get the size of file (in bytes)
|
||||
device_size = device_file.tell()
|
||||
# Convert the bytes size into GB
|
||||
device_size = int(math.ceil(float(device_size) / units.Gi))
|
||||
finally:
|
||||
# Restore the file pointer to original position
|
||||
device_file.seek(current_pos, os.SEEK_SET)
|
||||
return device_size
|
||||
|
||||
@staticmethod
|
||||
|
@ -46,6 +46,7 @@ class TestScaleioBrickConnector(
|
||||
'cacheable': False,
|
||||
'driver_volume_type': 'scaleio',
|
||||
'attachment_id': '22914c3a-5818-4840-9188-2ac9833b9f7b'}
|
||||
self.scaleio_connector = scaleio.ScaleIOBrickConnector
|
||||
super().setUp(connection_info=connection_info)
|
||||
|
||||
def test__get_device_size(self):
|
||||
@ -54,21 +55,34 @@ class TestScaleioBrickConnector(
|
||||
fake_file = io.BytesIO(fake_data)
|
||||
# Get current file pointer
|
||||
original_pos = fake_file.tell()
|
||||
dev_size = scaleio.ScaleIOBrickConnector._get_device_size(fake_file)
|
||||
dev_size = self.scaleio_connector._get_device_size(fake_file)
|
||||
self.assertEqual(fake_len, dev_size)
|
||||
# Verify that file pointer points to the original location
|
||||
self.assertEqual(original_pos, fake_file.tell())
|
||||
|
||||
def test__get_device_size_exception(self):
|
||||
fake_data = b"fake binary data"
|
||||
fake_file = io.BytesIO(fake_data)
|
||||
# Get current file pointer
|
||||
original_pos = fake_file.tell()
|
||||
with mock.patch.object(
|
||||
math, 'ceil', side_effect=exceptions.BackendException):
|
||||
self.assertRaises(
|
||||
exceptions.BackendException,
|
||||
self.scaleio_connector._get_device_size, fake_file)
|
||||
# Verify that file pointer points to the original location
|
||||
self.assertEqual(original_pos, fake_file.tell())
|
||||
|
||||
@mock.patch.object(time, 'sleep')
|
||||
def test__wait_resize_device_resized(self, mock_sleep):
|
||||
fake_vol = mock.MagicMock()
|
||||
fake_vol.size = 2
|
||||
fake_file = io.BytesIO(b"fake binary data")
|
||||
with mock.patch.object(
|
||||
scaleio.ScaleIOBrickConnector,
|
||||
self.scaleio_connector,
|
||||
'_get_device_size') as mock_get_dev_size:
|
||||
mock_get_dev_size.side_effect = [1, 2]
|
||||
scaleio.ScaleIOBrickConnector._wait_resize_device(
|
||||
self.scaleio_connector._wait_resize_device(
|
||||
fake_vol, fake_file)
|
||||
|
||||
@mock.patch.object(time, 'sleep')
|
||||
@ -77,11 +91,11 @@ class TestScaleioBrickConnector(
|
||||
fake_vol.size = 2
|
||||
fake_file = io.BytesIO(b"fake binary data")
|
||||
with mock.patch.object(
|
||||
scaleio.ScaleIOBrickConnector, '_get_device_size',
|
||||
self.scaleio_connector, '_get_device_size',
|
||||
return_value=1):
|
||||
self.assertRaises(
|
||||
exceptions.BackendException,
|
||||
scaleio.ScaleIOBrickConnector._wait_resize_device,
|
||||
self.scaleio_connector._wait_resize_device,
|
||||
fake_vol, fake_file)
|
||||
|
||||
def test_yield_path(self):
|
||||
|
Loading…
Reference in New Issue
Block a user