Correct validation of wait conditon

This changes:
1. if the referenced wait condition is invalid,
   return '' when resolve attribute.
2. if the referenced wait condition is invalid,
   raise error with correct message.

Closes-Bug: #1666747

Change-Id: I69248c2bd80246ea6d08c1f140885e4b461353a0
This commit is contained in:
huangtianhua 2017-02-22 12:14:01 +08:00
parent 14cbf5c167
commit a4e85e2190
2 changed files with 36 additions and 10 deletions

View File

@ -92,9 +92,12 @@ class HeatWaitCondition(resource.Resource):
return self.stack.resource_by_refid(self.properties[self.HANDLE]) return self.stack.resource_by_refid(self.properties[self.HANDLE])
def _validate_handle_resource(self, handle): def _validate_handle_resource(self, handle):
if not isinstance(handle, wc_base.BaseWaitConditionHandle): if handle is not None and isinstance(
raise ValueError(_('%(name)s is not a valid wait condition ' handle, wc_base.BaseWaitConditionHandle):
'handle.') % {'name': handle.name}) return
hn = handle.name if handle else self.properties[self.HANDLE]
msg = _('%s is not a valid wait condition handle.') % hn
raise ValueError(msg)
def _wait(self, handle, started_at, timeout_in): def _wait(self, handle, started_at, timeout_in):
if timeutils.is_older_than(started_at, timeout_in): if timeutils.is_older_than(started_at, timeout_in):
@ -144,6 +147,8 @@ class HeatWaitCondition(resource.Resource):
def _resolve_attribute(self, key): def _resolve_attribute(self, key):
handle = self._get_handle_resource() handle = self._get_handle_resource()
if handle is None:
return ''
if key == self.DATA: if key == self.DATA:
meta = handle.metadata_get(refresh=True) meta = handle.metadata_get(refresh=True)
res = {k: meta[k][handle.DATA] for k in meta} res = {k: meta[k][handle.DATA] for k in meta}

View File

@ -108,7 +108,7 @@ resources:
type: OS::Heat::UpdateWaitConditionHandle type: OS::Heat::UpdateWaitConditionHandle
''' '''
test_template_bad_waithandle = ''' test_template_waithandle_bad_type = '''
heat_template_version: 2013-05-23 heat_template_version: 2013-05-23
resources: resources:
wait_condition: wait_condition:
@ -120,6 +120,20 @@ resources:
type: OS::Heat::RandomString type: OS::Heat::RandomString
''' '''
test_template_waithandle_bad_reference = '''
heat_template_version: pike
resources:
wait_condition:
type: OS::Heat::WaitCondition
properties:
handle: wait_handel
timeout: 5
wait_handle:
type: OS::Heat::WaitConditionHandle
properties:
signal_transport: NO_SIGNAL
'''
class HeatWaitConditionTest(common.HeatTestCase): class HeatWaitConditionTest(common.HeatTestCase):
@ -206,17 +220,24 @@ class HeatWaitConditionTest(common.HeatTestCase):
self.assertEqual('wait_handle', r.name) self.assertEqual('wait_handle', r.name)
self.m.VerifyAll() self.m.VerifyAll()
def test_bad_wait_handle(self): def _test_wait_handle_invalid(self, tmpl, handle_name):
self.stack = self.create_stack( self.stack = self.create_stack(template=tmpl)
template=test_template_bad_waithandle)
self.m.ReplayAll() self.m.ReplayAll()
self.stack.create() self.stack.create()
rsrc = self.stack['wait_condition'] rsrc = self.stack['wait_condition']
self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state) self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state)
reason = rsrc.status_reason reason = rsrc.status_reason
self.assertEqual(reason, 'ValueError: resources.wait_condition: ' error_msg = ('ValueError: resources.wait_condition: '
'wait_handle is not a valid wait condition ' '%s is not a valid wait condition handle.') % handle_name
'handle.') self.assertEqual(reason, error_msg)
def test_wait_handle_bad_type(self):
self._test_wait_handle_invalid(test_template_waithandle_bad_type,
'wait_handle')
def test_wait_handle_bad_reference(self):
self._test_wait_handle_invalid(
test_template_waithandle_bad_reference, 'wait_handel')
def test_timeout(self): def test_timeout(self):
self.stack = self.create_stack() self.stack = self.create_stack()