mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-24 04:54:00 -07:00
* Porting tests to pytest
* Achievement Get: No longer need mock/generator.py
* Now done via pytest's parametrization
* Port safe_eval to pytest
* Port text tests to pytest
* Port test_set_mode_if_different to pytest
* Change conftest AnsibleModule fixtures to be more flexible
* Move the AnsibleModules fixtures to module_utils/conftest.py for sharing
* Testing the argspec code requires:
* injecting both the argspec and the arguments.
* Patching the arguments into sys.stdin at a different level
* More porting to obsolete mock/procenv.py
* Port run_command to pytest
* Port known_hosts tests to pytest
* Port safe_eval to pytest
* Port test_distribution_version.py to pytest
* Port test_log to pytest
* Port test__log_invocation to pytest
* Remove unneeded import of procenv in test_postgresql
* Port test_pip to pytest style
* As part of this, create a pytest ansiblemodule fixture in
modules/conftest.py. This is slightly different than the
approach taken in module_utils because here we need to override the
AnsibleModule that the modules will inherit from instead of one that
we're instantiating ourselves.
* Fixup usage of parametrization in test_deprecate_warn
* Check that the pip module failed in our test
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
# Copyright (c) 2017 Ansible Project
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
import json
|
|
import sys
|
|
from collections import MutableMapping
|
|
from io import BytesIO
|
|
|
|
import pytest
|
|
|
|
import ansible.module_utils.basic
|
|
from ansible.module_utils.six import PY3, string_types
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
|
|
@pytest.fixture
|
|
def stdin(mocker, request):
|
|
old_args = ansible.module_utils.basic._ANSIBLE_ARGS
|
|
ansible.module_utils.basic._ANSIBLE_ARGS = None
|
|
old_argv = sys.argv
|
|
sys.argv = ['ansible_unittest']
|
|
|
|
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')
|
|
|
|
fake_stdin = BytesIO(to_bytes(args, errors='surrogate_or_strict'))
|
|
if PY3:
|
|
mocker.patch('ansible.module_utils.basic.sys.stdin', mocker.MagicMock())
|
|
mocker.patch('ansible.module_utils.basic.sys.stdin.buffer', fake_stdin)
|
|
else:
|
|
mocker.patch('ansible.module_utils.basic.sys.stdin', fake_stdin)
|
|
|
|
yield fake_stdin
|
|
|
|
ansible.module_utils.basic._ANSIBLE_ARGS = old_args
|
|
sys.argv = old_argv
|
|
|
|
|
|
@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']
|
|
|
|
argspec = {}
|
|
if hasattr(request, 'param'):
|
|
if isinstance(request.param, dict):
|
|
argspec = request.param
|
|
|
|
am = ansible.module_utils.basic.AnsibleModule(
|
|
argument_spec=argspec,
|
|
)
|
|
am._name = 'ansible_unittest'
|
|
|
|
yield am
|
|
|
|
ansible.module_utils.basic._ANSIBLE_ARGS = old_args
|
|
sys.argv = old_argv
|