From aa2f5dcc5f2a4666601905af6b6318da05a8c34e Mon Sep 17 00:00:00 2001 From: Nikola Dipanov Date: Fri, 29 May 2015 20:59:10 +0100 Subject: [PATCH] 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 --- nova/block_device.py | 2 ++ nova/tests/unit/compute/test_compute_api.py | 2 +- nova/tests/unit/test_block_device.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/nova/block_device.py b/nova/block_device.py index a917e7ef304d..08edaa828df1 100644 --- a/nova/block_device.py +++ b/nova/block_device.py @@ -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))) diff --git a/nova/tests/unit/compute/test_compute_api.py b/nova/tests/unit/compute/test_compute_api.py index c6f6c1fb3781..5d13edd7e04d 100644 --- a/nova/tests/unit/compute/test_compute_api.py +++ b/nova/tests/unit/compute/test_compute_api.py @@ -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( diff --git a/nova/tests/unit/test_block_device.py b/nova/tests/unit/test_block_device.py index 7d1bee0bde81..39f9b0dc469f 100644 --- a/nova/tests/unit/test_block_device.py +++ b/nova/tests/unit/test_block_device.py @@ -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,