From f7eddbef839f9a13b1aa1f1e071d51aae809aece Mon Sep 17 00:00:00 2001 From: Zane Bitter Date: Wed, 24 Apr 2024 12:06:07 +1200 Subject: [PATCH] Fix 'if' function with only 2 arguments This had a similar problem as when null values were passed explicitly before the fix for bug 1709620. Change-Id: I02fb4c8815bc24d4c0c339022aa857dc344d58e9 Story: 2011108 Task: 49921 --- heat/engine/function.py | 4 +++- heat/tests/test_hot.py | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/heat/engine/function.py b/heat/engine/function.py index 2169e21943..708b8a2c6b 100644 --- a/heat/engine/function.py +++ b/heat/engine/function.py @@ -243,7 +243,9 @@ class Macro(Function, metaclass=abc.ABCMeta): if isinstance(self.parsed, Function): return self.parsed.__reduce__() if self.parsed is None: - return lambda x: None, (None,) + return type(None), tuple() + if self.parsed is Ellipsis: + return type(Ellipsis), tuple() return type(self.parsed), (self.parsed,) def _repr_result(self): diff --git a/heat/tests/test_hot.py b/heat/tests/test_hot.py index a4f4c3d32a..3c826309fd 100644 --- a/heat/tests/test_hot.py +++ b/heat/tests/test_hot.py @@ -165,8 +165,9 @@ class HOTemplateTest(common.HeatTestCase): """Test processing of HOT templates.""" @staticmethod - def resolve(snippet, template, stack=None): - return function.resolve(template.parse(stack and stack.defn, snippet)) + def resolve(snippet, template, stack=None, nullable=False): + return function.resolve(template.parse(stack and stack.defn, snippet), + nullable=nullable) @staticmethod def resolve_condition(snippet, template, stack=None): @@ -1445,6 +1446,15 @@ class HOTemplateTest(common.HeatTestCase): resolved = self.resolve(snippet, tmpl, stack) self.assertIsNone(resolved) + def test_if_empty_return(self): + snippet = {'if': [False, 'value_if_true']} + # when condition is true, if function resolve to value_if_true + tmpl = template.Template(hot_wallaby_tpl_empty) + stack = parser.Stack(utils.dummy_context(), + 'test_if_empty_return', tmpl) + resolved = self.resolve(snippet, tmpl, stack, nullable=True) + self.assertIs(Ellipsis, resolved) + def test_if_using_condition_function(self): tmpl_with_conditions = template_format.parse(''' heat_template_version: 2016-10-14 @@ -1509,6 +1519,29 @@ resources: self.stack.state) self.assertEqual('', self.stack['AResource'].properties['Foo']) + def test_if_referenced_by_resource_empty(self): + tmpl_with_conditions = template_format.parse(''' +heat_template_version: wallaby +conditions: + create_prod: False +resources: + AResource: + type: ResourceWithPropsType + properties: + Foo: + if: + - create_prod + - "prod" +''') + tmpl = template.Template(tmpl_with_conditions) + self.stack = parser.Stack(utils.dummy_context(), + 'test_if_referenced_by_resource_null', tmpl) + self.stack.store() + self.stack.create() + self.assertEqual((parser.Stack.CREATE, parser.Stack.COMPLETE), + self.stack.state) + self.assertIsNone(self.stack['AResource'].properties['Foo']) + def test_if_invalid_args(self): snippets = [ {'if': ['create_prod', 'one_value']},