Set bootable when creating volume from snapshot

When creating a volume from a snapshot of a bootable volume the
bootable setting is ignored. This changes the creation logic so
when snapshot_id is not None, bootable can be set to be the same
as the snapshot's source_volume.

Change-Id: Ifca9ca04dfa8eca987dc44ec23cda5e0d431cba2
This commit is contained in:
caixiaoyu 2018-07-24 15:44:02 +08:00
parent 0681500902
commit b622a17e29
2 changed files with 55 additions and 1 deletions

View File

@ -166,6 +166,54 @@ class CreateVolumeFlowTestCase(test.TestCase):
consistencygroup_get_by_id.assert_called_once_with(self.ctxt, 5)
mock_extract_host.assert_called_once_with('host@backend#pool')
@mock.patch('cinder.db.volume_create')
@mock.patch('cinder.objects.Volume.get_by_id')
@mock.patch('cinder.objects.Snapshot.get_by_id')
def test_create_volume_from_snapshot(self, snapshot_get_by_id,
volume_get_by_id,
volume_create):
volume_db = {'bootable': True}
volume_obj = fake_volume.fake_volume_obj(self.ctxt, **volume_db)
snapshot_obj = fake_snapshot.fake_snapshot_obj(self.ctxt)
snapshot_get_by_id.return_value = snapshot_obj
volume_get_by_id.return_value = volume_obj
volume_create.return_value = {'id': '123456'}
task = create_volume.EntryCreateTask()
result = task.execute(self.ctxt,
optional_args=None,
source_volid=None,
snapshot_id=snapshot_obj.id,
availability_zones=['nova'],
size=1,
encryption_key_id=None,
description='123',
name='123',
multiattach=None)
self.assertTrue(result['volume_properties']['bootable'])
volume_db = {'bootable': False}
volume_obj = fake_volume.fake_volume_obj(self.ctxt, **volume_db)
snapshot_obj = fake_snapshot.fake_snapshot_obj(self.ctxt)
snapshot_get_by_id.return_value = snapshot_obj
volume_get_by_id.return_value = volume_obj
task = create_volume.EntryCreateTask()
result = task.execute(self.ctxt,
optional_args=None,
source_volid=None,
snapshot_id=snapshot_obj.id,
availability_zones=['nova'],
size=1,
encryption_key_id=None,
description='123',
name='123',
multiattach=None)
self.assertFalse(result['volume_properties']['bootable'])
@ddt.data(('enabled', {'replication_enabled': '<is> True'}),
('disabled', {'replication_enabled': '<is> False'}),
('disabled', {}))

View File

@ -537,7 +537,13 @@ class EntryCreateTask(flow_utils.CinderTask):
bootable = False
if src_vol is not None:
bootable = src_vol.bootable
elif kwargs.get('snapshot_id'):
snapshot = objects.Snapshot.get_by_id(context,
kwargs.get('snapshot_id'))
volume_id = snapshot.volume_id
snp_vol = objects.Volume.get_by_id(context, volume_id)
if snp_vol is not None:
bootable = snp_vol.bootable
availability_zones = kwargs.pop('availability_zones')
volume_properties = {
'size': kwargs.pop('size'),