2012-05-10 10:11:14 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-05-09 16:05:06 +02:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
import ansible.utils
|
|
|
|
|
|
|
|
class TestUtils(unittest.TestCase):
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
### varReplace function tests
|
|
|
|
|
|
|
|
def test_varReplace_simple(self):
|
|
|
|
template = 'hello $who'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
2012-10-20 16:53:34 +02:00
|
|
|
def test_varReplace_trailing_dollar(self):
|
|
|
|
template = '$what $who $'
|
|
|
|
vars = dict(what='hello', who='world')
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-10-20 16:53:34 +02:00
|
|
|
assert res == 'hello world $'
|
|
|
|
|
2012-05-09 16:05:06 +02:00
|
|
|
def test_varReplace_multiple(self):
|
|
|
|
template = '$what $who'
|
|
|
|
vars = {
|
|
|
|
'what': 'hello',
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
2012-05-11 14:14:53 +02:00
|
|
|
def test_varReplace_caps(self):
|
|
|
|
template = 'hello $whoVar'
|
|
|
|
vars = {
|
|
|
|
'whoVar': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-11 14:14:53 +02:00
|
|
|
print res
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
2012-05-09 16:05:06 +02:00
|
|
|
def test_varReplace_middle(self):
|
|
|
|
template = 'hello $who!'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world!'
|
|
|
|
|
|
|
|
def test_varReplace_alternative(self):
|
|
|
|
template = 'hello ${who}'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_almost_alternative(self):
|
|
|
|
template = 'hello $who}'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world}'
|
|
|
|
|
|
|
|
def test_varReplace_almost_alternative2(self):
|
|
|
|
template = 'hello ${who'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_alternative_greed(self):
|
|
|
|
template = 'hello ${who} }'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world }'
|
|
|
|
|
|
|
|
def test_varReplace_notcomplex(self):
|
|
|
|
template = 'hello $mydata.who'
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': 'world',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
print res
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_nested(self):
|
|
|
|
template = 'hello ${data.who}'
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': 'world'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_nested_int(self):
|
|
|
|
template = '$what ${data.who}'
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': 2
|
|
|
|
},
|
|
|
|
'what': 'hello',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello 2'
|
|
|
|
|
2012-05-10 10:11:14 +02:00
|
|
|
def test_varReplace_unicode(self):
|
|
|
|
template = 'hello $who'
|
|
|
|
vars = {
|
|
|
|
'who': u'wórld',
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-10 10:11:14 +02:00
|
|
|
|
|
|
|
assert res == u'hello wórld'
|
|
|
|
|
2012-05-10 19:25:28 +02:00
|
|
|
def test_varReplace_list(self):
|
|
|
|
template = 'hello ${data[1]}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', 'world' ]
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-10 19:25:28 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_invalid_list(self):
|
|
|
|
template = 'hello ${data[1}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', 'world' ]
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-10 19:25:28 +02:00
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_list_oob(self):
|
|
|
|
template = 'hello ${data[2]}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', 'world' ]
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-10 19:25:28 +02:00
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_list_nolist(self):
|
|
|
|
template = 'hello ${data[1]}'
|
|
|
|
vars = {
|
|
|
|
'data': { 'no-one': 0, 'world': 1 }
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-10 19:25:28 +02:00
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_nested_list(self):
|
|
|
|
template = 'hello ${data[1].msg[0]}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', {'msg': [ 'world'] } ]
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-05-10 19:25:28 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
2012-09-28 00:56:10 +02:00
|
|
|
def test_varReplace_consecutive_vars(self):
|
|
|
|
vars = {
|
|
|
|
'foo': 'foo',
|
|
|
|
'bar': 'bar',
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${foo}${bar}'
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-09-28 00:56:10 +02:00
|
|
|
assert res == 'foobar'
|
|
|
|
|
|
|
|
def test_varReplace_escape_dot(self):
|
|
|
|
vars = {
|
|
|
|
'hostvars': {
|
|
|
|
'test.example.com': {
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${hostvars.{test.example.com}.foo}'
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-09-28 00:56:10 +02:00
|
|
|
assert res == 'bar'
|
|
|
|
|
2012-09-30 11:41:44 +02:00
|
|
|
def test_varReplace_list_join(self):
|
|
|
|
vars = {
|
|
|
|
'list': [
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
template = 'yum pkg=${list} state=installed'
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars, expand_lists=True)
|
2012-09-30 11:41:44 +02:00
|
|
|
assert res == 'yum pkg=foo,bar,baz state=installed'
|
|
|
|
|
2012-10-20 22:34:31 +02:00
|
|
|
def test_varReplace_escaped_var(self):
|
|
|
|
vars = {
|
|
|
|
'foo': 'bar',
|
|
|
|
}
|
|
|
|
template = 'action \$foo'
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-10-20 22:34:31 +02:00
|
|
|
assert res == 'action $foo'
|
|
|
|
|
2012-10-30 13:05:45 +01:00
|
|
|
def test_varReplace_var_part(self):
|
|
|
|
vars = {
|
|
|
|
'foo': {
|
|
|
|
'bar': 'result',
|
|
|
|
},
|
|
|
|
'key': 'bar',
|
|
|
|
}
|
|
|
|
template = 'test ${foo.$key}'
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-10-30 13:05:45 +01:00
|
|
|
assert res == 'test result'
|
|
|
|
|
|
|
|
def test_varReplace_var_partial_part(self):
|
|
|
|
vars = {
|
|
|
|
'foo': {
|
|
|
|
'barbaz': 'result',
|
|
|
|
},
|
|
|
|
'key': 'bar',
|
|
|
|
}
|
|
|
|
template = 'test ${foo.${key}baz}'
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.varReplace(None, template, vars)
|
2012-10-30 13:05:45 +01:00
|
|
|
assert res == 'test result'
|
|
|
|
|
2013-01-09 13:41:40 +01:00
|
|
|
def test_varReplace_var_complex_var(self):
|
|
|
|
vars = {
|
|
|
|
'x': '$y',
|
|
|
|
'y': {
|
|
|
|
'foo': 'result',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
template = '${x.foo}'
|
|
|
|
res = ansible.utils.template(None, template, vars)
|
|
|
|
assert res == 'result'
|
|
|
|
|
2012-06-18 00:12:42 +02:00
|
|
|
def test_template_varReplace_iterated(self):
|
|
|
|
template = 'hello $who'
|
|
|
|
vars = {
|
|
|
|
'who': 'oh great $person',
|
|
|
|
'person': 'one',
|
|
|
|
}
|
|
|
|
|
2012-09-17 14:02:30 +02:00
|
|
|
res = ansible.utils.template(None, template, vars)
|
2012-06-18 00:12:42 +02:00
|
|
|
|
|
|
|
assert res == u'hello oh great one'
|
|
|
|
|
2012-09-17 14:02:30 +02:00
|
|
|
def test_varReplace_include(self):
|
2012-11-14 11:16:53 +01:00
|
|
|
template = 'hello $FILE(world) $LOOKUP(file, $filename)'
|
2012-09-17 14:02:30 +02:00
|
|
|
|
2012-11-14 11:16:53 +01:00
|
|
|
res = ansible.utils.template("test", template, {'filename': 'world'}, expand_lists=True)
|
2012-09-17 14:02:30 +02:00
|
|
|
|
2012-11-01 11:06:55 +01:00
|
|
|
assert res == u'hello world world'
|
2012-09-17 14:02:30 +02:00
|
|
|
|
|
|
|
def test_varReplace_include_script(self):
|
2012-11-01 11:06:55 +01:00
|
|
|
template = 'hello $PIPE(echo world) $LOOKUP(pipe, echo world)'
|
2012-09-17 14:02:30 +02:00
|
|
|
|
2012-11-13 02:03:16 +01:00
|
|
|
res = ansible.utils.template("test", template, {}, expand_lists=True)
|
2012-09-17 14:02:30 +02:00
|
|
|
|
2012-11-01 11:06:55 +01:00
|
|
|
assert res == u'hello world world'
|
2012-09-17 14:02:30 +02:00
|
|
|
|
2012-10-01 17:07:51 +02:00
|
|
|
#####################################
|
2012-11-09 15:43:15 +01:00
|
|
|
### template_ds function tests
|
2012-10-01 17:07:51 +02:00
|
|
|
|
2012-11-09 15:43:15 +01:00
|
|
|
def test_template_ds_basic(self):
|
2012-10-01 17:07:51 +02:00
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'var': [
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'baz',
|
|
|
|
],
|
|
|
|
'types': [
|
|
|
|
'str',
|
|
|
|
u'unicode',
|
|
|
|
1,
|
|
|
|
1L,
|
|
|
|
1.2,
|
|
|
|
],
|
|
|
|
'alphas': '$alphas',
|
|
|
|
},
|
|
|
|
'alphas': [
|
|
|
|
'abc',
|
|
|
|
'def',
|
|
|
|
'ghi',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${data.var}'
|
2012-11-09 15:43:15 +01:00
|
|
|
res = ansible.utils.template_ds(None, template, vars)
|
2012-10-01 17:07:51 +02:00
|
|
|
assert sorted(res) == sorted(vars['data']['var'])
|
|
|
|
|
|
|
|
template = '${data.types}'
|
2012-11-09 15:43:15 +01:00
|
|
|
res = ansible.utils.template_ds(None, template, vars)
|
2012-10-01 17:07:51 +02:00
|
|
|
assert sorted(res) == sorted(vars['data']['types'])
|
|
|
|
|
|
|
|
template = '${data.alphas}'
|
2012-11-09 15:43:15 +01:00
|
|
|
res = ansible.utils.template_ds(None, template, vars)
|
2012-10-01 17:07:51 +02:00
|
|
|
assert sorted(res) == sorted(vars['alphas'])
|
|
|
|
|
2012-11-01 19:23:08 +01:00
|
|
|
template = '${data.nonexisting}'
|
2012-11-09 15:43:15 +01:00
|
|
|
res = ansible.utils.template_ds(None, template, vars)
|
2012-11-01 19:23:08 +01:00
|
|
|
assert res == template
|
|
|
|
|
2012-05-09 16:05:06 +02:00
|
|
|
#####################################
|
|
|
|
### Template function tests
|
|
|
|
|
|
|
|
def test_template_basic(self):
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-07-17 02:28:32 +02:00
|
|
|
res = ansible.utils.template_from_file("test", "template-basic", vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_template_whitespace(self):
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
2012-07-17 02:28:32 +02:00
|
|
|
res = ansible.utils.template_from_file("test", "template-whitespace", vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert res == 'hello world\n'
|
2012-05-10 10:11:14 +02:00
|
|
|
|
|
|
|
def test_template_unicode(self):
|
|
|
|
vars = {
|
|
|
|
'who': u'wórld',
|
|
|
|
}
|
|
|
|
|
2012-07-17 02:28:32 +02:00
|
|
|
res = ansible.utils.template_from_file("test", "template-basic", vars)
|
2012-05-10 10:11:14 +02:00
|
|
|
|
|
|
|
assert res == u'hello wórld'
|
2012-05-23 01:58:05 +02:00
|
|
|
|
|
|
|
#####################################
|
|
|
|
### key-value parsing
|
|
|
|
|
|
|
|
def test_parse_kv_basic(self):
|
|
|
|
assert (ansible.utils.parse_kv('a=simple b="with space" c="this=that"') ==
|
|
|
|
{'a': 'simple', 'b': 'with space', 'c': 'this=that'})
|