From cc8acd8f1216ac734fde00a4d098881cdf8261dd Mon Sep 17 00:00:00 2001 From: Andrew Plunk Date: Mon, 11 Nov 2013 17:19:10 -0600 Subject: [PATCH] Using a number in str_replace causes an exception When passing in a number as a parameter value to the string replace function, an exception is raised. Cast the value as a string before using it in the string replace function. Change-Id: I38bdc8d97c7d1f9b62f52b0a783bf4d3fde3611a Closes-Bug: #1250251 --- heat/engine/hot.py | 2 +- heat/tests/test_hot.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/heat/engine/hot.py b/heat/engine/hot.py index f3755d2eda..3599fa0e5e 100644 --- a/heat/engine/hot.py +++ b/heat/engine/hot.py @@ -248,7 +248,7 @@ class HOTemplate(template.Template): _('"params" parameter must be a dictionary')) for key in params.iterkeys(): value = params.get(key, '') or "" - text = text.replace(key, value) + text = text.replace(key, str(value)) return text match_str_replace = lambda k, v: k in ['str_replace', 'Fn::Replace'] diff --git a/heat/tests/test_hot.py b/heat/tests/test_hot.py index 401c0fa1f9..0919f320cf 100644 --- a/heat/tests/test_hot.py +++ b/heat/tests/test_hot.py @@ -177,6 +177,18 @@ class HOTemplateTest(HeatTestCase): self.assertEqual(snippet_resolved, tmpl.resolve_replace(snippet)) + def test_str_replace_number(self): + """Test str_replace function with numbers.""" + + snippet = {'str_replace': {'template': 'Template number string bar', + 'params': {'number': 1}}} + snippet_resolved = 'Template 1 string bar' + + tmpl = parser.Template(hot_tpl_empty) + + self.assertEqual(snippet_resolved, + tmpl.resolve_replace(snippet)) + def test_str_fn_replace(self): """Test Fn:Replace function."""