From f1ea0f2d9be21e1a013ed9990f0f86adabb2ecbd Mon Sep 17 00:00:00 2001 From: "Zhang Lei (Sneeze)" Date: Wed, 26 Jun 2013 13:39:42 +0800 Subject: [PATCH] Raise suitable exception when stack validation fails fixed Bug#1191009 If nova returns an error looking up images during stack validation, we return a StackValidationFailed, but we should probably catch this and return ServerError instead, since this is really an internal error not a validation failure. Change-Id: Id509b2fa9f78e197cacfe44bae6c3db37cfd6179 --- heat/engine/parser.py | 4 ++++ heat/engine/resources/instance.py | 5 ++++- heat/tests/test_validate.py | 34 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/heat/engine/parser.py b/heat/engine/parser.py index 14b260ae..062e693e 100644 --- a/heat/engine/parser.py +++ b/heat/engine/parser.py @@ -32,6 +32,7 @@ from heat.engine.clients import Clients from heat.db import api as db_api from heat.openstack.common import log as logging +from heat.common.exception import ServerError from heat.common.exception import StackValidationFailed logger = logging.getLogger(__name__) @@ -238,6 +239,9 @@ class Stack(object): for res in self: try: result = res.validate() + except ServerError as ex: + logger.exception(ex) + raise ex except Exception as ex: logger.exception(ex) raise StackValidationFailed(message=str(ex)) diff --git a/heat/engine/resources/instance.py b/heat/engine/resources/instance.py index 1d8fbca0..58fb1b59 100644 --- a/heat/engine/resources/instance.py +++ b/heat/engine/resources/instance.py @@ -485,7 +485,10 @@ class Instance(resource.Resource): % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) else: - image_list = self.nova().images.list() + try: + image_list = self.nova().images.list() + except clients.novaclient.exceptions.ClientException as ex: + raise exception.ServerError(message=str(ex)) image_names = dict( (o.id, o.name) for o in image_list if o.name == image_identifier) diff --git a/heat/tests/test_validate.py b/heat/tests/test_validate.py index 1cbe1b66..bf6f3c64 100644 --- a/heat/tests/test_validate.py +++ b/heat/tests/test_validate.py @@ -14,6 +14,7 @@ from testtools import skipIf +from heat.engine import clients from heat.engine import environment from heat.tests.v1_1 import fakes from heat.common import exception @@ -430,6 +431,23 @@ test_template_invalid_secgroupids = ''' } ''' +test_template_nova_client_exception = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "test.", + "Resources" : { + "Instance": { + "Type": "AWS::EC2::Instance", + "DeletionPolicy": "Delete", + "Properties": { + "ImageId": "image_name", + "InstanceType": "m1.large" + } + } + } +} +''' + class validateTest(HeatTestCase): def setUp(self): @@ -617,3 +635,19 @@ class validateTest(HeatTestCase): resource = stack.resources['Instance'] self.assertNotEqual(resource.validate(), None) + + def test_client_exception_from_nova_client(self): + t = template_format.parse(test_template_nova_client_exception) + template = parser.Template(t) + stack = parser.Stack(None, 'test_stack', template) + + self.m.StubOutWithMock(self.fc.images, 'list') + self.fc.images.list()\ + .AndRaise(clients.novaclient.exceptions.ClientException(500)) + self.m.StubOutWithMock(instances.Instance, 'nova') + instances.Instance.nova().AndReturn(self.fc) + self.m.ReplayAll() + + self.assertRaises(exception.ServerError, + stack.validate) + self.m.VerifyAll() -- 2.45.2