bdm: Make sure that delete_on_termination is a boolean

Make sure that 'delete_on_termination' is always a boolean, even if it
was not passed in and thus got defaulted to None up until now.

This patch is really more of a pedantic fix, as we use objects for BDMs
everywhere outside of the API service, and object fields are of the
correct type.

Related-bug: #1370177

Change-Id: I2724bcbe159490f3bdd85f833412f3b20c4e1e23
This commit is contained in:
Nikola Dipanov 2015-05-29 20:59:10 +01:00
parent 65d6eb01b9
commit aa2f5dcc5f
3 changed files with 15 additions and 1 deletions

View File

@ -86,6 +86,8 @@ class BlockDeviceDict(dict):
self._validate(bdm_dict)
if bdm_dict.get('device_name'):
bdm_dict['device_name'] = prepend_dev(bdm_dict['device_name'])
bdm_dict['delete_on_termination'] = bool(
bdm_dict.get('delete_on_termination'))
# NOTE (ndipanov): Never default db fields
self.update({field: None for field in self._fields - do_not_default})
self.update(list(six.iteritems(bdm_dict)))

View File

@ -1989,7 +1989,7 @@ class _ComputeAPIUnitTestMixIn(object):
'image_id': None, 'volume_id': None, 'disk_bus': None,
'volume_size': None, 'source_type': 'snapshot',
'device_type': None, 'snapshot_id': '1-snapshot',
'destination_type': 'volume', 'delete_on_termination': None})
'destination_type': 'volume', 'delete_on_termination': False})
# All the db_only fields and the volume ones are removed
self.compute_api.snapshot_volume_backed(

View File

@ -427,6 +427,18 @@ class TestBlockDeviceDict(test.NoDBTestCase):
bdm_dict = block_device.BlockDeviceDict(bdm)
self.assertIsNone(bdm_dict['device_name'])
def test_init_boolify_delete_on_termination(self):
# Make sure that when delete_on_termination is not passed it's
# still set to False and not None
bdm = {'id': 3, 'instance_uuid': 'fake-instance',
'device_name': 'vda',
'source_type': 'volume',
'destination_type': 'volume',
'volume_id': 'fake-volume-id-1',
'boot_index': 0}
bdm_dict = block_device.BlockDeviceDict(bdm)
self.assertEqual(False, bdm_dict['delete_on_termination'])
def test_validate(self):
self.assertRaises(exception.InvalidBDMFormat,
block_device.BlockDeviceDict,