Fix wrong type of exception raised

generate_template and resource_schema methods of service.py
raises wrong type of exceptions after catching them.

Also resource validation failure should not raise stack
validation exception.

Change-Id: I44b8d37db7d2687cee2a7b675dd062b3af3ee167
Closes-Bug: #1447194
This commit is contained in:
tyagi 2015-04-22 01:02:41 -07:00
parent 8b2723e23f
commit 9a4d719ea1
11 changed files with 48 additions and 34 deletions

View File

@ -320,10 +320,18 @@ class SnapshotNotFound(HeatException):
"could not be found.")
class TemplateNotFound(HeatException):
msg_fmt = _("%(message)s")
class ResourceTypeNotFound(HeatException):
msg_fmt = _("The Resource Type (%(type_name)s) could not be found.")
class InvalidResourceType(HeatException):
msg_fmt = _("%(message)s")
class ResourceNotAvailable(HeatException):
msg_fmt = _("The Resource (%(resource_name)s) is not available.")

View File

@ -389,20 +389,19 @@ class ResourceRegistry(object):
def get_class(self, resource_type, resource_name=None):
if resource_type == "":
msg = _('Resource "%s" has no type') % resource_name
raise exception.StackValidationFailed(message=msg)
raise exception.InvalidResourceType(message=msg)
elif resource_type is None:
msg = _('Non-empty resource type is required '
'for resource "%s"') % resource_name
raise exception.StackValidationFailed(message=msg)
raise exception.InvalidResourceType(message=msg)
elif not isinstance(resource_type, six.string_types):
msg = _('Resource "%s" type is not a string') % resource_name
raise exception.StackValidationFailed(message=msg)
raise exception.InvalidResourceType(message=msg)
info = self.get_resource_info(resource_type,
resource_name=resource_name)
if info is None:
msg = _("Unknown resource Type : %s") % resource_type
raise exception.StackValidationFailed(message=msg)
raise exception.ResourceTypeNotFound(type_name=resource_type)
return info.get_class()
def as_dict(self):

View File

@ -131,7 +131,7 @@ class Resource(object):
try:
ResourceClass = registry.get_class(definition.resource_type,
resource_name=name)
except exception.NotFound:
except exception.TemplateNotFound:
ResourceClass = template_resource.TemplateResource
assert issubclass(ResourceClass, Resource)

View File

@ -189,7 +189,7 @@ class ResourceGroup(stack_resource.StackResource):
# make sure we can resolve the nested resource type
try:
self.stack.env.get_class(res_def.resource_type)
except exception.NotFound:
except exception.TemplateNotFound:
# its a template resource
pass

View File

@ -81,7 +81,7 @@ class TemplateResource(stack_resource.StackResource):
args = {'name': template_name, 'exc': six.text_type(r_exc)}
msg = _('Could not fetch remote template '
'"%(name)s": %(exc)s') % args
raise exception.NotFound(msg_fmt=msg)
raise exception.TemplateNotFound(message=msg)
@staticmethod
def get_schemas(tmpl, param_defaults):
@ -94,7 +94,7 @@ class TemplateResource(stack_resource.StackResource):
self._parsed_nested = None
try:
tmpl = template.Template(self.child_template())
except (exception.NotFound, ValueError) as download_error:
except (exception.TemplateNotFound, ValueError) as download_error:
self.validation_exception = download_error
tmpl = template.Template(
{"HeatTemplateFormatVersion": "2012-12-12"})
@ -169,7 +169,7 @@ class TemplateResource(stack_resource.StackResource):
try:
t_data = self.get_template_file(self.template_name,
self.allowed_schemes)
except exception.NotFound as err:
except exception.TemplateNotFound as err:
reported_excp = err
if t_data is None:

View File

