Merge "Fix TypeError: __str__ returned non-string (type ImageRefValidationFailed)"

This commit is contained in:
Zuul 2019-03-07 04:41:19 +00:00 committed by Gerrit Code Review
commit faf0bccbb0
5 changed files with 22 additions and 9 deletions

View File

@ -128,10 +128,12 @@ class IronicException(Exception):
def __str__(self): def __str__(self):
"""Encode to utf-8 then wsme api can consume it as well.""" """Encode to utf-8 then wsme api can consume it as well."""
if not six.PY3: value = self.__unicode__()
return six.text_type(self.args[0]).encode('utf-8') if six.PY3:
# On Python 3 unicode is the same as str
return self.args[0] return value
else:
return value.encode('utf-8')
def __unicode__(self): def __unicode__(self):
"""Return a unicode representation of the exception message.""" """Return a unicode representation of the exception message."""

View File

@ -829,7 +829,7 @@ def validate_image_properties(ctx, deploy_info, properties):
raise exception.InvalidParameterValue(_( raise exception.InvalidParameterValue(_(
"Image %s can not be found.") % image_href) "Image %s can not be found.") % image_href)
except exception.ImageRefValidationFailed as e: except exception.ImageRefValidationFailed as e:
raise exception.InvalidParameterValue(e) raise exception.InvalidParameterValue(err=e)
missing_props = [] missing_props = []
for prop in properties: for prop in properties:

View File

@ -32,7 +32,7 @@ class TestException(exception.IronicException):
class TestIronicException(base.TestCase): class TestIronicException(base.TestCase):
def test___init__(self): def test___str__encoding(self):
expected = b'\xc3\xa9\xe0\xaf\xb2\xe0\xbe\x84' expected = b'\xc3\xa9\xe0\xaf\xb2\xe0\xbe\x84'
if six.PY3: if six.PY3:
expected = expected.decode('utf-8') expected = expected.decode('utf-8')
@ -40,6 +40,11 @@ class TestIronicException(base.TestCase):
exc = exception.IronicException(message) exc = exception.IronicException(message)
self.assertEqual(expected, exc.__str__()) self.assertEqual(expected, exc.__str__())
def test___str__non_string(self):
exc = exception.IronicException(42)
self.assertEqual("42", exc.__str__())
self.assertEqual(u"42", exc.__unicode__())
@mock.patch.object(exception.LOG, 'error', autospec=True) @mock.patch.object(exception.LOG, 'error', autospec=True)
def test___init___invalid_kwarg(self, log_mock): def test___init___invalid_kwarg(self, log_mock):
self.config(fatal_exception_format_errors=False) self.config(fatal_exception_format_errors=False)

View File

@ -1955,9 +1955,10 @@ class ValidateImagePropertiesTestCase(db_base.DbTestCase):
driver_internal_info=DRV_INTERNAL_INFO_DICT, driver_internal_info=DRV_INTERNAL_INFO_DICT,
) )
inst_info = utils.get_image_instance_info(node) inst_info = utils.get_image_instance_info(node)
self.assertRaises(exception.InvalidParameterValue, self.assertRaisesRegex(exception.InvalidParameterValue,
utils.validate_image_properties, self.context, 'HTTPError',
inst_info, ['kernel', 'ramdisk']) utils.validate_image_properties, self.context,
inst_info, ['kernel', 'ramdisk'])
class ValidateParametersTestCase(db_base.DbTestCase): class ValidateParametersTestCase(db_base.DbTestCase):

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Returns the correct error message on providing an invalid reference to
``image_source``. Previously an internal error was raised.