1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Port AnsibleModule deprecate and warning tests to pytest (#33162)

This commit is contained in:
Toshio Kuratomi 2017-11-28 15:43:24 -08:00 committed by ansibot
parent a1da3dc997
commit aa7bd8bc11

View file

@ -17,73 +17,96 @@
import json
import sys
from collections import MutableMapping
from io import BytesIO
from ansible.compat.tests import unittest
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
import pytest
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):
"""Test the AnsibleModule Warn Method"""
@pytest.fixture
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):
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
with swap_stdin_and_argv(stdin_data=args):
with swap_stdout():
real_stdin = sys.stdin
fake_stdin = BytesIO(to_bytes(args, errors='surrogate_or_strict'))
if PY3:
sys.stdin = mocker.MagicMock()
sys.stdin.buffer = fake_stdin
else:
sys.stdin = fake_stdin
yield fake_stdin
sys.stdin = real_stdin
@pytest.fixture
def am(stdin, request):
old_args = ansible.module_utils.basic._ANSIBLE_ARGS
ansible.module_utils.basic._ANSIBLE_ARGS = None
old_argv = sys.argv
sys.argv = ['ansible_unittest']
am = ansible.module_utils.basic.AnsibleModule(
argument_spec=dict(),
)
am._name = 'unittest'
am._name = 'ansible_unittest'
yield am
ansible.module_utils.basic._ANSIBLE_ARGS = old_args
sys.argv = old_argv
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_warn(am, capfd):
am.warn('warning1')
with self.assertRaises(SystemExit):
with pytest.raises(SystemExit):
am.exit_json(warnings=['warning2'])
self.assertEquals(json.loads(sys.stdout.getvalue())['warnings'], ['warning1', 'warning2'])
out, err = capfd.readouterr()
assert json.loads(out)['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'
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_deprecate(am, capfd):
am.deprecate('deprecation1')
am.deprecate('deprecation2', '2.3')
with self.assertRaises(SystemExit):
with pytest.raises(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'], [
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'},
])
]
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
am = ansible.module_utils.basic.AnsibleModule(
argument_spec=dict(),
)
am._name = 'unittest'
with self.assertRaises(SystemExit):
@pytest.mark.parametrize('stdin', [{}], indirect=True)
def test_deprecate_without_list(am, capfd):
with pytest.raises(SystemExit):
am.exit_json(deprecations='Simple deprecation warning')
output = json.loads(sys.stdout.getvalue())
self.assertTrue('warnings' not in output or output['warnings'] == [])
self.assertEquals(output['deprecations'], [
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},
])
]