Merge "Refactor AllocationFixture in placement test"

This commit is contained in:
Zuul 2018-08-07 00:46:16 +00:00 committed by Gerrit Code Review
commit 954038d795
2 changed files with 62 additions and 98 deletions

View File

@ -60,6 +60,31 @@ def set_traits(rp, *traits):
return tlist return tlist
def ensure_consumer(ctx, user, project, consumer_id=None):
# NOTE(efried): If not specified, use a random consumer UUID - we don't
# want to override any existing allocations from the test case.
consumer_id = consumer_id or uuidutils.generate_uuid()
try:
consumer = consumer_obj.Consumer.get_by_uuid(ctx, consumer_id)
except exception.NotFound:
consumer = consumer_obj.Consumer(
ctx, uuid=consumer_id, user=user, project=project)
consumer.create()
return consumer
def set_allocation(ctx, rp, consumer, rc_used_dict):
alloc = [
rp_obj.Allocation(
ctx, resource_provider=rp, resource_class=rc,
consumer=consumer, used=used)
for rc, used in rc_used_dict.items()
]
alloc_list = rp_obj.AllocationList(ctx, objects=alloc)
alloc_list.replace_all()
return alloc_list
class PlacementDbBaseTestCase(base.TestCase): class PlacementDbBaseTestCase(base.TestCase):
def setUp(self): def setUp(self):
@ -83,25 +108,10 @@ class PlacementDbBaseTestCase(base.TestCase):
def allocate_from_provider(self, rp, rc, used, consumer_id=None, def allocate_from_provider(self, rp, rc, used, consumer_id=None,
consumer=None): consumer=None):
# NOTE(efried): If not specified, use a random consumer UUID - we don't
# want to override any existing allocations from the test case.
consumer_id = consumer_id or uuidutils.generate_uuid()
if consumer is None: if consumer is None:
try: consumer = ensure_consumer(
consumer = consumer_obj.Consumer.get_by_uuid( self.ctx, self.user_obj, self.project_obj, consumer_id)
self.ctx, consumer_id) alloc_list = set_allocation(self.ctx, rp, consumer, {rc: used})
except exception.NotFound:
consumer = consumer_obj.Consumer(
self.ctx, uuid=consumer_id, user=self.user_obj,
project=self.project_obj)
consumer.create()
alloc_list = rp_obj.AllocationList(
self.ctx, objects=[
rp_obj.Allocation(
self.ctx, resource_provider=rp, resource_class=rc,
consumer=consumer, used=used)]
)
alloc_list.replace_all()
return alloc_list return alloc_list
def _make_allocation(self, inv_dict, alloc_dict): def _make_allocation(self, inv_dict, alloc_dict):
@ -111,15 +121,10 @@ class PlacementDbBaseTestCase(base.TestCase):
inv_list = rp_obj.InventoryList(objects=[disk_inv]) inv_list = rp_obj.InventoryList(objects=[disk_inv])
rp.set_inventory(inv_list) rp.set_inventory(inv_list)
consumer_id = alloc_dict['consumer_id'] consumer_id = alloc_dict['consumer_id']
try: consumer = ensure_consumer(
c = consumer_obj.Consumer.get_by_uuid(self.ctx, consumer_id) self.ctx, self.user_obj, self.project_obj, consumer_id)
except exception.NotFound:
c = consumer_obj.Consumer(
self.ctx, uuid=consumer_id, user=self.user_obj,
project=self.project_obj)
c.create()
alloc = rp_obj.Allocation(self.ctx, resource_provider=rp, alloc = rp_obj.Allocation(self.ctx, resource_provider=rp,
consumer=c, **alloc_dict) consumer=consumer, **alloc_dict)
alloc_list = rp_obj.AllocationList(self.ctx, objects=[alloc]) alloc_list = rp_obj.AllocationList(self.ctx, objects=[alloc])
alloc_list.replace_all() alloc_list.replace_all()
return rp, alloc return rp, alloc

View File

