mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 05:40:23 -07:00
Prepare 10.0.0 release (#8921)
* Bump version to 10.0.0, remove deprecated modules and plugins. * Remove redhat module utils. * Drop support for ansible-core 2.13 and ansible-core 2.14.
This commit is contained in:
parent
447d4b0267
commit
ec6496024f
15 changed files with 30 additions and 2238 deletions
|
@ -1,35 +0,0 @@
|
|||
# Copyright (c) Ansible project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.module_utils.six.moves import xmlrpc_client
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def get_method_name(request_body):
|
||||
return xmlrpc_client.loads(request_body)[1]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_request(request, mocker):
|
||||
responses = request.getfixturevalue('testcase')['calls']
|
||||
module_name = request.module.TESTED_MODULE
|
||||
|
||||
def transport_request(host, handler, request_body, verbose=0):
|
||||
"""Fake request"""
|
||||
method_name = get_method_name(request_body)
|
||||
excepted_name, response = responses.pop(0)
|
||||
if method_name == excepted_name:
|
||||
if isinstance(response, Exception):
|
||||
raise response
|
||||
else:
|
||||
return response
|
||||
else:
|
||||
raise Exception('Expected call: %r, called with: %r' % (excepted_name, method_name))
|
||||
|
||||
target = '{0}.xmlrpc_client.Transport.request'.format(module_name)
|
||||
mocker.patch(target, side_effect=transport_request)
|
|
@ -1,147 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2017 Pierre-Louis Bonicoli <pierre-louis@libregerbil.fr>
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
|
||||
from ansible_collections.community.general.plugins.modules import rhn_channel
|
||||
|
||||
from .rhn_conftest import mock_request # noqa: F401, pylint: disable=unused-import
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
pytestmark = pytest.mark.usefixtures('patch_ansible_module')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||
def test_without_required_parameters(capfd):
|
||||
with pytest.raises(SystemExit):
|
||||
rhn_channel.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'missing required arguments' in results['msg']
|
||||
|
||||
|
||||
TESTED_MODULE = rhn_channel.__name__
|
||||
TEST_CASES = [
|
||||
[
|
||||
# add channel already added, check that result isn't changed
|
||||
{
|
||||
'name': 'rhel-x86_64-server-6',
|
||||
'sysname': 'server01',
|
||||
'url': 'https://rhn.redhat.com/rpc/api',
|
||||
'user': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('system.listUserSystems',
|
||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||
('channel.software.listSystemChannels',
|
||||
[[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'changed': False,
|
||||
'msg': 'Channel rhel-x86_64-server-6 already exists',
|
||||
}
|
||||
],
|
||||
[
|
||||
# add channel, check that result is changed
|
||||
{
|
||||
'name': 'rhel-x86_64-server-6-debuginfo',
|
||||
'sysname': 'server01',
|
||||
'url': 'https://rhn.redhat.com/rpc/api',
|
||||
'user': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('system.listUserSystems',
|
||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||
('channel.software.listSystemChannels',
|
||||
[[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||
('channel.software.listSystemChannels',
|
||||
[[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||
('system.setChildChannels', [1]),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'changed': True,
|
||||
'msg': 'Channel rhel-x86_64-server-6-debuginfo added',
|
||||
}
|
||||
],
|
||||
[
|
||||
# remove inexistent channel, check that result isn't changed
|
||||
{
|
||||
'name': 'rhel-x86_64-server-6-debuginfo',
|
||||
'state': 'absent',
|
||||
'sysname': 'server01',
|
||||
'url': 'https://rhn.redhat.com/rpc/api',
|
||||
'user': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('system.listUserSystems',
|
||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||
('channel.software.listSystemChannels',
|
||||
[[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'changed': False,
|
||||
'msg': 'Not subscribed to channel rhel-x86_64-server-6-debuginfo.',
|
||||
}
|
||||
],
|
||||
[
|
||||
# remove channel, check that result is changed
|
||||
{
|
||||
'name': 'rhel-x86_64-server-6-debuginfo',
|
||||
'state': 'absent',
|
||||
'sysname': 'server01',
|
||||
'url': 'https://rhn.redhat.com/rpc/api',
|
||||
'user': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('system.listUserSystems',
|
||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||
('channel.software.listSystemChannels', [[
|
||||
{'channel_name': 'RHEL Server Debuginfo (v.6 for x86_64)', 'channel_label': 'rhel-x86_64-server-6-debuginfo'},
|
||||
{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}
|
||||
]]),
|
||||
('channel.software.listSystemChannels', [[
|
||||
{'channel_name': 'RHEL Server Debuginfo (v.6 for x86_64)', 'channel_label': 'rhel-x86_64-server-6-debuginfo'},
|
||||
{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}
|
||||
]]),
|
||||
('system.setChildChannels', [1]),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'changed': True,
|
||||
'msg': 'Channel rhel-x86_64-server-6-debuginfo removed'
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, indirect=['patch_ansible_module'])
|
||||
def test_rhn_channel(capfd, mocker, testcase, mock_request):
|
||||
"""Check 'msg' and 'changed' results"""
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
rhn_channel.main()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['changed'] == testcase['changed']
|
||||
assert results['msg'] == testcase['msg']
|
||||
assert not testcase['calls'] # all calls should have been consumed
|
|
@ -1,293 +0,0 @@
|
|||
# Copyright (c) Ansible project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import mock_open
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
import ansible.module_utils.six
|
||||
from ansible.module_utils.six.moves import xmlrpc_client
|
||||
from ansible_collections.community.general.plugins.modules import rhn_register
|
||||
|
||||
from .rhn_conftest import mock_request # noqa: F401, pylint: disable=unused-import
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
SYSTEMID = """<?xml version="1.0"?>
|
||||
<params>
|
||||
<param>
|
||||
<value><struct>
|
||||
<member>
|
||||
<name>system_id</name>
|
||||
<value><string>ID-123456789</string></value>
|
||||
</member>
|
||||
</struct></value>
|
||||
</param>
|
||||
</params>
|
||||
"""
|
||||
|
||||
|
||||
def skipWhenAllModulesMissing(modules):
|
||||
"""Skip the decorated test unless one of modules is available."""
|
||||
for module in modules:
|
||||
try:
|
||||
__import__(module)
|
||||
return False
|
||||
except ImportError:
|
||||
continue
|
||||
|
||||
return True
|
||||
|
||||
|
||||
orig_import = __import__
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def import_libxml(mocker):
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name in ['libxml2', 'libxml']:
|
||||
raise ImportError()
|
||||
else:
|
||||
return orig_import(name, *args, **kwargs)
|
||||
|
||||
if ansible.module_utils.six.PY3:
|
||||
mocker.patch('builtins.__import__', side_effect=mock_import)
|
||||
else:
|
||||
mocker.patch('__builtin__.__import__', side_effect=mock_import)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_rhn(mocker):
|
||||
load_config_return = {
|
||||
'serverURL': 'https://xmlrpc.rhn.redhat.com/XMLRPC',
|
||||
'systemIdPath': '/etc/sysconfig/rhn/systemid'
|
||||
}
|
||||
|
||||
mocker.patch.object(rhn_register.Rhn, 'load_config', return_value=load_config_return)
|
||||
mocker.patch.object(rhn_register, 'HAS_UP2DATE_CLIENT', mocker.PropertyMock(return_value=True))
|
||||
|
||||
|
||||
@pytest.mark.skipif(skipWhenAllModulesMissing(['libxml2', 'libxml']), reason='none are available: libxml2, libxml')
|
||||
def test_systemid_with_requirements(capfd, mocker, patch_rhn):
|
||||
"""Check 'msg' and 'changed' results"""
|
||||
|
||||
mocker.patch.object(rhn_register.Rhn, 'enable')
|
||||
mock_isfile = mocker.patch('os.path.isfile', return_value=True)
|
||||
mocker.patch('ansible_collections.community.general.plugins.modules.rhn_register.open', mock_open(read_data=SYSTEMID), create=True)
|
||||
rhn = rhn_register.Rhn()
|
||||
assert '123456789' == to_native(rhn.systemid)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_systemid_requirements_missing(capfd, mocker, patch_rhn, import_libxml):
|
||||
"""Check that missing dependencies are detected"""
|
||||
|
||||
mocker.patch('os.path.isfile', return_value=True)
|
||||
mocker.patch('ansible_collections.community.general.plugins.modules.rhn_register.open', mock_open(read_data=SYSTEMID), create=True)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
rhn_register.main()
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'Missing arguments' in results['msg']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_without_required_parameters(capfd, patch_rhn):
|
||||
"""Failure must occurs when all parameters are missing"""
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
rhn_register.main()
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results['failed']
|
||||
assert 'Missing arguments' in results['msg']
|
||||
|
||||
|
||||
TESTED_MODULE = rhn_register.__name__
|
||||
TEST_CASES = [
|
||||
[
|
||||
# Registering an unregistered host with channels
|
||||
{
|
||||
'channels': 'rhel-x86_64-server-6',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('channel.software.listSystemChannels',
|
||||
[[{'channel_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||
('channel.software.setSystemChannels', [1]),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'is_registered': False,
|
||||
'is_registered.call_count': 1,
|
||||
'enable.call_count': 1,
|
||||
'systemid.call_count': 2,
|
||||
'changed': True,
|
||||
'msg': "System successfully registered to 'rhn.redhat.com'.",
|
||||
'run_command.call_count': 1,
|
||||
'run_command.call_args': '/usr/sbin/rhnreg_ks',
|
||||
'request_called': True,
|
||||
'unlink.call_count': 0,
|
||||
}
|
||||
],
|
||||
[
|
||||
# Registering an unregistered host without channels
|
||||
{
|
||||
'activationkey': 'key',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
],
|
||||
'is_registered': False,
|
||||
'is_registered.call_count': 1,
|
||||
'enable.call_count': 1,
|
||||
'systemid.call_count': 0,
|
||||
'changed': True,
|
||||
'msg': "System successfully registered to 'rhn.redhat.com'.",
|
||||
'run_command.call_count': 1,
|
||||
'run_command.call_args': '/usr/sbin/rhnreg_ks',
|
||||
'request_called': False,
|
||||
'unlink.call_count': 0,
|
||||
}
|
||||
],
|
||||
[
|
||||
# Register an host already registered, check that result is unchanged
|
||||
{
|
||||
'activationkey': 'key',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
],
|
||||
'is_registered': True,
|
||||
'is_registered.call_count': 1,
|
||||
'enable.call_count': 0,
|
||||
'systemid.call_count': 0,
|
||||
'changed': False,
|
||||
'msg': 'System already registered.',
|
||||
'run_command.call_count': 0,
|
||||
'request_called': False,
|
||||
'unlink.call_count': 0,
|
||||
},
|
||||
],
|
||||
[
|
||||
# Unregister an host, check that result is changed
|
||||
{
|
||||
'activationkey': 'key',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'state': 'absent',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('system.deleteSystems', [1]),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'is_registered': True,
|
||||
'is_registered.call_count': 1,
|
||||
'enable.call_count': 0,
|
||||
'systemid.call_count': 1,
|
||||
'changed': True,
|
||||
'msg': 'System successfully unregistered from rhn.redhat.com.',
|
||||
'run_command.call_count': 0,
|
||||
'request_called': True,
|
||||
'unlink.call_count': 1,
|
||||
}
|
||||
],
|
||||
[
|
||||
# Unregister a unregistered host (systemid missing) locally, check that result is unchanged
|
||||
{
|
||||
'activationkey': 'key',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'state': 'absent',
|
||||
},
|
||||
{
|
||||
'calls': [],
|
||||
'is_registered': False,
|
||||
'is_registered.call_count': 1,
|
||||
'enable.call_count': 0,
|
||||
'systemid.call_count': 0,
|
||||
'changed': False,
|
||||
'msg': 'System already unregistered.',
|
||||
'run_command.call_count': 0,
|
||||
'request_called': False,
|
||||
'unlink.call_count': 0,
|
||||
}
|
||||
|
||||
],
|
||||
[
|
||||
# Unregister an unknown host (an host with a systemid available locally, check that result contains failed
|
||||
{
|
||||
'activationkey': 'key',
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'state': 'absent',
|
||||
},
|
||||
{
|
||||
'calls': [
|
||||
('auth.login', ['X' * 43]),
|
||||
('system.deleteSystems', xmlrpc_client.Fault(1003, 'The following systems were NOT deleted: 123456789')),
|
||||
('auth.logout', [1]),
|
||||
],
|
||||
'is_registered': True,
|
||||
'is_registered.call_count': 1,
|
||||
'enable.call_count': 0,
|
||||
'systemid.call_count': 1,
|
||||
'failed': True,
|
||||
'msg': "Failed to unregister: <Fault 1003: 'The following systems were NOT deleted: 123456789'>",
|
||||
'run_command.call_count': 0,
|
||||
'request_called': True,
|
||||
'unlink.call_count': 0,
|
||||
}
|
||||
],
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, indirect=['patch_ansible_module'])
|
||||
@pytest.mark.usefixtures('patch_ansible_module')
|
||||
def test_register_parameters(mocker, capfd, mock_request, patch_rhn, testcase):
|
||||
# successful execution, no output
|
||||
mocker.patch.object(basic.AnsibleModule, 'run_command', return_value=(0, '', ''))
|
||||
mock_is_registered = mocker.patch.object(rhn_register.Rhn, 'is_registered', mocker.PropertyMock(return_value=testcase['is_registered']))
|
||||
mocker.patch.object(rhn_register.Rhn, 'enable')
|
||||
mock_systemid = mocker.patch.object(rhn_register.Rhn, 'systemid', mocker.PropertyMock(return_value=12345))
|
||||
mocker.patch('os.unlink', return_value=True)
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
rhn_register.main()
|
||||
|
||||
assert basic.AnsibleModule.run_command.call_count == testcase['run_command.call_count']
|
||||
if basic.AnsibleModule.run_command.call_count:
|
||||
assert basic.AnsibleModule.run_command.call_args[0][0][0] == testcase['run_command.call_args']
|
||||
|
||||
assert mock_is_registered.call_count == testcase['is_registered.call_count']
|
||||
assert rhn_register.Rhn.enable.call_count == testcase['enable.call_count']
|
||||
assert mock_systemid.call_count == testcase['systemid.call_count']
|
||||
assert xmlrpc_client.Transport.request.called == testcase['request_called']
|
||||
assert os.unlink.call_count == testcase['unlink.call_count']
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
results = json.loads(out)
|
||||
assert results.get('changed') == testcase.get('changed')
|
||||
assert results.get('failed') == testcase.get('failed')
|
||||
assert results['msg'] == testcase['msg']
|
||||
assert not testcase['calls'] # all calls should have been consumed
|
Loading…
Add table
Add a link
Reference in a new issue