@ -1006,10 +1006,10 @@ class EngineService(service.Service):
"""
try:
resource_class = resources.global_env().get_class(type_name)
except exception.StackValidationFailed:
raise exception.ResourceTypeNotFound(type_name=type_name)
except exception.NotFound as ex:
raise exception.StackValidationFailed(message=ex.message)
except (exception.InvalidResourceType,
exception.ResourceTypeNotFound,
exception.TemplateNotFound) as ex:
raise ex
def properties_schema():
for name, schema_dict in resource_class.properties_schema.items():
@ -1038,10 +1038,10 @@ class EngineService(service.Service):
try:
return resources.global_env().get_class(
type_name).resource_to_template(type_name)
except exception.StackValidationFailed:
raise exception.ResourceTypeNotFound(type_name=type_name)
except exception.NotFound as ex:
raise exception.StackValidationFailed(message=ex.message)
except (exception.InvalidResourceType,
exception.ResourceTypeNotFound,
exception.TemplateNotFound) as ex:
raise ex
@context.request_context
def list_events(self, cnxt, stack_identity, filters=None, limit=None,

View File

@ -2380,7 +2380,7 @@ class StackServiceTest(common.HeatTestCase):
mock_iterable = mock.MagicMock(return_value=iter([info]))
with mock.patch('heat.engine.environment.ResourceRegistry.iterable_by',
new=mock_iterable):
ex = self.assertRaises(exception.StackValidationFailed,
ex = self.assertRaises(exception.TemplateNotFound,
function,
self.ctx,
type_name='ResourceWithWrongRefOnFile')

View File

@ -522,7 +522,7 @@ class ProviderTemplateTest(common.HeatTestCase):
env_str = {'resource_registry': {'resources': {'fred': {
"OS::ResourceType": "some_magic.yaml"}}}}
env = environment.Environment(env_str)
ex = self.assertRaises(exception.NotFound, env.get_class,
ex = self.assertRaises(exception.TemplateNotFound, env.get_class,
'OS::ResourceType', 'fred')
self.assertIn('Could not fetch remote template "some_magic.yaml"',
six.text_type(ex))

View File

@ -75,7 +75,7 @@ class ResourceTest(common.HeatTestCase):
self.assertEqual(generic_rsrc.GenericResource, cls)
def test_get_class_noexist(self):
self.assertRaises(exception.StackValidationFailed,
self.assertRaises(exception.ResourceTypeNotFound,
resources.global_env().get_class,
'NoExistResourceType')
@ -109,13 +109,13 @@ class ResourceTest(common.HeatTestCase):
def test_resource_new_err(self):
snippet = rsrc_defn.ResourceDefinition('aresource',
'NoExistResourceType')
self.assertRaises(exception.StackValidationFailed,
self.assertRaises(exception.ResourceTypeNotFound,
resource.Resource, 'aresource', snippet, self.stack)
def test_resource_non_type(self):
resource_name = 'aresource'
snippet = rsrc_defn.ResourceDefinition(resource_name, '')
ex = self.assertRaises(exception.StackValidationFailed,
ex = self.assertRaises(exception.InvalidResourceType,
resource.Resource, resource_name,
snippet, self.stack)
self.assertIn(_('Resource "%s" has no type') % resource_name,

View File

@ -308,9 +308,10 @@ class ResourceGroupTest(common.HeatTestCase):
stack = utils.parse_stack(tmp)
snip = stack.t.resource_definitions(stack)['group1']
resg = resource_group.ResourceGroup('test', snip, stack)
exc = self.assertRaises(exception.StackValidationFailed,
exc = self.assertRaises(exception.ResourceTypeNotFound,
resg.validate)
self.assertIn('Unknown resource Type', six.text_type(exc))
exp_msg = 'The Resource Type (idontexist) could not be found.'
self.assertIn(exp_msg, six.text_type(exc))
def test_reference_attr(self):
stack = utils.parse_stack(template2)

View File

@ -370,23 +370,29 @@ class StackResourceTest(common.HeatTestCase):
rsrc.validate)
self.assertIn(raise_exc_msg, six.text_type(exc))
def _test_validate_unknown_resource_type(self, stack_name,
tmpl, resource_name):
raise_exc_msg = ('Unknown resource Type : idontexist')
def _test_validate_unknown_resource_type(self, stack_name, tmpl,
resource_name,
stack_resource=True):
raise_exc_msg = ('The Resource Type (idontexist) could not be found.')
stack = parser.Stack(utils.dummy_context(), stack_name, tmpl)
rsrc = stack[resource_name]
exc = self.assertRaises(exception.StackValidationFailed,
rsrc.validate)
if stack_resource:
exc = self.assertRaises(exception.StackValidationFailed,
rsrc.validate)
else:
exc = self.assertRaises(exception.ResourceTypeNotFound,
rsrc.validate)
self.assertIn(raise_exc_msg, six.text_type(exc))
def test_validate_resource_group(self):
# test validate without nested template
# resource group validate without nested template is a normal
# resource validation
stack_name = 'validate_resource_group_template'
t = template_format.parse(resource_group_template)
tmpl = templatem.Template(t)
self._test_validate_unknown_resource_type(stack_name, tmpl,
'my_resource_group')
'my_resource_group',
stack_resource=False)
# validate with nested template
res_prop = t['resources']['my_resource_group']['properties']
@ -397,7 +403,7 @@ class StackResourceTest(common.HeatTestCase):
'my_resource_group')
def test_validate_heat_autoscaling_group(self):
# test validate without nested template
# Autoscaling validation is a nested stack validation
stack_name = 'validate_heat_autoscaling_group_template'
t = template_format.parse(heat_autoscaling_group_template)
tmpl = templatem.Template(t)