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.") "could not be found.")
class TemplateNotFound(HeatException):
msg_fmt = _("%(message)s")
class ResourceTypeNotFound(HeatException): class ResourceTypeNotFound(HeatException):
msg_fmt = _("The Resource Type (%(type_name)s) could not be found.") msg_fmt = _("The Resource Type (%(type_name)s) could not be found.")
class InvalidResourceType(HeatException):
msg_fmt = _("%(message)s")
class ResourceNotAvailable(HeatException): class ResourceNotAvailable(HeatException):
msg_fmt = _("The Resource (%(resource_name)s) is not available.") 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): def get_class(self, resource_type, resource_name=None):
if resource_type == "": if resource_type == "":
msg = _('Resource "%s" has no type') % resource_name msg = _('Resource "%s" has no type') % resource_name
raise exception.StackValidationFailed(message=msg) raise exception.InvalidResourceType(message=msg)
elif resource_type is None: elif resource_type is None:
msg = _('Non-empty resource type is required ' msg = _('Non-empty resource type is required '
'for resource "%s"') % resource_name 'for resource "%s"') % resource_name
raise exception.StackValidationFailed(message=msg) raise exception.InvalidResourceType(message=msg)
elif not isinstance(resource_type, six.string_types): elif not isinstance(resource_type, six.string_types):
msg = _('Resource "%s" type is not a string') % resource_name 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, info = self.get_resource_info(resource_type,
resource_name=resource_name) resource_name=resource_name)
if info is None: if info is None:
msg = _("Unknown resource Type : %s") % resource_type raise exception.ResourceTypeNotFound(type_name=resource_type)
raise exception.StackValidationFailed(message=msg)
return info.get_class() return info.get_class()
def as_dict(self): def as_dict(self):

View File

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

View File

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

View File

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

View File

@ -1006,10 +1006,10 @@ class EngineService(service.Service):
""" """
try: try:
resource_class = resources.global_env().get_class(type_name) resource_class = resources.global_env().get_class(type_name)
except exception.StackValidationFailed: except (exception.InvalidResourceType,
raise exception.ResourceTypeNotFound(type_name=type_name) exception.ResourceTypeNotFound,
except exception.NotFound as ex: exception.TemplateNotFound) as ex:
raise exception.StackValidationFailed(message=ex.message) raise ex
def properties_schema(): def properties_schema():
for name, schema_dict in resource_class.properties_schema.items(): for name, schema_dict in resource_class.properties_schema.items():
@ -1038,10 +1038,10 @@ class EngineService(service.Service):
try: try:
return resources.global_env().get_class( return resources.global_env().get_class(
type_name).resource_to_template(type_name) type_name).resource_to_template(type_name)
except exception.StackValidationFailed: except (exception.InvalidResourceType,
raise exception.ResourceTypeNotFound(type_name=type_name) exception.ResourceTypeNotFound,
except exception.NotFound as ex: exception.TemplateNotFound) as ex:
raise exception.StackValidationFailed(message=ex.message) raise ex
@context.request_context @context.request_context
def list_events(self, cnxt, stack_identity, filters=None, limit=None, 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])) mock_iterable = mock.MagicMock(return_value=iter([info]))
with mock.patch('heat.engine.environment.ResourceRegistry.iterable_by', with mock.patch('heat.engine.environment.ResourceRegistry.iterable_by',
new=mock_iterable): new=mock_iterable):
ex = self.assertRaises(exception.StackValidationFailed, ex = self.assertRaises(exception.TemplateNotFound,
function, function,
self.ctx, self.ctx,
type_name='ResourceWithWrongRefOnFile') type_name='ResourceWithWrongRefOnFile')

View File

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

View File

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

View File

@ -308,9 +308,10 @@ class ResourceGroupTest(common.HeatTestCase):
stack = utils.parse_stack(tmp) stack = utils.parse_stack(tmp)
snip = stack.t.resource_definitions(stack)['group1'] snip = stack.t.resource_definitions(stack)['group1']
resg = resource_group.ResourceGroup('test', snip, stack) resg = resource_group.ResourceGroup('test', snip, stack)
exc = self.assertRaises(exception.StackValidationFailed, exc = self.assertRaises(exception.ResourceTypeNotFound,
resg.validate) 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): def test_reference_attr(self):
stack = utils.parse_stack(template2) stack = utils.parse_stack(template2)

View File

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