2018-11-01 22:42:32 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2018 Matt Martz <matt@sivel.net>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
from ansible.module_utils.six import PY3
|
2019-08-07 17:39:01 +02:00
|
|
|
from ansible.utils.unsafe_proxy import AnsibleUnsafe, AnsibleUnsafeBytes, AnsibleUnsafeText, wrap_var
|
2018-11-01 22:42:32 +01:00
|
|
|
|
|
|
|
|
2019-08-07 17:39:01 +02:00
|
|
|
def test_wrap_var_text():
|
|
|
|
assert isinstance(wrap_var(u'foo'), AnsibleUnsafeText)
|
|
|
|
|
2018-11-01 22:42:32 +01:00
|
|
|
|
2019-08-07 17:39:01 +02:00
|
|
|
def test_wrap_var_bytes():
|
|
|
|
assert isinstance(wrap_var(b'foo'), AnsibleUnsafeBytes)
|
2018-11-01 22:42:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_string():
|
|
|
|
if PY3:
|
2019-08-07 17:39:01 +02:00
|
|
|
assert isinstance(wrap_var('foo'), AnsibleUnsafeText)
|
2018-11-01 22:42:32 +01:00
|
|
|
else:
|
2019-08-07 17:39:01 +02:00
|
|
|
assert isinstance(wrap_var('foo'), AnsibleUnsafeBytes)
|
2018-11-01 22:42:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_dict():
|
|
|
|
assert isinstance(wrap_var(dict(foo='bar')), dict)
|
|
|
|
assert not isinstance(wrap_var(dict(foo='bar')), AnsibleUnsafe)
|
2019-08-07 17:39:01 +02:00
|
|
|
assert isinstance(wrap_var(dict(foo=u'bar'))['foo'], AnsibleUnsafeText)
|
2018-11-01 22:42:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_dict_None():
|
|
|
|
assert wrap_var(dict(foo=None))['foo'] is None
|
|
|
|
assert not isinstance(wrap_var(dict(foo=None))['foo'], AnsibleUnsafe)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_list():
|
|
|
|
assert isinstance(wrap_var(['foo']), list)
|
|
|
|
assert not isinstance(wrap_var(['foo']), AnsibleUnsafe)
|
2019-08-07 17:39:01 +02:00
|
|
|
assert isinstance(wrap_var([u'foo'])[0], AnsibleUnsafeText)
|
2018-11-01 22:42:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_list_None():
|
|
|
|
assert wrap_var([None])[0] is None
|
|
|
|
assert not isinstance(wrap_var([None])[0], AnsibleUnsafe)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_set():
|
|
|
|
assert isinstance(wrap_var(set(['foo'])), set)
|
|
|
|
assert not isinstance(wrap_var(set(['foo'])), AnsibleUnsafe)
|
2019-08-07 17:39:01 +02:00
|
|
|
for item in wrap_var(set([u'foo'])):
|
2018-11-01 22:42:32 +01:00
|
|
|
assert isinstance(item, AnsibleUnsafeText)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_set_None():
|
|
|
|
for item in wrap_var(set([None])):
|
|
|
|
assert item is None
|
|
|
|
assert not isinstance(item, AnsibleUnsafe)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_tuple():
|
|
|
|
assert isinstance(wrap_var(('foo',)), tuple)
|
|
|
|
assert not isinstance(wrap_var(('foo',)), AnsibleUnsafe)
|
2020-01-07 15:41:37 +01:00
|
|
|
assert isinstance(wrap_var(('foo',))[0], AnsibleUnsafe)
|
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_tuple_None():
|
|
|
|
assert wrap_var((None,))[0] is None
|
|
|
|
assert not isinstance(wrap_var((None,))[0], AnsibleUnsafe)
|
2018-11-01 22:42:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_wrap_var_None():
|
|
|
|
assert wrap_var(None) is None
|
|
|
|
assert not isinstance(wrap_var(None), AnsibleUnsafe)
|
|
|
|
|
|
|
|
|
2019-08-07 17:39:01 +02:00
|
|
|
def test_wrap_var_unsafe_text():
|
2018-11-01 22:42:32 +01:00
|
|
|
assert isinstance(wrap_var(AnsibleUnsafeText(u'foo')), AnsibleUnsafeText)
|
|
|
|
|
|
|
|
|
2019-08-07 17:39:01 +02:00
|
|
|
def test_wrap_var_unsafe_bytes():
|
|
|
|
assert isinstance(wrap_var(AnsibleUnsafeBytes(b'foo')), AnsibleUnsafeBytes)
|
|
|
|
|
|
|
|
|
2020-01-07 15:41:37 +01:00
|
|
|
def test_wrap_var_no_ref():
|
|
|
|
thing = {
|
|
|
|
'foo': {
|
|
|
|
'bar': 'baz'
|
|
|
|
},
|
|
|
|
'bar': ['baz', 'qux'],
|
|
|
|
'baz': ('qux',),
|
|
|
|
'none': None,
|
|
|
|
'text': 'text',
|
|
|
|
}
|
|
|
|
wrapped_thing = wrap_var(thing)
|
|
|
|
thing is not wrapped_thing
|
|
|
|
thing['foo'] is not wrapped_thing['foo']
|
|
|
|
thing['bar'][0] is not wrapped_thing['bar'][0]
|
|
|
|
thing['baz'][0] is not wrapped_thing['baz'][0]
|
|
|
|
thing['none'] is not wrapped_thing['none']
|
|
|
|
thing['text'] is not wrapped_thing['text']
|
|
|
|
|
|
|
|
|
2018-11-01 22:42:32 +01:00
|
|
|
def test_AnsibleUnsafeText():
|
|
|
|
assert isinstance(AnsibleUnsafeText(u'foo'), AnsibleUnsafe)
|
2019-08-07 17:39:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_AnsibleUnsafeBytes():
|
|
|
|
assert isinstance(AnsibleUnsafeBytes(b'foo'), AnsibleUnsafe)
|