1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Some continued work on new-style templates and associated test code changes. Legacy template functions

are marked with "legacy_" for possible future removal.
This commit is contained in:
Michael DeHaan 2013-04-10 19:09:57 -04:00
parent f0b21dcc0f
commit 81a926547c
3 changed files with 50 additions and 43 deletions

View file

@ -18,7 +18,7 @@
import os import os
from ansible import utils from ansible import utils
from ansible import template import ansible.utils.template as template
from ansible import errors from ansible import errors
from ansible.runner.return_data import ReturnData from ansible.runner.return_data import ReturnData
import base64 import base64

View file

@ -23,6 +23,7 @@ from jinja2.runtime import StrictUndefined
import yaml import yaml
import json import json
from ansible import errors from ansible import errors
import ansible.utils as utils
import ansible.constants as C import ansible.constants as C
import time import time
import subprocess import subprocess
@ -174,7 +175,8 @@ def _legacy_varFind(basedir, text, vars, lookup_fatal, depth, expand_lists):
lookup_plugin_name, args = args.split(",", 1) lookup_plugin_name, args = args.split(",", 1)
args = args.strip() args = args.strip()
# args have to be templated # args have to be templated
args = varReplace(basedir, args, vars, lookup_fatal, depth + 1, True) args = legacy_varReplace(basedir, args, vars, lookup_fatal, depth + 1, True)
instance = utils.plugins.lookup_loader.get(lookup_plugin_name.lower(), basedir=basedir) instance = utils.plugins.lookup_loader.get(lookup_plugin_name.lower(), basedir=basedir)
if instance is not None: if instance is not None:
try: try:
@ -186,6 +188,7 @@ def _legacy_varFind(basedir, text, vars, lookup_fatal, depth, expand_lists):
replacement = None replacement = None
else: else:
raise raise
else: else:
replacement = None replacement = None
return {'replacement': replacement, 'start': start, 'end': end} return {'replacement': replacement, 'start': start, 'end': end}
@ -222,7 +225,7 @@ def legacy_varReplace(basedir, raw, vars, lookup_fatal=True, depth=0, expand_lis
if expand_lists and isinstance(replacement, (list, tuple)): if expand_lists and isinstance(replacement, (list, tuple)):
replacement = ",".join([str(x) for x in replacement]) replacement = ",".join([str(x) for x in replacement])
if isinstance(replacement, (str, unicode)): if isinstance(replacement, (str, unicode)):
replacement = varReplace(basedir, replacement, vars, lookup_fatal, depth=depth+1, expand_lists=expand_lists) replacement = legacy_varReplace(basedir, replacement, vars, lookup_fatal, depth=depth+1, expand_lists=expand_lists)
if replacement is None: if replacement is None:
replacement = raw[m['start']:m['end']] replacement = raw[m['start']:m['end']]
@ -436,10 +439,14 @@ def template_from_string(basedir, data, vars):
else: else:
return data return data
def test_foo(): def lookup(name, *args, **kwargs):
return 'test_foo!' instance = utils.plugins.lookup_loader.get(name.lower(), basedir=basedir)
if instance is not None:
return ",".join(instance.run(*args, inject=vars, **kwargs))
else:
raise errors.AnsibleError("lookup plugin (%s) not found" % name)
t.globals['test_foo'] = test_foo t.globals['lookup'] = lookup
res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True))) res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True)))
return res return res

View file

