def test_build_request_spec_without_image(self, mock_get): image = None instance = {'uuid': 'fake-uuid'} instance_type = objects.Flavor(**test_flavor.fake_flavor) mock_get.return_value = objects.Flavor(extra_specs={}) self.mox.StubOutWithMock(flavors, 'extract_flavor') flavors.extract_flavor(mox.IgnoreArg()).AndReturn(instance_type) self.mox.ReplayAll() request_spec = scheduler_utils.build_request_spec( self.context, image, [instance]) self.assertEqual({}, request_spec['image'])
def extract_flavor(instance, prefix=''): """Create a Flavor object from instance's system_metadata information. """ flavor = objects.Flavor() sys_meta = utils.instance_sys_meta(instance) if not sys_meta: return None for key in system_metadata_flavor_props.keys(): type_key = '%sinstance_type_%s' % (prefix, key) setattr(flavor, key, sys_meta[type_key]) # NOTE(danms): We do NOT save all of extra_specs, but only the # NUMA-related ones that we need to avoid an uglier alternative. This # should be replaced by a general split-out of flavor information from # system_metadata very soon. extra_specs = [(k, v) for k, v in sys_meta.items() if k.startswith('%sinstance_type_extra_' % prefix)] if extra_specs: flavor.extra_specs = {} for key, value in extra_specs: extra_key = key[len('%sinstance_type_extra_' % prefix):] flavor.extra_specs[extra_key] = value return flavor
def _test_get_root_vhd_size_gb(self, old_flavor=True): if old_flavor: mock_flavor = objects.Flavor(**test_flavor.fake_flavor) self.instance.old_flavor = mock_flavor else: self.instance.old_flavor = None return self.imagecache._get_root_vhd_size_gb(self.instance)
def test_get_image_meta(self, mock_get): mock_get.return_value = objects.Flavor(extra_specs={}) image_meta = compute_utils.get_image_metadata( self.ctx, self.mock_image_api, 'fake-image', self.instance_obj) self.image['properties'] = 'DONTCARE' self.assertThat(self.image, matchers.DictMatches(image_meta))
def test_get_image_meta_with_image_id_none(self, mock_flavor_get): mock_flavor_get.return_value = objects.Flavor(extra_specs={}) self.image['properties'] = {'fake_property': 'fake_value'} inst = self.instance_obj with mock.patch.object(flavors, "extract_flavor") as mock_extract_flavor: with mock.patch.object(utils, "get_system_metadata_from_image" ) as mock_get_sys_metadata: image_meta = compute_utils.get_image_metadata( self.ctx, self.mock_image_api, None, inst) self.assertEqual(0, self.mock_image_api.get.call_count) self.assertEqual(0, mock_extract_flavor.call_count) self.assertEqual(0, mock_get_sys_metadata.call_count) self.assertNotIn('fake_property', image_meta['properties']) # Checking mock_image_api_get is called with 0 image_id # as 0 is a valid image ID image_meta = compute_utils.get_image_metadata(self.ctx, self.mock_image_api, 0, self.instance_obj) self.assertEqual(1, self.mock_image_api.get.call_count) self.assertIn('fake_property', image_meta['properties'])
def fake_db_instance(**updates): if 'instance_type' in updates: if isinstance(updates['instance_type'], objects.Flavor): flavor = updates['instance_type'] else: flavor = objects.Flavor(**updates['instance_type']) flavorinfo = jsonutils.dumps({ 'cur': flavor.obj_to_primitive(), 'old': None, 'new': None, }) else: flavorinfo = None db_instance = { 'id': 1, 'deleted': False, 'uuid': str(uuid.uuid4()), 'user_id': 'fake-user', 'project_id': 'fake-project', 'host': 'fake-host', 'created_at': datetime.datetime(1955, 11, 5), 'pci_devices': [], 'security_groups': [], 'metadata': {}, 'system_metadata': {}, 'root_gb': 0, 'ephemeral_gb': 0, 'extra': { 'pci_requests': None, 'flavor': flavorinfo, 'numa_topology': None, 'vcpu_model': None, }, 'tags': [] } for name, field in objects.Instance.fields.items(): if name in db_instance: continue if field.nullable: db_instance[name] = None elif field.default != fields.UnspecifiedDefault: db_instance[name] = field.default elif name in ['flavor']: pass else: raise Exception('fake_db_instance needs help with %s' % name) if updates: db_instance.update(updates) if db_instance.get('security_groups'): db_instance['security_groups'] = fake_db_secgroups( db_instance, db_instance['security_groups']) return db_instance
def test_build_request_spec_with_object(self): instance_type = objects.Flavor() instance = fake_instance.fake_instance_obj(self.context) with mock.patch.object(instance, 'get_flavor') as mock_get: mock_get.return_value = instance_type request_spec = scheduler_utils.build_request_spec( self.context, None, [instance]) mock_get.assert_called_once_with() self.assertIsInstance(request_spec['instance_properties'], dict)
def destroy(name): """Marks flavor as deleted.""" try: if not name: raise ValueError() flavor = objects.Flavor(context=context.get_admin_context(), name=name) flavor.destroy() except (ValueError, exception.NotFound): LOG.exception(_LE('Instance type %s not found for deletion'), name) raise exception.FlavorNotFoundByName(flavor_name=name)
def test_get_image_meta_no_image_system_meta(self): for k in self.instance_obj.system_metadata.keys(): if k.startswith('image_'): del self.instance_obj.system_metadata[k] with mock.patch('patron.objects.Flavor.get_by_flavor_id') as get: get.return_value = objects.Flavor(extra_specs={}) image_meta = compute_utils.get_image_metadata( self.ctx, self.mock_image_api, 'fake-image', self.instance_obj) self.image['properties'] = 'DONTCARE' self.assertThat(self.image, matchers.DictMatches(image_meta))
def get_test_flavor(**kw): default_extra_specs = { 'baremetal:deploy_kernel_id': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'baremetal:deploy_ramdisk_id': 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb' } flavor = { 'name': kw.get('name', 'fake.flavor'), 'extra_specs': kw.get('extra_specs', default_extra_specs), 'swap': kw.get('swap', 0), 'ephemeral_gb': kw.get('ephemeral_gb', 0) } return objects.Flavor(**flavor)
def test_get_image_meta_no_image_no_image_system_meta(self): e = exception.ImageNotFound(image_id='fake-image') self.mock_image_api.get.side_effect = e for k in self.instance_obj.system_metadata.keys(): if k.startswith('image_'): del self.instance_obj.system_metadata[k] with mock.patch('patron.objects.Flavor.get_by_flavor_id') as get: get.return_value = objects.Flavor(extra_specs={}) image_meta = compute_utils.get_image_metadata( self.ctx, self.mock_image_api, 'fake-image', self.instance_obj) expected = {'properties': 'DONTCARE'} self.assertThat(expected, matchers.DictMatches(image_meta))
def _remove_tenant_access(self, req, id, body): context = req.environ['patron.context'] authorize(context, action="remove_tenant_access") vals = body['removeTenantAccess'] tenant = vals['tenant'] flavor = objects.Flavor(context=context, flavorid=id) try: flavor.remove_access(tenant) except (exception.FlavorAccessNotFound, exception.FlavorNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.AdminRequired as e: raise webob.exc.HTTPForbidden(explanation=e.format_message()) return _marshall_flavor_access(flavor)
def test_stat_consumption_from_instance_pci(self): inst_topology = objects.InstanceNUMATopology(cells=[ objects.InstanceNUMACell(cpuset=set([0]), memory=512, id=0) ]) fake_requests = [{ 'request_id': 'fake_request1', 'count': 1, 'spec': [{ 'vendor_id': '8086' }] }] fake_requests_obj = objects.InstancePCIRequests( requests=[objects.InstancePCIRequest(**r) for r in fake_requests], instance_uuid='fake-uuid') instance = objects.Instance(root_gb=0, ephemeral_gb=0, memory_mb=512, vcpus=1, project_id='12345', vm_state=vm_states.BUILDING, task_state=task_states.SCHEDULING, os_type='Linux', uuid='fake-uuid', numa_topology=inst_topology, pci_requests=fake_requests_obj, id=1243) req_spec = sched_utils.build_request_spec( None, None, [instance], objects.Flavor(root_gb=0, ephemeral_gb=0, memory_mb=1024, vcpus=1)) host = host_manager.HostState("fakehost", "fakenode") host.pci_stats = pci_stats.PciDeviceStats([ objects.PciDevicePool(vendor_id='8086', product_id='15ed', numa_node=1, count=1) ]) host.numa_topology = fakes.NUMA_TOPOLOGY host.consume_from_instance(req_spec['instance_properties']) self.assertIsInstance(req_spec['instance_properties']['numa_topology'], objects.InstanceNUMATopology) self.assertEqual(512, host.numa_topology.cells[1].memory_usage) self.assertEqual(1, host.numa_topology.cells[1].cpu_usage) self.assertEqual(0, len(host.pci_stats.pools))
def setUp(self): super(ComputeGetImageMetadataTestCase, self).setUp() self.context = context.RequestContext('fake', 'fake') self.image = { "min_ram": 10, "min_disk": 1, "disk_format": "raw", "container_format": "bare", "properties": {}, } self.mock_image_api = mock.Mock() self.mock_image_api.get.return_value = self.image self.ctx = context.RequestContext('fake', 'fake') sys_meta = { 'image_min_ram': 10, 'image_min_disk': 1, 'image_disk_format': 'raw', 'image_container_format': 'bare', } flavor = objects.Flavor( id=0, name='m1.fake', memory_mb=10, vcpus=1, root_gb=1, ephemeral_gb=1, flavorid='0', swap=1, rxtx_factor=0.0, vcpu_weight=None) instance = fake_instance.fake_db_instance( memory_mb=0, root_gb=0, system_metadata=sys_meta) self.instance_obj = objects.Instance._from_db_object( self.ctx, objects.Instance(), instance, expected_attrs=instance_obj.INSTANCE_DEFAULT_FIELDS) with mock.patch.object(self.instance_obj, 'save'): self.instance_obj.set_flavor(flavor)
def build_instances(self, ctxt, build_inst_kwargs): """Pick a cell (possibly ourselves) to build new instance(s) and forward the request accordingly. """ # Target is ourselves first. filter_properties = build_inst_kwargs.get('filter_properties') if (filter_properties is not None and not isinstance( filter_properties['instance_type'], objects.Flavor)): # NOTE(danms): Handle pre-1.30 build_instances() call. Remove me # when we bump the RPC API version to 2.0. flavor = objects.Flavor(**filter_properties['instance_type']) build_inst_kwargs['filter_properties'] = dict(filter_properties, instance_type=flavor) instances = build_inst_kwargs['instances'] if not isinstance(instances[0], objects.Instance): # NOTE(danms): Handle pre-1.32 build_instances() call. Remove me # when we bump the RPC API version to 2.0 build_inst_kwargs['instances'] = instance_obj._make_instance_list( ctxt, objects.InstanceList(), instances, ['system_metadata', 'metadata']) our_cell = self.state_manager.get_my_state() self.msg_runner.build_instances(ctxt, our_cell, build_inst_kwargs)
def _removeTenantAccess(self, req, id, body): context = req.environ['patron.context'] authorize(context, action="removeTenantAccess") # NOTE(alex_xu): back-compatible with db layer hard-code admin # permission checks. patron_context.require_admin_context(context) self._check_body(body) vals = body['removeTenantAccess'] tenant = vals.get('tenant') if not tenant: msg = _("Missing tenant parameter") raise webob.exc.HTTPBadRequest(explanation=msg) flavor = objects.Flavor(context=context, flavorid=id) try: flavor.remove_access(tenant) except (exception.FlavorNotFound, exception.FlavorAccessNotFound) as err: raise webob.exc.HTTPNotFound(explanation=err.format_message()) return _marshall_flavor_access(flavor)
def setUp(self): super(LibvirtBlockInfoTest, self).setUp() self.user_id = 'fake' self.project_id = 'fake' self.context = context.get_admin_context() patron.tests.unit.image.fake.stub_out_image_service(self.stubs) self.test_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, # m1.tiny 'config_drive': None, 'system_metadata': {}, } flavor = objects.Flavor(memory_mb=128, root_gb=0, name='m1.micro', ephemeral_gb=0, vcpus=1, swap=0, rxtx_factor=1.0, flavorid='1', vcpu_weight=None, id=2) self.test_instance['flavor'] = flavor self.test_instance['old_flavor'] = None self.test_instance['new_flavor'] = None
def setUp(self): super(ComputeValidateDeviceTestCase, self).setUp() self.context = context.RequestContext('fake', 'fake') # check if test name includes "xen" if 'xen' in self.id(): self.flags(compute_driver='xenapi.XenAPIDriver') self.instance = objects.Instance(uuid=uuid.uuid4().hex, root_device_name=None, default_ephemeral_device=None) else: self.instance = objects.Instance(uuid=uuid.uuid4().hex, root_device_name='/dev/vda', default_ephemeral_device='/dev/vdb') flavor = objects.Flavor(**test_flavor.fake_flavor) self.instance.system_metadata = {} with mock.patch.object(self.instance, 'save'): self.instance.set_flavor(flavor) self.instance.default_swap_device = None self.data = [] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', lambda context, instance, use_slave=False: self.data)
def setUp(self): super(VmCommandsTestCase, self).setUp() self.commands = manage.VmCommands() self.fake_flavor = objects.Flavor(**test_flavors.DEFAULT_FLAVORS[0])
def _test_finish_migration(self, power_on, ephemeral_storage=False, config_drive=False, config_drive_format='iso'): self._instance = self._get_instance() self._instance.memory_mb = 100 self._instance.vcpus = 2 self._instance['system_metadata'] = {} self._instance['old_flavor'] = objects.Flavor(root_gb=5) network_info = fake_network.fake_get_instance_nw_info(self.stubs) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) m.AndReturn(self._test_instance_dir) self._mox.StubOutWithMock(fake.PathUtils, 'exists') m = fake.PathUtils.exists(mox.IsA(unicode)) m.AndReturn(True) fake_parent_vhd_path = (os.path.join( 'FakeParentPath', '%s.vhd' % self._instance["image_ref"])) m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(str)) m.AndReturn({'ParentPath': fake_parent_vhd_path, 'MaxInternalSize': 1}) m = vhdutils.VHDUtils.get_internal_vhd_size_by_file_size( mox.IsA(unicode), mox.IsA(object)) m.AndReturn(1025) vhdutils.VHDUtils.reconnect_parent_vhd(mox.IsA(str), mox.IsA(unicode)) m = vhdutils.VHDUtils.get_vhd_info(mox.IsA(unicode)) m.AndReturn({'MaxInternalSize': 1024}) m = fake.PathUtils.exists(mox.IsA(unicode)) m.AndReturn(True) m = fake.PathUtils.get_instance_dir(mox.IsA(str)) if ephemeral_storage: return m.AndReturn(self._test_instance_dir) else: m.AndReturn(None) self._set_vm_name(self._instance.name) self._setup_create_instance_mocks(None, False, ephemeral_storage=ephemeral_storage) if power_on: vmutils.VMUtils.set_vm_state(mox.Func(self._check_instance_name), constants.HYPERV_VM_STATE_ENABLED) self._setup_log_vm_output_mocks() if config_drive: self._mock_attach_config_drive(self._instance, config_drive_format) self._mox.ReplayAll() image_meta = { 'properties': { constants.IMAGE_PROP_VM_GEN: constants.IMAGE_PROP_VM_GEN_1 } } self._conn.finish_migration(self._context, None, self._instance, "", network_info, image_meta, False, None, power_on) self._mox.VerifyAll() if config_drive: self._verify_attach_config_drive(config_drive_format)
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 create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None, swap=0, rxtx_factor=1.0, is_public=True): """Creates flavors.""" if not flavorid: flavorid = uuid.uuid4() kwargs = { 'memory_mb': memory, 'vcpus': vcpus, 'root_gb': root_gb, 'ephemeral_gb': ephemeral_gb, 'swap': swap, 'rxtx_factor': rxtx_factor, } if isinstance(name, six.string_types): name = name.strip() # ensure name do not exceed 255 characters utils.check_string_length(name, 'name', min_length=1, max_length=255) # ensure name does not contain any special characters valid_name = VALID_NAME_REGEX.search(name) if not valid_name: msg = _("Flavor names can only contain printable characters " "and horizontal spaces.") raise exception.InvalidInput(reason=msg) # NOTE(vish): Internally, flavorid is stored as a string but it comes # in through json as an integer, so we convert it here. flavorid = unicode(flavorid) # ensure leading/trailing whitespaces not present. if flavorid.strip() != flavorid: msg = _("id cannot contain leading and/or trailing whitespace(s)") raise exception.InvalidInput(reason=msg) # ensure flavor id does not exceed 255 characters utils.check_string_length(flavorid, 'id', min_length=1, max_length=255) # ensure flavor id does not contain any special characters valid_flavor_id = VALID_ID_REGEX.search(flavorid) if not valid_flavor_id: msg = _("Flavor id can only contain letters from A-Z (both cases), " "periods, dashes, underscores and spaces.") raise exception.InvalidInput(reason=msg) # NOTE(wangbo): validate attributes of the creating flavor. # ram and vcpus should be positive ( > 0) integers. # disk, ephemeral and swap should be non-negative ( >= 0) integers. flavor_attributes = { 'memory_mb': ('ram', 1), 'vcpus': ('vcpus', 1), 'root_gb': ('disk', 0), 'ephemeral_gb': ('ephemeral', 0), 'swap': ('swap', 0) } for key, value in flavor_attributes.items(): kwargs[key] = utils.validate_integer(kwargs[key], value[0], value[1], db.MAX_INT) # rxtx_factor should be a positive float try: kwargs['rxtx_factor'] = float(kwargs['rxtx_factor']) if (kwargs['rxtx_factor'] <= 0 or kwargs['rxtx_factor'] > SQL_SP_FLOAT_MAX): raise ValueError() except ValueError: msg = (_("'rxtx_factor' argument must be a float between 0 and %g") % SQL_SP_FLOAT_MAX) raise exception.InvalidInput(reason=msg) kwargs['name'] = name kwargs['flavorid'] = flavorid # ensure is_public attribute is boolean try: kwargs['is_public'] = strutils.bool_from_string( is_public, strict=True) except ValueError: raise exception.InvalidInput(reason=_("is_public must be a boolean")) flavor = objects.Flavor(context=context.get_admin_context(), **kwargs) flavor.create() return flavor
def setUp(self, mock_register): super(ConfigDriveTestCase, self).setUp() vm_util.vm_refs_cache_reset() self.context = context.RequestContext('fake', 'fake', is_admin=False) cluster_name = 'test_cluster' self.flags(cluster_name=[cluster_name], host_ip='test_url', host_username='******', host_password='******', use_linked_clone=False, group='vmware') self.flags(vnc_enabled=False) vmwareapi_fake.reset() stubs.set_stubs(self.stubs) patron.tests.unit.image.fake.stub_out_image_service(self.stubs) self.conn = driver.VMwareVCDriver(fake.FakeVirtAPI) self.network_info = utils.get_test_network_info() self.node_name = '%s(%s)' % (self.conn.dict_mors.keys()[0], cluster_name) image_ref = patron.tests.unit.image.fake.get_valid_image_id() instance_values = { 'vm_state': 'building', 'project_id': 'fake', 'user_id': 'fake', 'name': '1', 'kernel_id': '1', 'ramdisk_id': '1', 'mac_addresses': [{'address': 'de:ad:be:ef:be:ef'}], 'memory_mb': 8192, 'flavor': 'm1.large', 'instance_type_id': 0, 'vcpus': 4, 'root_gb': 80, 'image_ref': image_ref, 'host': 'fake_host', 'task_state': 'scheduling', 'reservation_id': 'r-3t8muvr0', 'id': 1, 'uuid': 'fake-uuid', 'node': self.node_name, 'metadata': [], 'expected_attrs': ['system_metadata'], } self.test_instance = fake_instance.fake_instance_obj(self.context, **instance_values) self.test_instance.flavor = objects.Flavor(extra_specs={}) (image_service, image_id) = glance.get_remote_image_service(context, image_ref) metadata = image_service.show(context, image_id) self.image = { 'id': image_ref, 'disk_format': 'vmdk', 'size': int(metadata['size']), } class FakeInstanceMetadata(object): def __init__(self, instance, content=None, extra_md=None): pass def metadata_for_config_drive(self): return [] self.useFixture(fixtures.MonkeyPatch( 'patron.api.metadata.base.InstanceMetadata', FakeInstanceMetadata)) def fake_make_drive(_self, _path): pass # We can't actually make a config drive v2 because ensure_tree has # been faked out self.stubs.Set(patron.virt.configdrive.ConfigDriveBuilder, 'make_drive', fake_make_drive) def fake_upload_iso_to_datastore(iso_path, instance, **kwargs): pass self.stubs.Set(images, 'upload_iso_to_datastore', fake_upload_iso_to_datastore)