Пример #1
0
 def test_legacy_mapping_from_object_list(self):
     bdm1 = block_device_obj.BlockDeviceMapping()
     bdm1 = block_device_obj.BlockDeviceMapping._from_db_object(
         None, bdm1,
         fake_block_device.FakeDbBlockDeviceDict(self.new_mapping[0]))
     bdm2 = block_device_obj.BlockDeviceMapping()
     bdm2 = block_device_obj.BlockDeviceMapping._from_db_object(
         None, bdm2,
         fake_block_device.FakeDbBlockDeviceDict(self.new_mapping[1]))
     bdmlist = block_device_obj.BlockDeviceMappingList()
     bdmlist.objects = [bdm1, bdm2]
     block_device.legacy_mapping(bdmlist)
 def test_snapshot_from_object(self):
     for bdm in self.new_mapping[:-1]:
         obj = block_device_obj.BlockDeviceMapping()
         obj = block_device_obj.BlockDeviceMapping._from_db_object(
                None, obj, fake_block_device.FakeDbBlockDeviceDict(
                    bdm))
         self._test_snapshot_from_bdm(obj)
Пример #3
0
 def setUp(self):
     super(ComputeRpcAPITestCase, self).setUp()
     self.context = context.get_admin_context()
     self.fake_flavor_obj = fake_flavor.fake_flavor_obj(self.context)
     self.fake_flavor = jsonutils.to_primitive(self.fake_flavor_obj)
     instance_attr = {
         'host': 'fake_host',
         'instance_type_id': self.fake_flavor_obj['id'],
         'instance_type': self.fake_flavor_obj
     }
     self.fake_instance_obj = fake_instance.fake_instance_obj(
         self.context, **instance_attr)
     self.fake_instance = jsonutils.to_primitive(self.fake_instance_obj)
     self.fake_volume_bdm = objects_block_dev.BlockDeviceMapping(
         **fake_block_device.FakeDbBlockDeviceDict(
             {
                 'source_type': 'volume',
                 'destination_type': 'volume',
                 'instance_uuid': self.fake_instance_obj.uuid,
                 'volume_id': 'fake-volume-id'
             }))
     # FIXME(melwitt): Temporary while things have no mappings
     self.patcher1 = mock.patch('nova.objects.InstanceMapping.'
                                'get_by_instance_uuid')
     self.patcher2 = mock.patch('nova.objects.HostMapping.get_by_host')
     mock_inst_mapping = self.patcher1.start()
     mock_host_mapping = self.patcher2.start()
     mock_inst_mapping.side_effect = exception.InstanceMappingNotFound(
         uuid=self.fake_instance_obj.uuid)
     mock_host_mapping.side_effect = exception.HostMappingNotFound(
         name=self.fake_instance_obj.host)
