Skip to content

Commit

Permalink
rhn_channel/register: Porting tests to pytest (ansible#33719)
Browse files Browse the repository at this point in the history
* rhn_channel/register: Porting tests to pytest

Related: ansible#33387

* rhn unit tests: use mock_request as a fixture
  • Loading branch information
pilou- authored and abadger committed Dec 12, 2017
1 parent a2650cb commit 58fdbe7
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 332 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json

from ansible.compat.tests.mock import patch
from ansible.module_utils import basic
from ansible.module_utils.six.moves import xmlrpc_client
from ansible.module_utils._text import to_bytes

import pytest


def get_method_name(request_body):
return xmlrpc_client.loads(request_body)[1]


def mock_request(responses, module_name):
@pytest.fixture
def mock_request(request, mocker):
responses = request.getfuncargvalue('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)
Expand All @@ -24,4 +26,4 @@ def transport_request(host, handler, request_body, verbose=0):
raise Exception('Expected call: %r, called with: %r' % (excepted_name, method_name))

target = '{0}.xmlrpc_client.Transport.request'.format(module_name)
return patch(target, side_effect=transport_request)
mocker.patch(target, side_effect=transport_request)
211 changes: 110 additions & 101 deletions test/units/modules/packaging/os/test_rhn_channel.py
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
Loading

0 comments on commit 58fdbe7

Please sign in to comment.