mirror of https://github.com/ansible/ansible.git
rhn_channel/register: Porting tests to pytest (#33719)
* rhn_channel/register: Porting tests to pytest Related: #33387 * rhn unit tests: use mock_request as a fixturepull/33820/head
parent
a2650cbe05
commit
58fdbe7415
@ -1,133 +1,142 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright (c) 2017 Pierre-Louis Bonicoli <pierre-louis@libregerbil.fr>
|
||||||
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
|
|
||||||
|
from itertools import product
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from ansible.modules.packaging.os import rhn_channel
|
from ansible.modules.packaging.os import rhn_channel
|
||||||
|
|
||||||
from units.modules.packaging.utils import mock_request
|
import pytest
|
||||||
from units.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase
|
|
||||||
|
|
||||||
|
|
||||||
class TestRhnChannel(ModuleTestCase):
|
pytestmark = pytest.mark.usefixtures('patch_ansible_module')
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super(TestRhnChannel, self).setUp()
|
|
||||||
|
|
||||||
self.module = rhn_channel
|
@pytest.mark.parametrize('patch_ansible_module', [{}], indirect=['patch_ansible_module'])
|
||||||
self.module.HAS_UP2DATE_CLIENT = True
|
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']
|
||||||
|
|
||||||
def test_without_required_parameters(self):
|
|
||||||
"""Failure must occurs when all parameters are missing"""
|
|
||||||
with self.assertRaises(AnsibleFailJson):
|
|
||||||
set_module_args({})
|
|
||||||
self.module.main()
|
|
||||||
|
|
||||||
def test_channel_already_here(self):
|
TESTED_MODULE = rhn_channel.__name__
|
||||||
"""Check that result isn't changed"""
|
TEST_CASES = [
|
||||||
set_module_args({
|
[
|
||||||
|
# add channel already added, check that result isn't changed
|
||||||
|
{
|
||||||
'name': 'rhel-x86_64-server-6',
|
'name': 'rhel-x86_64-server-6',
|
||||||
'sysname': 'server01',
|
'sysname': 'server01',
|
||||||
'url': 'https://rhn.redhat.com/rpc/api',
|
'url': 'https://rhn.redhat.com/rpc/api',
|
||||||
'user': 'user',
|
'user': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
})
|
},
|
||||||
|
{
|
||||||
responses = [
|
'calls': [
|
||||||
('auth.login', ['X' * 43]),
|
('auth.login', ['X' * 43]),
|
||||||
('system.listUserSystems',
|
('system.listUserSystems',
|
||||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||||
('channel.software.listSystemChannels',
|
('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_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||||
('auth.logout', [1]),
|
('auth.logout', [1]),
|
||||||
]
|
],
|
||||||
|
'changed': False,
|
||||||
with mock_request(responses, self.module.__name__):
|
'msg': 'Channel rhel-x86_64-server-6 already exists',
|
||||||
with self.assertRaises(AnsibleExitJson) as result:
|
}
|
||||||
self.module.main()
|
],
|
||||||
self.assertFalse(result.exception.args[0]['changed'])
|
[
|
||||||
self.assertFalse(responses) # all responses should have been consumed
|
# add channel, check that result is changed
|
||||||
|
{
|
||||||
def test_add_channel(self):
|
|
||||||
"""Add another channel: check that result is changed"""
|
|
||||||
set_module_args({
|
|
||||||
'name': 'rhel-x86_64-server-6-debuginfo',
|
'name': 'rhel-x86_64-server-6-debuginfo',
|
||||||
'sysname': 'server01',
|
'sysname': 'server01',
|
||||||
'url': 'https://rhn.redhat.com/rpc/api',
|
'url': 'https://rhn.redhat.com/rpc/api',
|
||||||
'user': 'user',
|
'user': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
})
|
},
|
||||||
|
{
|
||||||
responses = [
|
'calls': [
|
||||||
('auth.login', ['X' * 43]),
|
('auth.login', ['X' * 43]),
|
||||||
('system.listUserSystems',
|
('system.listUserSystems',
|
||||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||||
('channel.software.listSystemChannels',
|
('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_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||||
('channel.software.listSystemChannels',
|
('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_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||||
('system.setChildChannels', [1]),
|
('system.setChildChannels', [1]),
|
||||||
('auth.logout', [1]),
|
('auth.logout', [1]),
|
||||||
]
|
],
|
||||||
|
'changed': True,
|
||||||
with mock_request(responses, self.module.__name__):
|
'msg': 'Channel rhel-x86_64-server-6-debuginfo added',
|
||||||
with self.assertRaises(AnsibleExitJson) as result:
|
}
|
||||||
self.module.main()
|
],
|
||||||
self.assertTrue(result.exception.args[0]['changed'])
|
[
|
||||||
self.assertFalse(responses) # all responses should have been consumed
|
# remove inexistent channel, check that result isn't changed
|
||||||
|
{
|
||||||
def test_remove_inexistent_channel(self):
|
|
||||||
"""Check that result isn't changed"""
|
|
||||||
set_module_args({
|
|
||||||
'name': 'rhel-x86_64-server-6-debuginfo',
|
'name': 'rhel-x86_64-server-6-debuginfo',
|
||||||
'state': 'absent',
|
'state': 'absent',
|
||||||
'sysname': 'server01',
|
'sysname': 'server01',
|
||||||
'url': 'https://rhn.redhat.com/rpc/api',
|
'url': 'https://rhn.redhat.com/rpc/api',
|
||||||
'user': 'user',
|
'user': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
})
|
},
|
||||||
|
{
|
||||||
responses = [
|
'calls': [
|
||||||
('auth.login', ['X' * 43]),
|
('auth.login', ['X' * 43]),
|
||||||
('system.listUserSystems',
|
('system.listUserSystems',
|
||||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||||
('channel.software.listSystemChannels',
|
('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_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}]]),
|
||||||
('auth.logout', [1]),
|
('auth.logout', [1]),
|
||||||
]
|
],
|
||||||
|
'changed': False,
|
||||||
with mock_request(responses, self.module.__name__):
|
'msg': 'Not subscribed to channel rhel-x86_64-server-6-debuginfo.',
|
||||||
with self.assertRaises(AnsibleExitJson) as result:
|
}
|
||||||
self.module.main()
|
],
|
||||||
self.assertFalse(result.exception.args[0]['changed'])
|
[
|
||||||
self.assertFalse(responses) # all responses should have been consumed
|
# remove channel, check that result is changed
|
||||||
|
{
|
||||||
def test_remove_channel(self):
|
|
||||||
"""Check that result isn't changed"""
|
|
||||||
set_module_args({
|
|
||||||
'name': 'rhel-x86_64-server-6-debuginfo',
|
'name': 'rhel-x86_64-server-6-debuginfo',
|
||||||
'state': 'absent',
|
'state': 'absent',
|
||||||
'sysname': 'server01',
|
'sysname': 'server01',
|
||||||
'url': 'https://rhn.redhat.com/rpc/api',
|
'url': 'https://rhn.redhat.com/rpc/api',
|
||||||
'user': 'user',
|
'user': 'user',
|
||||||
'password': 'pass',
|
'password': 'pass',
|
||||||
})
|
},
|
||||||
|
{
|
||||||
responses = [
|
'calls': [
|
||||||
('auth.login', ['X' * 43]),
|
('auth.login', ['X' * 43]),
|
||||||
('system.listUserSystems',
|
('system.listUserSystems',
|
||||||
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
[[{'last_checkin': '2017-08-06 19:49:52.0', 'id': '0123456789', 'name': 'server01'}]]),
|
||||||
('channel.software.listSystemChannels', [[
|
('channel.software.listSystemChannels', [[
|
||||||
{'channel_name': 'RHEL Server Debuginfo (v.6 for x86_64)', 'channel_label': 'rhel-x86_64-server-6-debuginfo'},
|
{'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_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}
|
||||||
]]),
|
]]),
|
||||||
('channel.software.listSystemChannels', [[
|
('channel.software.listSystemChannels', [[
|
||||||
{'channel_name': 'RHEL Server Debuginfo (v.6 for x86_64)', 'channel_label': 'rhel-x86_64-server-6-debuginfo'},
|
{'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_name': 'Red Hat Enterprise Linux Server (v. 6 for 64-bit x86_64)', 'channel_label': 'rhel-x86_64-server-6'}
|
||||||
]]),
|
]]),
|
||||||
('system.setChildChannels', [1]),
|
('system.setChildChannels', [1]),
|
||||||
('auth.logout', [1]),
|
('auth.logout', [1]),
|
||||||
]
|
],
|
||||||
|
'changed': True,
|
||||||
with mock_request(responses, self.module.__name__):
|
'msg': 'Channel rhel-x86_64-server-6-debuginfo removed'
|
||||||
with self.assertRaises(AnsibleExitJson) as result:
|
}
|
||||||
self.module.main()
|
]
|
||||||
self.assertTrue(result.exception.args[0]['changed'])
|
]
|
||||||
self.assertFalse(responses) # all responses should have been consumed
|
|
||||||
|
|
||||||
|
@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
|
||||||
|
Loading…
Reference in New Issue