2018-10-13 03:01:14 +00:00
|
|
|
from units.compat.mock import patch
|
2017-08-07 16:50:15 +00:00
|
|
|
from ansible.module_utils.six.moves import xmlrpc_client
|
2017-12-12 15:25:49 +00:00
|
|
|
|
|
|
|
import pytest
|
2017-08-07 16:50:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_method_name(request_body):
|
|
|
|
return xmlrpc_client.loads(request_body)[1]
|
|
|
|
|
|
|
|
|
2017-12-12 15:25:49 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_request(request, mocker):
|
2018-11-15 01:41:15 +00:00
|
|
|
responses = request.getfixturevalue('testcase')['calls']
|
2017-12-12 15:25:49 +00:00
|
|
|
module_name = request.module.TESTED_MODULE
|
|
|
|
|
2017-08-07 16:50:15 +00:00
|
|
|
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)
|
2017-12-12 15:25:49 +00:00
|
|
|
mocker.patch(target, side_effect=transport_request)
|