Repro allocation show bug with empty allocation

This patch adds a functional test case to reproduce the bug when an
allocation show command on an empty (non existent) allocation causes
KeyError as the code expects project_id in the response but the
response is {"allocations":{}}. Also similarly for allocation unset
where microversion 1.38 fails on parsing out consumer_type from the
empty allocation too.

Related-Bug: #1942740

Change-Id: Iea27c9376480e3e94d12eec56b466331e5c6f171
This commit is contained in:
Balazs Gibizer 2021-09-06 11:00:58 +02:00
parent 5e38a59317
commit 087dd64dd2
2 changed files with 70 additions and 7 deletions

View File

@ -228,11 +228,10 @@ class BaseTestCase(base.BaseTestCase):
def resource_provider_delete(self, uuid): def resource_provider_delete(self, uuid):
return self.openstack('resource provider delete ' + uuid) return self.openstack('resource provider delete ' + uuid)
def resource_allocation_show(self, consumer_uuid): def resource_allocation_show(self, consumer_uuid, columns=()):
return self.openstack( cmd = 'resource provider allocation show ' + consumer_uuid
'resource provider allocation show ' + consumer_uuid, cmd += ' '.join(' --column %s' % c for c in columns)
use_json=True return self.openstack(cmd, use_json=True)
)
def resource_allocation_set(self, consumer_uuid, allocations, def resource_allocation_set(self, consumer_uuid, allocations,
project_id=None, user_id=None, project_id=None, user_id=None,
@ -262,8 +261,10 @@ class BaseTestCase(base.BaseTestCase):
return result return result
def resource_allocation_unset(self, consumer_uuid, provider=None, def resource_allocation_unset(
resource_class=None, use_json=True): self, consumer_uuid, provider=None, resource_class=None, use_json=True,
columns=(),
):
cmd = 'resource provider allocation unset %s' % consumer_uuid cmd = 'resource provider allocation unset %s' % consumer_uuid
if resource_class: if resource_class:
cmd += ' ' + ' '.join( cmd += ' ' + ' '.join(
@ -275,6 +276,9 @@ class BaseTestCase(base.BaseTestCase):
provider = [provider] provider = [provider]
cmd += ' ' + ' '.join( cmd += ' ' + ' '.join(
'--provider %s' % rp_uuid for rp_uuid in provider) '--provider %s' % rp_uuid for rp_uuid in provider)
cmd += ' '.join(' --column %s' % c for c in columns)
result = self.openstack(cmd, use_json=use_json) result = self.openstack(cmd, use_json=use_json)
def cleanup(uuid): def cleanup(uuid):

View File

@ -180,6 +180,38 @@ class TestAllocation112(base.BaseTestCase):
self.assertEqual(expected, created_alloc) self.assertEqual(expected, created_alloc)
self.assertEqual(expected, retrieved_alloc) self.assertEqual(expected, retrieved_alloc)
def test_allocation_update_to_empty(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid'])],
project_id=project_uuid,
user_id=user_uuid,
)
result = self.resource_allocation_unset(
consumer_uuid, columns=("resources",))
self.assertEqual([], result)
def test_allocation_show_empty(self):
# FIXME(gibi): this is bug https://bugs.launchpad.net/nova/+bug/1942740
# the command fails with KeyError on project_id when the allocation is
# empty
self.assertCommandFailed(
'project_id',
self.resource_allocation_show,
str(uuid.uuid4()),
columns=("resources",),
)
# it should be
# alloc = self.resource_allocation_show(
# str(uuid.uuid4()), columns=("resources",))
# self.assertEqual([], alloc)
class TestAllocation128(TestAllocation112): class TestAllocation128(TestAllocation112):
"""Tests allocation set command with --os-placement-api-version 1.28. """Tests allocation set command with --os-placement-api-version 1.28.
@ -272,6 +304,33 @@ class TestAllocation138(TestAllocation128):
}) })
self.assertEqual(expected, updated_alloc) self.assertEqual(expected, updated_alloc)
def test_allocation_update_to_empty(self):
consumer_uuid = str(uuid.uuid4())
project_uuid = str(uuid.uuid4())
user_uuid = str(uuid.uuid4())
self.resource_allocation_set(
consumer_uuid,
['rp={},VCPU=2'.format(self.rp1['uuid'])],
project_id=project_uuid,
user_id=user_uuid,
consumer_type="INSTANCE",
)
# FIXME(gibi): This is bug https://bugs.launchpad.net/nova/+bug/1942740
# The command fails with a KeyError as it tries to parse the
# consumer_type out from and empty allocation
self.assertCommandFailed(
"consumer_type",
self.resource_allocation_unset,
consumer_uuid,
columns=('resources', 'consumer_type')
)
# it should be
# result = self.resource_allocation_unset(
# consumer_uuid, columns=('resources', 'consumer_type'))
# self.assertEqual([], result)
class TestAllocationUnsetOldVersion(base.BaseTestCase): class TestAllocationUnsetOldVersion(base.BaseTestCase):