Skip to content

Commit f426334

Browse files
committed
Add test for retry_request_decorator
1 parent 6ba8cf3 commit f426334

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

tests/test_hubproxy.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import socket
2+
import http.client as httplib
3+
import six.moves.xmlrpc_client as xmlrpclib
4+
15
import pytest
26
import gssapi
37
import mock
48
import sys
59

6-
from kobo.xmlrpc import SafeCookieTransport
10+
from kobo.xmlrpc import SafeCookieTransport, retry_request_decorator
711
from kobo.conf import PyConfigParser
812
from kobo.client import HubProxy
913

@@ -36,6 +40,31 @@ def request(self, host, path, request, verbose=False):
3640
return []
3741

3842

43+
def error_raising_transport(exception, bad_function, exception_args=(),
44+
exception_kwargs={}):
45+
# Fake Transport class that raises specific exceptions.
46+
class FakeTransport(SafeCookieTransport):
47+
"""A fake XML-RPC transport where every request succeeds without doing anything.
48+
49+
Subclasses the real SafeCookieTransport so we get a real CookieJar.
50+
"""
51+
52+
def __init__(self, *args, **kwargs):
53+
super().__init__(*args, **kwargs)
54+
self.exception_count = 0
55+
56+
def request(self, host, path, request, verbose=False):
57+
# Only raise error for a specific faulty function. Hub proxy makes
58+
# a lot of requests during init we want to allow. Request is binary
59+
# xml which should contain our function as a string
60+
if bad_function in request:
61+
self.exception_count += 1
62+
raise exception(*exception_args, **exception_kwargs)
63+
64+
return []
65+
66+
return FakeTransport
67+
3968
def test_login_token_oidc(requests_session):
4069
"""Login with OIDC client credentials flow."""
4170

@@ -252,3 +281,27 @@ def test_pass_transport_args(requests_session):
252281
mock_transport_class.assert_called_with(context=mock.ANY,
253282
retry_count=2,
254283
retry_timeout=45)
284+
285+
286+
@pytest.mark.parametrize("exception, exception_args, exception_kwargs",
287+
[(socket.error, (), {}),
288+
(httplib.CannotSendRequest, (), {}),
289+
(xmlrpclib.Fault, ["1", "PermissionDenied"], {})]
290+
)
291+
def test_proxy_retries_on_error(requests_session, capsys, exception, exception_args, exception_kwargs):
292+
"""HubProxy proxy retry class captures exceptions"""
293+
retry_count = 2
294+
conf = PyConfigParser()
295+
conf.load_from_dict({"HUB_URL": 'https://example.com/hub'})
296+
TransportClass = retry_request_decorator(
297+
error_raising_transport(exception, b"faulty.function", exception_args, exception_kwargs)
298+
)
299+
transport = TransportClass(retry_count=retry_count, retry_timeout=1)
300+
proxy = HubProxy(conf, transport=transport)
301+
302+
with pytest.raises(exception):
303+
proxy.faulty.function()
304+
305+
assert transport.exception_count == retry_count + 1
306+
captured = capsys.readouterr()
307+
assert captured.err.count("XML-RPC connection to example.com failed") == retry_count

0 commit comments

Comments
 (0)