Refactor AllocationFixture in placement test

This patch refactors the allocation fixture used in some placement
api tests by substituting common functions in test_base.py for the
existing object management functions.

Change-Id: Ide6544d1cf9e1ed154b42075acdd7af986c3afe8
This commit is contained in:
Tetsuro Nakamura 2018-08-02 12:00:23 +09:00
parent e4923abaeb
commit 7d824e6d37
2 changed files with 62 additions and 98 deletions

View File

@ -60,6 +60,31 @@ def set_traits(rp, *traits):
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):
def setUp(self):
@ -83,25 +108,10 @@ class PlacementDbBaseTestCase(base.TestCase):
def allocate_from_provider(self, rp, rc, used, consumer_id=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:
try:
consumer = consumer_obj.Consumer.get_by_uuid(
self.ctx, consumer_id)
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()
consumer = ensure_consumer(
self.ctx, self.user_obj, self.project_obj, consumer_id)
alloc_list = set_allocation(self.ctx, rp, consumer, {rc: used})
return alloc_list
def _make_allocation(self, inv_dict, alloc_dict):
@ -111,15 +121,10 @@ class PlacementDbBaseTestCase(base.TestCase):
inv_list = rp_obj.InventoryList(objects=[disk_inv])
rp.set_inventory(inv_list)
consumer_id = alloc_dict['consumer_id']
try:
c = consumer_obj.Consumer.get_by_uuid(self.ctx, consumer_id)
except exception.NotFound:
c = consumer_obj.Consumer(
self.ctx, uuid=consumer_id, user=self.user_obj,
project=self.project_obj)
c.create()
consumer = ensure_consumer(
self.ctx, self.user_obj, self.project_obj, consumer_id)
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.replace_all()
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 deploy
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 resource_provider as rp_obj
from nova.api.openstack.placement.objects import user as user_obj
@ -123,9 +122,22 @@ class APIFixture(fixture.GabbiFixture):
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):
super(AllocationFixture, self).start_fixture()
@ -145,82 +157,29 @@ class AllocationFixture(APIFixture):
# Stealing from the super
rp_name = os.environ['RP_NAME']
rp_uuid = os.environ['RP_UUID']
rp = rp_obj.ResourceProvider(
self.context, name=rp_name, uuid=rp_uuid)
rp.create()
# Create the rp with VCPU and DISK_GB inventory
rp = tb.create_provider(self.context, rp_name, uuid=rp_uuid)
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
consumer_id = uuidutils.generate_uuid()
consumer = consumer_obj.Consumer(
self.context, uuid=consumer_id, user=user, project=project)
consumer.create()
# Create a first consumer for the DISK_GB allocations
consumer1 = tb.ensure_consumer(self.context, user, project)
tb.set_allocation(self.context, rp, consumer1, {'DISK_GB': 1000})
# Create some DISK_GB inventory and allocations.
inventory = rp_obj.Inventory(
self.context, resource_provider=rp,
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()
# Create a second consumer for the VCPU allocations
consumer2 = tb.ensure_consumer(self.context, user, project)
tb.set_allocation(self.context, rp, consumer2, {'VCPU': 6})
# This consumer is referenced from the gabbits
os.environ['CONSUMER_ID'] = consumer_id
# 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()
os.environ['CONSUMER_ID'] = consumer2.uuid
# Create a consumer object for a different user
alt_consumer_id = uuidutils.generate_uuid()
alt_consumer = consumer_obj.Consumer(
self.context, uuid=alt_consumer_id, user=alt_user,
project=project)
alt_consumer.create()
os.environ['ALT_CONSUMER_ID'] = alt_consumer_id
alt_consumer = tb.ensure_consumer(self.context, alt_user, project)
os.environ['ALT_CONSUMER_ID'] = alt_consumer.uuid
# Create a couple of allocations for a different user.
alloc1 = rp_obj.Allocation(
self.context, resource_provider=rp,
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()
tb.set_allocation(self.context, rp, alt_consumer,
{'DISK_GB': 20, 'VCPU': 1})
# The ALT_RP_XXX variables are for a resource provider that has
# not been created in the Allocation fixture