Merge "Remove InventoryList class"

This commit is contained in:
Zuul 2019-03-15 07:48:44 +00:00 committed by Gerrit Code Review
commit 8b33f5137a
12 changed files with 116 additions and 145 deletions

View File

@ -154,7 +154,8 @@ def _validate_inventory_capacity(version, inventories):
"""Validate inventory capacity.
:param version: request microversion.
:param inventories: Inventory or InventoryList to validate capacities of.
:param inventories: One Inventory or a list of Inventory objects to
validate capacities of.
:raises: exception.InvalidInventoryCapacityReservedCanBeTotal if request
microversion is 1.26 or higher and any inventory has capacity < 0.
:raises: exception.InvalidInventoryCapacity if request
@ -167,7 +168,7 @@ def _validate_inventory_capacity(version, inventories):
op = operator.lt
exc_class = exception.InvalidInventoryCapacityReservedCanBeTotal
if isinstance(inventories, inv_obj.Inventory):
inventories = inv_obj.InventoryList(objects=[inventories])
inventories = [inventories]
for inventory in inventories:
if op(inventory.capacity, 0):
raise exc_class(
@ -272,7 +273,7 @@ def get_inventories(req):
_("No resource provider with uuid %(uuid)s found : %(error)s") %
{'uuid': uuid, 'error': exc})
inv_list = inv_obj.InventoryList.get_all_by_resource_provider(context, rp)
inv_list = inv_obj.get_all_by_resource_provider(context, rp)
return _send_inventories(req, rp, inv_list)
@ -296,8 +297,8 @@ def get_inventory(req):
_("No resource provider with uuid %(uuid)s found : %(error)s") %
{'uuid': uuid, 'error': exc})
inv_list = inv_obj.InventoryList.get_all_by_resource_provider(context, rp)
inventory = inv_list.find(resource_class)
inv_list = inv_obj.get_all_by_resource_provider(context, rp)
inventory = inv_obj.find(inv_list, resource_class)
if not inventory:
raise webob.exc.HTTPNotFound(
@ -335,12 +336,11 @@ def set_inventories(req):
_('resource provider generation conflict'),
comment=errors.CONCURRENT_UPDATE)
inv_list = []
inventories = []
for res_class, inventory_data in data['inventories'].items():
inventory = make_inventory_object(
resource_provider, res_class, **inventory_data)
inv_list.append(inventory)
inventories = inv_obj.InventoryList(objects=inv_list)
inventories.append(inventory)
try:
_validate_inventory_capacity(
@ -391,10 +391,8 @@ def delete_inventories(req):
resource_provider = rp_obj.ResourceProvider.get_by_uuid(
context, uuid)
inventories = inv_obj.InventoryList(objects=[])
try:
resource_provider.set_inventory(inventories)
resource_provider.set_inventory([])
except exception.ConcurrentUpdateDetected:
raise webob.exc.HTTPConflict(
_('Unable to delete inventory for resource provider '

View File

@ -30,7 +30,6 @@ from placement.handlers import allocation
from placement.handlers import inventory
from placement.i18n import _
from placement import microversion
from placement.objects import inventory as inv_obj
from placement.objects import reshaper
from placement.objects import resource_provider as rp_obj
from placement.policies import reshaper as policies
@ -49,7 +48,8 @@ def reshape(req):
data = util.extract_json(req.body, schema.POST_RESHAPER_SCHEMA)
inventories = data['inventories']
allocations = data['allocations']
# We're going to create several InventoryList, by rp uuid.
# We're going to create several lists of Inventory objects, keyed by rp
# uuid.
inventory_by_rp = {}
# TODO(cdent): this has overlaps with inventory:set_inventories
@ -82,8 +82,7 @@ def reshape(req):
inv_object = inventory.make_inventory_object(
resource_provider, res_class, **inv_data)
inv_list.append(inv_object)
inventory_by_rp[resource_provider] = inv_obj.InventoryList(
objects=inv_list)
inventory_by_rp[resource_provider] = inv_list
# Make the consumer objects associated with the allocations.
consumers, new_consumers_created = allocation.inspect_consumers(

View File

@ -15,7 +15,6 @@ import sqlalchemy as sa
from placement.db.sqlalchemy import models
from placement import db_api
from placement.objects import common as common_obj
from placement import resource_class_cache as rc_cache
@ -48,43 +47,40 @@ class Inventory(object):
return int((self.total - self.reserved) * self.allocation_ratio)
class InventoryList(common_obj.ObjectList):
ITEM_CLS = Inventory
def find(inventories, res_class):
"""Return the inventory record from the list of Inventory records that
matches the supplied resource class, or None.
def find(self, res_class):
"""Return the inventory record from the list of Inventory records that
matches the supplied resource class, or None.
:param inventories: A list of Inventory objects.
:param res_class: An integer or string representing a resource
class. If the value is a string, the method first
looks up the resource class identifier from the
string.
"""
if not isinstance(res_class, six.string_types):
raise ValueError
:param res_class: An integer or string representing a resource
class. If the value is a string, the method first
looks up the resource class identifier from the
string.
"""
if not isinstance(res_class, six.string_types):
raise ValueError
for inv_rec in inventories:
if inv_rec.resource_class == res_class:
return inv_rec
for inv_rec in self.objects:
if inv_rec.resource_class == res_class:
return inv_rec
@classmethod
def get_all_by_resource_provider(cls, context, rp):
db_inv = _get_inventory_by_provider_id(context, rp.id)
# Build up a list of Inventory objects, setting the Inventory object
# fields to the same-named database record field we got from
# _get_inventory_by_provider_id(). We already have the ResourceProvider
# object so we just pass that object to the Inventory object
# constructor as-is
objs = [
Inventory(
resource_provider=rp,
resource_class=rc_cache.RC_CACHE.string_from_id(
rec['resource_class_id']),
**rec)
for rec in db_inv
]
inv_list = cls(objects=objs)
return inv_list
def get_all_by_resource_provider(context, rp):
db_inv = _get_inventory_by_provider_id(context, rp.id)
# Build up a list of Inventory objects, setting the Inventory object
# fields to the same-named database record field we got from
# _get_inventory_by_provider_id(). We already have the ResourceProvider
# object so we just pass that object to the Inventory object
# constructor as-is
inv_list = [
Inventory(
resource_provider=rp,
resource_class=rc_cache.RC_CACHE.string_from_id(
rec['resource_class_id']),
**rec)
for rec in db_inv
]
return inv_list
@db_api.placement_context_manager.reader

View File

@ -37,9 +37,9 @@ def reshape(ctx, inventories, allocations):
:param ctx: `placement.context.RequestContext` object
containing the DB transaction context.
:param inventories: dict, keyed by ResourceProvider, of `InventoryList`
objects representing the replaced inventory information
for the provider.
:param inventories: dict, keyed by ResourceProvider, of lists of
`Inventory` objects representing the replaced inventory
information for the provider.
:param allocations: `AllocationList` object containing all allocations for
all consumers being modified by the reshape operation.
:raises: `exception.ConcurrentUpdateDetected` when any resource provider or
@ -84,15 +84,14 @@ def reshape(ctx, inventories, allocations):
# with the original inventory list.
inv_by_rc = {
inv.resource_class: inv for inv in
inv_obj.InventoryList.get_all_by_resource_provider(ctx, rp)}
inv_obj.get_all_by_resource_provider(ctx, rp)}
# Now add each inventory in the new inventory list. If an inventory for
# that resource class existed in the original inventory list, it is
# overwritten.
for inv in new_inv_list:
inv_by_rc[inv.resource_class] = inv
# Set the interim inventory structure.
rp.set_inventory(
inv_obj.InventoryList(objects=list(inv_by_rc.values())))
rp.set_inventory(list(inv_by_rc.values()))
# NOTE(jaypipes): The above inventory replacements will have
# incremented the resource provider generations, so we need to look in

View File

@ -127,13 +127,13 @@ def _add_inventory_to_provider(ctx, rp, inv_list, to_add):
:param ctx: `placement.context.RequestContext` that contains an oslo_db
Session
:param rp: Resource provider to add inventory to.
:param inv_list: InventoryList object
:param inv_list: List of Inventory objects
:param to_add: set() containing resource class IDs to search inv_list for
adding to resource provider.
"""
for rc_id in to_add:
rc_str = rc_cache.RC_CACHE.string_from_id(rc_id)
inv_record = inv_list.find(rc_str)
inv_record = inv_obj.find(inv_list, rc_str)
ins_stmt = _INV_TBL.insert().values(
resource_provider_id=rp.id,
resource_class_id=rc_id,
@ -152,7 +152,7 @@ def _update_inventory_for_provider(ctx, rp, inv_list, to_update):
:param ctx: `placement.context.RequestContext` that contains an oslo_db
Session
:param rp: Resource provider on which to update inventory.
:param inv_list: InventoryList object
:param inv_list: List of Inventory objects
:param to_update: set() containing resource class IDs to search inv_list
for updating in resource provider.
:returns: A list of (uuid, class) tuples that have exceeded their
@ -161,7 +161,7 @@ def _update_inventory_for_provider(ctx, rp, inv_list, to_update):
exceeded = []
for rc_id in to_update:
rc_str = rc_cache.RC_CACHE.string_from_id(rc_id)
inv_record = inv_list.find(rc_str)
inv_record = inv_obj.find(inv_list, rc_str)
allocation_query = sa.select(
[func.sum(_ALLOC_TBL.c.used).label('usage')]).\
where(sa.and_(
@ -198,9 +198,8 @@ def _add_inventory(context, rp, inventory):
cannot be found in the DB.
"""
rc_id = rc_cache.RC_CACHE.id_from_string(inventory.resource_class)
inv_list = inv_obj.InventoryList(objects=[inventory])
_add_inventory_to_provider(
context, rp, inv_list, set([rc_id]))
context, rp, [inventory], set([rc_id]))
rp.increment_generation()
@ -212,9 +211,8 @@ def _update_inventory(context, rp, inventory):
cannot be found in the DB.
"""
rc_id = rc_cache.RC_CACHE.id_from_string(inventory.resource_class)
inv_list = inv_obj.InventoryList(objects=[inventory])
exceeded = _update_inventory_for_provider(
context, rp, inv_list, set([rc_id]))
context, rp, [inventory], set([rc_id]))
rp.increment_generation()
return exceeded
@ -236,13 +234,13 @@ def _delete_inventory(context, rp, resource_class):
@db_api.placement_context_manager.writer
def _set_inventory(context, rp, inv_list):
"""Given an InventoryList object, replaces the inventory of the
"""Given a list of Inventory objects, replaces the inventory of the
resource provider in a safe, atomic fashion using the resource
provider's generation as a consistent view marker.
:param context: Nova RequestContext.
:param rp: `ResourceProvider` object upon which to set inventory.
:param inv_list: `InventoryList` object to save to backend storage.
:param inv_list: A list of `Inventory` objects to save to backend storage.
:returns: A list of (uuid, class) tuples that have exceeded their
capacity after this inventory update.
:raises placement.exception.ConcurrentUpdateDetected: if another thread
@ -256,7 +254,7 @@ def _set_inventory(context, rp, inv_list):
"""
existing_resources = _get_current_inventory_resources(context, rp)
these_resources = set([rc_cache.RC_CACHE.id_from_string(r.resource_class)
for r in inv_list.objects])
for r in inv_list])
# Determine which resources we should be adding, deleting and/or
# updating in the resource provider's inventory by comparing sets

View File

@ -141,11 +141,11 @@ class TestAllocationListCreateDelete(tb.PlacementDbBaseTestCase):
inv1 = inv_obj.Inventory(resource_provider=rp1,
resource_class=rp1_class,
total=1024, max_unit=max_unit)
rp1.set_inventory(inv_obj.InventoryList(objects=[inv1]))
rp1.set_inventory([inv1])
inv2 = inv_obj.Inventory(resource_provider=rp2,
resource_class=rp2_class,
total=255, reserved=2, max_unit=max_unit)
rp2.set_inventory(inv_obj.InventoryList(objects=[inv2]))
rp2.set_inventory([inv2])
# Now we can finally allocate.
alloc_obj.replace_all(self.ctx, allocation_list)

View File

@ -143,8 +143,7 @@ class PlacementDbBaseTestCase(base.TestCase):
alloc_dict = copy.copy(alloc_dict)
rp = self._create_provider('allocation_resource_provider')
disk_inv = inv_obj.Inventory(resource_provider=rp, **inv_dict)
inv_list = inv_obj.InventoryList(objects=[disk_inv])
rp.set_inventory(inv_list)
rp.set_inventory([disk_inv])
consumer_id = alloc_dict.pop('consumer_id')
consumer = ensure_consumer(
self.ctx, self.user_obj, self.project_obj, consumer_id)

View File

@ -104,36 +104,36 @@ class ReshapeTestCase(tb.PlacementDbBaseTestCase):
# storage provider.
after_inventories = {
# cn1 keeps the RAM only
cn1: inv_obj.InventoryList(objects=[
cn1: [
inv_obj.Inventory(
resource_provider=cn1,
resource_class='MEMORY_MB', total=32768, reserved=0,
max_unit=32768, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
],
# each NUMA node gets half of the CPUs
cn1_numa0: inv_obj.InventoryList(objects=[
cn1_numa0: [
inv_obj.Inventory(
resource_provider=cn1_numa0,
resource_class='VCPU', total=8, reserved=0,
max_unit=8, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
cn1_numa1: inv_obj.InventoryList(objects=[
],
cn1_numa1: [
inv_obj.Inventory(
resource_provider=cn1_numa1,
resource_class='VCPU', total=8, reserved=0,
max_unit=8, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
],
# The sharing provider gets a bunch of disk
ss: inv_obj.InventoryList(objects=[
ss: [
inv_obj.Inventory(
resource_provider=ss,
resource_class='DISK_GB', total=100000, reserved=0,
max_unit=1000, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
],
}
# We do a fetch from the DB for each instance to get its latest
# generation. This would be done by the resource tracker or scheduler
@ -173,25 +173,21 @@ class ReshapeTestCase(tb.PlacementDbBaseTestCase):
# providers in the AFTER scenario
# The root compute node should only have MEMORY_MB, nothing else
cn1_inv = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, cn1)
cn1_inv = inv_obj.get_all_by_resource_provider(self.ctx, cn1)
self.assertEqual(1, len(cn1_inv))
self.assertEqual('MEMORY_MB', cn1_inv[0].resource_class)
self.assertEqual(32768, cn1_inv[0].total)
# Each NUMA node should only have half the original VCPU, nothing else
numa0_inv = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, cn1_numa0)
numa0_inv = inv_obj.get_all_by_resource_provider(self.ctx, cn1_numa0)
self.assertEqual(1, len(numa0_inv))
self.assertEqual('VCPU', numa0_inv[0].resource_class)
self.assertEqual(8, numa0_inv[0].total)
numa1_inv = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, cn1_numa1)
numa1_inv = inv_obj.get_all_by_resource_provider(self.ctx, cn1_numa1)
self.assertEqual(1, len(numa1_inv))
self.assertEqual('VCPU', numa1_inv[0].resource_class)
self.assertEqual(8, numa1_inv[0].total)
# The sharing storage provider should only have DISK_GB, nothing else
ss_inv = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, ss)
ss_inv = inv_obj.get_all_by_resource_provider(self.ctx, ss)
self.assertEqual(1, len(ss_inv))
self.assertEqual('DISK_GB', ss_inv[0].resource_class)
self.assertEqual(100000, ss_inv[0].total)
@ -280,36 +276,36 @@ class ReshapeTestCase(tb.PlacementDbBaseTestCase):
# storage provider.
after_inventories = {
# cn1 keeps the RAM only
cn1: inv_obj.InventoryList(objects=[
cn1: [
inv_obj.Inventory(
resource_provider=cn1,
resource_class='MEMORY_MB', total=32768, reserved=0,
max_unit=32768, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
],
# each NUMA node gets half of the CPUs
cn1_numa0: inv_obj.InventoryList(objects=[
cn1_numa0: [
inv_obj.Inventory(
resource_provider=cn1_numa0,
resource_class='VCPU', total=8, reserved=0,
max_unit=8, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
cn1_numa1: inv_obj.InventoryList(objects=[
],
cn1_numa1: [
inv_obj.Inventory(
resource_provider=cn1_numa1,
resource_class='VCPU', total=8, reserved=0,
max_unit=8, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
],
# The sharing provider gets a bunch of disk
ss: inv_obj.InventoryList(objects=[
ss: [
inv_obj.Inventory(
resource_provider=ss,
resource_class='DISK_GB', total=100000, reserved=0,
max_unit=1000, min_unit=1, step_size=1,
allocation_ratio=1.0),
]),
],
}
# We do a fetch from the DB for each instance to get its latest
# generation. This would be done by the resource tracker or scheduler
@ -338,11 +334,11 @@ class ReshapeTestCase(tb.PlacementDbBaseTestCase):
# generation was validated and the actual call to reshape()
ss_threadB = rp_obj.ResourceProvider.get_by_uuid(self.ctx, ss.uuid)
# Reduce the amount of storage to 2000, from 100000.
new_ss_inv = inv_obj.InventoryList(objects=[
new_ss_inv = [
inv_obj.Inventory(
resource_provider=ss_threadB, resource_class='DISK_GB',
total=2000, reserved=0, max_unit=1000, min_unit=1, step_size=1,
allocation_ratio=1.0)])
allocation_ratio=1.0)]
ss_threadB.set_inventory(new_ss_inv)
# Double check our storage provider's generation is now greater than
# the original storage provider record being sent to reshape()

View File

@ -227,13 +227,12 @@ class ResourceClassTestCase(tb.PlacementDbBaseTestCase):
resource_class='CUSTOM_IRON_NFV',
total=1,
)
inv_list = inv_obj.InventoryList(objects=[inv])
rp.set_inventory(inv_list)
rp.set_inventory([inv])
self.assertRaises(exception.ResourceClassInUse,
rc.destroy)
rp.set_inventory(inv_obj.InventoryList(objects=[]))
rp.set_inventory([])
rc.destroy()
rc_list = rc_obj.get_all(self.ctx)
rc_ids = (r.id for r in rc_list)

View File

@ -524,11 +524,11 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
tb.add_inventory(resource_provider,
tb.DISK_INVENTORY['resource_class'],
tb.DISK_INVENTORY['total'])
inventories = inv_obj.InventoryList.get_all_by_resource_provider(
inventories = inv_obj.get_all_by_resource_provider(
self.ctx, resource_provider)
self.assertEqual(1, len(inventories))
resource_provider.destroy()
inventories = inv_obj.InventoryList.get_all_by_resource_provider(
inventories = inv_obj.get_all_by_resource_provider(
self.ctx, resource_provider)
self.assertEqual(0, len(inventories))
@ -588,10 +588,9 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
allocation_ratio=1.0,
)
inv_list = inv_obj.InventoryList(objects=[inv])
self.assertRaises(exception.InventoryInUse,
rp.set_inventory,
inv_list)
[inv])
@mock.patch('placement.objects.resource_provider.LOG')
def test_set_inventory_over_capacity(self, mock_log):
@ -612,7 +611,7 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
# Update our inventory to over-subscribe us after the above allocation
disk_inv.total = 400
rp.set_inventory(inv_obj.InventoryList(objects=[disk_inv, vcpu_inv]))
rp.set_inventory([disk_inv, vcpu_inv])
# We should succeed, but have logged a warning for going over on disk
mock_log.warning.assert_called_once_with(
@ -635,23 +634,20 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
self.assertEqual(saved_generation + 2, rp.generation)
saved_generation = rp.generation
new_inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
new_inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
self.assertEqual(2, len(new_inv_list))
resource_classes = [inv.resource_class for inv in new_inv_list]
self.assertIn(orc.VCPU, resource_classes)
self.assertIn(orc.DISK_GB, resource_classes)
# reset list to just disk_inv
inv_list = inv_obj.InventoryList(objects=[disk_inv])
rp.set_inventory(inv_list)
# reset inventory to just disk_inv
rp.set_inventory([disk_inv])
# generation has bumped
self.assertEqual(saved_generation + 1, rp.generation)
saved_generation = rp.generation
new_inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
new_inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
self.assertEqual(1, len(new_inv_list))
resource_classes = [inv.resource_class for inv in new_inv_list]
self.assertNotIn(orc.VCPU, resource_classes)
@ -674,8 +670,7 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
self.assertEqual(saved_generation + 1, rp.generation)
saved_generation = rp.generation
new_inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
new_inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
self.assertEqual(1, len(new_inv_list))
self.assertEqual(2048, new_inv_list[0].total)
@ -686,22 +681,19 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
self.assertEqual(saved_generation + 1, rp.generation)
saved_generation = rp.generation
new_inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
result = new_inv_list.find(orc.DISK_GB)
new_inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
result = inv_obj.find(new_inv_list, orc.DISK_GB)
self.assertIsNone(result)
self.assertRaises(exception.NotFound, rp.delete_inventory,
orc.DISK_GB)
# check inventory list is empty
inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
self.assertEqual(0, len(inv_list))
# add some inventory
rp.add_inventory(vcpu_inv)
inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
self.assertEqual(1, len(inv_list))
# generation has bumped
@ -766,8 +758,7 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
self.ctx, rp.uuid)
self.assertEqual(allocation.used, usages[0].usage)
inv_list = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp)
inv_list = inv_obj.get_all_by_resource_provider(self.ctx, rp)
self.assertEqual(new_total, inv_list[0].total)
mock_log.warning.assert_called_once_with(
mock.ANY, {'uuid': rp.uuid, 'resource': 'DISK_GB'})
@ -793,8 +784,7 @@ class ResourceProviderTestCase(tb.PlacementDbBaseTestCase):
# Get inventories for the first resource provider and validate
# the inventory records have a matching resource provider
got_inv = inv_obj.InventoryList.get_all_by_resource_provider(
self.ctx, rp1)
got_inv = inv_obj.get_all_by_resource_provider(self.ctx, rp1)
for inv in got_inv:
self.assertEqual(rp1.id, inv.resource_provider.id)

View File

@ -34,8 +34,7 @@ class UsageListTestCase(tb.PlacementDbBaseTestCase):
inv = inv_obj.Inventory(resource_provider=db_rp,
resource_class=orc.DISK_GB,
total=1024)
inv_list = inv_obj.InventoryList(objects=[inv])
db_rp.set_inventory(inv_list)
db_rp.set_inventory([inv])
usages = usage_obj.get_all_by_resource_provider_uuid(
self.ctx, db_rp.uuid)

View File

@ -60,8 +60,7 @@ class TestInventoryNoDB(base.TestCase):
rp = resource_provider.ResourceProvider(self.context,
id=_RESOURCE_PROVIDER_ID,
uuid=_RESOURCE_PROVIDER_UUID)
objs = inventory.InventoryList.get_all_by_resource_provider(
self.context, rp)
objs = inventory.get_all_by_resource_provider(self.context, rp)
self.assertEqual(2, len(objs))
self.assertEqual(_INVENTORY_DB['id'], objs[0].id)
self.assertEqual(_INVENTORY_DB['id'] + 1, objs[1].id)
@ -99,36 +98,35 @@ class TestInventoryNoDB(base.TestCase):
self.assertEqual(2, inv.capacity)
class TestInventoryList(base.TestCase):
class TestListOfInventory(base.TestCase):
def test_find(self):
rp = resource_provider.ResourceProvider(
self.context, uuid=uuids.rp_uuid)
inv_list = inventory.InventoryList(
objects=[
inventory.Inventory(
resource_provider=rp,
resource_class=orc.VCPU,
total=24),
inventory.Inventory(
resource_provider=rp,
resource_class=orc.MEMORY_MB,
total=10240),
])
inv_list = [
inventory.Inventory(
resource_provider=rp,
resource_class=orc.VCPU,
total=24),
inventory.Inventory(
resource_provider=rp,
resource_class=orc.MEMORY_MB,
total=10240),
]
found = inv_list.find(orc.MEMORY_MB)
found = inventory.find(inv_list, orc.MEMORY_MB)
self.assertIsNotNone(found)
self.assertEqual(10240, found.total)
found = inv_list.find(orc.VCPU)
found = inventory.find(inv_list, orc.VCPU)
self.assertIsNotNone(found)
self.assertEqual(24, found.total)
found = inv_list.find(orc.DISK_GB)
found = inventory.find(inv_list, orc.DISK_GB)
self.assertIsNone(found)
# Try an integer resource class identifier...
self.assertRaises(ValueError, inv_list.find, VCPU_ID)
self.assertRaises(ValueError, inventory.find, inv_list, VCPU_ID)
# Use an invalid string...
self.assertIsNone(inv_list.find('HOUSE'))
self.assertIsNone(inventory.find(inv_list, 'HOUSE'))