Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Port AnsibleModule deprecate and warning tests to pytest (ansible#33162)
Browse files Browse the repository at this point in the history
  • Loading branch information
abadger authored and ansibot committed Nov 28, 2017
1 parent a1da3dc commit aa7bd8b
Showing 1 changed file with 88 additions and 65 deletions.
153 changes: 88 additions & 65 deletions test/units/module_utils/basic/test_deprecate_warn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

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'},
])

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):
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},
])
@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')

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 = '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 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},
]

0 comments on commit aa7bd8b

Please sign in to comment.