Пример #4
0
    def _test_compute_api(self, method, rpc_method,
                          expected_args=None, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')

        rpcapi = kwargs.pop('rpcapi_class', compute_rpcapi.ComputeAPI)()
        self.assertIsNotNone(rpcapi.client)
        self.assertEqual(rpcapi.client.target.topic, CONF.compute_topic)

        orig_prepare = rpcapi.client.prepare
        base_version = rpcapi.client.target.version
        expected_version = kwargs.pop('version', base_version)

        expected_kwargs = kwargs.copy()
        if expected_args:
            expected_kwargs.update(expected_args)
        if 'host_param' in expected_kwargs:
            expected_kwargs['host'] = expected_kwargs.pop('host_param')
        else:
            expected_kwargs.pop('host', None)

        cast_and_call = ['confirm_resize', 'stop_instance']
        if rpc_method == 'call' and method in cast_and_call:
            if method == 'confirm_resize':
                kwargs['cast'] = False
            else:
                kwargs['do_cast'] = False
        if 'host' in kwargs:
            host = kwargs['host']
        elif 'instances' in kwargs:
            host = kwargs['instances'][0]['host']
        else:
            host = kwargs['instance']['host']

        with contextlib.nested(
            mock.patch.object(rpcapi.client, rpc_method),
            mock.patch.object(rpcapi.client, 'prepare'),
            mock.patch.object(rpcapi.client, 'can_send_version'),
        ) as (
            rpc_mock, prepare_mock, csv_mock
        ):
            prepare_mock.return_value = rpcapi.client
            if '_return_value' in kwargs:
                rpc_mock.return_value = kwargs.pop('_return_value')
                del expected_kwargs['_return_value']
            elif 'return_bdm_object' in kwargs:
                del kwargs['return_bdm_object']
                rpc_mock.return_value = objects_block_dev.BlockDeviceMapping()
            elif rpc_method == 'call':
                rpc_mock.return_value = 'foo'
            else:
                rpc_mock.return_value = None
            csv_mock.side_effect = (
                lambda v: orig_prepare(version=v).can_send_version())

            retval = getattr(rpcapi, method)(ctxt, **kwargs)
            self.assertEqual(retval, rpc_mock.return_value)

            prepare_mock.assert_called_once_with(version=expected_version,
                                                 server=host)
            rpc_mock.assert_called_once_with(ctxt, method, **expected_kwargs)
Пример #5
0
 def test_create_fails_instance(self):
     values = {'source_type': 'volume', 'volume_id': 'fake-vol-id',
               'destination_type': 'volume',
               'instance_uuid': 'fake-instance',
               'instance': instance_obj.Instance()}
     bdm = block_device_obj.BlockDeviceMapping(**values)
     self.assertRaises(exception.ObjectActionError,
                       bdm.create, self.context)
Пример #6
0
 def test_reserve_block_device_name(self):
     self.flags(long_rpc_timeout=1234)
     self._test_compute_api('reserve_block_device_name', 'call',
             instance=self.fake_instance_obj, device='device',
             volume_id='id', disk_bus='ide', device_type='cdrom',
             tag='foo', multiattach=True, version='5.0',
             timeout=1234, call_monitor_timeout=60,
             _return_value=objects_block_dev.BlockDeviceMapping())
Пример #7
0
    def test_create(self):
        values = {'source_type': 'volume', 'volume_id': 'fake-vol-id',
                  'destination_type': 'volume',
                  'instance_uuid': 'fake-instance'}
        bdm = block_device_obj.BlockDeviceMapping(**values)
        with mock.patch.object(cells_rpcapi.CellsAPI,
                               'bdm_update_or_create_at_top'):
            bdm.create(self.context)

        for k, v in values.iteritems():
            self.assertEqual(v, getattr(bdm, k))
Пример #8
0
 def test_reserve_block_device_name(self):
     self._test_compute_api(
         'reserve_block_device_name',
         'call',
         instance=self.fake_instance_obj,
         device='device',
         volume_id='id',
         disk_bus='ide',
         device_type='cdrom',
         version='4.0',
         _return_value=objects_block_dev.BlockDeviceMapping())
Пример #9
0
    def test_reserve_block_device_name(self):
        self._test_compute_api('reserve_block_device_name', 'call',
                instance=self.fake_instance_obj, device='device',
                volume_id='id', disk_bus='ide', device_type='cdrom',
                version='4.0',
                _return_value=objects_block_dev.BlockDeviceMapping())

        self.flags(compute='kilo', group='upgrade_levels')
        self._test_compute_api('reserve_block_device_name', 'call',
                instance=self.fake_instance_obj, device='device',
                volume_id='id', disk_bus='ide', device_type='cdrom',
                version='3.35', return_bdm_object=True)
Пример #10
0
 def setUp(self):
     super(ComputeRpcAPITestCase, self).setUp()
     self.context = context.get_admin_context()
     instance_attr = {'host': 'fake_host',
                      'instance_type_id': 1}
     self.fake_instance_obj = fake_instance.fake_instance_obj(self.context,
                                                **instance_attr)
     self.fake_volume_bdm = objects_block_dev.BlockDeviceMapping(
             **fake_block_device.FakeDbBlockDeviceDict(
                 {'source_type': 'volume', 'destination_type': 'volume',
                  'instance_uuid': self.fake_instance_obj.uuid,
                  'volume_id': 'fake-volume-id'}))
Пример #11
0
    def __init__(self, bdm):
        # TODO(ndipanov): Remove this check when we have all the rpc methods
        # use objects for block devices.
        if isinstance(bdm, block_device_obj.BlockDeviceMapping):
            self.__dict__['_bdm_obj'] = bdm
        else:
            self.__dict__['_bdm_obj'] = block_device_obj.BlockDeviceMapping()
            self._bdm_obj.update(block_device.BlockDeviceDict(bdm))
            self._bdm_obj.obj_reset_changes()

        if self._bdm_obj.no_device:
            raise _NotTransformable()

        self.update(dict((field, None) for field in self._fields))
        self._transform()
Пример #12
0
        def _fake_bdm(volume_id, target_lun):
            connection_info = {'driver_volume_type': 'fibre_channel',
                               'data': {'volume_id': volume_id,
                                        'target_lun': target_lun,
                                        'initiator_target_map':
                                        {'21000024F5': ['50050768']}}}
            mapping_dict = {'source_type': 'volume', 'volume_id': volume_id,
                            'destination_type': 'volume',
                            'connection_info':
                            jsonutils.dumps(connection_info),
                            }
            bdm_dict = nova_block_device.BlockDeviceDict(mapping_dict)
            bdm_obj = bdmobj.BlockDeviceMapping(**bdm_dict)

            return nova_virt_bdm.DriverVolumeBlockDevice(bdm_obj)
Пример #13
0
 def test_destroy_mocked(self):
     values = {'source_type': 'volume', 'volume_id': 'fake-vol-id',
               'destination_type': 'volume', 'id': 1,
               'instance_uuid': 'fake-instance', 'device_name': 'fake'}
     with contextlib.nested(
         mock.patch.object(db, 'block_device_mapping_destroy'),
         mock.patch.object(cells_rpcapi.CellsAPI, 'bdm_destroy_at_top')
     ) as (bdm_del, cells_destroy):
         bdm = block_device_obj.BlockDeviceMapping(**values)
         bdm.destroy(self.context)
         bdm_del.assert_called_once_with(self.context, values['id'])
         cells_destroy.assert_called_once_with(
             self.context, values['instance_uuid'],
             device_name=values['device_name'],
             volume_id=values['volume_id'])
Пример #14
0
    def test_save(self):
        fake_bdm = self.fake_bdm()
        with contextlib.nested(
            mock.patch.object(
                db, 'block_device_mapping_update', return_value=fake_bdm),
            mock.patch.object(
                cells_rpcapi.CellsAPI, 'bdm_update_or_create_at_top')
        ) as (bdm_update_mock, cells_update_mock):
            bdm_object = block_device_obj.BlockDeviceMapping()
            bdm_object.id = 123
            bdm_object.volume_id = 'fake_volume_id'
            bdm_object.save(self.context)

            bdm_update_mock.assert_called_once_with(
                    self.context, 123, {'volume_id': 'fake_volume_id'},
                    legacy=False)
            cells_update_mock.assert_called_once_with(self.context, fake_bdm)
Пример #15
0
    def test_create_mocked(self):
        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(cells_rpcapi.CellsAPI,
                              'bdm_update_or_create_at_top')
        ) as (bdm_create_mock, cells_update_mock):
            bdm = block_device_obj.BlockDeviceMapping(**values)
            bdm.create(self.context)
            bdm_create_mock.assert_called_once_with(
                    self.context, values, legacy=False)
            cells_update_mock.assert_called_once_with(
                    self.context, fake_bdm, create=True)
