Skip to content

Commit 81da9cc

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Further robustification of format_inspector"
2 parents ce8251f + 943124e commit 81da9cc

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

glance/location.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,23 @@ def set_data(self, data, size=None, backend=None, set_active=True):
584584

585585
self._upload_to_store(data, verifier, backend, size)
586586

587-
if fmt and fmt.format_match and fmt.virtual_size:
588-
self.image.virtual_size = fmt.virtual_size
589-
LOG.info('Image format matched and virtual size computed: %i',
590-
self.image.virtual_size)
587+
virtual_size = 0
588+
if fmt and fmt.format_match:
589+
try:
590+
virtual_size = fmt.virtual_size
591+
LOG.info('Image format matched and virtual size computed: %i',
592+
virtual_size)
593+
except Exception as e:
594+
LOG.error(_LE('Unable to determine virtual_size because: %s'),
595+
e)
591596
elif fmt:
592597
LOG.warning('Image format %s did not match; '
593598
'unable to calculate virtual size',
594599
self.image.disk_format)
595600

601+
if virtual_size:
602+
self.image.virtual_size = fmt.virtual_size
603+
596604
if set_active and self.image.status != 'active':
597605
self.image.status = 'active'
598606

glance/tests/unit/test_store_image.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,37 @@ def test_image_set_data_inspector_no_match(self):
283283
self.assertEqual('active', image.status)
284284
self.assertEqual(0, image.virtual_size)
285285

286+
@mock.patch('glance.common.format_inspector.QcowInspector.virtual_size',
287+
new_callable=mock.PropertyMock)
288+
@mock.patch('glance.common.format_inspector.QcowInspector.format_match',
289+
new_callable=mock.PropertyMock)
290+
def test_image_set_data_inspector_virtual_size_failure(self, mock_fm,
291+
mock_vs):
292+
# Force our format to match
293+
mock_fm.return_value = True
294+
295+
# Make virtual_size fail in some unexpected way
296+
mock_vs.side_effect = ValueError('some error')
297+
298+
context = glance.context.RequestContext(user=USER1)
299+
image_stub = ImageStub(UUID2, status='queued', locations=[])
300+
image_stub.disk_format = 'qcow2'
301+
# We are going to pass an iterable data source, so use the
302+
# FakeStoreAPIReader that actually reads from that data
303+
store_api = unit_test_utils.FakeStoreAPIReader()
304+
image = glance.location.ImageProxy(image_stub, context,
305+
store_api, self.store_utils)
306+
307+
# Make sure set_data proceeds even though the format clearly
308+
# does not match
309+
image.set_data(iter(['YYYY']), 4)
310+
self.assertEqual(4, image.size)
311+
# NOTE(markwash): FakeStore returns image_id for location
312+
self.assertEqual(UUID2, image.locations[0]['url'])
313+
self.assertEqual('Z', image.checksum)
314+
self.assertEqual('active', image.status)
315+
self.assertEqual(0, image.virtual_size)
316+
286317
@mock.patch('glance.common.format_inspector.get_inspector')
287318
def test_image_set_data_inspector_not_needed(self, mock_gi):
288319
context = glance.context.RequestContext(user=USER1)

0 commit comments

Comments
 (0)