2016-03-17 06:01:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2016, James Cammarata <jimi@sngx.net>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import json
|
2017-05-30 17:05:19 +00:00
|
|
|
import sys
|
2016-04-12 14:10:45 +00:00
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
from units.mock.procenv import swap_stdin_and_argv
|
2016-03-17 06:01:47 +00:00
|
|
|
|
|
|
|
from ansible.compat.tests import unittest
|
|
|
|
from ansible.compat.tests.mock import MagicMock
|
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
|
2016-03-17 06:01:47 +00:00
|
|
|
class TestModuleUtilsBasic(unittest.TestCase):
|
2016-04-14 16:06:38 +00:00
|
|
|
def test_module_utils_basic__log_invocation(self):
|
2017-05-30 17:05:19 +00:00
|
|
|
with swap_stdin_and_argv(stdin_data=json.dumps(dict(
|
|
|
|
ANSIBLE_MODULE_ARGS=dict(foo=False, bar=[1, 2, 3], bam="bam", baz=u'baz')),
|
|
|
|
)):
|
2016-04-14 16:06:38 +00:00
|
|
|
from ansible.module_utils import basic
|
2016-04-12 14:10:45 +00:00
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
# test basic log invocation
|
2017-02-04 01:19:59 +00:00
|
|
|
basic._ANSIBLE_ARGS = None
|
2016-04-14 16:06:38 +00:00
|
|
|
am = basic.AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2017-05-30 17:05:19 +00:00
|
|
|
foo=dict(default=True, type='bool'),
|
|
|
|
bar=dict(default=[], type='list'),
|
|
|
|
bam=dict(default="bam"),
|
|
|
|
baz=dict(default=u"baz"),
|
|
|
|
password=dict(default=True),
|
|
|
|
no_log=dict(default="you shouldn't see me", no_log=True),
|
2016-04-14 16:06:38 +00:00
|
|
|
),
|
|
|
|
)
|
2016-03-17 06:01:47 +00:00
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
am.log = MagicMock()
|
|
|
|
am._log_invocation()
|
2016-03-23 15:00:56 +00:00
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
# Message is generated from a dict so it will be in an unknown order.
|
|
|
|
# have to check this manually rather than with assert_called_with()
|
|
|
|
args = am.log.call_args[0]
|
|
|
|
self.assertEqual(len(args), 1)
|
|
|
|
message = args[0]
|
2016-03-23 15:00:56 +00:00
|
|
|
|
2017-02-10 17:33:48 +00:00
|
|
|
self.assertEqual(
|
|
|
|
len(message),
|
|
|
|
len('Invoked with bam=bam bar=[1, 2, 3] foo=False baz=baz no_log=NOT_LOGGING_PARAMETER password=NOT_LOGGING_PASSWORD')
|
|
|
|
)
|
2016-04-14 16:06:38 +00:00
|
|
|
self.assertTrue(message.startswith('Invoked with '))
|
|
|
|
self.assertIn(' bam=bam', message)
|
|
|
|
self.assertIn(' bar=[1, 2, 3]', message)
|
|
|
|
self.assertIn(' foo=False', message)
|
|
|
|
self.assertIn(' baz=baz', message)
|
|
|
|
self.assertIn(' no_log=NOT_LOGGING_PARAMETER', message)
|
|
|
|
self.assertIn(' password=NOT_LOGGING_PASSWORD', message)
|
2016-03-23 15:00:56 +00:00
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
kwargs = am.log.call_args[1]
|
2017-05-30 17:05:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
kwargs,
|
|
|
|
dict(log_args={
|
|
|
|
'foo': 'False',
|
|
|
|
'bar': '[1, 2, 3]',
|
|
|
|
'bam': 'bam',
|
|
|
|
'baz': 'baz',
|
|
|
|
'password': 'NOT_LOGGING_PASSWORD',
|
|
|
|
'no_log': 'NOT_LOGGING_PARAMETER',
|
|
|
|
})
|
|
|
|
)
|