From 2bb527b06dfcc0140b7295713559201f09557738 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Thu, 25 Aug 2016 13:56:30 +0000 Subject: [PATCH] In InventoryList.find() raise NotFound if invalid resource class If InventoryList.find() was provided with a string of a resource class that does not exist it would cause an uncaught ValueError. This would eventually raise up to the Placement API as a 500 in response, for example, to 'GET /resource_providers/{uuid}/inventories/HOUSE' NotFound makes sense at both the object and HTTP levels. A unit test for the object level has been added and a gabbi test for the HTTP level. Note: this was discovered while developing some test client code for the placement API. A simple, but wrong, argument was passed through the stack and caused a 500. Change-Id: I0d4066a3f213f726a92d673986b702e08bda4459 Partially-Implements: blueprint generic-resource-pools --- nova/objects/resource_provider.py | 6 +++++- .../api/openstack/placement/gabbits/inventory.yaml | 4 ++++ nova/tests/unit/objects/test_resource_provider.py | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/nova/objects/resource_provider.py b/nova/objects/resource_provider.py index 73db19e238..03b04ebb34 100644 --- a/nova/objects/resource_provider.py +++ b/nova/objects/resource_provider.py @@ -544,7 +544,11 @@ class InventoryList(base.ObjectListBase, base.NovaObject): string. """ if isinstance(res_class, six.string_types): - res_class = fields.ResourceClass.index(res_class) + try: + res_class = fields.ResourceClass.index(res_class) + except ValueError: + raise exception.NotFound("No such resource class '%s'" % + res_class) for inv_rec in self.objects: if fields.ResourceClass.index(inv_rec.resource_class) == res_class: diff --git a/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml b/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml index b21a6d13ea..e594c3f370 100644 --- a/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml +++ b/nova/tests/functional/api/openstack/placement/gabbits/inventory.yaml @@ -239,6 +239,10 @@ tests: GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/IPV4_ADDRESS status: 404 +- name: get invalid inventory class + GET: /resource_providers/$ENVIRON['RP_UUID']/inventories/HOUSE + status: 404 + - name: create another resource provider POST: /resource_providers request_headers: diff --git a/nova/tests/unit/objects/test_resource_provider.py b/nova/tests/unit/objects/test_resource_provider.py index 1dc4813f5a..910b3f556b 100644 --- a/nova/tests/unit/objects/test_resource_provider.py +++ b/nova/tests/unit/objects/test_resource_provider.py @@ -435,6 +435,12 @@ class TestInventory(test_objects._LocalTest): self.assertIsNotNone(found) self.assertEqual(24, found.total) + # Use an invalid string... + error = self.assertRaises(exception.NotFound, + inv_list.find, + 'HOUSE') + self.assertIn('No such resource class', str(error)) + class _TestAllocationNoDB(object): @mock.patch('nova.objects.Allocation._create_in_db',