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.
119 lines
4.0 KiB
Python
119 lines
4.0 KiB
Python
13 years ago
|
# -*- coding: utf-8 -*-
|
||
|
|
||
13 years ago
|
import unittest
|
||
12 years ago
|
import os
|
||
|
import os.path
|
||
|
import tempfile
|
||
|
|
||
|
from nose.plugins.skip import SkipTest
|
||
13 years ago
|
|
||
|
import ansible.utils
|
||
12 years ago
|
import ansible.utils.template as template2
|
||
13 years ago
|
|
||
11 years ago
|
import sys
|
||
|
reload(sys)
|
||
|
sys.setdefaultencoding("utf8")
|
||
|
|
||
13 years ago
|
class TestUtils(unittest.TestCase):
|
||
|
|
||
11 years ago
|
#####################################
|
||
|
### check_conditional tests
|
||
|
|
||
|
def test_check_conditional_jinja2_literals(self):
|
||
|
# see http://jinja.pocoo.org/docs/templates/#literals
|
||
|
|
||
|
# boolean
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'true', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'false', '/', {}) == False)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'True', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'False', '/', {}) == False)
|
||
11 years ago
|
|
||
|
# integer
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'1', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'0', '/', {}) == False)
|
||
11 years ago
|
|
||
|
# string, beware, a string is truthy unless empty
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'"yes"', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'"no"', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'""', '/', {}) == False)
|
||
11 years ago
|
|
||
|
|
||
|
def test_check_conditional_jinja2_variable_literals(self):
|
||
|
# see http://jinja.pocoo.org/docs/templates/#literals
|
||
|
|
||
|
# boolean
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 'True'}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 'true'}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 'False'}) == False)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 'false'}) == False)
|
||
11 years ago
|
|
||
|
# integer
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': '1'}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 1}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': '0'}) == False)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 0}) == False)
|
||
11 years ago
|
|
||
|
# string, beware, a string is truthy unless empty
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': '"yes"'}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': '"no"'}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': '""'}) == False)
|
||
11 years ago
|
|
||
|
# Python boolean in Jinja2 expression
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': True}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': False}) == False)
|
||
11 years ago
|
|
||
|
|
||
|
def test_check_conditional_jinja2_expression(self):
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'1 == 1', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'bar == 42', '/', {'bar': 42}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'bar != 42', '/', {'bar': 42}) == False)
|
||
11 years ago
|
|
||
|
|
||
|
def test_check_conditional_jinja2_expression_in_variable(self):
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': '1 == 1'}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 'bar == 42', 'bar': 42}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
'var', '/', {'var': 'bar != 42', 'bar': 42}) == False)
|
||
11 years ago
|
|
||
|
def test_check_conditional_jinja2_unicode(self):
|
||
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
u'"\u00df"', '/', {}) == True)
|
||
11 years ago
|
assert(ansible.utils.check_conditional(
|
||
11 years ago
|
u'var == "\u00df"', '/', {'var': u'\u00df'}) == True)
|
||
11 years ago
|
|
||
|
|
||
13 years ago
|
#####################################
|
||
|
### 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'})
|
||
12 years ago
|
|