From eb2ab3a40a7ddc6038a254c27870f92cfd012d24 Mon Sep 17 00:00:00 2001 From: "Kevin L. Mitchell" Date: Fri, 20 Jan 2012 13:31:47 -0600 Subject: [PATCH] Return 400 if registry returns 400. Fixes bug 919244. The registry would return 400 for invalid input, which resulted in an exception.Invalid being thrown in the registry client. There was no exception handler for this exception, so it got bubbled up and turned into a 500. This should fix the problem by adding the missing exception handlers. Change-Id: I75ecfec1c0b0b4b3df1a8c9ace83e75d19527c93 --- glance/api/v1/members.py | 8 ++++++++ glance/tests/functional/test_shared_images.py | 20 +++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/glance/api/v1/members.py b/glance/api/v1/members.py index ae9dd5f6aa..fbae772475 100644 --- a/glance/api/v1/members.py +++ b/glance/api/v1/members.py @@ -93,6 +93,10 @@ class Controller(object): can_share = bool(body['member']['can_share']) try: registry.add_member(req.context, image_id, id, can_share) + except exception.Invalid, e: + msg = "%s" % e + logger.debug(msg) + raise webob.exc.HTTPBadRequest(explanation=msg) except exception.NotFound, e: msg = "%s" % e logger.debug(msg) @@ -121,6 +125,10 @@ class Controller(object): try: registry.replace_members(req.context, image_id, body) + except exception.Invalid, e: + msg = "%s" % e + logger.debug(msg) + raise webob.exc.HTTPBadRequest(explanation=msg) except exception.NotFound, e: msg = "%s" % e logger.debug(msg) diff --git a/glance/tests/functional/test_shared_images.py b/glance/tests/functional/test_shared_images.py index c8882c4809..5a39d22365 100644 --- a/glance/tests/functional/test_shared_images.py +++ b/glance/tests/functional/test_shared_images.py @@ -148,6 +148,24 @@ class TestSharedImagesApi(keystone_utils.KeystoneTests): self.assertEqual(response.status, 200) self.assertEqual(content, '{"images": []}') + # Build path for the next couple of checks + path = ("http://%s:%d/v1/images/%s/members" % + ("0.0.0.0", self.api_port, image_id)) + + # Make sure update with invalid data gets rejected with 400 + body = { + 'test': [ + { + 'member_id': keystone_utils.bacon_id, + 'can_share': False, + }, + ], + } + response, _ = self._request(path, 'PUT', + keystone_utils.pattieblack_token, + body=json.dumps(body)) + self.assertEqual(response.status, 400) + # Replace froggy with bacon body = { 'memberships': [ @@ -157,8 +175,6 @@ class TestSharedImagesApi(keystone_utils.KeystoneTests): }, ], } - path = "http://%s:%d/v1/images/%s/members" % \ - ("0.0.0.0", self.api_port, image_id) response, content = self._request(path, 'PUT', keystone_utils.pattieblack_token,