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 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
|
||||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec=dict(),
|
||||
)
|
||||
am._name = 'unittest'
|
||||
yield fake_stdin
|
||||
|
||||
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):
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
||||
with swap_stdin_and_argv(stdin_data=args):
|
||||
with swap_stdout():
|
||||
@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']
|
||||
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
||||
am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec=dict(),
|
||||
)
|
||||
am._name = 'unittest'
|
||||
am = ansible.module_utils.basic.AnsibleModule(
|
||||
argument_spec=dict(),
|
||||
)
|
||||
am._name = 'ansible_unittest'
|
||||
|
||||
am.deprecate('deprecation1')
|
||||
am.deprecate('deprecation2', '2.3')
|
||||
yield am
|
||||
|
||||
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'},
|
||||
])
|
||||
ansible.module_utils.basic._ANSIBLE_ARGS = old_args
|
||||
sys.argv = old_argv
|
||||
|
||||
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'
|
||||
@pytest.mark.parametrize('stdin', [{}], indirect=True)
|
||||
def test_warn(am, capfd):
|
||||
|
||||
with self.assertRaises(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'], [
|
||||
{u'msg': u'Simple deprecation warning', u'version': None},
|
||||
])
|
||||
am.warn('warning1')
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
am.exit_json(warnings=['warning2'])
|
||||
out, err = capfd.readouterr()
|
||||
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…
Reference in a new issue