ansible/test/units/module_utils/basic/test_deprecate_warn.py
Pilou f2c22109fb Don't mix deprecations messages with warnings messages (#21337)
* Remove unused attribute '_passthrough'

* Don't mix deprecations with warnings

* Return values: add 'deprecations' key used internally

* 'deprecations' and 'warnings' return values: add tests
2017-02-13 23:49:39 -05:00

71 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
#
# 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/>.
import json
import sys
from ansible.compat.tests import unittest
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
import ansible.module_utils.basic
class TestAnsibleModuleWarnDeprecate(unittest.TestCase):
"""Test the AnsibleModule Warn Method"""
def test_warn(self):
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
with swap_stdin_and_argv(stdin_data=args):
with swap_stdout():
ansible.module_utils.basic._ANSIBLE_ARGS = None
am = ansible.module_utils.basic.AnsibleModule(
argument_spec = dict(),
)
am._name = 'unittest'
am.warn('warning1')
with self.assertRaises(SystemExit):
am.exit_json(warnings=['warning2'])
self.assertEquals(json.loads(sys.stdout.getvalue())['warnings'], ['warning1', 'warning2'])
def test_deprecate(self):
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
with swap_stdin_and_argv(stdin_data=args):
with swap_stdout():
ansible.module_utils.basic._ANSIBLE_ARGS = None
am = ansible.module_utils.basic.AnsibleModule(
argument_spec = dict(),
)
am._name = 'unittest'
am.deprecate('deprecation1')
am.deprecate('deprecation2', '2.3')
with self.assertRaises(SystemExit):
am.exit_json(deprecations=['deprecation3', ('deprecation4', '2.4')])
output = json.loads(sys.stdout.getvalue())
self.assertTrue('warnings' not in output or output['warnings'] == [])
self.assertEquals(output['deprecations'], [
{u'msg': u'deprecation1', u'version': None},
{u'msg': u'deprecation2', u'version': '2.3'},
{u'msg': u'deprecation3', u'version': None},
{u'msg': u'deprecation4', u'version': '2.4'},
])