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,84 +1,55 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# (c) 2016, James Cammarata <jimi@sngx.net>
|
||||
#
|
||||
# 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/>.
|
||||
# (c) 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)
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
from units.mock.procenv import swap_stdin_and_argv
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
|
||||
class TestModuleUtilsBasic(unittest.TestCase):
|
||||
def test_module_utils_basic__log_invocation(self):
|
||||
with swap_stdin_and_argv(stdin_data=json.dumps(dict(
|
||||
ANSIBLE_MODULE_ARGS=dict(foo=False, bar=[1, 2, 3], bam="bam", baz=u'baz')),
|
||||
)):
|
||||
from ansible.module_utils import basic
|
||||
ARGS = dict(foo=False, bar=[1, 2, 3], bam="bam", baz=u'baz')
|
||||
ARGUMENT_SPEC = dict(
|
||||
foo=dict(default=True, type='bool'),
|
||||
bar=dict(default=[], type='list'),
|
||||
bam=dict(default="bam"),
|
||||
baz=dict(default=u"baz"),
|
||||
password=dict(default=True),
|
||||
no_log=dict(default="you shouldn't see me", no_log=True),
|
||||
)
|
||||
|
||||
# test basic log invocation
|
||||
basic._ANSIBLE_ARGS = None
|
||||
am = basic.AnsibleModule(
|
||||
argument_spec=dict(
|
||||
foo=dict(default=True, type='bool'),
|
||||
bar=dict(default=[], type='list'),
|
||||
bam=dict(default="bam"),
|
||||
baz=dict(default=u"baz"),
|
||||
password=dict(default=True),
|
||||
no_log=dict(default="you shouldn't see me", no_log=True),
|
||||
),
|
||||
)
|
||||
|
||||
am.log = MagicMock()
|
||||
am._log_invocation()
|
||||
@pytest.mark.parametrize('am, stdin', [(ARGUMENT_SPEC, ARGS)], indirect=['am', 'stdin'])
|
||||
def test_module_utils_basic__log_invocation(am, mocker):
|
||||
|
||||
# Message is generated from a dict so it will be in an unknown order.
|
||||
# have to check this manually rather than with assert_called_with()
|
||||
args = am.log.call_args[0]
|
||||
self.assertEqual(len(args), 1)
|
||||
message = args[0]
|
||||
am.log = mocker.MagicMock()
|
||||
am._log_invocation()
|
||||
|
||||
self.assertEqual(
|
||||
len(message),
|
||||
len('Invoked with bam=bam bar=[1, 2, 3] foo=False baz=baz no_log=NOT_LOGGING_PARAMETER password=NOT_LOGGING_PASSWORD')
|
||||
)
|
||||
self.assertTrue(message.startswith('Invoked with '))
|
||||
self.assertIn(' bam=bam', message)
|
||||
self.assertIn(' bar=[1, 2, 3]', message)
|
||||
self.assertIn(' foo=False', message)
|
||||
self.assertIn(' baz=baz', message)
|
||||
self.assertIn(' no_log=NOT_LOGGING_PARAMETER', message)
|
||||
self.assertIn(' password=NOT_LOGGING_PASSWORD', message)
|
||||
# Message is generated from a dict so it will be in an unknown order.
|
||||
# have to check this manually rather than with assert_called_with()
|
||||
args = am.log.call_args[0]
|
||||
assert len(args) == 1
|
||||
message = args[0]
|
||||
|
||||
kwargs = am.log.call_args[1]
|
||||
self.assertEqual(
|
||||
kwargs,
|
||||
dict(log_args={
|
||||
'foo': 'False',
|
||||
'bar': '[1, 2, 3]',
|
||||
'bam': 'bam',
|
||||
'baz': 'baz',
|
||||
'password': 'NOT_LOGGING_PASSWORD',
|
||||
'no_log': 'NOT_LOGGING_PARAMETER',
|
||||
})
|
||||
)
|
||||
assert len(message) == \
|
||||
len('Invoked with bam=bam bar=[1, 2, 3] foo=False baz=baz no_log=NOT_LOGGING_PARAMETER password=NOT_LOGGING_PASSWORD')
|
||||
|
||||
assert message.startswith('Invoked with ')
|
||||
assert ' bam=bam' in message
|
||||
assert ' bar=[1, 2, 3]' in message
|
||||
assert ' foo=False' in message
|
||||
assert ' baz=baz' in message
|
||||
assert ' no_log=NOT_LOGGING_PARAMETER' in message
|
||||
assert ' password=NOT_LOGGING_PASSWORD' in message
|
||||
|
||||
kwargs = am.log.call_args[1]
|
||||
assert kwargs == \
|
||||
dict(log_args={
|
||||
'foo': 'False',
|
||||
'bar': '[1, 2, 3]',
|
||||
'bam': 'bam',
|
||||
'baz': 'baz',
|
||||
'password': 'NOT_LOGGING_PASSWORD',
|
||||
'no_log': 'NOT_LOGGING_PARAMETER',
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue