summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJeroen Hoekx <jeroen.hoekx@hamok.be>2012-05-09 16:05:06 +0200
committerJeroen Hoekx <jeroen.hoekx@hamok.be>2012-05-09 16:15:43 +0200
commit53bde0bf517d1302c80f80180f85995efa36a00e (patch)
treedc576e4e248b51b0c5ee6f32c78afd4807209c2c /test
parentc7c38e9a612c671a66665b0b18ce483c1ae57cff (diff)
downloadansible-53bde0bf517d1302c80f80180f85995efa36a00e.tar.gz
Support nested variables in varReplace.
Diffstat (limited to 'test')
-rw-r--r--test/TestUtils.py155
1 files changed, 155 insertions, 0 deletions
diff --git a/test/TestUtils.py b/test/TestUtils.py
new file mode 100644
index 0000000000..23459d84a2
--- /dev/null
+++ b/test/TestUtils.py
@@ -0,0 +1,155 @@
+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'