@ -22,7 +22,6 @@ from oslotest import output
from nova.api.openstack.placement import context from nova.api.openstack.placement import context
from nova.api.openstack.placement import deploy from nova.api.openstack.placement import deploy
from nova.api.openstack.placement import exception from nova.api.openstack.placement import exception
from nova.api.openstack.placement.objects import consumer as consumer_obj
from nova.api.openstack.placement.objects import project as project_obj from nova.api.openstack.placement.objects import project as project_obj
from nova.api.openstack.placement.objects import resource_provider as rp_obj from nova.api.openstack.placement.objects import resource_provider as rp_obj
from nova.api.openstack.placement.objects import user as user_obj from nova.api.openstack.placement.objects import user as user_obj
@ -123,9 +122,22 @@ class APIFixture(fixture.GabbiFixture):
class AllocationFixture(APIFixture): class AllocationFixture(APIFixture):
"""An APIFixture that has some pre-made Allocations.""" """An APIFixture that has some pre-made Allocations.
# TODO(jaypipes): Simplify and restructure this fixture +----- same user----+ alt_user
| | |
+----+----------+ +------+-----+ +-----+---------+
| consumer1 | | consumer2 | | alt_consumer |
| DISK_GB:1000 | | VCPU: 6 | | VCPU: 1 |
| | | | | DISK_GB:20 |
+-------------+-+ +------+-----+ +-+-------------+
| | |
+-+----------+---------+-+
| rp |
| VCPU: 10 |
| DISK_GB:2048 |
+------------------------+
"""
def start_fixture(self): def start_fixture(self):
super(AllocationFixture, self).start_fixture() super(AllocationFixture, self).start_fixture()
@ -145,82 +157,29 @@ class AllocationFixture(APIFixture):
# Stealing from the super # Stealing from the super
rp_name = os.environ['RP_NAME'] rp_name = os.environ['RP_NAME']
rp_uuid = os.environ['RP_UUID'] rp_uuid = os.environ['RP_UUID']
rp = rp_obj.ResourceProvider( # Create the rp with VCPU and DISK_GB inventory
self.context, name=rp_name, uuid=rp_uuid) rp = tb.create_provider(self.context, rp_name, uuid=rp_uuid)
rp.create() tb.add_inventory(rp, 'DISK_GB', 2048,
step_size=10, min_unit=10, max_unit=1000)
tb.add_inventory(rp, 'VCPU', 10, max_unit=10)
# Create a first consumer for the DISK_GB # Create a first consumer for the DISK_GB allocations
consumer_id = uuidutils.generate_uuid() consumer1 = tb.ensure_consumer(self.context, user, project)
consumer = consumer_obj.Consumer( tb.set_allocation(self.context, rp, consumer1, {'DISK_GB': 1000})
self.context, uuid=consumer_id, user=user, project=project)
consumer.create()
# Create some DISK_GB inventory and allocations. # Create a second consumer for the VCPU allocations
inventory = rp_obj.Inventory( consumer2 = tb.ensure_consumer(self.context, user, project)
self.context, resource_provider=rp, tb.set_allocation(self.context, rp, consumer2, {'VCPU': 6})
resource_class='DISK_GB', total=2048,
step_size=10, min_unit=10, max_unit=1000)
inventory.obj_set_defaults()
rp.add_inventory(inventory)
alloc = rp_obj.Allocation(
self.context, resource_provider=rp,
resource_class='DISK_GB',
consumer=consumer,
used=1000)
alloc_list = rp_obj.AllocationList(
self.context,
objects=[alloc]
)
alloc_list.replace_all()
# Create a second consumer for the VCPU
consumer_id = uuidutils.generate_uuid()
consumer = consumer_obj.Consumer(
self.context, uuid=consumer_id, user=user, project=project)
consumer.create()
# This consumer is referenced from the gabbits # This consumer is referenced from the gabbits
os.environ['CONSUMER_ID'] = consumer_id os.environ['CONSUMER_ID'] = consumer2.uuid
# Create some VCPU inventory and allocations.
inventory = rp_obj.Inventory(
self.context, resource_provider=rp,
resource_class='VCPU', total=10,
max_unit=10)
inventory.obj_set_defaults()
rp.add_inventory(inventory)
alloc = rp_obj.Allocation(
self.context, resource_provider=rp,
resource_class='VCPU',
consumer=consumer,
used=6)
alloc_list = rp_obj.AllocationList(
self.context,
objects=[alloc])
alloc_list.replace_all()
# Create a consumer object for a different user # Create a consumer object for a different user
alt_consumer_id = uuidutils.generate_uuid() alt_consumer = tb.ensure_consumer(self.context, alt_user, project)
alt_consumer = consumer_obj.Consumer( os.environ['ALT_CONSUMER_ID'] = alt_consumer.uuid
self.context, uuid=alt_consumer_id, user=alt_user,
project=project)
alt_consumer.create()
os.environ['ALT_CONSUMER_ID'] = alt_consumer_id
# Create a couple of allocations for a different user. # Create a couple of allocations for a different user.
alloc1 = rp_obj.Allocation( tb.set_allocation(self.context, rp, alt_consumer,
self.context, resource_provider=rp, {'DISK_GB': 20, 'VCPU': 1})
resource_class='DISK_GB',
consumer=alt_consumer,
used=20)
alloc2 = rp_obj.Allocation(
self.context, resource_provider=rp,
resource_class='VCPU',
consumer=alt_consumer,
used=1)
alloc_list = rp_obj.AllocationList(
self.context,
objects=[alloc1, alloc2])
alloc_list.replace_all()
# The ALT_RP_XXX variables are for a resource provider that has # The ALT_RP_XXX variables are for a resource provider that has
# not been created in the Allocation fixture # not been created in the Allocation fixture