mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
3.4 KiB
Python
156 lines
3.4 KiB
Python
13 years ago
|
import os
|
||
|
import unittest
|
||
|
|
||
|
import ansible.utils
|
||
|
|
||
|
class TestUtils(unittest.TestCase):
|
||
|
|
||
|
#####################################
|
||
|
### varLookup function tests
|
||
|
|
||
|
def test_varLookup_list(self):
|
||
|
vars = {
|
||
|
'data': {
|
||
|
'who': ['joe', 'jack', 'jeff']
|
||
|
}
|
||
|
}
|
||
|
|
||
|
res = ansible.utils.varLookup('data.who', vars)
|
||
|
|
||
|
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'
|
||
|
|
||
|
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'
|
||
|
|
||
|
#####################################
|
||
|
### Template function tests
|
||
|
|
||
|
def test_template_basic(self):
|
||
|
template = 'hello {{ who }}'
|
||
|
vars = {
|
||
|
'who': 'world',
|
||
|
}
|
||
|
|
||
|
res = ansible.utils.template(template, vars, {})
|
||
|
|
||
|
assert res == 'hello world'
|
||
|
|
||
|
def test_template_whitespace(self):
|
||
|
template = 'hello {{ who }}\n'
|
||
|
vars = {
|
||
|
'who': 'world',
|
||
|
}
|
||
|
|
||
|
res = ansible.utils.template(template, vars, {})
|
||
|
|
||
|
assert res == 'hello world\n'
|