summaryrefslogtreecommitdiff
path: root/tests/units/TestUtils.py
diff options
context:
space:
mode:
authorMichael DeHaan <michael@ansibleworks.com>2014-02-20 19:10:38 -0500
committerMichael DeHaan <michael@ansibleworks.com>2014-02-20 19:10:38 -0500
commitde600f004018ec9ce81971a0d6fd55decacbb3f5 (patch)
tree2630532d6d8e9d3db19df9a5cc93d9f534eb1453 /tests/units/TestUtils.py
parent876da62f82d123ce6310a55684363913b00482ff (diff)
downloadansible-de600f004018ec9ce81971a0d6fd55decacbb3f5.tar.gz
Move integration tests into tests_new.
Diffstat (limited to 'tests/units/TestUtils.py')
-rw-r--r--tests/units/TestUtils.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/units/TestUtils.py b/tests/units/TestUtils.py
new file mode 100644
index 0000000000..4bddb4748b
--- /dev/null
+++ b/tests/units/TestUtils.py
@@ -0,0 +1,118 @@
+# -*- coding: utf-8 -*-
+
+import unittest
+import os
+import os.path
+import tempfile
+
+from nose.plugins.skip import SkipTest
+
+import ansible.utils
+import ansible.utils.template as template2
+
+import sys
+reload(sys)
+sys.setdefaultencoding("utf8")
+
+class TestUtils(unittest.TestCase):
+
+ #####################################
+ ### check_conditional tests
+
+ def test_check_conditional_jinja2_literals(self):
+ # see http://jinja.pocoo.org/docs/templates/#literals
+
+ # boolean
+ assert(ansible.utils.check_conditional(
+ 'true', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ 'false', '/', {}) == False)
+ assert(ansible.utils.check_conditional(
+ 'True', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ 'False', '/', {}) == False)
+
+ # integer
+ assert(ansible.utils.check_conditional(
+ '1', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ '0', '/', {}) == False)
+
+ # string, beware, a string is truthy unless empty
+ assert(ansible.utils.check_conditional(
+ '"yes"', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ '"no"', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ '""', '/', {}) == False)
+
+
+ def test_check_conditional_jinja2_variable_literals(self):
+ # see http://jinja.pocoo.org/docs/templates/#literals
+
+ # boolean
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 'True'}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 'true'}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 'False'}) == False)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 'false'}) == False)
+
+ # integer
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': '1'}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 1}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': '0'}) == False)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 0}) == False)
+
+ # string, beware, a string is truthy unless empty
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': '"yes"'}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': '"no"'}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': '""'}) == False)
+
+ # Python boolean in Jinja2 expression
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': True}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': False}) == False)
+
+
+ def test_check_conditional_jinja2_expression(self):
+ assert(ansible.utils.check_conditional(
+ '1 == 1', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ 'bar == 42', '/', {'bar': 42}) == True)
+ assert(ansible.utils.check_conditional(
+ 'bar != 42', '/', {'bar': 42}) == False)
+
+
+ def test_check_conditional_jinja2_expression_in_variable(self):
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': '1 == 1'}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 'bar == 42', 'bar': 42}) == True)
+ assert(ansible.utils.check_conditional(
+ 'var', '/', {'var': 'bar != 42', 'bar': 42}) == False)
+
+ def test_check_conditional_jinja2_unicode(self):
+ assert(ansible.utils.check_conditional(
+ u'"\u00df"', '/', {}) == True)
+ assert(ansible.utils.check_conditional(
+ u'var == "\u00df"', '/', {'var': u'\u00df'}) == True)
+
+
+ #####################################
+ ### 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'})
+