mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-30 14:21:26 -07:00
Port AnsibleModule deprecate and warning tests to pytest (#33162)
This commit is contained in:
parent
a1da3dc997
commit
aa7bd8bc11
1 changed files with 77 additions and 54 deletions
|
@ -17,73 +17,96 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
from collections import MutableMapping
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
from ansible.compat.tests import unittest
|
import pytest
|
||||||
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
|
|
||||||
|
|
||||||
import ansible.module_utils.basic
|
import ansible.module_utils.basic
|
||||||
|
from ansible.module_utils.six import PY3, string_types
|
||||||
|
from ansible.module_utils._text import to_bytes
|
||||||
|
|
||||||
|
|
||||||
class TestAnsibleModuleWarnDeprecate(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Test the AnsibleModule Warn Method"""
|
def stdin(mocker, request):
|
||||||
|
if isinstance(request.param, string_types):
|
||||||
|
args = request.param
|
||||||
|
elif isinstance(request.param, MutableMapping):
|
||||||
|
if 'ANSIBLE_MODULE_ARGS' not in request.param:
|
||||||
|
request.param = {'ANSIBLE_MODULE_ARGS': request.param}
|
||||||
|
args = json.dumps(request.param)
|
||||||
|
else:
|
||||||
|
raise Exception('Malformed data to the stdin pytest fixture')
|
||||||
|
|
||||||
def test_warn(self):
|
real_stdin = sys.stdin
|
||||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
fake_stdin = BytesIO(to_bytes(args, errors='surrogate_or_strict'))
|
||||||
with swap_stdin_and_argv(stdin_data=args):
|
if PY3:
|
||||||
with swap_stdout():
|
sys.stdin = mocker.MagicMock()
|
||||||
|
sys.stdin.buffer = fake_stdin
|
||||||
|
else:
|
||||||
|
sys.stdin = fake_stdin
|
||||||
|
|
||||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
yield fake_stdin
|
||||||
am = ansible.module_utils.basic.AnsibleModule(
|
|
||||||
argument_spec=dict(),
|
|
||||||
)
|
|
||||||
am._name = 'unittest'
|
|
||||||
|
|
||||||
am.warn('warning1')
|
sys.stdin = real_stdin
|
||||||
|
|
||||||
with self.assertRaises(SystemExit):
|
|
||||||
am.exit_json(warnings=['warning2'])
|
|
||||||
self.assertEquals(json.loads(sys.stdout.getvalue())['warnings'], ['warning1', 'warning2'])
|
|
||||||
|
|
||||||
def test_deprecate(self):
|
@pytest.fixture
|
||||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
def am(stdin, request):
|
||||||
with swap_stdin_and_argv(stdin_data=args):
|
old_args = ansible.module_utils.basic._ANSIBLE_ARGS
|
||||||
with swap_stdout():
|
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||||
|
old_argv = sys.argv
|
||||||
|
sys.argv = ['ansible_unittest']
|
||||||
|
|
||||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
am = ansible.module_utils.basic.AnsibleModule(
|
||||||
am = ansible.module_utils.basic.AnsibleModule(
|
argument_spec=dict(),
|
||||||
argument_spec=dict(),
|
)
|
||||||
)
|
am._name = 'ansible_unittest'
|
||||||
am._name = 'unittest'
|
|
||||||
|
|
||||||
am.deprecate('deprecation1')
|
yield am
|
||||||
am.deprecate('deprecation2', '2.3')
|
|
||||||
|
|
||||||
with self.assertRaises(SystemExit):
|
ansible.module_utils.basic._ANSIBLE_ARGS = old_args
|
||||||
am.exit_json(deprecations=['deprecation3', ('deprecation4', '2.4')])
|
sys.argv = old_argv
|
||||||
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'},
|
|
||||||
])
|
|
||||||
|
|
||||||
def test_deprecate_without_list(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
|
@pytest.mark.parametrize('stdin', [{}], indirect=True)
|
||||||
am = ansible.module_utils.basic.AnsibleModule(
|
def test_warn(am, capfd):
|
||||||
argument_spec=dict(),
|
|
||||||
)
|
|
||||||
am._name = 'unittest'
|
|
||||||
|
|
||||||
with self.assertRaises(SystemExit):
|
am.warn('warning1')
|
||||||
am.exit_json(deprecations='Simple deprecation warning')
|
|
||||||
output = json.loads(sys.stdout.getvalue())
|
with pytest.raises(SystemExit):
|
||||||
self.assertTrue('warnings' not in output or output['warnings'] == [])
|
am.exit_json(warnings=['warning2'])
|
||||||
self.assertEquals(output['deprecations'], [
|
out, err = capfd.readouterr()
|
||||||
{u'msg': u'Simple deprecation warning', u'version': None},
|
assert json.loads(out)['warnings'] == ['warning1', 'warning2']
|
||||||
])
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('stdin', [{}], indirect=True)
|
||||||
|
def test_deprecate(am, capfd):
|
||||||
|
am.deprecate('deprecation1')
|
||||||
|
am.deprecate('deprecation2', '2.3')
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
am.exit_json(deprecations=['deprecation3', ('deprecation4', '2.4')])
|
||||||
|
|
||||||
|
out, err = capfd.readouterr()
|
||||||
|
output = json.loads(out)
|
||||||
|
assert ('warnings' not in output or output['warnings'] == [])
|
||||||
|
assert 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'},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('stdin', [{}], indirect=True)
|
||||||
|
def test_deprecate_without_list(am, capfd):
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
am.exit_json(deprecations='Simple deprecation warning')
|
||||||
|
|
||||||
|
out, err = capfd.readouterr()
|
||||||
|
output = json.loads(out)
|
||||||
|
assert ('warnings' not in output or output['warnings'] == [])
|
||||||
|
assert output['deprecations'] == [
|
||||||
|
{u'msg': u'Simple deprecation warning', u'version': None},
|
||||||
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue