From 0ea8f1b05d4aa4a6f144adff08771f5b84bda479 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Mon, 6 Sep 2021 11:49:51 +0200 Subject: [PATCH] Fix allocation show / unset with empty allocation Add a guards for empty allocations in the allocation parsing code of allocation show and allocation unset. Closes-Bug: #1942740 Change-Id: Ic0e6e981d6602a76935f6bc6d9ffb0a707a5b1a9 --- osc_placement/resources/allocation.py | 8 ++--- .../tests/functional/test_allocation.py | 32 ++++--------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/osc_placement/resources/allocation.py b/osc_placement/resources/allocation.py index cc2f951..34a6630 100644 --- a/osc_placement/resources/allocation.py +++ b/osc_placement/resources/allocation.py @@ -291,7 +291,7 @@ class UnsetAllocation(command.Lister, version.CheckerMixin): 'project_id', 'user_id') if self.compare_version(version.ge('1.38')): fields += ('consumer_type',) - props['consumer_type'] = resp['consumer_type'] + props['consumer_type'] = resp.get('consumer_type') allocs = [dict(project_id=resp['project_id'], user_id=resp['user_id'], resource_provider=k, **props, **v) for k, v in per_provider] @@ -332,11 +332,11 @@ class ShowAllocation(command.Lister, version.CheckerMixin): fields = ('resource_provider', 'generation', 'resources') if self.compare_version(version.ge('1.12')): fields += ('project_id', 'user_id') - props['project_id'] = resp['project_id'] - props['user_id'] = resp['user_id'] + props['project_id'] = resp.get('project_id') + props['user_id'] = resp.get('user_id') if self.compare_version(version.ge('1.38')): fields += ('consumer_type',) - props['consumer_type'] = resp['consumer_type'] + props['consumer_type'] = resp.get('consumer_type') allocs = [dict(resource_provider=k, **props, **v) for k, v in per_provider] diff --git a/osc_placement/tests/functional/test_allocation.py b/osc_placement/tests/functional/test_allocation.py index fee126e..6925060 100644 --- a/osc_placement/tests/functional/test_allocation.py +++ b/osc_placement/tests/functional/test_allocation.py @@ -198,19 +198,9 @@ class TestAllocation112(base.BaseTestCase): 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) + alloc = self.resource_allocation_show( + str(uuid.uuid4()), columns=('resources',)) + self.assertEqual([], alloc) class TestAllocation128(TestAllocation112): @@ -317,19 +307,9 @@ class TestAllocation138(TestAllocation128): 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) + result = self.resource_allocation_unset( + consumer_uuid, columns=('resources', 'consumer_type')) + self.assertEqual([], result) class TestAllocationUnsetOldVersion(base.BaseTestCase):