forked from ansible/ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rhn_channel/register: Porting tests to pytest (ansible#33719)
* rhn_channel/register: Porting tests to pytest Related: ansible#33387 * rhn unit tests: use mock_request as a fixture
- Loading branch information
Showing
3 changed files
with
327 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,142 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2017 Pierre-Louis Bonicoli <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from itertools import product | ||
import json | ||
|
||
from ansible.modules.packaging.os import rhn_channel | ||
|
||
from units.modules.packaging.utils import mock_request | ||
from units.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase | ||
import pytest | ||
|
||
|
||
class TestRhnChannel(ModuleTestCase): | ||
pytestmark = pytest.mark.usefixtures('patch_ansible_module') | ||
|
||
def setUp(self): | ||
super(TestRhnChannel, self).setUp() | ||
|
||
self.module = rhn_channel | ||
self.module.HAS_UP2DATE_CLIENT = True | ||
@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'] | ||
|
||
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): | ||
"""Check that result isn't changed""" | ||
set_module_args({ | ||
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', | ||
}) | ||
|
||
responses = [ | ||
('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]), | ||
] | ||
|
||
with mock_request(responses, self.module.__name__): | ||
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 | ||
|
||
def test_add_channel(self): | ||
"""Add another channel: check that result is changed""" | ||
set_module_args({ | ||
}, | ||
{ | ||
'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', | ||
}) | ||
|
||
responses = [ | ||
('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]), | ||
] | ||
|
||
with mock_request(responses, self.module.__name__): | ||
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 | ||
|
||
def test_remove_inexistent_channel(self): | ||
"""Check that result isn't changed""" | ||
set_module_args({ | ||
}, | ||
{ | ||
'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', | ||
}) | ||
|
||
responses = [ | ||
('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]), | ||
] | ||
|
||
with mock_request(responses, self.module.__name__): | ||
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 | ||
|
||
def test_remove_channel(self): | ||
"""Check that result isn't changed""" | ||
set_module_args({ | ||
}, | ||
{ | ||
'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', | ||
}) | ||
|
||
responses = [ | ||
('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]), | ||
] | ||
|
||
with mock_request(responses, self.module.__name__): | ||
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 | ||
}, | ||
{ | ||
'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 |
Oops, something went wrong.