rhn_channel: add unit tests

This commit is contained in:
Pierre-Louis Bonicoli 2017-08-07 18:50:15 +02:00 committed by Toshio Kuratomi
parent dbc422ea1c
commit 94a327dd09
3 changed files with 201 additions and 48 deletions

View file

@ -0,0 +1,51 @@
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
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
def get_method_name(request_body):
return xmlrpc_client.loads(request_body)[1]
def mock_request(responses, module_name):
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)
return patch(target, side_effect=transport_request)

View file

@ -0,0 +1,143 @@
import json
from ansible.compat.tests import unittest
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
from ansible.modules.packaging.os import rhn_channel
from .rhn_utils import (set_module_args, AnsibleExitJson, AnsibleFailJson,
exit_json, fail_json, get_method_name, mock_request)
class TestRhnChannel(unittest.TestCase):
def setUp(self):
self.module = rhn_channel
self.module.HAS_UP2DATE_CLIENT = True
self.mock_exit_fail = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
self.mock_exit_fail.start()
self.addCleanup(self.mock_exit_fail.stop)
def tearDown(self):
pass
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({
'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({
'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({
'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({
'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

View file

@ -4,32 +4,10 @@ from ansible.compat.tests import unittest
from ansible.compat.tests.mock import PropertyMock, patch, mock_open
from ansible.module_utils import basic
from ansible.module_utils.six.moves import xmlrpc_client
from ansible.module_utils._text import to_bytes
from ansible.modules.packaging.os import rhn_register
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)
def fail_json(*args, **kwargs):
kwargs['failed'] = True
raise AnsibleFailJson(kwargs)
from .rhn_utils import (set_module_args, AnsibleExitJson, AnsibleFailJson,
exit_json, fail_json, get_method_name, mock_request)
SYSTEMID = """<?xml version="1.0"?>
@ -58,25 +36,6 @@ def skipWhenAllModulesMissing(modules):
return unittest.skip("{0}: none are available".format(', '.join(modules)))
def get_method_name(request_body):
return xmlrpc_client.loads(request_body)[1]
def mock_request(responses):
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
target = 'ansible.modules.packaging.os.rhn_register.xmlrpc_client.Transport.request'
return patch(target, side_effect=transport_request)
class TestRhnRegister(unittest.TestCase):
def setUp(self):
@ -164,7 +123,7 @@ class TestRhnRegister(unittest.TestCase):
with patch.object(basic.AnsibleModule, 'run_command') as run_command:
run_command.return_value = 0, '', '' # successful execution, no output
with patch.object(rhn_register.Rhn, 'systemid', PropertyMock(return_value=12345)):
with mock_request(responses):
with mock_request(responses, self.module.__name__):
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertTrue(result.exception.args[0]['changed'])
@ -196,7 +155,7 @@ class TestRhnRegister(unittest.TestCase):
with patch.object(basic.AnsibleModule, 'run_command') as run_command:
run_command.return_value = 0, '', '' # successful execution, no output
with patch.object(rhn_register.Rhn, 'systemid', PropertyMock(return_value=12345)):
with mock_request(responses):
with mock_request(responses, self.module.__name__):
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertTrue(result.exception.args[0]['changed'])
@ -220,7 +179,7 @@ class TestRhnRegister(unittest.TestCase):
with patch.object(basic.AnsibleModule, 'run_command') as run_command:
with patch.object(rhn_register.Rhn, 'is_registered', PropertyMock(return_value=True)) as mock_systemid:
with mock_request(responses) as req:
with mock_request(responses, self.module.__name__) as req:
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertFalse(result.exception.args[0]['changed'])
@ -254,7 +213,7 @@ class TestRhnRegister(unittest.TestCase):
mock_is_registered = PropertyMock(return_value=True)
mock_systemid = PropertyMock(return_value=12345)
with patch.multiple(rhn_register.Rhn, systemid=mock_systemid, is_registered=mock_is_registered):
with mock_request(responses):
with mock_request(responses, self.module.__name__):
with self.assertRaises(AnsibleExitJson) as result:
self.module.main()
self.assertTrue(result.exception.args[0]['changed'])
@ -313,7 +272,7 @@ class TestRhnRegister(unittest.TestCase):
mock_is_registered = PropertyMock(return_value=True)
mock_systemid = PropertyMock(return_value=12345)
with patch.multiple(rhn_register.Rhn, systemid=mock_systemid, is_registered=mock_is_registered):
with mock_request(responses):
with mock_request(responses, self.module.__name__):
with self.assertRaises(AnsibleFailJson) as result:
self.module.main()
self.assertTrue(result.exception.args[0]['failed'])