Add tests and simplify GlanceExceptions.

This also fixes some incorrect use of GlanceException parameters.

Fixes bug: 1009122
Fixes bug: 1010140

Change-Id: Ic77856686a0259da2318e05b15f3d382508c00b9
This commit is contained in:
Alex Meade 2012-06-05 13:52:23 -04:00
parent 575651883d
commit e2c52ddfa6
9 changed files with 65 additions and 29 deletions

View File

@ -36,23 +36,15 @@ class GlanceException(Exception):
""" """
message = _("An unknown exception occurred") message = _("An unknown exception occurred")
def __init__(self, *args, **kwargs): def __init__(self, message=None, *args, **kwargs):
if not message:
message = self.message
try: try:
self._error_string = self.message % kwargs message = message % kwargs
except Exception: except Exception:
# at least get the core message out if something happened # at least get the core message out if something happened
self._error_string = self.message pass
if len(args) > 0: super(GlanceException, self).__init__(message)
# If there is a non-kwarg parameter, assume it's the error
# message or reason description and tack it on to the end
# of the exception message
# Convert all arguments into their string representations...
args = ["%s" % arg for arg in args]
self._error_string = (self._error_string +
"\nDetails: %s" % '\n'.join(args))
def __str__(self):
return self._error_string
class MissingArgumentError(GlanceException): class MissingArgumentError(GlanceException):

View File

@ -109,7 +109,7 @@ def legacy_parse_uri(self, uri):
"like so: " "like so: "
"swift+http://user:pass@authurl.com/v1/container/obj" "swift+http://user:pass@authurl.com/v1/container/obj"
) )
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
pieces = urlparse.urlparse(uri) pieces = urlparse.urlparse(uri)
assert pieces.scheme in ('swift', 'swift+http', 'swift+https') assert pieces.scheme in ('swift', 'swift+http', 'swift+https')
@ -140,7 +140,7 @@ def legacy_parse_uri(self, uri):
if len(cred_parts) == 1: if len(cred_parts) == 1:
reason = (_("Badly formed credentials '%(creds)s' in Swift " reason = (_("Badly formed credentials '%(creds)s' in Swift "
"URI") % locals()) "URI") % locals())
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
elif len(cred_parts) == 3: elif len(cred_parts) == 3:
user = ':'.join(cred_parts[0:2]) user = ':'.join(cred_parts[0:2])
else: else:
@ -160,4 +160,4 @@ def legacy_parse_uri(self, uri):
self.authurl = '/'.join(path_parts) self.authurl = '/'.join(path_parts)
except IndexError: except IndexError:
reason = _("Badly formed Swift URI") reason = _("Badly formed Swift URI")
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)

View File

@ -63,7 +63,7 @@ class StoreLocation(glance.store.location.StoreLocation):
path = (pieces.netloc + pieces.path).strip() path = (pieces.netloc + pieces.path).strip()
if path == '': if path == '':
reason = _("No path specified") reason = _("No path specified")
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
self.path = path self.path = path

View File

@ -75,12 +75,12 @@ class StoreLocation(glance.store.location.StoreLocation):
except ValueError: except ValueError:
reason = (_("Credentials '%s' not well-formatted.") reason = (_("Credentials '%s' not well-formatted.")
% "".join(creds)) % "".join(creds))
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
else: else:
self.user = None self.user = None
if netloc == '': if netloc == '':
reason = _("No address specified in HTTP URL") reason = _("No address specified in HTTP URL")
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
self.netloc = netloc self.netloc = netloc
self.path = path self.path = path

View File

@ -70,7 +70,8 @@ class StoreLocation(glance.store.location.StoreLocation):
def parse_uri(self, uri): def parse_uri(self, uri):
if not uri.startswith('rbd://'): if not uri.startswith('rbd://'):
raise exception.BadStoreUri(uri, _('URI must start with rbd://')) raise exception.BadStoreUri(uri=uri,
reason=_('URI must start with rbd://'))
self.image = uri[6:] self.image = uri[6:]

View File

