def test_create(self, mock_ensure_cache): rp = resource_provider.ResourceProvider(context=self.context, uuid=_RESOURCE_PROVIDER_UUID, name=_RESOURCE_PROVIDER_NAME) rp.create() inv = resource_provider.Inventory(context=self.context, resource_provider=rp, resource_class=_RESOURCE_CLASS_NAME, total=16, reserved=2, min_unit=1, max_unit=8, step_size=1, allocation_ratio=1.0) inv_list = resource_provider.InventoryList(context=self.context, objects=[inv]) rp.set_inventory(inv_list) obj = resource_provider.Allocation(context=self.context, resource_provider=rp, resource_class=_RESOURCE_CLASS_NAME, consumer_id=uuids.fake_instance, used=8) alloc_list = resource_provider.AllocationList(self.context, objects=[obj]) self.assertNotIn("id", obj) alloc_list.create_all() self.assertIn("id", obj)
def test_create_with_id_fails(self): rp = resource_provider.ResourceProvider(context=self.context, uuid=_RESOURCE_PROVIDER_UUID, name=_RESOURCE_PROVIDER_NAME) rp.create() inv = resource_provider.Inventory(context=self.context, resource_provider=rp, resource_class=_RESOURCE_CLASS_NAME, total=16, reserved=2, min_unit=1, max_unit=8, step_size=1, allocation_ratio=1.0) inv_list = resource_provider.InventoryList(context=self.context, objects=[inv]) rp.set_inventory(inv_list) obj = resource_provider.Allocation(context=self.context, id=99, resource_provider=rp, resource_class=_RESOURCE_CLASS_NAME, consumer_id=uuids.fake_instance, used=8) alloc_list = resource_provider.AllocationList(self.context, objects=[obj]) self.assertRaises(exception.ObjectActionError, alloc_list.create_all)
def test_find(self): rp = resource_provider.ResourceProvider(uuid=uuids.rp_uuid) inv_list = resource_provider.InventoryList(objects=[ resource_provider.Inventory( resource_provider=rp, resource_class=fields.ResourceClass.VCPU, total=24), resource_provider.Inventory( resource_provider=rp, resource_class=fields.ResourceClass.MEMORY_MB, total=10240), ]) found = inv_list.find(fields.ResourceClass.MEMORY_MB) self.assertIsNotNone(found) self.assertEqual(10240, found.total) found = inv_list.find(fields.ResourceClass.VCPU) self.assertIsNotNone(found) self.assertEqual(24, found.total) found = inv_list.find(fields.ResourceClass.DISK_GB) self.assertIsNone(found) # Try an integer resource class identifier... self.assertRaises(ValueError, inv_list.find, VCPU_ID) # Use an invalid string... self.assertIsNone(inv_list.find('HOUSE'))
def delete_inventories(req): """DELETE all inventory for a resource provider. Delete inventory as required to reset all the inventory. If an inventory to be deleted is in use, return a 409 Conflict. On success return a 204 No content. Return 405 Method Not Allowed if the wanted microversion does not match. """ context = req.environ['placement.context'] uuid = util.wsgi_path_item(req.environ, 'uuid') resource_provider = rp_obj.ResourceProvider.get_by_uuid(context, uuid) inventories = rp_obj.InventoryList(objects=[]) try: resource_provider.set_inventory(inventories) except exception.ConcurrentUpdateDetected: raise webob.exc.HTTPConflict( _('Unable to delete inventory for resource provider ' '%(rp_uuid)s because the inventory was updated by ' 'another process. Please retry your request.') % {'rp_uuid': resource_provider.uuid}) except exception.InventoryInUse as ex: # NOTE(mriedem): This message cannot change without impacting the # nova.scheduler.client.report._RE_INV_IN_USE regex. raise webob.exc.HTTPConflict(explanation=ex.format_message()) response = req.response response.status = 204 response.content_type = None return response
def set_inventories(req): """PUT to set all inventory for a resource provider. Create, update and delete inventory as required to reset all the inventory. If the resource generation is out of sync, return a 409. If an inventory to be deleted is in use, return a 409. If any inventory to be created or updated has settings which are invalid (for example reserved exceeds capacity), return a 400. On success return a 200 with an application/json body representing the inventories. """ context = req.environ['placement.context'] uuid = util.wsgi_path_item(req.environ, 'uuid') resource_provider = rp_obj.ResourceProvider.get_by_uuid(context, uuid) data = _extract_inventories(req.body, PUT_INVENTORY_SCHEMA) if data['resource_provider_generation'] != resource_provider.generation: raise webob.exc.HTTPConflict( _('resource provider generation conflict')) inv_list = [] for res_class, inventory_data in data['inventories'].items(): inventory = _make_inventory_object(resource_provider, res_class, **inventory_data) inv_list.append(inventory) inventories = rp_obj.InventoryList(objects=inv_list) try: resource_provider.set_inventory(inventories) except exception.ResourceClassNotFound as exc: raise webob.exc.HTTPBadRequest( _('Unknown resource class in inventory for resource provider ' '%(rp_uuid)s: %(error)s') % { 'rp_uuid': resource_provider.uuid, 'error': exc }) except exception.InventoryWithResourceClassNotFound as exc: raise webob.exc.HTTPConflict( _('Race condition detected when setting inventory. No inventory ' 'record with resource class for resource provider ' '%(rp_uuid)s: %(error)s') % { 'rp_uuid': resource_provider.uuid, 'error': exc }) except (exception.ConcurrentUpdateDetected, exception.InventoryInUse, db_exc.DBDuplicateEntry) as exc: raise webob.exc.HTTPConflict( _('update conflict: %(error)s') % {'error': exc}) except exception.InvalidInventoryCapacity as exc: raise webob.exc.HTTPBadRequest( _('Unable to update inventory for resource provider ' '%(rp_uuid)s: %(error)s') % { 'rp_uuid': resource_provider.uuid, 'error': exc }) return _send_inventories(req, resource_provider, inventories)
def _create_resource_provider(self, inventory): """Helper method to create a resource provider with inventory""" ctxt = context.get_admin_context() rp_uuid = uuidutils.generate_uuid() rp = rp_obj.ResourceProvider(context=ctxt, name=rp_uuid, uuid=rp_uuid) rp.create() inv = rp_obj.Inventory(context=ctxt, resource_provider=rp, **inventory) inv_list = rp_obj.InventoryList(objects=[inv]) rp.set_inventory(inv_list) return rp
def start_fixture(self): super(SharedStorageFixture, self).start_fixture() self.context = context.get_admin_context() cn1_uuid = uuidutils.generate_uuid() cn2_uuid = uuidutils.generate_uuid() ss_uuid = uuidutils.generate_uuid() agg_uuid = uuidutils.generate_uuid() os.environ['CN1_UUID'] = cn1_uuid os.environ['CN2_UUID'] = cn2_uuid os.environ['SS_UUID'] = ss_uuid os.environ['AGG_UUID'] = agg_uuid cn1 = rp_obj.ResourceProvider( self.context, name='cn1', uuid=cn1_uuid) cn1.create() cn2 = rp_obj.ResourceProvider( self.context, name='cn2', uuid=cn2_uuid) cn2.create() ss = rp_obj.ResourceProvider( self.context, name='ss', uuid=ss_uuid) ss.create() # Populate compute node inventory for VCPU and RAM for cn in (cn1, cn2): vcpu_inv = rp_obj.Inventory( self.context, resource_provider=cn, resource_class='VCPU', total=24, reserved=0, max_unit=24, min_unit=1, step_size=1, allocation_ratio=16.0) vcpu_inv.obj_set_defaults() ram_inv = rp_obj.Inventory( self.context, resource_provider=cn, resource_class='MEMORY_MB', total=128 * 1024, reserved=0, max_unit=128 * 1024, min_unit=256, step_size=256, allocation_ratio=1.5) ram_inv.obj_set_defaults() inv_list = rp_obj.InventoryList(objects=[vcpu_inv, ram_inv]) cn.set_inventory(inv_list) # Populate shared storage provider with DISK_GB inventory disk_inv = rp_obj.Inventory( self.context, resource_provider=ss, resource_class='DISK_GB', total=2000, reserved=100, max_unit=2000, min_unit=10, step_size=10, allocation_ratio=1.0) disk_inv.obj_set_defaults() inv_list = rp_obj.InventoryList(objects=[disk_inv]) ss.set_inventory(inv_list) # Mark the shared storage pool as having inventory shared among any # provider associated via aggregate t = rp_obj.Trait.get_by_name( self.context, "MISC_SHARES_VIA_AGGREGATE", ) ss.set_traits(rp_obj.TraitList(objects=[t])) # Now associate the shared storage pool and both compute nodes with the # same aggregate cn1.set_aggregates([agg_uuid]) cn2.set_aggregates([agg_uuid]) ss.set_aggregates([agg_uuid])