2015-05-16 20:45:01 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-02-08 21:09:34 +00:00
|
|
|
from jinja2.runtime import Context
|
|
|
|
|
2018-10-13 03:01:14 +00:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import patch
|
2015-05-16 20:45:01 +00:00
|
|
|
|
|
|
|
from ansible import constants as C
|
2017-02-08 21:09:34 +00:00
|
|
|
from ansible.errors import AnsibleError, AnsibleUndefinedVariable
|
2017-03-23 20:35:05 +00:00
|
|
|
from ansible.module_utils.six import string_types
|
2017-02-08 21:09:34 +00:00
|
|
|
from ansible.template import Templar, AnsibleContext, AnsibleEnvironment
|
2017-04-26 22:09:36 +00:00
|
|
|
from ansible.utils.unsafe_proxy import AnsibleUnsafe, wrap_var
|
2015-05-16 20:45:01 +00:00
|
|
|
from units.mock.loader import DictDataLoader
|
|
|
|
|
|
|
|
|
2017-02-08 21:09:34 +00:00
|
|
|
class BaseTemplar(object):
|
2015-05-16 20:45:01 +00:00
|
|
|
def setUp(self):
|
2017-02-08 21:09:34 +00:00
|
|
|
self.test_vars = dict(
|
2015-05-17 06:13:22 +00:00
|
|
|
foo="bar",
|
|
|
|
bam="{{foo}}",
|
|
|
|
num=1,
|
|
|
|
var_true=True,
|
|
|
|
var_false=False,
|
|
|
|
var_dict=dict(a="b"),
|
|
|
|
bad_dict="{a='b'",
|
|
|
|
var_list=[1],
|
|
|
|
recursive="{{recursive}}",
|
2017-02-08 21:09:34 +00:00
|
|
|
some_var="blip",
|
|
|
|
some_static_var="static_blip",
|
|
|
|
some_keyword="{{ foo }}",
|
|
|
|
some_unsafe_var=wrap_var("unsafe_blip"),
|
|
|
|
some_static_unsafe_var=wrap_var("static_unsafe_blip"),
|
|
|
|
some_unsafe_keyword=wrap_var("{{ foo }}"),
|
2018-11-29 14:56:23 +00:00
|
|
|
str_with_error="{{ 'str' | from_json }}",
|
2015-05-17 06:13:22 +00:00
|
|
|
)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.fake_loader = DictDataLoader({
|
|
|
|
"/path/to/my_file.txt": "foo\n",
|
|
|
|
})
|
|
|
|
self.templar = Templar(loader=self.fake_loader, variables=self.test_vars)
|
|
|
|
|
|
|
|
def is_unsafe(self, obj):
|
|
|
|
if obj is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if hasattr(obj, '__UNSAFE__'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if isinstance(obj, AnsibleUnsafe):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
for key in obj.keys():
|
|
|
|
if self.is_unsafe(key) or self.is_unsafe(obj[key]):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if isinstance(obj, list):
|
|
|
|
for item in obj:
|
|
|
|
if self.is_unsafe(item):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if isinstance(obj, string_types) and hasattr(obj, '__UNSAFE__'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# class used for testing arbitrary objects passed to template
|
|
|
|
class SomeClass(object):
|
|
|
|
foo = 'bar'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.blip = 'blip'
|
|
|
|
|
|
|
|
|
|
|
|
class SomeUnsafeClass(AnsibleUnsafe):
|
|
|
|
def __init__(self):
|
|
|
|
super(SomeUnsafeClass, self).__init__()
|
|
|
|
self.blip = 'unsafe blip'
|
|
|
|
|
|
|
|
|
|
|
|
class TestTemplarTemplate(BaseTemplar, unittest.TestCase):
|
|
|
|
def test_lookup_jinja_dict_key_in_static_vars(self):
|
|
|
|
res = self.templar.template("{'some_static_var': '{{ some_var }}'}",
|
2017-05-30 17:05:19 +00:00
|
|
|
static_vars=['some_static_var'])
|
|
|
|
# self.assertEqual(res['{{ a_keyword }}'], "blip")
|
2017-02-08 21:09:34 +00:00
|
|
|
print(res)
|
|
|
|
|
2019-06-25 15:54:37 +00:00
|
|
|
def test_is_possibly_template_true(self):
|
|
|
|
tests = [
|
|
|
|
'{{ foo }}',
|
|
|
|
'{% foo %}',
|
|
|
|
'{# foo #}',
|
|
|
|
'{# {{ foo }} #}',
|
|
|
|
'{# {{ nothing }} {# #}',
|
|
|
|
'{# {{ nothing }} {# #} #}',
|
|
|
|
'{% raw %}{{ foo }}{% endraw %}',
|
|
|
|
'{{',
|
|
|
|
'{%',
|
|
|
|
'{#',
|
|
|
|
'{% raw',
|
|
|
|
]
|
|
|
|
for test in tests:
|
|
|
|
self.assertTrue(self.templar.is_possibly_template(test))
|
|
|
|
|
|
|
|
def test_is_possibly_template_false(self):
|
|
|
|
tests = [
|
|
|
|
'{',
|
|
|
|
'%',
|
|
|
|
'#',
|
|
|
|
'foo',
|
|
|
|
'}}',
|
|
|
|
'%}',
|
|
|
|
'raw %}',
|
|
|
|
'#}',
|
|
|
|
]
|
|
|
|
for test in tests:
|
|
|
|
self.assertFalse(self.templar.is_possibly_template(test))
|
|
|
|
|
|
|
|
def test_is_possible_template(self):
|
|
|
|
"""This test ensures that a broken template still gets templated"""
|
|
|
|
# Purposefully invalid jinja
|
|
|
|
self.assertRaises(AnsibleError, self.templar.template, '{{ foo|default(False)) }}')
|
|
|
|
|
2019-06-06 19:49:27 +00:00
|
|
|
def test_is_template_true(self):
|
|
|
|
tests = [
|
|
|
|
'{{ foo }}',
|
|
|
|
'{% foo %}',
|
|
|
|
'{# foo #}',
|
|
|
|
'{# {{ foo }} #}',
|
|
|
|
'{# {{ nothing }} {# #}',
|
|
|
|
'{# {{ nothing }} {# #} #}',
|
|
|
|
'{% raw %}{{ foo }}{% endraw %}',
|
|
|
|
]
|
|
|
|
for test in tests:
|
|
|
|
self.assertTrue(self.templar.is_template(test))
|
|
|
|
|
|
|
|
def test_is_template_false(self):
|
|
|
|
tests = [
|
|
|
|
'foo',
|
|
|
|
'{{ foo',
|
|
|
|
'{% foo',
|
|
|
|
'{# foo',
|
|
|
|
'{{ foo %}',
|
|
|
|
'{{ foo #}',
|
|
|
|
'{% foo }}',
|
|
|
|
'{% foo #}',
|
|
|
|
'{# foo %}',
|
|
|
|
'{# foo }}',
|
|
|
|
'{{ foo {{',
|
|
|
|
'{% raw %}{% foo %}',
|
|
|
|
]
|
|
|
|
for test in tests:
|
|
|
|
self.assertFalse(self.templar.is_template(test))
|
|
|
|
|
|
|
|
def test_is_template_raw_string(self):
|
|
|
|
res = self.templar.is_template('foo')
|
|
|
|
self.assertFalse(res)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
2019-06-06 19:49:27 +00:00
|
|
|
def test_is_template_none(self):
|
|
|
|
res = self.templar.is_template(None)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertFalse(res)
|
|
|
|
|
|
|
|
def test_template_convert_bare_string(self):
|
|
|
|
res = self.templar.template('foo', convert_bare=True)
|
|
|
|
self.assertEqual(res, 'bar')
|
|
|
|
|
|
|
|
def test_template_convert_bare_nested(self):
|
2018-08-23 16:31:16 +00:00
|
|
|
res = self.templar.template('bam', convert_bare=True)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertEqual(res, 'bar')
|
|
|
|
|
|
|
|
def test_template_convert_bare_unsafe(self):
|
2018-08-23 16:31:16 +00:00
|
|
|
res = self.templar.template('some_unsafe_var', convert_bare=True)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertEqual(res, 'unsafe_blip')
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertTrue(self.is_unsafe(res), 'returned value from template.template (%s) is not marked unsafe' % res)
|
|
|
|
|
|
|
|
def test_template_convert_bare_filter(self):
|
2018-08-23 16:31:16 +00:00
|
|
|
res = self.templar.template('bam|capitalize', convert_bare=True)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertEqual(res, 'Bar')
|
|
|
|
|
|
|
|
def test_template_convert_bare_filter_unsafe(self):
|
2018-08-23 16:31:16 +00:00
|
|
|
res = self.templar.template('some_unsafe_var|capitalize', convert_bare=True)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertEqual(res, 'Unsafe_blip')
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertTrue(self.is_unsafe(res), 'returned value from template.template (%s) is not marked unsafe' % res)
|
|
|
|
|
|
|
|
def test_template_convert_data(self):
|
|
|
|
res = self.templar.template('{{foo}}', convert_data=True)
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertEqual(res, 'bar')
|
|
|
|
|
|
|
|
@patch('ansible.template.safe_eval', side_effect=AnsibleError)
|
|
|
|
def test_template_convert_data_template_in_data(self, mock_safe_eval):
|
|
|
|
res = self.templar.template('{{bam}}', convert_data=True)
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertEqual(res, 'bar')
|
|
|
|
|
|
|
|
def test_template_convert_data_bare(self):
|
|
|
|
res = self.templar.template('bam', convert_data=True)
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertEqual(res, 'bam')
|
|
|
|
|
|
|
|
def test_template_convert_data_to_json(self):
|
|
|
|
res = self.templar.template('{{bam|to_json}}', convert_data=True)
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertEqual(res, '"bar"')
|
|
|
|
|
|
|
|
def test_template_convert_data_convert_bare_data_bare(self):
|
|
|
|
res = self.templar.template('bam', convert_data=True, convert_bare=True)
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertEqual(res, 'bar')
|
|
|
|
|
|
|
|
def test_template_unsafe_non_string(self):
|
|
|
|
unsafe_obj = AnsibleUnsafe()
|
|
|
|
res = self.templar.template(unsafe_obj)
|
|
|
|
self.assertTrue(self.is_unsafe(res), 'returned value from template.template (%s) is not marked unsafe' % res)
|
|
|
|
|
|
|
|
def test_template_unsafe_non_string_subclass(self):
|
|
|
|
unsafe_obj = SomeUnsafeClass()
|
|
|
|
res = self.templar.template(unsafe_obj)
|
|
|
|
self.assertTrue(self.is_unsafe(res), 'returned value from template.template (%s) is not marked unsafe' % res)
|
|
|
|
|
|
|
|
def test_weird(self):
|
|
|
|
data = u'''1 2 #}huh{# %}ddfg{% }}dfdfg{{ {%what%} {{#foo#}} {%{bar}%} {#%blip%#} {{asdfsd%} 3 4 {{foo}} 5 6 7'''
|
|
|
|
self.assertRaisesRegexp(AnsibleError,
|
|
|
|
'template error while templating string',
|
|
|
|
self.templar.template,
|
|
|
|
data)
|
2015-09-04 00:45:17 +00:00
|
|
|
|
2018-11-29 14:56:23 +00:00
|
|
|
def test_template_with_error(self):
|
|
|
|
"""Check that AnsibleError is raised, fail if an unhandled exception is raised"""
|
|
|
|
self.assertRaises(AnsibleError, self.templar.template, "{{ str_with_error }}")
|
|
|
|
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
class TestTemplarMisc(BaseTemplar, unittest.TestCase):
|
2015-09-04 00:45:17 +00:00
|
|
|
def test_templar_simple(self):
|
|
|
|
|
|
|
|
templar = self.templar
|
2015-05-16 20:45:01 +00:00
|
|
|
# test some basic templating
|
|
|
|
self.assertEqual(templar.template("{{foo}}"), "bar")
|
2015-09-02 18:58:01 +00:00
|
|
|
self.assertEqual(templar.template("{{foo}}\n"), "bar\n")
|
2015-05-16 20:45:01 +00:00
|
|
|
self.assertEqual(templar.template("{{foo}}\n", preserve_trailing_newlines=True), "bar\n")
|
2015-09-02 18:58:01 +00:00
|
|
|
self.assertEqual(templar.template("{{foo}}\n", preserve_trailing_newlines=False), "bar")
|
2015-05-16 20:45:01 +00:00
|
|
|
self.assertEqual(templar.template("{{bam}}"), "bar")
|
|
|
|
self.assertEqual(templar.template("{{num}}"), 1)
|
|
|
|
self.assertEqual(templar.template("{{var_true}}"), True)
|
|
|
|
self.assertEqual(templar.template("{{var_false}}"), False)
|
|
|
|
self.assertEqual(templar.template("{{var_dict}}"), dict(a="b"))
|
|
|
|
self.assertEqual(templar.template("{{bad_dict}}"), "{a='b'")
|
|
|
|
self.assertEqual(templar.template("{{var_list}}"), [1])
|
2015-05-17 06:06:02 +00:00
|
|
|
self.assertEqual(templar.template(1, convert_bare=True), 1)
|
2015-06-11 16:47:29 +00:00
|
|
|
|
|
|
|
# force errors
|
2015-08-16 17:15:23 +00:00
|
|
|
self.assertRaises(AnsibleUndefinedVariable, templar.template, "{{bad_var}}")
|
2015-08-16 14:35:09 +00:00
|
|
|
self.assertRaises(AnsibleUndefinedVariable, templar.template, "{{lookup('file', bad_var)}}")
|
2015-05-17 06:06:02 +00:00
|
|
|
self.assertRaises(AnsibleError, templar.template, "{{lookup('bad_lookup')}}")
|
|
|
|
self.assertRaises(AnsibleError, templar.template, "{{recursive}}")
|
|
|
|
self.assertRaises(AnsibleUndefinedVariable, templar.template, "{{foo-bar}}")
|
|
|
|
|
|
|
|
# test with fail_on_undefined=False
|
2015-06-11 16:47:29 +00:00
|
|
|
self.assertEqual(templar.template("{{bad_var}}", fail_on_undefined=False), "{{bad_var}}")
|
2015-05-16 20:45:01 +00:00
|
|
|
|
2019-05-20 15:49:54 +00:00
|
|
|
# test setting available_variables
|
|
|
|
templar.available_variables = dict(foo="bam")
|
2015-05-16 20:45:01 +00:00
|
|
|
self.assertEqual(templar.template("{{foo}}"), "bam")
|
2019-05-20 15:49:54 +00:00
|
|
|
# variables must be a dict() for available_variables setter
|
|
|
|
# FIXME Use assertRaises() as a context manager (added in 2.7) once we do not run tests on Python 2.6 anymore.
|
|
|
|
try:
|
|
|
|
templar.available_variables = "foo=bam"
|
2019-09-17 17:28:37 +00:00
|
|
|
except AssertionError:
|
2019-05-20 15:49:54 +00:00
|
|
|
pass
|
2019-09-17 17:28:37 +00:00
|
|
|
except Exception as e:
|
2019-05-20 15:49:54 +00:00
|
|
|
self.fail(e)
|
2015-05-16 20:45:01 +00:00
|
|
|
|
2015-09-04 00:45:17 +00:00
|
|
|
def test_templar_escape_backslashes(self):
|
|
|
|
# Rule of thumb: If escape backslashes is True you should end up with
|
|
|
|
# the same number of backslashes as when you started.
|
|
|
|
self.assertEqual(self.templar.template("\t{{foo}}", escape_backslashes=True), "\tbar")
|
|
|
|
self.assertEqual(self.templar.template("\t{{foo}}", escape_backslashes=False), "\tbar")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo}}", escape_backslashes=True), "\\bar")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo}}", escape_backslashes=False), "\\bar")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo + '\t' }}", escape_backslashes=True), "\\bar\t")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo + '\t' }}", escape_backslashes=False), "\\bar\t")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo + '\\t' }}", escape_backslashes=True), "\\bar\\t")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo + '\\t' }}", escape_backslashes=False), "\\bar\t")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo + '\\\\t' }}", escape_backslashes=True), "\\bar\\\\t")
|
|
|
|
self.assertEqual(self.templar.template("\\{{foo + '\\\\t' }}", escape_backslashes=False), "\\bar\\t")
|
|
|
|
|
2015-05-16 20:45:01 +00:00
|
|
|
def test_template_jinja2_extensions(self):
|
|
|
|
fake_loader = DictDataLoader({})
|
|
|
|
templar = Templar(loader=fake_loader)
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2015-05-16 20:45:01 +00:00
|
|
|
old_exts = C.DEFAULT_JINJA2_EXTENSIONS
|
|
|
|
try:
|
|
|
|
C.DEFAULT_JINJA2_EXTENSIONS = "foo,bar"
|
|
|
|
self.assertEqual(templar._get_extensions(), ['foo', 'bar'])
|
|
|
|
finally:
|
|
|
|
C.DEFAULT_JINJA2_EXTENSIONS = old_exts
|
|
|
|
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
class TestTemplarLookup(BaseTemplar, unittest.TestCase):
|
|
|
|
def test_lookup_missing_plugin(self):
|
|
|
|
self.assertRaisesRegexp(AnsibleError,
|
2017-11-21 03:08:30 +00:00
|
|
|
r'lookup plugin \(not_a_real_lookup_plugin\) not found',
|
2017-02-08 21:09:34 +00:00
|
|
|
self.templar._lookup,
|
|
|
|
'not_a_real_lookup_plugin',
|
|
|
|
'an_arg', a_keyword_arg='a_keyword_arg_value')
|
|
|
|
|
|
|
|
def test_lookup_list(self):
|
|
|
|
res = self.templar._lookup('list', 'an_arg', 'another_arg')
|
|
|
|
self.assertEqual(res, 'an_arg,another_arg')
|
|
|
|
|
|
|
|
def test_lookup_jinja_undefined(self):
|
|
|
|
self.assertRaisesRegexp(AnsibleUndefinedVariable,
|
|
|
|
"'an_undefined_jinja_var' is undefined",
|
|
|
|
self.templar._lookup,
|
|
|
|
'list', '{{ an_undefined_jinja_var }}')
|
|
|
|
|
|
|
|
def test_lookup_jinja_defined(self):
|
|
|
|
res = self.templar._lookup('list', '{{ some_var }}')
|
|
|
|
self.assertTrue(self.is_unsafe(res))
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
def test_lookup_jinja_dict_string_passed(self):
|
|
|
|
self.assertRaisesRegexp(AnsibleError,
|
|
|
|
"with_dict expects a dict",
|
|
|
|
self.templar._lookup,
|
|
|
|
'dict',
|
|
|
|
'{{ some_var }}')
|
|
|
|
|
|
|
|
def test_lookup_jinja_dict_list_passed(self):
|
|
|
|
self.assertRaisesRegexp(AnsibleError,
|
|
|
|
"with_dict expects a dict",
|
|
|
|
self.templar._lookup,
|
|
|
|
'dict',
|
|
|
|
['foo', 'bar'])
|
|
|
|
|
|
|
|
def test_lookup_jinja_kwargs(self):
|
|
|
|
res = self.templar._lookup('list', 'blip', random_keyword='12345')
|
|
|
|
self.assertTrue(self.is_unsafe(res))
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
def test_lookup_jinja_list_wantlist(self):
|
|
|
|
res = self.templar._lookup('list', '{{ some_var }}', wantlist=True)
|
|
|
|
self.assertEqual(res, ["blip"])
|
|
|
|
|
|
|
|
def test_lookup_jinja_list_wantlist_undefined(self):
|
|
|
|
self.assertRaisesRegexp(AnsibleUndefinedVariable,
|
|
|
|
"'some_undefined_var' is undefined",
|
|
|
|
self.templar._lookup,
|
|
|
|
'list',
|
|
|
|
'{{ some_undefined_var }}',
|
|
|
|
wantlist=True)
|
|
|
|
|
|
|
|
def test_lookup_jinja_list_wantlist_unsafe(self):
|
|
|
|
res = self.templar._lookup('list', '{{ some_unsafe_var }}', wantlist=True)
|
|
|
|
for lookup_result in res:
|
|
|
|
self.assertTrue(self.is_unsafe(lookup_result))
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(lookup_result, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
# Should this be an AnsibleUnsafe
|
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
|
|
|
|
|
|
|
def test_lookup_jinja_dict(self):
|
|
|
|
res = self.templar._lookup('list', {'{{ a_keyword }}': '{{ some_var }}'})
|
|
|
|
self.assertEqual(res['{{ a_keyword }}'], "blip")
|
|
|
|
# TODO: Should this be an AnsibleUnsafe
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res['{{ a_keyword }}'], AnsibleUnsafe)
|
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
def test_lookup_jinja_dict_unsafe(self):
|
|
|
|
res = self.templar._lookup('list', {'{{ some_unsafe_key }}': '{{ some_unsafe_var }}'})
|
|
|
|
self.assertTrue(self.is_unsafe(res['{{ some_unsafe_key }}']))
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res['{{ some_unsafe_key }}'], AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
# TODO: Should this be an AnsibleUnsafe
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
def test_lookup_jinja_dict_unsafe_value(self):
|
|
|
|
res = self.templar._lookup('list', {'{{ a_keyword }}': '{{ some_unsafe_var }}'})
|
|
|
|
self.assertTrue(self.is_unsafe(res['{{ a_keyword }}']))
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res['{{ a_keyword }}'], AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
# TODO: Should this be an AnsibleUnsafe
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
|
|
|
|
def test_lookup_jinja_none(self):
|
|
|
|
res = self.templar._lookup('list', None)
|
|
|
|
self.assertIsNone(res)
|
|
|
|
|
|
|
|
|
|
|
|
class TestAnsibleContext(BaseTemplar, unittest.TestCase):
|
|
|
|
def _context(self, variables=None):
|
|
|
|
variables = variables or {}
|
|
|
|
|
|
|
|
env = AnsibleEnvironment()
|
|
|
|
context = AnsibleContext(env, parent={}, name='some_context',
|
|
|
|
blocks={})
|
|
|
|
|
|
|
|
for key, value in variables.items():
|
|
|
|
context.vars[key] = value
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
context = self._context()
|
|
|
|
self.assertIsInstance(context, AnsibleContext)
|
|
|
|
self.assertIsInstance(context, Context)
|
|
|
|
|
|
|
|
def test_resolve_unsafe(self):
|
|
|
|
context = self._context(variables={'some_unsafe_key': wrap_var('some_unsafe_string')})
|
|
|
|
res = context.resolve('some_unsafe_key')
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertTrue(self.is_unsafe(res),
|
|
|
|
'return of AnsibleContext.resolve (%s) was expected to be marked unsafe but was not' % res)
|
|
|
|
|
|
|
|
def test_resolve_unsafe_list(self):
|
|
|
|
context = self._context(variables={'some_unsafe_key': [wrap_var('some unsafe string 1')]})
|
|
|
|
res = context.resolve('some_unsafe_key')
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertIsInstance(res[0], AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertTrue(self.is_unsafe(res),
|
|
|
|
'return of AnsibleContext.resolve (%s) was expected to be marked unsafe but was not' % res)
|
|
|
|
|
|
|
|
def test_resolve_unsafe_dict(self):
|
|
|
|
context = self._context(variables={'some_unsafe_key':
|
|
|
|
{'an_unsafe_dict': wrap_var('some unsafe string 1')}
|
|
|
|
})
|
|
|
|
res = context.resolve('some_unsafe_key')
|
|
|
|
self.assertTrue(self.is_unsafe(res['an_unsafe_dict']),
|
|
|
|
'return of AnsibleContext.resolve (%s) was expected to be marked unsafe but was not' % res['an_unsafe_dict'])
|
|
|
|
|
|
|
|
def test_resolve(self):
|
|
|
|
context = self._context(variables={'some_key': 'some_string'})
|
|
|
|
res = context.resolve('some_key')
|
|
|
|
self.assertEqual(res, 'some_string')
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertNotIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertFalse(self.is_unsafe(res),
|
|
|
|
'return of AnsibleContext.resolve (%s) was not expected to be marked unsafe but was' % res)
|
|
|
|
|
|
|
|
def test_resolve_none(self):
|
|
|
|
context = self._context(variables={'some_key': None})
|
|
|
|
res = context.resolve('some_key')
|
|
|
|
self.assertEqual(res, None)
|
2017-05-30 17:05:19 +00:00
|
|
|
# self.assertNotIsInstance(res, AnsibleUnsafe)
|
2017-02-08 21:09:34 +00:00
|
|
|
self.assertFalse(self.is_unsafe(res),
|
|
|
|
'return of AnsibleContext.resolve (%s) was not expected to be marked unsafe but was' % res)
|