From 980ac0e981de248f9d687b2dd91ea93f5ebdae6a Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 23 Jun 2011 16:10:26 -0400 Subject: Fixed issue with zero flavors returning HTTP 500 --- nova/api/openstack/flavors.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nova/api/openstack/flavors.py b/nova/api/openstack/flavors.py index a21ff6cb2..be295b372 100644 --- a/nova/api/openstack/flavors.py +++ b/nova/api/openstack/flavors.py @@ -42,7 +42,10 @@ class Controller(object): def _get_flavors(self, req, is_detail=True): """Helper function that returns a list of flavor dicts.""" ctxt = req.environ['nova.context'] - flavors = db.api.instance_type_get_all(ctxt) + try: + flavors = db.api.instance_type_get_all(ctxt) + except exception.NoInstanceTypesFound: + flavors = {} builder = self._get_view_builder(req) items = [builder.build(flavor, is_detail=is_detail) for flavor in flavors.values()] -- cgit From c2cc453a6912bc37e2e9a9c5f5a3c2830b61f5be Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 23 Jun 2011 16:55:45 -0400 Subject: added tests --- nova/tests/api/openstack/test_flavors.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index d1c62e454..34c627ce0 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -87,6 +87,20 @@ class FlavorsTest(test.TestCase): ] self.assertEqual(flavors, expected) + def test_get_empty_flavor_list_v1_0(self): + def _throw_NoInstanceTypesFound(self): + raise exception.NoInstanceTypesFound + + self.stubs.Set(nova.db.api, "instance_type_get_all", + _throw_NoInstanceTypesFound) + + req = webob.Request.blank('/v1.0/flavors') + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavors = json.loads(res.body)["flavors"] + expected = [] + self.assertEqual(flavors, expected) + def test_get_flavor_list_detail_v1_0(self): req = webob.Request.blank('/v1.0/flavors/detail') res = req.get_response(fakes.wsgi_app()) @@ -261,3 +275,17 @@ class FlavorsTest(test.TestCase): }, ] self.assertEqual(flavor, expected) + + def test_get_empty_flavor_list_v1_1(self): + def _throw_NoInstanceTypesFound(self): + raise exception.NoInstanceTypesFound + + self.stubs.Set(nova.db.api, "instance_type_get_all", + _throw_NoInstanceTypesFound) + + req = webob.Request.blank('/v1.1/flavors') + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + flavors = json.loads(res.body)["flavors"] + expected = [] + self.assertEqual(flavors, expected) -- cgit From d75e776815f2fc7710ba9d937985787b49a9947f Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 23 Jun 2011 22:21:10 -0400 Subject: pep8 --- nova/tests/api/openstack/test_flavors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index 34c627ce0..3c4e70e1a 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -93,7 +93,7 @@ class FlavorsTest(test.TestCase): self.stubs.Set(nova.db.api, "instance_type_get_all", _throw_NoInstanceTypesFound) - + req = webob.Request.blank('/v1.0/flavors') res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) @@ -282,7 +282,7 @@ class FlavorsTest(test.TestCase): self.stubs.Set(nova.db.api, "instance_type_get_all", _throw_NoInstanceTypesFound) - + req = webob.Request.blank('/v1.1/flavors') res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) -- cgit From c12861f6068ea18156ff9c395ed40791585032d7 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 30 Jun 2011 09:28:21 -0400 Subject: refactored instance type code --- nova/api/openstack/flavors.py | 5 +---- nova/db/sqlalchemy/api.py | 6 ++---- nova/tests/api/openstack/test_flavors.py | 14 ++++++-------- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/nova/api/openstack/flavors.py b/nova/api/openstack/flavors.py index be295b372..a21ff6cb2 100644 --- a/nova/api/openstack/flavors.py +++ b/nova/api/openstack/flavors.py @@ -42,10 +42,7 @@ class Controller(object): def _get_flavors(self, req, is_detail=True): """Helper function that returns a list of flavor dicts.""" ctxt = req.environ['nova.context'] - try: - flavors = db.api.instance_type_get_all(ctxt) - except exception.NoInstanceTypesFound: - flavors = {} + flavors = db.api.instance_type_get_all(ctxt) builder = self._get_view_builder(req) items = [builder.build(flavor, is_detail=is_detail) for flavor in flavors.values()] diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index a2500a38d..e23bd0a5d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2581,13 +2581,11 @@ def instance_type_get_all(context, inactive=False): filter_by(deleted=False).\ order_by("name").\ all() + inst_dict = {} if inst_types: - inst_dict = {} for i in inst_types: inst_dict[i['name']] = dict(i) - return inst_dict - else: - raise exception.NoInstanceTypesFound() + return inst_dict @require_context diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index 3c4e70e1a..fba4d593a 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -88,11 +88,10 @@ class FlavorsTest(test.TestCase): self.assertEqual(flavors, expected) def test_get_empty_flavor_list_v1_0(self): - def _throw_NoInstanceTypesFound(self): - raise exception.NoInstanceTypesFound - + def _return_empty(self): + return {} self.stubs.Set(nova.db.api, "instance_type_get_all", - _throw_NoInstanceTypesFound) + _return_empty) req = webob.Request.blank('/v1.0/flavors') res = req.get_response(fakes.wsgi_app()) @@ -277,11 +276,10 @@ class FlavorsTest(test.TestCase): self.assertEqual(flavor, expected) def test_get_empty_flavor_list_v1_1(self): - def _throw_NoInstanceTypesFound(self): - raise exception.NoInstanceTypesFound - + def _return_empty(self): + return {} self.stubs.Set(nova.db.api, "instance_type_get_all", - _throw_NoInstanceTypesFound) + _return_empty) req = webob.Request.blank('/v1.1/flavors') res = req.get_response(fakes.wsgi_app()) -- cgit