From a4e85e21905732c8179f263929b751c645f10f41 Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Wed, 22 Feb 2017 12:14:01 +0800 Subject: [PATCH] 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 --- .../openstack/heat/wait_condition.py | 11 ++++-- .../openstack/heat/test_waitcondition.py | 35 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/heat/engine/resources/openstack/heat/wait_condition.py b/heat/engine/resources/openstack/heat/wait_condition.py index 068d49b260..7843e3ce8e 100644 --- a/heat/engine/resources/openstack/heat/wait_condition.py +++ b/heat/engine/resources/openstack/heat/wait_condition.py @@ -92,9 +92,12 @@ class HeatWaitCondition(resource.Resource): return self.stack.resource_by_refid(self.properties[self.HANDLE]) def _validate_handle_resource(self, handle): - if not isinstance(handle, wc_base.BaseWaitConditionHandle): - raise ValueError(_('%(name)s is not a valid wait condition ' - 'handle.') % {'name': handle.name}) + if handle is not None and isinstance( + handle, wc_base.BaseWaitConditionHandle): + 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): if timeutils.is_older_than(started_at, timeout_in): @@ -144,6 +147,8 @@ class HeatWaitCondition(resource.Resource): def _resolve_attribute(self, key): handle = self._get_handle_resource() + if handle is None: + return '' if key == self.DATA: meta = handle.metadata_get(refresh=True) res = {k: meta[k][handle.DATA] for k in meta} diff --git a/heat/tests/openstack/heat/test_waitcondition.py b/heat/tests/openstack/heat/test_waitcondition.py index d6e5a30cb2..12ff908543 100644 --- a/heat/tests/openstack/heat/test_waitcondition.py +++ b/heat/tests/openstack/heat/test_waitcondition.py @@ -108,7 +108,7 @@ resources: type: OS::Heat::UpdateWaitConditionHandle ''' -test_template_bad_waithandle = ''' +test_template_waithandle_bad_type = ''' heat_template_version: 2013-05-23 resources: wait_condition: @@ -120,6 +120,20 @@ resources: 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): @@ -206,17 +220,24 @@ class HeatWaitConditionTest(common.HeatTestCase): self.assertEqual('wait_handle', r.name) self.m.VerifyAll() - def test_bad_wait_handle(self): - self.stack = self.create_stack( - template=test_template_bad_waithandle) + def _test_wait_handle_invalid(self, tmpl, handle_name): + self.stack = self.create_stack(template=tmpl) self.m.ReplayAll() self.stack.create() rsrc = self.stack['wait_condition'] self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state) reason = rsrc.status_reason - self.assertEqual(reason, 'ValueError: resources.wait_condition: ' - 'wait_handle is not a valid wait condition ' - 'handle.') + error_msg = ('ValueError: resources.wait_condition: ' + '%s is not a valid wait condition handle.') % handle_name + 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): self.stack = self.create_stack()