2015-10-20 07:32:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2015, Toshio Kuratomi <tkuratomi@ansible.com>
|
2017-12-18 19:17:13 +01:00
|
|
|
# (c) 2017, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2015-10-20 07:32:21 +02:00
|
|
|
|
2017-12-18 19:17:13 +01:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-10-20 07:32:21 +02:00
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
from ansible.compat.tests import unittest
|
|
|
|
|
2015-10-21 03:51:34 +02:00
|
|
|
from ansible.module_utils.basic import return_values, remove_values
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestReturnValues(unittest.TestCase):
|
|
|
|
dataset = (
|
2017-01-29 08:28:53 +01:00
|
|
|
('string', frozenset(['string'])),
|
|
|
|
('', frozenset()),
|
|
|
|
(1, frozenset(['1'])),
|
|
|
|
(1.0, frozenset(['1.0'])),
|
|
|
|
(False, frozenset()),
|
|
|
|
(['1', '2', '3'], frozenset(['1', '2', '3'])),
|
|
|
|
(('1', '2', '3'), frozenset(['1', '2', '3'])),
|
|
|
|
({'one': 1, 'two': 'dos'}, frozenset(['1', 'dos'])),
|
2017-05-30 19:05:19 +02:00
|
|
|
(
|
|
|
|
{
|
|
|
|
'one': 1,
|
|
|
|
'two': 'dos',
|
|
|
|
'three': [
|
|
|
|
'amigos', 'musketeers', None, {
|
|
|
|
'ping': 'pong',
|
|
|
|
'base': (
|
|
|
|
'balls', 'raquets'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
frozenset(['1', 'dos', 'amigos', 'musketeers', 'pong', 'balls', 'raquets'])
|
|
|
|
),
|
2017-01-29 08:28:53 +01:00
|
|
|
(u'Toshio くらとみ', frozenset(['Toshio くらとみ'])),
|
|
|
|
('Toshio くらとみ', frozenset(['Toshio くらとみ'])),
|
|
|
|
)
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
def test_return_values(self):
|
|
|
|
for data, expected in self.dataset:
|
2015-10-21 03:51:34 +02:00
|
|
|
self.assertEquals(frozenset(return_values(data)), expected)
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
def test_unknown_type(self):
|
2015-10-21 03:51:34 +02:00
|
|
|
self.assertRaises(TypeError, frozenset, return_values(object()))
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestRemoveValues(unittest.TestCase):
|
|
|
|
OMIT = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER'
|
|
|
|
dataset_no_remove = (
|
2017-01-29 08:28:53 +01:00
|
|
|
('string', frozenset(['nope'])),
|
|
|
|
(1234, frozenset(['4321'])),
|
|
|
|
(False, frozenset(['4321'])),
|
|
|
|
(1.0, frozenset(['4321'])),
|
|
|
|
(['string', 'strang', 'strung'], frozenset(['nope'])),
|
|
|
|
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['nope'])),
|
2017-05-30 19:05:19 +02:00
|
|
|
(
|
|
|
|
{
|
|
|
|
'one': 1,
|
|
|
|
'two': 'dos',
|
|
|
|
'three': [
|
|
|
|
'amigos', 'musketeers', None, {
|
|
|
|
'ping': 'pong', 'base': ['balls', 'raquets']
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
frozenset(['nope'])
|
|
|
|
),
|
2017-05-21 15:18:35 +02:00
|
|
|
(u'Toshio くら'.encode('utf-8'), frozenset([u'とみ'.encode('utf-8')])),
|
|
|
|
(u'Toshio くら', frozenset([u'とみ'])),
|
2017-05-30 19:05:19 +02:00
|
|
|
)
|
2015-10-20 07:32:21 +02:00
|
|
|
dataset_remove = (
|
2017-01-29 08:28:53 +01:00
|
|
|
('string', frozenset(['string']), OMIT),
|
|
|
|
(1234, frozenset(['1234']), OMIT),
|
|
|
|
(1234, frozenset(['23']), OMIT),
|
|
|
|
(1.0, frozenset(['1.0']), OMIT),
|
|
|
|
(['string', 'strang', 'strung'], frozenset(['strang']), ['string', OMIT, 'strung']),
|
|
|
|
(['string', 'strang', 'strung'], frozenset(['strang', 'string', 'strung']), [OMIT, OMIT, OMIT]),
|
|
|
|
(('string', 'strang', 'strung'), frozenset(['string', 'strung']), [OMIT, 'strang', OMIT]),
|
|
|
|
((1234567890, 345678, 987654321), frozenset(['1234567890']), [OMIT, 345678, 987654321]),
|
|
|
|
((1234567890, 345678, 987654321), frozenset(['345678']), [OMIT, OMIT, 987654321]),
|
2017-05-30 19:05:19 +02:00
|
|
|
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key']), {'one': 1, 'two': 'dos', 'secret': OMIT}),
|
|
|
|
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key', 'dos', '1']), {'one': OMIT, 'two': OMIT, 'secret': OMIT}),
|
|
|
|
({'one': 1, 'two': 'dos', 'secret': 'key'}, frozenset(['key', 'dos', '1']), {'one': OMIT, 'two': OMIT, 'secret': OMIT}),
|
|
|
|
(
|
|
|
|
{
|
|
|
|
'one': 1,
|
|
|
|
'two': 'dos',
|
|
|
|
'three': [
|
|
|
|
'amigos', 'musketeers', None, {
|
|
|
|
'ping': 'pong', 'base': [
|
|
|
|
'balls', 'raquets'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
frozenset(['balls', 'base', 'pong', 'amigos']),
|
|
|
|
{
|
|
|
|
'one': 1,
|
|
|
|
'two': 'dos',
|
|
|
|
'three': [
|
|
|
|
OMIT, 'musketeers', None, {
|
|
|
|
'ping': OMIT,
|
|
|
|
'base': [
|
|
|
|
OMIT, 'raquets'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'This sentence has an enigma wrapped in a mystery inside of a secret. - mr mystery',
|
|
|
|
frozenset(['enigma', 'mystery', 'secret']),
|
|
|
|
'This sentence has an ******** wrapped in a ******** inside of a ********. - mr ********'
|
|
|
|
),
|
2017-05-21 15:18:35 +02:00
|
|
|
(u'Toshio くらとみ'.encode('utf-8'), frozenset([u'くらとみ'.encode('utf-8')]), u'Toshio ********'.encode('utf-8')),
|
|
|
|
(u'Toshio くらとみ', frozenset([u'くらとみ']), u'Toshio ********'),
|
2017-05-30 19:05:19 +02:00
|
|
|
)
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
def test_no_removal(self):
|
|
|
|
for value, no_log_strings in self.dataset_no_remove:
|
2015-10-21 03:51:34 +02:00
|
|
|
self.assertEquals(remove_values(value, no_log_strings), value)
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
def test_strings_to_remove(self):
|
|
|
|
for value, no_log_strings, expected in self.dataset_remove:
|
2015-10-21 03:51:34 +02:00
|
|
|
self.assertEquals(remove_values(value, no_log_strings), expected)
|
2015-10-20 07:32:21 +02:00
|
|
|
|
|
|
|
def test_unknown_type(self):
|
2015-10-21 03:51:34 +02:00
|
|
|
self.assertRaises(TypeError, remove_values, object(), frozenset())
|
2017-05-21 15:18:35 +02:00
|
|
|
|
|
|
|
def test_hit_recursion_limit(self):
|
|
|
|
""" Check that we do not hit a recursion limit"""
|
|
|
|
data_list = []
|
|
|
|
inner_list = data_list
|
|
|
|
for i in range(0, 10000):
|
|
|
|
new_list = []
|
|
|
|
inner_list.append(new_list)
|
|
|
|
inner_list = new_list
|
|
|
|
inner_list.append('secret')
|
|
|
|
|
|
|
|
# Check that this does not hit a recursion limit
|
|
|
|
actual_data_list = remove_values(data_list, frozenset(('secret',)))
|
|
|
|
|
|
|
|
levels = 0
|
|
|
|
inner_list = actual_data_list
|
|
|
|
while inner_list:
|
|
|
|
if isinstance(inner_list, list):
|
|
|
|
self.assertEquals(len(inner_list), 1)
|
|
|
|
else:
|
|
|
|
levels -= 1
|
|
|
|
break
|
|
|
|
inner_list = inner_list[0]
|
|
|
|
levels += 1
|
|
|
|
|
|
|
|
self.assertEquals(inner_list, self.OMIT)
|
|
|
|
self.assertEquals(levels, 10000)
|