Merge "Adapt the code to the new get_by_volume BDM functions"
This commit is contained in:
commit
cc22d46e2f
@ -3259,7 +3259,7 @@ class API(base.Base):
|
||||
|
||||
@wrap_check_policy
|
||||
def volume_snapshot_create(self, context, volume_id, create_info):
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume(
|
||||
context, volume_id, expected_attrs=['instance'])
|
||||
self.compute_rpcapi.volume_snapshot_create(context, bdm.instance,
|
||||
volume_id, create_info)
|
||||
@ -3274,7 +3274,7 @@ class API(base.Base):
|
||||
@wrap_check_policy
|
||||
def volume_snapshot_delete(self, context, volume_id, snapshot_id,
|
||||
delete_info):
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume(
|
||||
context, volume_id, expected_attrs=['instance'])
|
||||
self.compute_rpcapi.volume_snapshot_delete(context, bdm.instance,
|
||||
volume_id, snapshot_id, delete_info)
|
||||
|
@ -4740,8 +4740,8 @@ class ComputeManager(manager.Manager):
|
||||
|
||||
"""
|
||||
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
context, volume_id)
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
context, volume_id, instance.uuid)
|
||||
if CONF.volume_usage_poll_interval > 0:
|
||||
vol_stats = []
|
||||
mp = bdm.device_name
|
||||
@ -4869,8 +4869,8 @@ class ComputeManager(manager.Manager):
|
||||
"""Swap volume for an instance."""
|
||||
context = context.elevated()
|
||||
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
context, old_volume_id, instance_uuid=instance.uuid)
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
context, old_volume_id, instance.uuid)
|
||||
connector = self.driver.get_volume_connector(instance)
|
||||
|
||||
resize_to = 0
|
||||
@ -4919,8 +4919,8 @@ class ComputeManager(manager.Manager):
|
||||
# connection from this host.
|
||||
|
||||
try:
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
context, volume_id)
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
context, volume_id, instance.uuid)
|
||||
self._driver_detach_volume(context, instance, bdm)
|
||||
connector = self.driver.get_volume_connector(instance)
|
||||
self.volume_api.terminate_connection(context, volume_id, connector)
|
||||
|
@ -812,8 +812,8 @@ class ComputeAPI(object):
|
||||
version=version)
|
||||
volume_bdm = cctxt.call(ctxt, 'reserve_block_device_name', **kw)
|
||||
if not isinstance(volume_bdm, objects.BlockDeviceMapping):
|
||||
volume_bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
ctxt, volume_id)
|
||||
volume_bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
ctxt, volume_id, instance.uuid)
|
||||
return volume_bdm
|
||||
|
||||
def backup_instance(self, ctxt, instance, image_id, backup_type,
|
||||
|
@ -262,7 +262,7 @@ class VolumeAttachmentsSample(test_servers.ServersSampleBase):
|
||||
'nova.compute.manager.ComputeManager.attach_volume',
|
||||
lambda *a, **k: None)
|
||||
self.stub_out(
|
||||
'nova.objects.BlockDeviceMapping.get_by_volume_id',
|
||||
'nova.objects.BlockDeviceMapping.get_by_volume_and_instance',
|
||||
classmethod(lambda *a, **k: None))
|
||||
|
||||
volume = fakes.stub_volume_get(None, context.get_admin_context(),
|
||||
|
@ -429,7 +429,7 @@ class ComputeVolumeTestCase(BaseTestCase):
|
||||
mock.patch.object(self.compute, '_driver_detach_volume'),
|
||||
mock.patch.object(self.compute.volume_api, 'detach'),
|
||||
mock.patch.object(objects.BlockDeviceMapping,
|
||||
'get_by_volume_id'),
|
||||
'get_by_volume_and_instance'),
|
||||
mock.patch.object(fake_bdm, 'destroy')
|
||||
) as (mock_internal_detach, mock_detach, mock_get, mock_destroy):
|
||||
mock_detach.side_effect = test.TestingException
|
||||
@ -744,15 +744,15 @@ class ComputeVolumeTestCase(BaseTestCase):
|
||||
'volume_id': uuids.volume_id}
|
||||
|
||||
self.mox.StubOutWithMock(objects.BlockDeviceMapping,
|
||||
'get_by_volume_id')
|
||||
'get_by_volume_and_instance')
|
||||
self.mox.StubOutWithMock(self.compute.driver, 'block_stats')
|
||||
self.mox.StubOutWithMock(self.compute, '_get_host_volume_bdms')
|
||||
self.mox.StubOutWithMock(self.compute.driver, 'get_all_volume_usage')
|
||||
|
||||
# The following methods will be called
|
||||
objects.BlockDeviceMapping.get_by_volume_id(self.context,
|
||||
uuids.volume_id).AndReturn(
|
||||
bdm.obj_clone())
|
||||
objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
self.context, uuids.volume_id, instance.uuid).AndReturn(
|
||||
bdm.obj_clone())
|
||||
self.compute.driver.block_stats(instance, 'vdb').\
|
||||
AndReturn([1, 30, 1, 20, None])
|
||||
self.compute._get_host_volume_bdms(self.context,
|
||||
@ -9762,10 +9762,11 @@ class ComputeAPITestCase(BaseTestCase):
|
||||
fake_libvirt_driver_detach_volume_fails)
|
||||
|
||||
self.mox.StubOutWithMock(objects.BlockDeviceMapping,
|
||||
'get_by_volume_id')
|
||||
objects.BlockDeviceMapping.get_by_volume_id(
|
||||
self.context, 1).AndReturn(objects.BlockDeviceMapping(
|
||||
context=self.context, **fake_bdm))
|
||||
'get_by_volume_and_instance')
|
||||
objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
self.context, 1, instance.uuid).\
|
||||
AndReturn(objects.BlockDeviceMapping(
|
||||
context=self.context, **fake_bdm))
|
||||
self.mox.ReplayAll()
|
||||
|
||||
self.assertRaises(AttributeError, self.compute.detach_volume,
|
||||
@ -9788,7 +9789,7 @@ class ComputeAPITestCase(BaseTestCase):
|
||||
mock.patch.object(self.compute.driver, 'detach_volume',
|
||||
side_effect=exception.DiskNotFound('sdb')),
|
||||
mock.patch.object(objects.BlockDeviceMapping,
|
||||
'get_by_volume_id', return_value=bdm),
|
||||
'get_by_volume_and_instance', return_value=bdm),
|
||||
mock.patch.object(cinder.API, 'terminate_connection'),
|
||||
mock.patch.object(bdm, 'destroy'),
|
||||
mock.patch.object(self.compute, '_notify_about_instance_usage'),
|
||||
|
@ -2225,11 +2225,11 @@ class _ComputeAPIUnitTestMixIn(object):
|
||||
fake_bdm, expected_attrs=['instance'])
|
||||
|
||||
self.mox.StubOutWithMock(objects.BlockDeviceMapping,
|
||||
'get_by_volume_id')
|
||||
'get_by_volume')
|
||||
self.mox.StubOutWithMock(self.compute_api.compute_rpcapi,
|
||||
'volume_snapshot_create')
|
||||
|
||||
objects.BlockDeviceMapping.get_by_volume_id(
|
||||
objects.BlockDeviceMapping.get_by_volume(
|
||||
self.context, volume_id,
|
||||
expected_attrs=['instance']).AndReturn(fake_bdm)
|
||||
self.compute_api.compute_rpcapi.volume_snapshot_create(self.context,
|
||||
@ -2266,11 +2266,11 @@ class _ComputeAPIUnitTestMixIn(object):
|
||||
fake_bdm, expected_attrs=['instance'])
|
||||
|
||||
self.mox.StubOutWithMock(objects.BlockDeviceMapping,
|
||||
'get_by_volume_id')
|
||||
'get_by_volume')
|
||||
self.mox.StubOutWithMock(self.compute_api.compute_rpcapi,
|
||||
'volume_snapshot_delete')
|
||||
|
||||
objects.BlockDeviceMapping.get_by_volume_id(
|
||||
objects.BlockDeviceMapping.get_by_volume(
|
||||
self.context, volume_id,
|
||||
expected_attrs=['instance']).AndReturn(fake_bdm)
|
||||
self.compute_api.compute_rpcapi.volume_snapshot_delete(self.context,
|
||||
|
@ -1722,8 +1722,9 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
fake_vol_unreserve)
|
||||
self.stubs.Set(self.compute.volume_api, 'terminate_connection',
|
||||
fake_vol_api_func)
|
||||
self.stubs.Set(db, 'block_device_mapping_get_all_by_volume_id',
|
||||
lambda x, y, z: [fake_bdm])
|
||||
self.stubs.Set(db,
|
||||
'block_device_mapping_get_by_instance_and_volume_id',
|
||||
lambda x, y, z, v: fake_bdm)
|
||||
self.stubs.Set(self.compute.driver, 'get_volume_connector',
|
||||
lambda x: {})
|
||||
self.stubs.Set(self.compute.driver, 'swap_volume',
|
||||
@ -2173,18 +2174,20 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
self.assertFalse(allow_reboot)
|
||||
self.assertEqual(reboot_type, 'HARD')
|
||||
|
||||
@mock.patch('nova.objects.BlockDeviceMapping.get_by_volume_id')
|
||||
@mock.patch('nova.objects.BlockDeviceMapping.get_by_volume_and_instance')
|
||||
@mock.patch('nova.compute.manager.ComputeManager._driver_detach_volume')
|
||||
@mock.patch('nova.objects.Instance._from_db_object')
|
||||
def test_remove_volume_connection(self, inst_from_db, detach, bdm_get):
|
||||
bdm = mock.sentinel.bdm
|
||||
inst_obj = mock.sentinel.inst_obj
|
||||
inst_obj = mock.Mock()
|
||||
inst_obj.uuid = 'uuid'
|
||||
bdm_get.return_value = bdm
|
||||
inst_from_db.return_value = inst_obj
|
||||
with mock.patch.object(self.compute, 'volume_api'):
|
||||
self.compute.remove_volume_connection(self.context, 'vol',
|
||||
inst_obj)
|
||||
detach.assert_called_once_with(self.context, inst_obj, bdm)
|
||||
bdm_get.assert_called_once_with(self.context, 'vol', 'uuid')
|
||||
|
||||
def test_detach_volume(self):
|
||||
self._test_detach_volume()
|
||||
@ -2192,14 +2195,15 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
def test_detach_volume_not_destroy_bdm(self):
|
||||
self._test_detach_volume(destroy_bdm=False)
|
||||
|
||||
@mock.patch('nova.objects.BlockDeviceMapping.get_by_volume_id')
|
||||
@mock.patch('nova.objects.BlockDeviceMapping.get_by_volume_and_instance')
|
||||
@mock.patch('nova.compute.manager.ComputeManager._driver_detach_volume')
|
||||
@mock.patch('nova.compute.manager.ComputeManager.'
|
||||
'_notify_about_instance_usage')
|
||||
def _test_detach_volume(self, notify_inst_usage, detach,
|
||||
bdm_get, destroy_bdm=True):
|
||||
volume_id = '123'
|
||||
inst_obj = mock.sentinel.inst_obj
|
||||
inst_obj = mock.Mock()
|
||||
inst_obj.uuid = 'uuid'
|
||||
|
||||
bdm = mock.MagicMock(spec=objects.BlockDeviceMapping)
|
||||
bdm.device_name = 'vdb'
|
||||
|
@ -12472,14 +12472,15 @@ class LibvirtConnTestCase(test.NoDBTestCase):
|
||||
@mock.patch('nova.virt.libvirt.driver.LibvirtDriver._swap_volume')
|
||||
@mock.patch('nova.virt.block_device.DriverVolumeBlockDevice.save')
|
||||
@mock.patch('nova.objects.block_device.BlockDeviceMapping.'
|
||||
'get_by_volume_id')
|
||||
'get_by_volume_and_instance')
|
||||
@mock.patch('nova.virt.libvirt.driver.LibvirtDriver._get_volume_config')
|
||||
@mock.patch('nova.virt.libvirt.driver.LibvirtDriver._connect_volume')
|
||||
@mock.patch('nova.virt.libvirt.host.Host.get_guest')
|
||||
def test_swap_volume_driver_bdm_save(self, get_guest,
|
||||
connect_volume, get_volume_config,
|
||||
get_by_volume_id, volume_save,
|
||||
swap_volume, disconnect_volume):
|
||||
get_by_volume_and_instance,
|
||||
volume_save, swap_volume,
|
||||
disconnect_volume):
|
||||
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI())
|
||||
instance = objects.Instance(**self.test_instance)
|
||||
old_connection_info = {'driver_volume_type': 'fake',
|
||||
@ -12516,7 +12517,7 @@ class LibvirtConnTestCase(test.NoDBTestCase):
|
||||
'destination_type': 'volume',
|
||||
'volume_id': 'fake-volume-id-2',
|
||||
'boot_index': 0}))
|
||||
get_by_volume_id.return_value = bdm
|
||||
get_by_volume_and_instance.return_value = bdm
|
||||
|
||||
conn.swap_volume(old_connection_info, new_connection_info, instance,
|
||||
'/dev/vdb', 1)
|
||||
@ -14872,9 +14873,11 @@ class LibvirtVolumeSnapshotTestCase(test.NoDBTestCase):
|
||||
@mock.patch('nova.virt.block_device.DriverVolumeBlockDevice.'
|
||||
'refresh_connection_info')
|
||||
@mock.patch('nova.objects.block_device.BlockDeviceMapping.'
|
||||
'get_by_volume_id')
|
||||
def test_volume_refresh_connection_info(self, mock_get_by_volume_id,
|
||||
'get_by_volume_and_instance')
|
||||
def test_volume_refresh_connection_info(self,
|
||||
mock_get_by_volume_and_instance,
|
||||
mock_refresh_connection_info):
|
||||
instance = objects.Instance(**self.inst)
|
||||
fake_bdm = fake_block_device.FakeDbBlockDeviceDict({
|
||||
'id': 123,
|
||||
'instance_uuid': 'fake-instance',
|
||||
@ -14884,13 +14887,14 @@ class LibvirtVolumeSnapshotTestCase(test.NoDBTestCase):
|
||||
'volume_id': 'fake-volume-id-1',
|
||||
'connection_info': '{"fake": "connection_info"}'})
|
||||
fake_bdm = objects.BlockDeviceMapping(self.c, **fake_bdm)
|
||||
mock_get_by_volume_id.return_value = fake_bdm
|
||||
mock_get_by_volume_and_instance.return_value = fake_bdm
|
||||
|
||||
self.drvr._volume_refresh_connection_info(self.c, self.inst,
|
||||
self.drvr._volume_refresh_connection_info(self.c, instance,
|
||||
self.volume_uuid)
|
||||
|
||||
mock_get_by_volume_id.assert_called_once_with(self.c, self.volume_uuid)
|
||||
mock_refresh_connection_info.assert_called_once_with(self.c, self.inst,
|
||||
mock_get_by_volume_and_instance.assert_called_once_with(
|
||||
self.c, self.volume_uuid, instance.uuid)
|
||||
mock_refresh_connection_info.assert_called_once_with(self.c, instance,
|
||||
self.drvr._volume_api, self.drvr)
|
||||
|
||||
def test_volume_snapshot_create(self, quiesce=True):
|
||||
|
@ -1239,8 +1239,8 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
|
||||
# Save updates made in connection_info when connect_volume was called
|
||||
volume_id = new_connection_info.get('serial')
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(
|
||||
nova_context.get_admin_context(), volume_id)
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
nova_context.get_admin_context(), volume_id, instance.uuid)
|
||||
driver_bdm = driver_block_device.DriverVolumeBlockDevice(bdm)
|
||||
driver_bdm['connection_info'] = new_connection_info
|
||||
driver_bdm.save()
|
||||
@ -1793,8 +1793,8 @@ class LibvirtDriver(driver.ComputeDriver):
|
||||
raise
|
||||
|
||||
def _volume_refresh_connection_info(self, context, instance, volume_id):
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_id(context,
|
||||
volume_id)
|
||||
bdm = objects.BlockDeviceMapping.get_by_volume_and_instance(
|
||||
context, volume_id, instance.uuid)
|
||||
|
||||
driver_bdm = driver_block_device.convert_volume(bdm)
|
||||
if driver_bdm:
|
||||
|
Loading…
x
Reference in New Issue
Block a user