Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't mix deprecations messages with warnings messages #21337

Merged
merged 4 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docsite/rst/common_return_values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ warnings
````````
This key contains a list of strings that will be presented to the user.

deprecations
````````````
This key contains a list of dictionaries that will be presented to the user. Keys of the dictionaries are `msg` and `version`, values are string, value for the `version` key can be an empty string.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if the documentation should describe the format used after of before call to _return_formatted ?


.. seealso::

:doc:`modules`
Expand Down
9 changes: 6 additions & 3 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ def __init__(self, argument_spec, bypass_checks=False, no_log=False,
self.run_command_environ_update = {}
self._warnings = []
self._deprecations = []
self._passthrough = ['warnings', 'deprecations']

self.aliases = {}
self._legal_inputs = ['_ansible_check_mode', '_ansible_no_log', '_ansible_debug', '_ansible_diff', '_ansible_verbosity', '_ansible_selinux_special_fs', '_ansible_module_name', '_ansible_version', '_ansible_syslog_facility', '_ansible_socket']
Expand Down Expand Up @@ -1949,15 +1948,19 @@ def _return_formatted(self, kwargs):
self.warn(w)
else:
self.warn(kwargs['warnings'])

if self._warnings:
kwargs['warnings'] = self._warnings

if 'deprecations' in kwargs:
if isinstance(kwargs['deprecations'], list):
for d in kwargs['deprecations']:
self.deprecate(d)
if isinstance(d, SEQUENCETYPE) and len(d) == 2:
self.deprecate(d[0], version=d[1])
else:
self.deprecate(d)
else:
self.warn(kwargs['deprecations'])
self.deprecate(d)

if self._deprecations:
kwargs['deprecations'] = self._deprecations
Expand Down
71 changes: 71 additions & 0 deletions test/units/module_utils/basic/test_deprecate_warn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- 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'},
])