2012-05-10 10:11:14 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-05-09 16:05:06 +02:00
|
|
|
import unittest
|
2013-06-21 01:34:47 +02:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from nose.plugins.skip import SkipTest
|
2012-05-09 16:05:06 +02:00
|
|
|
|
|
|
|
import ansible.utils
|
2013-04-11 01:09:57 +02:00
|
|
|
import ansible.utils.template as template2
|
2012-05-09 16:05:06 +02:00
|
|
|
|
2013-12-09 19:49:03 +01:00
|
|
|
import sys
|
|
|
|
reload(sys)
|
|
|
|
sys.setdefaultencoding("utf8")
|
|
|
|
|
2012-05-09 16:05:06 +02:00
|
|
|
class TestUtils(unittest.TestCase):
|
|
|
|
|
2014-03-06 02:10:25 +01:00
|
|
|
def test_before_comment(self):
|
|
|
|
''' see if we can detect the part of a string before a comment. Used by INI parser in inventory '''
|
|
|
|
|
|
|
|
input = "before # comment"
|
|
|
|
expected = "before "
|
|
|
|
actual = ansible.utils.before_comment(input)
|
|
|
|
assert expected == actual
|
|
|
|
|
|
|
|
input = "before \# not a comment"
|
|
|
|
expected = "before # not a comment"
|
|
|
|
actual = ansible.utils.before_comment(input)
|
|
|
|
assert expected == actual
|
|
|
|
|
|
|
|
input = ""
|
|
|
|
expected = ""
|
|
|
|
actual = ansible.utils.before_comment(input)
|
|
|
|
assert expected == actual
|
|
|
|
|
|
|
|
input = "#"
|
|
|
|
expected = ""
|
|
|
|
actual = ansible.utils.before_comment(input)
|
|
|
|
assert expected == actual
|
|
|
|
|
2013-07-23 22:24:53 +02:00
|
|
|
#####################################
|
|
|
|
### check_conditional tests
|
|
|
|
|
|
|
|
def test_check_conditional_jinja2_literals(self):
|
|
|
|
# see http://jinja.pocoo.org/docs/templates/#literals
|
|
|
|
|
|
|
|
# boolean
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'true', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'false', '/', {}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'True', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'False', '/', {}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
# integer
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'1', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'0', '/', {}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
# string, beware, a string is truthy unless empty
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'"yes"', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'"no"', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'""', '/', {}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_conditional_jinja2_variable_literals(self):
|
|
|
|
# see http://jinja.pocoo.org/docs/templates/#literals
|
|
|
|
|
|
|
|
# boolean
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 'True'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 'true'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 'False'}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 'false'}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
# integer
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': '1'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 1}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': '0'}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 0}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
# string, beware, a string is truthy unless empty
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': '"yes"'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': '"no"'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': '""'}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
# Python boolean in Jinja2 expression
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': True}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': False}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_conditional_jinja2_expression(self):
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'1 == 1', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'bar == 42', '/', {'bar': 42}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'bar != 42', '/', {'bar': 42}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_conditional_jinja2_expression_in_variable(self):
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': '1 == 1'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 'bar == 42', 'bar': 42}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
'var', '/', {'var': 'bar != 42', 'bar': 42}) == False)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
def test_check_conditional_jinja2_unicode(self):
|
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
u'"\u00df"', '/', {}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
assert(ansible.utils.check_conditional(
|
2014-01-04 01:13:21 +01:00
|
|
|
u'var == "\u00df"', '/', {'var': u'\u00df'}) == True)
|
2013-07-23 22:24:53 +02:00
|
|
|
|
|
|
|
|
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'})
|
2013-06-21 01:34:47 +02:00
|
|
|
|