mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 20:01:25 -07:00
Porting tests to pytest (#33387)
* 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
This commit is contained in:
parent
ed376abe42
commit
cd36164239
16 changed files with 812 additions and 1117 deletions
|
@ -1,98 +1,76 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# (c) 2015, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
# Copyright (c) 2015-2017 Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division)
|
||||
__metaclass__ = type
|
||||
|
||||
import copy
|
||||
import json
|
||||
import sys
|
||||
from itertools import chain
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.module_utils import basic
|
||||
from units.mock.procenv import swap_stdin_and_argv, swap_stdout
|
||||
import pytest
|
||||
|
||||
|
||||
empty_invocation = {u'module_args': {}}
|
||||
EMPTY_INVOCATION = {u'module_args': {}}
|
||||
|
||||
|
||||
class TestAnsibleModuleExitJson(unittest.TestCase):
|
||||
def setUp(self):
|
||||
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={}))
|
||||
self.stdin_swap_ctx = swap_stdin_and_argv(stdin_data=args)
|
||||
self.stdin_swap_ctx.__enter__()
|
||||
class TestAnsibleModuleExitJson:
|
||||
"""
|
||||
Test that various means of calling exitJson and FailJson return the messages they've been given
|
||||
"""
|
||||
DATA = (
|
||||
({}, {'invocation': EMPTY_INVOCATION}),
|
||||
({'msg': 'message'}, {'msg': 'message', 'invocation': EMPTY_INVOCATION}),
|
||||
({'msg': 'success', 'changed': True},
|
||||
{'msg': 'success', 'changed': True, 'invocation': EMPTY_INVOCATION}),
|
||||
({'msg': 'nochange', 'changed': False},
|
||||
{'msg': 'nochange', 'changed': False, 'invocation': EMPTY_INVOCATION}),
|
||||
)
|
||||
|
||||
# since we can't use context managers and "with" without overriding run(), call them directly
|
||||
self.stdout_swap_ctx = swap_stdout()
|
||||
self.fake_stream = self.stdout_swap_ctx.__enter__()
|
||||
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
||||
# pylint: disable=undefined-variable
|
||||
@pytest.mark.parametrize('args, expected, stdin', ((a, e, {}) for a, e in DATA), indirect=['stdin'])
|
||||
def test_exit_json_exits(self, am, capfd, args, expected):
|
||||
with pytest.raises(SystemExit) as ctx:
|
||||
am.exit_json(**args)
|
||||
assert ctx.value.code == 0
|
||||
|
||||
basic._ANSIBLE_ARGS = None
|
||||
self.module = basic.AnsibleModule(argument_spec=dict())
|
||||
out, err = capfd.readouterr()
|
||||
return_val = json.loads(out)
|
||||
assert return_val == expected
|
||||
|
||||
def tearDown(self):
|
||||
# since we can't use context managers and "with" without overriding run(), call them directly to clean up
|
||||
self.stdin_swap_ctx.__exit__(None, None, None)
|
||||
self.stdout_swap_ctx.__exit__(None, None, None)
|
||||
# Fail_json is only legal if it's called with a message
|
||||
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
||||
@pytest.mark.parametrize('args, expected, stdin',
|
||||
((a, e, {}) for a, e in DATA if 'msg' in a), # pylint: disable=undefined-variable
|
||||
indirect=['stdin'])
|
||||
def test_fail_json_exits(self, am, capfd, args, expected):
|
||||
with pytest.raises(SystemExit) as ctx:
|
||||
am.fail_json(**args)
|
||||
assert ctx.value.code == 1
|
||||
|
||||
def test_exit_json_no_args_exits(self):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.module.exit_json()
|
||||
if isinstance(ctx.exception, int):
|
||||
# Python2.6... why does sys.exit behave this way?
|
||||
self.assertEquals(ctx.exception, 0)
|
||||
else:
|
||||
self.assertEquals(ctx.exception.code, 0)
|
||||
return_val = json.loads(self.fake_stream.getvalue())
|
||||
self.assertEquals(return_val, dict(invocation=empty_invocation))
|
||||
out, err = capfd.readouterr()
|
||||
return_val = json.loads(out)
|
||||
# Fail_json should add failed=True
|
||||
expected['failed'] = True
|
||||
assert return_val == expected
|
||||
|
||||
def test_exit_json_args_exits(self):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.module.exit_json(msg='message')
|
||||
if isinstance(ctx.exception, int):
|
||||
# Python2.6... why does sys.exit behave this way?
|
||||
self.assertEquals(ctx.exception, 0)
|
||||
else:
|
||||
self.assertEquals(ctx.exception.code, 0)
|
||||
return_val = json.loads(self.fake_stream.getvalue())
|
||||
self.assertEquals(return_val, dict(msg="message", invocation=empty_invocation))
|
||||
|
||||
def test_fail_json_exits(self):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.module.fail_json(msg='message')
|
||||
if isinstance(ctx.exception, int):
|
||||
# Python2.6... why does sys.exit behave this way?
|
||||
self.assertEquals(ctx.exception, 1)
|
||||
else:
|
||||
self.assertEquals(ctx.exception.code, 1)
|
||||
return_val = json.loads(self.fake_stream.getvalue())
|
||||
self.assertEquals(return_val, dict(msg="message", failed=True, invocation=empty_invocation))
|
||||
|
||||
def test_exit_json_proper_changed(self):
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.module.exit_json(changed=True, msg='success')
|
||||
return_val = json.loads(self.fake_stream.getvalue())
|
||||
self.assertEquals(return_val, dict(changed=True, msg='success', invocation=empty_invocation))
|
||||
@pytest.mark.parametrize('stdin', [{}], indirect=['stdin'])
|
||||
def test_fail_json_no_msg(self, am):
|
||||
with pytest.raises(AssertionError) as ctx:
|
||||
am.fail_json()
|
||||
assert ctx.value.args[0] == "implementation error -- msg to explain the error is required"
|
||||
|
||||
|
||||
class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
||||
class TestAnsibleModuleExitValuesRemoved:
|
||||
"""
|
||||
Test that ExitJson and FailJson remove password-like values
|
||||
"""
|
||||
OMIT = 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER'
|
||||
dataset = (
|
||||
|
||||
DATA = (
|
||||
(
|
||||
dict(username='person', password='$ecret k3y'),
|
||||
dict(one=1, pwd='$ecret k3y', url='https://username:password12345@foo.com/login/',
|
||||
|
@ -119,43 +97,27 @@ class TestAnsibleModuleExitValuesRemoved(unittest.TestCase):
|
|||
),
|
||||
)
|
||||
|
||||
def test_exit_json_removes_values(self):
|
||||
self.maxDiff = None
|
||||
for args, return_val, expected in self.dataset:
|
||||
params = dict(ANSIBLE_MODULE_ARGS=args)
|
||||
params = json.dumps(params)
|
||||
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
||||
@pytest.mark.parametrize('am, stdin, return_val, expected',
|
||||
(({'username': {}, 'password': {'no_log': True}, 'token': {'no_log': True}}, s, r, e)
|
||||
for s, r, e in DATA), # pylint: disable=undefined-variable
|
||||
indirect=['am', 'stdin'])
|
||||
def test_exit_json_removes_values(self, am, capfd, return_val, expected):
|
||||
with pytest.raises(SystemExit) as ctx:
|
||||
am.exit_json(**return_val)
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
with swap_stdin_and_argv(stdin_data=params):
|
||||
with swap_stdout():
|
||||
basic._ANSIBLE_ARGS = None
|
||||
module = basic.AnsibleModule(
|
||||
argument_spec=dict(
|
||||
username=dict(),
|
||||
password=dict(no_log=True),
|
||||
token=dict(no_log=True),
|
||||
),
|
||||
)
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.assertEquals(module.exit_json(**return_val), expected)
|
||||
self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
|
||||
assert json.loads(out) == expected
|
||||
|
||||
def test_fail_json_removes_values(self):
|
||||
self.maxDiff = None
|
||||
for args, return_val, expected in self.dataset:
|
||||
expected = copy.deepcopy(expected)
|
||||
expected['failed'] = True
|
||||
params = dict(ANSIBLE_MODULE_ARGS=args)
|
||||
params = json.dumps(params)
|
||||
with swap_stdin_and_argv(stdin_data=params):
|
||||
with swap_stdout():
|
||||
basic._ANSIBLE_ARGS = None
|
||||
module = basic.AnsibleModule(
|
||||
argument_spec=dict(
|
||||
username=dict(),
|
||||
password=dict(no_log=True),
|
||||
token=dict(no_log=True),
|
||||
),
|
||||
)
|
||||
with self.assertRaises(SystemExit) as ctx:
|
||||
self.assertEquals(module.fail_json(**return_val), expected)
|
||||
self.assertEquals(json.loads(sys.stdout.getvalue()), expected)
|
||||
# pylint bug: https://github.com/PyCQA/pylint/issues/511
|
||||
@pytest.mark.parametrize('am, stdin, return_val, expected',
|
||||
(({'username': {}, 'password': {'no_log': True}, 'token': {'no_log': True}}, s, r, e)
|
||||
for s, r, e in DATA), # pylint: disable=undefined-variable
|
||||
indirect=['am', 'stdin'])
|
||||
def test_fail_json_removes_values(self, am, capfd, return_val, expected):
|
||||
expected['failed'] = True
|
||||
with pytest.raises(SystemExit) as ctx:
|
||||
am.fail_json(**return_val) == expected
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
assert json.loads(out) == expected
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue