mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
969 B
Python
30 lines
969 B
Python
7 years ago
|
from ansible.compat.tests.mock import patch
|
||
|
from ansible.module_utils.six.moves import xmlrpc_client
|
||
7 years ago
|
|
||
|
import pytest
|
||
7 years ago
|
|
||
|
|
||
|
def get_method_name(request_body):
|
||
|
return xmlrpc_client.loads(request_body)[1]
|
||
|
|
||
|
|
||
7 years ago
|
@pytest.fixture
|
||
|
def mock_request(request, mocker):
|
||
|
responses = request.getfuncargvalue('testcase')['calls']
|
||
|
module_name = request.module.TESTED_MODULE
|
||
|
|
||
7 years ago
|
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)
|
||
7 years ago
|
mocker.patch(target, side_effect=transport_request)
|