@ -3,7 +3,7 @@
import unittest import unittest
import ansible.utils import ansible.utils
import ansible.template as template import ansible.utils.template as template2
class TestUtils(unittest.TestCase): class TestUtils(unittest.TestCase):
@ -16,14 +16,14 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world' assert res == 'hello world'
def test_varReplace_trailing_dollar(self): def test_varReplace_trailing_dollar(self):
template = '$what $who $' template = '$what $who $'
vars = dict(what='hello', who='world') vars = dict(what='hello', who='world')
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world $' assert res == 'hello world $'
def test_varReplace_multiple(self): def test_varReplace_multiple(self):
@ -33,7 +33,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world' assert res == 'hello world'
@ -43,7 +43,7 @@ class TestUtils(unittest.TestCase):
'whoVar': 'world', 'whoVar': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
print res print res
assert res == 'hello world' assert res == 'hello world'
@ -53,7 +53,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world!' assert res == 'hello world!'
@ -63,7 +63,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world' assert res == 'hello world'
@ -73,7 +73,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world}' assert res == 'hello world}'
@ -83,7 +83,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == template assert res == template
@ -93,7 +93,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world }' assert res == 'hello world }'
@ -105,7 +105,7 @@ class TestUtils(unittest.TestCase):
}, },
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
print res print res
assert res == template assert res == template
@ -118,7 +118,7 @@ class TestUtils(unittest.TestCase):
}, },
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world' assert res == 'hello world'
@ -131,7 +131,7 @@ class TestUtils(unittest.TestCase):
'what': 'hello', 'what': 'hello',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello 2' assert res == 'hello 2'
@ -141,7 +141,7 @@ class TestUtils(unittest.TestCase):
'who': u'wórld', 'who': u'wórld',
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == u'hello wórld' assert res == u'hello wórld'
@ -151,7 +151,7 @@ class TestUtils(unittest.TestCase):
'data': [ 'no-one', 'world' ] 'data': [ 'no-one', 'world' ]
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world' assert res == 'hello world'
@ -161,7 +161,7 @@ class TestUtils(unittest.TestCase):
'data': [ 'no-one', 'world' ] 'data': [ 'no-one', 'world' ]
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == template assert res == template
@ -171,7 +171,7 @@ class TestUtils(unittest.TestCase):
'data': [ 'no-one', 'world' ] 'data': [ 'no-one', 'world' ]
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == template assert res == template
@ -181,7 +181,7 @@ class TestUtils(unittest.TestCase):
'data': { 'no-one': 0, 'world': 1 } 'data': { 'no-one': 0, 'world': 1 }
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == template assert res == template
@ -191,7 +191,7 @@ class TestUtils(unittest.TestCase):
'data': [ 'no-one', {'msg': [ 'world'] } ] 'data': [ 'no-one', {'msg': [ 'world'] } ]
} }
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'hello world' assert res == 'hello world'
@ -202,7 +202,7 @@ class TestUtils(unittest.TestCase):
} }
template = '${foo}${bar}' template = '${foo}${bar}'
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'foobar' assert res == 'foobar'
def test_varReplace_escape_dot(self): def test_varReplace_escape_dot(self):
@ -215,7 +215,7 @@ class TestUtils(unittest.TestCase):
} }
template = '${hostvars.{test.example.com}.foo}' template = '${hostvars.{test.example.com}.foo}'
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'bar' assert res == 'bar'
def test_varReplace_list_join(self): def test_varReplace_list_join(self):
@ -228,7 +228,7 @@ class TestUtils(unittest.TestCase):
} }
template = 'yum pkg=${list} state=installed' template = 'yum pkg=${list} state=installed'
res = ansible.utils.varReplace(None, template, vars, expand_lists=True) res = template2.legacy_varReplace(None, template, vars, expand_lists=True)
assert res == 'yum pkg=foo,bar,baz state=installed' assert res == 'yum pkg=foo,bar,baz state=installed'
def test_varReplace_escaped_var(self): def test_varReplace_escaped_var(self):
@ -236,7 +236,7 @@ class TestUtils(unittest.TestCase):
'foo': 'bar', 'foo': 'bar',
} }
template = 'action \$foo' template = 'action \$foo'
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'action $foo' assert res == 'action $foo'
def test_varReplace_var_part(self): def test_varReplace_var_part(self):
@ -247,7 +247,7 @@ class TestUtils(unittest.TestCase):
'key': 'bar', 'key': 'bar',
} }
template = 'test ${foo.$key}' template = 'test ${foo.$key}'
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'test result' assert res == 'test result'
def test_varReplace_var_partial_part(self): def test_varReplace_var_partial_part(self):
@ -258,7 +258,7 @@ class TestUtils(unittest.TestCase):
'key': 'bar', 'key': 'bar',
} }
template = 'test ${foo.${key}baz}' template = 'test ${foo.${key}baz}'
res = ansible.utils.varReplace(None, template, vars) res = template2.legacy_varReplace(None, template, vars)
assert res == 'test result' assert res == 'test result'
def test_varReplace_var_complex_var(self): def test_varReplace_var_complex_var(self):
@ -269,7 +269,7 @@ class TestUtils(unittest.TestCase):
}, },
} }
template = '${x.foo}' template = '${x.foo}'
res = template.template(None, template, vars) res = template2.template(None, template, vars)
assert res == 'result' assert res == 'result'
def test_template_varReplace_iterated(self): def test_template_varReplace_iterated(self):
@ -279,21 +279,21 @@ class TestUtils(unittest.TestCase):
'person': 'one', 'person': 'one',
} }
res = template.template(None, template, vars) res = template2.template(None, template, vars)
assert res == u'hello oh great one' assert res == u'hello oh great one'
def test_varReplace_include(self): def test_varReplace_include(self):
template = 'hello $FILE(world) $LOOKUP(file, $filename)' template = 'hello $FILE(world) $LOOKUP(file, $filename)'
res = template.template("test", template, {'filename': 'world'}, expand_lists=True) res = template2.template("test", template, {'filename': 'world'}, expand_lists=True)
assert res == u'hello world world' assert res == u'hello world world'
def test_varReplace_include_script(self): def test_varReplace_include_script(self):
template = 'hello $PIPE(echo world) $LOOKUP(pipe, echo world)' template = 'hello $PIPE(echo world) $LOOKUP(pipe, echo world)'
res = template.template("test", template, {}, expand_lists=True) res = template2.template("test", template, {}, expand_lists=True)
assert res == u'hello world world' assert res == u'hello world world'
@ -325,19 +325,19 @@ class TestUtils(unittest.TestCase):
} }
template = '${data.var}' template = '${data.var}'
res = template.template(None, template, vars) res = template2.template(None, template, vars)
assert sorted(res) == sorted(vars['data']['var']) assert sorted(res) == sorted(vars['data']['var'])
template = '${data.types}' template = '${data.types}'
res = template.template(None, template, vars) res = template2.template(None, template, vars)
assert sorted(res) == sorted(vars['data']['types']) assert sorted(res) == sorted(vars['data']['types'])
template = '${data.alphas}' template = '${data.alphas}'
res = template.template(None, template, vars) res = template2.template(None, template, vars)
assert sorted(res) == sorted(vars['alphas']) assert sorted(res) == sorted(vars['alphas'])
template = '${data.nonexisting}' template = '${data.nonexisting}'
res = template.template(None, template, vars) res = template2.template(None, template, vars)
assert res == template assert res == template
##################################### #####################################
@ -348,7 +348,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = template.template_from_file("test", "template-basic", vars) res = template2.template_from_file("test", "template-basic", vars)
assert res == 'hello world' assert res == 'hello world'
@ -357,7 +357,7 @@ class TestUtils(unittest.TestCase):
'who': 'world', 'who': 'world',
} }
res = template.template_from_file("test", "template-whitespace", vars) res = template2.template_from_file("test", "template-whitespace", vars)
assert res == 'hello world\n' assert res == 'hello world\n'
@ -366,7 +366,7 @@ class TestUtils(unittest.TestCase):
'who': u'wórld', 'who': u'wórld',
} }
res = template.template_from_file("test", "template-basic", vars) res = template2.template_from_file("test", "template-basic", vars)
assert res == u'hello wórld' assert res == u'hello wórld'