@ -111,7 +111,7 @@ class StoreLocation(glance.store.location.StoreLocation):
"s3+https://accesskey:secretkey@s3.amazonaws.com/bucket/" "s3+https://accesskey:secretkey@s3.amazonaws.com/bucket/"
"key-id" "key-id"
) )
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
pieces = urlparse.urlparse(uri) pieces = urlparse.urlparse(uri)
assert pieces.scheme in ('s3', 's3+http', 's3+https') assert pieces.scheme in ('s3', 's3+http', 's3+https')
@ -137,7 +137,7 @@ class StoreLocation(glance.store.location.StoreLocation):
self.secretkey = secret_key self.secretkey = secret_key
except IndexError: except IndexError:
reason = _("Badly formed S3 credentials %s") % creds reason = _("Badly formed S3 credentials %s") % creds
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
else: else:
self.accesskey = None self.accesskey = None
path = entire_path path = entire_path
@ -149,10 +149,10 @@ class StoreLocation(glance.store.location.StoreLocation):
self.s3serviceurl = '/'.join(path_parts).strip('/') self.s3serviceurl = '/'.join(path_parts).strip('/')
else: else:
reason = _("Badly formed S3 URI. Missing s3 service URL.") reason = _("Badly formed S3 URI. Missing s3 service URL.")
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
except IndexError: except IndexError:
reason = _("Badly formed S3 URI") reason = _("Badly formed S3 URI")
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
class ChunkedFile(object): class ChunkedFile(object):

View File

@ -128,7 +128,7 @@ class StoreLocation(glance.store.location.StoreLocation):
"like so: " "like so: "
"swift+http://user:pass@authurl.com/v1/container/obj" "swift+http://user:pass@authurl.com/v1/container/obj"
) )
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
pieces = urlparse.urlparse(uri) pieces = urlparse.urlparse(uri)
assert pieces.scheme in ('swift', 'swift+http', 'swift+https') assert pieces.scheme in ('swift', 'swift+http', 'swift+https')
@ -155,7 +155,7 @@ class StoreLocation(glance.store.location.StoreLocation):
if len(cred_parts) != 2: if len(cred_parts) != 2:
reason = (_("Badly formed credentials '%(creds)s' in Swift " reason = (_("Badly formed credentials '%(creds)s' in Swift "
"URI") % locals()) "URI") % locals())
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
user, key = cred_parts user, key = cred_parts
self.user = urllib.unquote(user) self.user = urllib.unquote(user)
self.key = urllib.unquote(key) self.key = urllib.unquote(key)
@ -171,7 +171,7 @@ class StoreLocation(glance.store.location.StoreLocation):
self.authurl = '/'.join(path_parts) self.authurl = '/'.join(path_parts)
except IndexError: except IndexError:
reason = _("Badly formed Swift URI") reason = _("Badly formed Swift URI")
raise exception.BadStoreUri(uri, reason) raise exception.BadStoreUri(uri=uri, reason=reason)
@property @property
def swift_auth_url(self): def swift_auth_url(self):

View File

@ -0,0 +1,44 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 OpenStack, LLC
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from glance.common import exception
from glance.tests import utils as test_utils
class GlanceExceptionTestCase(test_utils.BaseTestCase):
def test_default_error_msg(self):
class FakeGlanceException(exception.GlanceException):
message = "default message"
exc = FakeGlanceException()
self.assertEquals(unicode(exc), 'default message')
def test_specified_error_msg(self):
self.assertTrue('test' in unicode(exception.GlanceException('test')))
def test_default_error_msg_with_kwargs(self):
class FakeGlanceException(exception.GlanceException):
message = "default message: %(code)s"
exc = FakeGlanceException(code=500)
self.assertEquals(unicode(exc), "default message: 500")
def test_specified_error_msg_with_kwargs(self):
self.assertTrue('test: 500' in
unicode(exception.GlanceException('test: %(code)s',
code=500)))

View File

@ -21,7 +21,6 @@ import datetime
import re import re
from glance.common import crypt from glance.common import crypt
from glance.common import exception
from glance.common import utils from glance.common import utils
from glance.openstack.common import importutils from glance.openstack.common import importutils
from glance.tests import utils as test_utils from glance.tests import utils as test_utils