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):
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
### varLookup function tests
|
|
|
|
|
|
|
|
def test_varLookup_list(self):
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': ['joe', 'jack', 'jeff']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 14:02:30 +02:00
|
|
|
res = ansible.utils.varLookup('${data.who}', vars)
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
assert sorted(res) == sorted(vars['data']['who'])
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
### varReplace function tests
|
|
|
|
|
|
|
|
def test_varReplace_simple(self):
|
|
|
|
template = 'hello $who'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_multiple(self):
|
|
|
|
template = '$what $who'
|
|
|
|
vars = {
|
|
|
|
'what': 'hello',
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
2012-05-11 14:14:53 +02:00
|
|
|
def test_varReplace_caps(self):
|
|
|
|
template = 'hello $whoVar'
|
|
|
|
vars = {
|
|
|
|
'whoVar': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world!'
|
|
|
|
|
|
|
|
def test_varReplace_alternative(self):
|
|
|
|
template = 'hello ${who}'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_almost_alternative(self):
|
|
|
|
template = 'hello $who}'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world}'
|
|
|
|
|
|
|
|
def test_varReplace_almost_alternative2(self):
|
|
|
|
template = 'hello ${who'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_alternative_greed(self):
|
|
|
|
template = 'hello ${who} }'
|
|
|
|
vars = {
|
|
|
|
'who': 'world',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world }'
|
|
|
|
|
|
|
|
def test_varReplace_notcomplex(self):
|
|
|
|
template = 'hello $mydata.who'
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': 'world',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
print res
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_nested(self):
|
|
|
|
template = 'hello ${data.who}'
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': 'world'
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_nested_int(self):
|
|
|
|
template = '$what ${data.who}'
|
|
|
|
vars = {
|
|
|
|
'data': {
|
|
|
|
'who': 2
|
|
|
|
},
|
|
|
|
'what': 'hello',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
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' ]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
|
|
|
def test_varReplace_invalid_list(self):
|
|
|
|
template = 'hello ${data[1}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', 'world' ]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_list_oob(self):
|
|
|
|
template = 'hello ${data[2]}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', 'world' ]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_list_nolist(self):
|
|
|
|
template = 'hello ${data[1]}'
|
|
|
|
vars = {
|
|
|
|
'data': { 'no-one': 0, 'world': 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == template
|
|
|
|
|
|
|
|
def test_varReplace_nested_list(self):
|
|
|
|
template = 'hello ${data[1].msg[0]}'
|
|
|
|
vars = {
|
|
|
|
'data': [ 'no-one', {'msg': [ 'world'] } ]
|
|
|
|
}
|
|
|
|
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
|
|
|
|
assert res == 'hello world'
|
|
|
|
|
2012-09-25 15:43:25 +02:00
|
|
|
def test_varReplace_repr_basic(self):
|
|
|
|
vars = {
|
|
|
|
'color': '$favorite_color',
|
|
|
|
'favorite_color': 'blue',
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '$color == "blue"'
|
|
|
|
res = ansible.utils.varReplace(template, vars, do_repr=True)
|
|
|
|
assert eval(res)
|
|
|
|
|
|
|
|
def test_varReplace_repr_varinvar(self):
|
|
|
|
vars = {
|
|
|
|
'foo': 'foo',
|
|
|
|
'bar': 'bar',
|
|
|
|
'foobar': '$foo$bar',
|
|
|
|
'var': {
|
|
|
|
'foo': 'foo',
|
|
|
|
'foobar': '$foo$bar',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '$foobar == "foobar"'
|
|
|
|
res = ansible.utils.varReplace(template, vars, do_repr=True)
|
|
|
|
assert eval(res)
|
|
|
|
|
|
|
|
def test_varReplace_repr_varindex(self):
|
|
|
|
vars = {
|
|
|
|
'foo': 'foo',
|
|
|
|
'var': {
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${var.$foo} == "bar"'
|
|
|
|
res = ansible.utils.varReplace(template, vars, do_repr=True)
|
|
|
|
assert eval(res)
|
|
|
|
|
|
|
|
def test_varReplace_repr_varpartindex(self):
|
|
|
|
vars = {
|
|
|
|
'foo': 'foo',
|
|
|
|
'var': {
|
|
|
|
'foobar': 'foobar',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${var.${foo}bar} == "foobar"'
|
|
|
|
res = ansible.utils.varReplace(template, vars, do_repr=True)
|
|
|
|
assert eval(res)
|
|
|
|
|
2012-09-27 18:36:52 +02:00
|
|
|
def test_varReplace_repr_nonstr(self):
|
|
|
|
vars = {
|
|
|
|
'foo': True,
|
|
|
|
'bar': 1L,
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${foo} == $bar'
|
|
|
|
res = ansible.utils.varReplace(template, vars, do_repr=True)
|
|
|
|
assert res == 'True == 1L'
|
|
|
|
|
2012-09-28 00:56:10 +02:00
|
|
|
def test_varReplace_consecutive_vars(self):
|
|
|
|
vars = {
|
|
|
|
'foo': 'foo',
|
|
|
|
'bar': 'bar',
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${foo}${bar}'
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
assert res == 'foobar'
|
|
|
|
|
|
|
|
def test_varReplace_escape_dot(self):
|
|
|
|
vars = {
|
|
|
|
'hostvars': {
|
|
|
|
'test.example.com': {
|
|
|
|
'foo': 'bar',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
template = '${hostvars.{test.example.com}.foo}'
|
|
|
|
res = ansible.utils.varReplace(template, vars)
|
|
|
|
assert res == 'bar'
|
|
|
|
|
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-09-19 03:34:55 +02:00
|
|
|
template = 'hello $FILE(world)'
|
2012-09-17 14:02:30 +02:00
|
|
|
|
|
|
|
res = ansible.utils.template("test", template, {})
|
|
|
|
|
|
|
|
assert res == u'hello world\n'
|
|
|
|
|
|
|
|
def test_varReplace_include_script(self):
|
2012-09-19 03:34:55 +02:00
|
|
|
template = 'hello $PIPE(echo world)'
|
2012-09-17 14:02:30 +02:00
|
|
|
|
|
|
|
res = ansible.utils.template("test", template, {})
|
|
|
|
|
|
|
|
assert res == u'hello world\n'
|
|
|
|
|
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'})
|