def fake_bdm_get(ctxt, uuid, use_slave=False): return [fake_block_device.FakeDbBlockDeviceDict( {'volume_id': 87654321, 'snapshot_id': None, 'no_device': None, 'source_type': 'volume', 'destination_type': 'volume', 'delete_on_termination': True, 'device_name': '/dev/sdh'}), fake_block_device.FakeDbBlockDeviceDict( {'volume_id': None, 'snapshot_id': None, 'no_device': None, 'source_type': 'blank', 'destination_type': 'local', 'guest_format': 'swap', 'delete_on_termination': None, 'device_name': '/dev/sdc'}), fake_block_device.FakeDbBlockDeviceDict( {'volume_id': None, 'snapshot_id': None, 'no_device': None, 'source_type': 'blank', 'destination_type': 'local', 'guest_format': None, 'delete_on_termination': None, 'device_name': '/dev/sdb'})]
def stub_bdm_get_all_by_instance(context, instance_uuid, use_slave=False): return [fake_block_device.FakeDbBlockDeviceDict( {'id': 1, 'source_type': 'volume', 'destination_type': 'volume', 'volume_id': 'volume_id1', 'instance_uuid': instance_uuid}), fake_block_device.FakeDbBlockDeviceDict( {'id': 2, 'source_type': 'volume', 'destination_type': 'volume', 'volume_id': 'volume_id2', 'instance_uuid': instance_uuid})]
def fake_bdms_get_all_by_instance(context, instance_uuid, use_slave=False): return [ fake_block_device.FakeDbBlockDeviceDict({ 'id': 1, 'instance_uuid': instance_uuid, 'device_name': '/dev/fake0', 'delete_on_termination': 'False', 'source_type': 'volume', 'destination_type': 'volume', 'snapshot_id': None, 'volume_id': FAKE_UUID_A, 'volume_size': 1 }), fake_block_device.FakeDbBlockDeviceDict({ 'id': 2, 'instance_uuid': instance_uuid, 'device_name': '/dev/fake1', 'delete_on_termination': 'False', 'source_type': 'volume', 'destination_type': 'volume', 'snapshot_id': None, 'volume_id': FAKE_UUID_B, 'volume_size': 1 }) ]
def fake_bdms_get_all_by_instance(context, instance_uuid, use_slave=False): bdms = [ fake_block_device.FakeDbBlockDeviceDict({ 'id': 1, 'volume_id': 'a26887c6-c47b-4654-abb5-dfadf7d3f803', 'instance_uuid': server_id, 'source_type': 'volume', 'destination_type': 'volume', 'device_name': '/dev/sdd' }), fake_block_device.FakeDbBlockDeviceDict({ 'id': 2, 'volume_id': 'a26887c6-c47b-4654-abb5-dfadf7d3f804', 'instance_uuid': server_id, 'source_type': 'volume', 'destination_type': 'volume', 'device_name': '/dev/sdc' }) ] return bdms
def test_legacy_mapping_from_object_list(self): bdm1 = objects.BlockDeviceMapping() bdm1 = objects.BlockDeviceMapping._from_db_object( None, bdm1, fake_block_device.FakeDbBlockDeviceDict( self.new_mapping[0])) bdm2 = objects.BlockDeviceMapping() bdm2 = objects.BlockDeviceMapping._from_db_object( None, bdm2, fake_block_device.FakeDbBlockDeviceDict( self.new_mapping[1])) bdmlist = objects.BlockDeviceMappingList() bdmlist.objects = [bdm1, bdm2] block_device.legacy_mapping(bdmlist)
def test_root_volume_metadata(self): fake_volume = { 'volume_image_metadata': { 'vol_test_key': 'vol_test_value' } } class FakeVolumeApi(object): def get(*args, **kwargs): return fake_volume block_device_mapping = block_device_obj.block_device_make_list( None, [ fake_block_device.FakeDbBlockDeviceDict( { 'id': 1, 'boot_index': 0, 'source_type': 'volume', 'destination_type': 'volume', 'volume_id': 'fake_volume_id', 'delete_on_termination': False }) ]) volume_meta = block_device_mapping.root_metadata( self.context, None, FakeVolumeApi()) self.assertEqual(fake_volume['volume_image_metadata'], volume_meta)
def test_snapshot_from_object(self): for bdm in self.new_mapping[:-1]: obj = objects.BlockDeviceMapping() obj = objects.BlockDeviceMapping._from_db_object( None, obj, fake_block_device.FakeDbBlockDeviceDict( bdm)) self._test_snapshot_from_bdm(obj)
def fake_bdms_get_all_by_instance(*args, **kwargs): return [ fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': UUID1, 'source_type': 'volume', 'destination_type': 'volume', 'id': 1, 'delete_on_termination': True }), fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': UUID2, 'source_type': 'volume', 'destination_type': 'volume', 'id': 2, 'delete_on_termination': False }) ]
def test_image_mapping(self): removed_fields = ['id', 'instance_uuid', 'connection_info', 'device_name', 'created_at', 'updated_at', 'deleted_at', 'deleted'] for bdm in self.new_mapping: mapping_bdm = fake_block_device.FakeDbBlockDeviceDict( bdm).get_image_mapping() for fld in removed_fields: self.assertNotIn(fld, mapping_bdm)
def _fake_bdm(device): return fake_block_device.FakeDbBlockDeviceDict({ 'source_type': 'volume', 'destination_type': 'volume', 'device_name': device, 'no_device': None, 'volume_id': 'fake', 'snapshot_id': None, 'guest_format': None })
def fake_bdm(self, bdm_id): fake_bdm = fake_block_device.FakeDbBlockDeviceDict({ 'id': bdm_id, 'instance_uuid': 'fake-instance', 'device_name': '/dev/sda2', 'source_type': 'snapshot', 'destination_type': 'volume', 'connection_info': "{'fake': 'connection_info'}", 'snapshot_id': 'fake-snapshot-id-1', 'boot_index': -1, }) return fake_bdm
def _test_create_mocked(self, cell_type=None, update_or_create=False): if cell_type: self.flags(enable=True, cell_type=cell_type, group='cells') else: self.flags(enable=False, group='cells') values = { 'source_type': 'volume', 'volume_id': 'fake-vol-id', 'destination_type': 'volume', 'instance_uuid': 'fake-instance' } fake_bdm = fake_block_device.FakeDbBlockDeviceDict(values) with contextlib.nested( mock.patch.object(db, 'block_device_mapping_create', return_value=fake_bdm), mock.patch.object(db, 'block_device_mapping_update_or_create', return_value=fake_bdm), mock.patch.object(cells_rpcapi.CellsAPI, 'bdm_update_or_create_at_top')) as ( bdm_create_mock, bdm_update_or_create_mock, cells_update_mock): bdm = objects.BlockDeviceMapping(context=self.context, **values) if update_or_create: method = bdm.update_or_create else: method = bdm.create if cell_type == 'api': self.assertRaises(exception.ObjectActionError, method) else: method() if update_or_create: bdm_update_or_create_mock.assert_called_once_with( self.context, values, legacy=False) else: bdm_create_mock.assert_called_once_with(self.context, values, legacy=False) if cell_type == 'compute': self.assertEqual(1, cells_update_mock.call_count) self.assertTrue(len(cells_update_mock.call_args[0]) > 1) self.assertEqual(cells_update_mock.call_args[0][0], self.context) self.assertIsInstance(cells_update_mock.call_args[0][1], block_device_obj.BlockDeviceMapping) self.assertEqual(cells_update_mock.call_args[1], {'create': update_or_create or None}) else: self.assertFalse(cells_update_mock.called)
def test_create_instances_here(self): # Just grab the first instance type inst_type = flavors.get_flavor(1) image = {'properties': {}} instance_uuids = self.instance_uuids instance_props = {'id': 'removed', 'security_groups': 'removed', 'info_cache': 'removed', 'name': 'instance-00000001', 'hostname': 'meow', 'display_name': 'moo', 'image_ref': 'fake_image_ref', 'user_id': self.ctxt.user_id, # Test these as lists 'metadata': {'moo': 'cow'}, 'system_metadata': {'meow': 'cat'}, 'flavor': inst_type, 'project_id': self.ctxt.project_id} call_info = {'uuids': []} block_device_mapping = [ objects.BlockDeviceMapping(context=self.ctxt, **fake_block_device.FakeDbBlockDeviceDict( block_device.create_image_bdm('fake_image_ref'), anon=True)) ] def _fake_instance_update_at_top(_ctxt, instance): call_info['uuids'].append(instance['uuid']) self.stubs.Set(self.msg_runner, 'instance_update_at_top', _fake_instance_update_at_top) self.scheduler._create_instances_here(self.ctxt, instance_uuids, instance_props, inst_type, image, ['default'], block_device_mapping) self.assertEqual(instance_uuids, call_info['uuids']) for count, instance_uuid in enumerate(instance_uuids): instance = db.instance_get_by_uuid(self.ctxt, instance_uuid) meta = utils.instance_meta(instance) self.assertEqual('cow', meta['moo']) sys_meta = utils.instance_sys_meta(instance) self.assertEqual('cat', sys_meta['meow']) self.assertEqual('meow', instance['hostname']) self.assertEqual('moo-%d' % (count + 1), instance['display_name']) self.assertEqual('fake_image_ref', instance['image_ref'])
def test_get_device_name(self): bdm_obj = objects.BlockDeviceMapping( self.context, **fake_block_device.FakeDbBlockDeviceDict({ 'id': 3, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vda', 'source_type': 'volume', 'destination_type': 'volume', 'volume_id': 'fake-volume-id-1', 'boot_index': 0 })) self.assertEqual('/dev/vda', blockinfo.get_device_name(bdm_obj)) driver_bdm = driver_block_device.DriverVolumeBlockDevice(bdm_obj) self.assertEqual('/dev/vda', blockinfo.get_device_name(driver_bdm)) bdm_obj.device_name = None self.assertIsNone(blockinfo.get_device_name(bdm_obj)) driver_bdm = driver_block_device.DriverVolumeBlockDevice(bdm_obj) self.assertIsNone(blockinfo.get_device_name(driver_bdm))
def test_attach_detach_different_power_states(self): instance_ref, network_info = self._get_running_instance() connection_info = { "driver_volume_type": "fake", "serial": "fake_serial", "data": {} } self.connection.power_off(instance_ref) self.connection.attach_volume(None, connection_info, instance_ref, '/dev/sda') bdm = { 'root_device_name': None, 'swap': None, 'ephemerals': [], 'block_device_mapping': driver_block_device.convert_volumes([ fake_block_device.FakeDbBlockDeviceDict( {'id': 1, 'instance_uuid': instance_ref['uuid'], 'device_name': '/dev/sda', 'source_type': 'volume', 'destination_type': 'volume', 'delete_on_termination': False, 'snapshot_id': None, 'volume_id': 'abcdedf', 'volume_size': None, 'no_device': None }), ]) } bdm['block_device_mapping'][0]['connection_info'] = ( {'driver_volume_type': 'fake', 'data': {}}) with mock.patch.object( driver_block_device.DriverVolumeBlockDevice, 'save'): self.connection.power_on( self.ctxt, instance_ref, network_info, bdm) self.connection.detach_volume(connection_info, instance_ref, '/dev/sda')
def test_root_image_metadata(self): fake_image = {'properties': {'img_test_key': 'img_test_value'}} class FakeImageApi(object): def show(*args, **kwargs): return fake_image block_device_mapping = block_device_obj.block_device_make_list( None, [ fake_block_device.FakeDbBlockDeviceDict( { 'id': 1, 'boot_index': 0, 'source_type': 'image', 'destination_type': 'local', 'image_id': "fake-image", 'delete_on_termination': True }) ]) image_meta = block_device_mapping.root_metadata( self.context, FakeImageApi(), None) self.assertEqual(fake_image['properties'], image_meta)
def fake_bdm(self, instance=None): instance = instance or {} fake_bdm = fake_block_device.FakeDbBlockDeviceDict({ 'id': 123, 'instance_uuid': instance.get('uuid') or 'fake-instance', 'device_name': '/dev/sda2', 'source_type': 'snapshot', 'destination_type': 'volume', 'connection_info': "{'fake': 'connection_info'}", 'snapshot_id': 'fake-snapshot-id-1', 'boot_index': -1 }) if instance: fake_bdm['instance'] = instance return fake_bdm
def setUp(self): super(DefaultDeviceNamesTestCase, self).setUp() self.context = context.get_admin_context() self.instance = objects.Instance( uuid='32dfcb37-5af1-552b-357c-be8c3aa38310', memory_kb='1024000', basepath='/some/path', bridge_name='br100', vcpus=2, project_id='fake', bridge='br101', image_ref='155d900f-4e14-4e4c-a73d-069cbf4541e6', root_gb=10, ephemeral_gb=20, instance_type_id=2, config_drive=False, system_metadata={}) self.root_device_name = '/dev/vda' self.virt_type = 'kvm' self.flavor = objects.Flavor(swap=4) self.patchers = [] self.patchers.append( mock.patch.object(self.instance, 'get_flavor', return_value=self.flavor)) self.patchers.append( mock.patch('patron.objects.block_device.BlockDeviceMapping.save')) for patcher in self.patchers: patcher.start() self.ephemerals = [ objects.BlockDeviceMapping( self.context, **fake_block_device.FakeDbBlockDeviceDict({ 'id': 1, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vdb', 'source_type': 'blank', 'destination_type': 'local', 'device_type': 'disk', 'disk_bus': 'virtio', 'delete_on_termination': True, 'guest_format': None, 'volume_size': 1, 'boot_index': -1 })) ] self.swap = [ objects.BlockDeviceMapping( self.context, **fake_block_device.FakeDbBlockDeviceDict({ 'id': 2, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vdc', 'source_type': 'blank', 'destination_type': 'local', 'device_type': 'disk', 'disk_bus': 'virtio', 'delete_on_termination': True, 'guest_format': 'swap', 'volume_size': 1, 'boot_index': -1 })) ] self.block_device_mapping = [ objects.BlockDeviceMapping( self.context, **fake_block_device.FakeDbBlockDeviceDict({ 'id': 3, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vda', 'source_type': 'volume', 'destination_type': 'volume', 'device_type': 'disk', 'disk_bus': 'virtio', 'volume_id': 'fake-volume-id-1', 'boot_index': 0 })), objects.BlockDeviceMapping( self.context, **fake_block_device.FakeDbBlockDeviceDict({ 'id': 4, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vdd', 'source_type': 'snapshot', 'device_type': 'disk', 'disk_bus': 'virtio', 'destination_type': 'volume', 'snapshot_id': 'fake-snapshot-id-1', 'boot_index': -1 })), objects.BlockDeviceMapping( self.context, **fake_block_device.FakeDbBlockDeviceDict({ 'id': 5, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vde', 'source_type': 'blank', 'device_type': 'disk', 'disk_bus': 'virtio', 'destination_type': 'volume', 'boot_index': -1 })) ]
def setUp(self): super(DefaultDeviceNamesForInstanceTestCase, self).setUp() self.context = context.RequestContext('fake', 'fake') self.ephemerals = block_device_obj.block_device_make_list( self.context, [fake_block_device.FakeDbBlockDeviceDict( {'id': 1, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vdb', 'source_type': 'blank', 'destination_type': 'local', 'delete_on_termination': True, 'guest_format': None, 'boot_index': -1})]) self.swap = block_device_obj.block_device_make_list( self.context, [fake_block_device.FakeDbBlockDeviceDict( {'id': 2, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vdc', 'source_type': 'blank', 'destination_type': 'local', 'delete_on_termination': True, 'guest_format': 'swap', 'boot_index': -1})]) self.block_device_mapping = block_device_obj.block_device_make_list( self.context, [fake_block_device.FakeDbBlockDeviceDict( {'id': 3, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vda', 'source_type': 'volume', 'destination_type': 'volume', 'volume_id': 'fake-volume-id-1', 'boot_index': 0}), fake_block_device.FakeDbBlockDeviceDict( {'id': 4, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vdd', 'source_type': 'snapshot', 'destination_type': 'volume', 'snapshot_id': 'fake-snapshot-id-1', 'boot_index': -1}), fake_block_device.FakeDbBlockDeviceDict( {'id': 5, 'instance_uuid': 'fake-instance', 'device_name': '/dev/vde', 'source_type': 'blank', 'destination_type': 'volume', 'boot_index': -1})]) self.flavor = {'swap': 4} self.instance = {'uuid': 'fake_instance', 'ephemeral_gb': 2} self.is_libvirt = False self.root_device_name = '/dev/vda' self.update_called = False def fake_extract_flavor(instance): return self.flavor def fake_driver_matches(driver_string): if driver_string == 'libvirt.LibvirtDriver': return self.is_libvirt return False self.patchers = [] self.patchers.append( mock.patch.object(objects.BlockDeviceMapping, 'save')) self.patchers.append( mock.patch.object( flavors, 'extract_flavor', new=mock.Mock(side_effect=fake_extract_flavor))) self.patchers.append( mock.patch.object(driver, 'compute_driver_matches', new=mock.Mock( side_effect=fake_driver_matches))) for patcher in self.patchers: patcher.start()