Пример #16
0
    def test_get_device_name(self):
        bdm_obj = block_device_obj.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.assertEqual(None, blockinfo.get_device_name(bdm_obj))

        driver_bdm = driver_block_device.DriverVolumeBlockDevice(bdm_obj)
        self.assertEqual(None, blockinfo.get_device_name(driver_bdm))
Пример #17
0
    def _test_compute_api(self, method, rpc_method,
                          assert_dict=False, **kwargs):
        ctxt = context.RequestContext('fake_user', 'fake_project')

        rpcapi = kwargs.pop('rpcapi_class', compute_rpcapi.ComputeAPI)()
        self.assertIsNotNone(rpcapi.client)
        self.assertEqual(rpcapi.client.target.topic, CONF.compute_topic)

        orig_prepare = rpcapi.client.prepare
        # TODO(danms): Remove this special case when we drop 3.x
        if CONF.upgrade_levels.compute == '3.40':
            base_version = '3.0'
        else:
            base_version = rpcapi.client.target.version
        expected_version = kwargs.pop('version', base_version)
        nova_network = kwargs.pop('nova_network', False)

        expected_kwargs = kwargs.copy()
        if ('requested_networks' in expected_kwargs and
               expected_version == '3.23'):
            expected_kwargs['requested_networks'] = []
            for requested_network in kwargs['requested_networks']:
                if not nova_network:
                    expected_kwargs['requested_networks'].append(
                        (requested_network.network_id,
                         str(requested_network.address),
                         requested_network.port_id))
                else:
                    expected_kwargs['requested_networks'].append(
                        (requested_network.network_id,
                         str(requested_network.address)))
        if 'host_param' in expected_kwargs:
            expected_kwargs['host'] = expected_kwargs.pop('host_param')
        else:
            expected_kwargs.pop('host', None)
        if 'legacy_limits' in expected_kwargs:
            expected_kwargs['limits'] = expected_kwargs.pop('legacy_limits')
            kwargs.pop('legacy_limits', None)
        expected_kwargs.pop('destination', None)

        if 'mountpoint' in expected_kwargs and expected_version == '4.0':
            # TODO(danms): Remove me when we drop 3.x
            del expected_kwargs['mountpoint']
            del expected_kwargs['volume_id']

        if assert_dict:
            expected_kwargs['instance'] = jsonutils.to_primitive(
                expected_kwargs['instance'])

        cast_and_call = ['confirm_resize', 'stop_instance']
        if rpc_method == 'call' and method in cast_and_call:
            if method == 'confirm_resize':
                kwargs['cast'] = False
            else:
                kwargs['do_cast'] = False
        if 'host' in kwargs:
            host = kwargs['host']
        elif 'destination' in kwargs:
            host = kwargs['destination']
        elif 'instances' in kwargs:
            host = kwargs['instances'][0]['host']
        else:
            host = kwargs['instance']['host']

        with contextlib.nested(
            mock.patch.object(rpcapi.client, rpc_method),
            mock.patch.object(rpcapi.client, 'prepare'),
            mock.patch.object(rpcapi.client, 'can_send_version'),
        ) as (
            rpc_mock, prepare_mock, csv_mock
        ):
            prepare_mock.return_value = rpcapi.client
            if '_return_value' in kwargs:
                rpc_mock.return_value = kwargs.pop('_return_value')
                del expected_kwargs['_return_value']
            elif 'return_bdm_object' in kwargs:
                del kwargs['return_bdm_object']
                rpc_mock.return_value = objects_block_dev.BlockDeviceMapping()
            elif rpc_method == 'call':
                rpc_mock.return_value = 'foo'
            else:
                rpc_mock.return_value = None
            csv_mock.side_effect = (
                lambda v: orig_prepare(version=v).can_send_version())

            retval = getattr(rpcapi, method)(ctxt, **kwargs)
            self.assertEqual(retval, rpc_mock.return_value)

            prepare_mock.assert_called_once_with(version=expected_version,
                                                 server=host)
            rpc_mock.assert_called_once_with(ctxt, method, **expected_kwargs)
Пример #18
0
    def setUp(self):
        super(DefaultDeviceNamesTestCase, self).setUp()
        self.context = context.get_admin_context()
        self.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
        }
        self.root_device_name = '/dev/vda'
        self.virt_type = 'kvm'
        self.flavor = {'swap': 4}
        self.patchers = []
        self.patchers.append(
            mock.patch('nova.compute.flavors.extract_flavor',
                       return_value=self.flavor))
        self.patchers.append(
            mock.patch('nova.objects.block_device.BlockDeviceMapping.save'))
        for patcher in self.patchers:
            patcher.start()

        self.ephemerals = [
            block_device_obj.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 = [
            block_device_obj.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 = [
            block_device_obj.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
                })),
            block_device_obj.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
                }))
        ]
Пример #19
0
 def test_save_instance_changed(self):
     bdm_object = block_device_obj.BlockDeviceMapping()
     bdm_object.instance = instance_obj.Instance()
     self.assertRaises(exception.ObjectActionError,
                       bdm_object.save, self.context)