Skip to content

Commit

Permalink
Add tests and simplify GlanceExceptions.
Browse files Browse the repository at this point in the history
This also fixes some incorrect use of GlanceException parameters.

Fixes bug: 1009122
Fixes bug: 1010140

Change-Id: Ic77856686a0259da2318e05b15f3d382508c00b9
  • Loading branch information
ameade committed Jun 11, 2012
1 parent 5756518 commit e2c52dd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
20 changes: 6 additions & 14 deletions glance/common/exception.py
Expand Up @@ -36,23 +36,15 @@ class GlanceException(Exception):
"""
message = _("An unknown exception occurred")

def __init__(self, *args, **kwargs):
def __init__(self, message=None, *args, **kwargs):
if not message:
message = self.message
try:
self._error_string = self.message % kwargs
message = message % kwargs
except Exception:
# at least get the core message out if something happened
self._error_string = self.message
if len(args) > 0:
# 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
pass
super(GlanceException, self).__init__(message)


class MissingArgumentError(GlanceException):
Expand Down
Expand Up @@ -109,7 +109,7 @@ def legacy_parse_uri(self, uri):
"like so: "
"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)
assert pieces.scheme in ('swift', 'swift+http', 'swift+https')
Expand Down Expand Up @@ -140,7 +140,7 @@ def legacy_parse_uri(self, uri):
if len(cred_parts) == 1:
reason = (_("Badly formed credentials '%(creds)s' in Swift "
"URI") % locals())
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
elif len(cred_parts) == 3:
user = ':'.join(cred_parts[0:2])
else:
Expand All @@ -160,4 +160,4 @@ def legacy_parse_uri(self, uri):
self.authurl = '/'.join(path_parts)
except IndexError:
reason = _("Badly formed Swift URI")
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
2 changes: 1 addition & 1 deletion glance/store/filesystem.py
Expand Up @@ -63,7 +63,7 @@ def parse_uri(self, uri):
path = (pieces.netloc + pieces.path).strip()
if path == '':
reason = _("No path specified")
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
self.path = path


Expand Down
4 changes: 2 additions & 2 deletions glance/store/http.py
Expand Up @@ -75,12 +75,12 @@ def parse_uri(self, uri):
except ValueError:
reason = (_("Credentials '%s' not well-formatted.")
% "".join(creds))
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
else:
self.user = None
if netloc == '':
reason = _("No address specified in HTTP URL")
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
self.netloc = netloc
self.path = path

Expand Down
3 changes: 2 additions & 1 deletion glance/store/rbd.py
Expand Up @@ -70,7 +70,8 @@ def get_uri(self):

def parse_uri(self, uri):
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:]


Expand Down
8 changes: 4 additions & 4 deletions glance/store/s3.py
Expand Up @@ -111,7 +111,7 @@ def parse_uri(self, uri):
"s3+https://accesskey:secretkey@s3.amazonaws.com/bucket/"
"key-id"
)
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)

pieces = urlparse.urlparse(uri)
assert pieces.scheme in ('s3', 's3+http', 's3+https')
Expand All @@ -137,7 +137,7 @@ def parse_uri(self, uri):
self.secretkey = secret_key
except IndexError:
reason = _("Badly formed S3 credentials %s") % creds
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
else:
self.accesskey = None
path = entire_path
Expand All @@ -149,10 +149,10 @@ def parse_uri(self, uri):
self.s3serviceurl = '/'.join(path_parts).strip('/')
else:
reason = _("Badly formed S3 URI. Missing s3 service URL.")
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
except IndexError:
reason = _("Badly formed S3 URI")
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)


class ChunkedFile(object):
Expand Down
6 changes: 3 additions & 3 deletions glance/store/swift.py
Expand Up @@ -128,7 +128,7 @@ def parse_uri(self, uri):
"like so: "
"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)
assert pieces.scheme in ('swift', 'swift+http', 'swift+https')
Expand All @@ -155,7 +155,7 @@ def parse_uri(self, uri):
if len(cred_parts) != 2:
reason = (_("Badly formed credentials '%(creds)s' in Swift "
"URI") % locals())
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)
user, key = cred_parts
self.user = urllib.unquote(user)
self.key = urllib.unquote(key)
Expand All @@ -171,7 +171,7 @@ def parse_uri(self, uri):
self.authurl = '/'.join(path_parts)
except IndexError:
reason = _("Badly formed Swift URI")
raise exception.BadStoreUri(uri, reason)
raise exception.BadStoreUri(uri=uri, reason=reason)

@property
def swift_auth_url(self):
Expand Down
44 changes: 44 additions & 0 deletions glance/tests/unit/common/test_exception.py
@@ -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)))
1 change: 0 additions & 1 deletion glance/tests/unit/test_misc.py
Expand Up @@ -21,7 +21,6 @@
import re

from glance.common import crypt
from glance.common import exception
from glance.common import utils
from glance.openstack.common import importutils
from glance.tests import utils as test_utils
Expand Down

0 comments on commit e2c52dd

Please sign in to comment.