2015-10-21 01:51:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2015, Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
#
|
|
|
|
# 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 copy
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from ansible.compat.tests import unittest
|
2016-04-14 16:06:38 +00:00
|
|
|
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
from ansible.module_utils import basic
|
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
|
2015-12-23 16:45:07 +00:00
|
|
|
empty_invocation = {u'module_args': {}}
|
|
|
|
|
2015-10-21 01:51:34 +00:00
|
|
|
class TestAnsibleModuleExitJson(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2016-05-13 03:30:05 +00:00
|
|
|
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
2016-04-14 16:06:38 +00:00
|
|
|
self.stdin_swap_ctx = swap_stdin_and_argv(stdin_data=args)
|
|
|
|
self.stdin_swap_ctx.__enter__()
|
2015-10-21 01:51:34 +00:00
|
|
|
|
2016-04-14 16:06:38 +00:00
|
|
|
# since we can't use context managers and "with" without overriding run(), call them directly
|
|
|
|
self.stdout_swap_ctx = swap_stdout()
|
|
|
|
self.fake_stream = self.stdout_swap_ctx.__enter__()
|
2015-10-21 01:51:34 +00:00
|
|
|
|
2016-05-10 15:13:48 +00:00
|
|
|
basic._ANSIBLE_ARGS = None
|
2015-10-21 01:51:34 +00:00
|
|
|
self.module = basic.AnsibleModule(argument_spec=dict())
|
|
|
|
|
|
|
|
def tearDown(self):
|
2016-04-14 16:06:38 +00:00
|
|
|
# since we can't use context managers and "with" without overriding run(), call them directly to clean up
|
|
|
|
self.stdin_swap_ctx.__exit__(None, None, None)
|
|
|
|
self.stdout_swap_ctx.__exit__(None, None, None)
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
def test_exit_json_no_args_exits(self):
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
self.module.exit_json()
|
2015-10-21 18:53:30 +00:00
|
|
|
if isinstance(ctx.exception, int):
|
|
|
|
# Python2.6... why does sys.exit behave this way?
|
|
|
|
self.assertEquals(ctx.exception, 0)
|
|
|
|
else:
|
|
|
|
self.assertEquals(ctx.exception.code, 0)
|
2015-10-21 01:51:34 +00:00
|
|
|
return_val = json.loads(self.fake_stream.getvalue())
|
2015-12-23 16:45:07 +00:00
|
|
|
self.assertEquals(return_val, dict(changed=False, invocation=empty_invocation))
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
def test_exit_json_args_exits(self):
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
self.module.exit_json(msg='message')
|
2015-10-21 18:53:30 +00:00
|
|
|
if isinstance(ctx.exception, int):
|
|
|
|
# Python2.6... why does sys.exit behave this way?
|
|
|
|
self.assertEquals(ctx.exception, 0)
|
|
|
|
else:
|
|
|
|
self.assertEquals(ctx.exception.code, 0)
|
2015-10-21 01:51:34 +00:00
|
|
|
return_val = json.loads(self.fake_stream.getvalue())
|
2015-12-23 16:45:07 +00:00
|
|
|
self.assertEquals(return_val, dict(msg="message", changed=False, invocation=empty_invocation))
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
def test_fail_json_exits(self):
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
self.module.fail_json(msg='message')
|
2015-10-21 18:53:30 +00:00
|
|
|
if isinstance(ctx.exception, int):
|
|
|
|
# Python2.6... why does sys.exit behave this way?
|
|
|
|
self.assertEquals(ctx.exception, 1)
|
|
|
|
else:
|
|
|
|
self.assertEquals(ctx.exception.code, 1)
|
2015-10-21 01:51:34 +00:00
|
|
|
return_val = json.loads(self.fake_stream.getvalue())
|
2015-12-23 16:45:07 +00:00
|
|
|
self.assertEquals(return_val, dict(msg="message", failed=True, invocation=empty_invocation))
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
def test_exit_json_proper_changed(self):
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
self.module.exit_json(changed=True, msg='success')
|
|
|
|
return_val = json.loads(self.fake_stream.getvalue())
|
2015-12-23 16:45:07 +00:00
|
|
|
self.assertEquals(return_val, dict(changed=True, msg='success', invocation=empty_invocation))
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
|
|
|
OMIT = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER'
|
|
|
|
dataset = (
|
2017-01-29 07:28:53 +00:00
|
|
|
(dict(username='person', password='$ecret k3y'),
|
2015-10-21 01:51:34 +00:00
|
|
|
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
|
|
|
not_secret='following the leader', msg='here'),
|
|
|
|
dict(one=1, pwd=OMIT, url='https://username:password12345@foo.com/login/',
|
2015-12-19 23:49:06 +00:00
|
|
|
not_secret='following the leader', changed=False, msg='here',
|
2015-12-23 16:45:07 +00:00
|
|
|
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
2015-10-21 01:51:34 +00:00
|
|
|
),
|
2017-01-29 07:28:53 +00:00
|
|
|
(dict(username='person', password='password12345'),
|
2015-10-21 01:51:34 +00:00
|
|
|
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
|
|
|
not_secret='following the leader', msg='here'),
|
|
|
|
dict(one=1, pwd='$ecret k3y', url='https://username:********@foo.com/login/',
|
2015-12-19 23:49:06 +00:00
|
|
|
not_secret='following the leader', changed=False, msg='here',
|
2015-12-23 16:45:07 +00:00
|
|
|
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
2015-10-21 01:51:34 +00:00
|
|
|
),
|
2017-01-29 07:28:53 +00:00
|
|
|
(dict(username='person', password='$ecret k3y'),
|
2015-10-21 01:51:34 +00:00
|
|
|
dict(one=1, pwd='$ecret k3y', url='https://username:$ecret k3y@foo.com/login/',
|
|
|
|
not_secret='following the leader', msg='here'),
|
|
|
|
dict(one=1, pwd=OMIT, url='https://username:********@foo.com/login/',
|
2015-12-19 23:49:06 +00:00
|
|
|
not_secret='following the leader', changed=False, msg='here',
|
2015-12-23 16:45:07 +00:00
|
|
|
invocation=dict(module_args=dict(password=OMIT, token=None, username='person'))),
|
2015-10-21 01:51:34 +00:00
|
|
|
),
|
2017-01-29 07:28:53 +00:00
|
|
|
)
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
def test_exit_json_removes_values(self):
|
2015-12-23 16:45:07 +00:00
|
|
|
self.maxDiff = None
|
2015-10-21 01:51:34 +00:00
|
|
|
for args, return_val, expected in self.dataset:
|
2016-05-13 03:30:05 +00:00
|
|
|
params = dict(ANSIBLE_MODULE_ARGS=args)
|
2016-04-12 14:10:45 +00:00
|
|
|
params = json.dumps(params)
|
2016-04-14 16:06:38 +00:00
|
|
|
|
|
|
|
with swap_stdin_and_argv(stdin_data=params):
|
|
|
|
with swap_stdout():
|
2016-05-10 15:13:48 +00:00
|
|
|
basic._ANSIBLE_ARGS = None
|
2016-04-14 16:06:38 +00:00
|
|
|
module = basic.AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
username=dict(),
|
|
|
|
password=dict(no_log=True),
|
|
|
|
token=dict(no_log=True),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
self.assertEquals(module.exit_json(**return_val), expected)
|
|
|
|
self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
|
2015-10-21 01:51:34 +00:00
|
|
|
|
|
|
|
def test_fail_json_removes_values(self):
|
2015-12-23 16:45:07 +00:00
|
|
|
self.maxDiff = None
|
2015-10-21 01:51:34 +00:00
|
|
|
for args, return_val, expected in self.dataset:
|
|
|
|
expected = copy.deepcopy(expected)
|
|
|
|
del expected['changed']
|
|
|
|
expected['failed'] = True
|
2016-05-13 03:30:05 +00:00
|
|
|
params = dict(ANSIBLE_MODULE_ARGS=args)
|
2016-04-12 14:10:45 +00:00
|
|
|
params = json.dumps(params)
|
2016-04-14 16:06:38 +00:00
|
|
|
with swap_stdin_and_argv(stdin_data=params):
|
|
|
|
with swap_stdout():
|
2016-05-10 15:13:48 +00:00
|
|
|
basic._ANSIBLE_ARGS = None
|
2016-04-14 16:06:38 +00:00
|
|
|
module = basic.AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
username=dict(),
|
|
|
|
password=dict(no_log=True),
|
|
|
|
token=dict(no_log=True),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
with self.assertRaises(SystemExit) as ctx:
|
|
|
|
self.assertEquals(module.fail_json(**return_val), expected)
|
|
|
|
self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
|