Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert an integration test to unit test. #18438

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/18438.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`utils.http.RequestsWrapper` accepts a session at initialization, useful for testing and controlling sessions in general.
4 changes: 2 additions & 2 deletions datadog_checks_base/datadog_checks/base/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class RequestsWrapper(object):
'tls_protocols_allowed',
)

def __init__(self, instance, init_config, remapper=None, logger=None):
def __init__(self, instance, init_config, remapper=None, logger=None, session=None):
self.logger = logger or LOGGER
default_fields = dict(STANDARD_FIELDS)

Expand Down Expand Up @@ -329,7 +329,7 @@ def __init__(self, instance, init_config, remapper=None, logger=None):
# https://requests.readthedocs.io/en/latest/user/advanced/#session-objects
# https://requests.readthedocs.io/en/latest/user/advanced/#keep-alive
self.persist_connections = self.tls_use_host_header or is_affirmative(config['persist_connections'])
self._session = None
self._session = session

# Whether or not to log request information like method and url
self.log_requests = is_affirmative(config['log_requests'])
Expand Down
28 changes: 21 additions & 7 deletions datadog_checks_base/tests/base/utils/http/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ def test_attributes(self):
assert hasattr(http.session, key)
assert getattr(http.session, key) == value

def test_timeout(self):
"""
Respect the request timeout.

Here we test two things:
- We pass the timemout option correctly to the requests library.
- We let the requests.exceptions.Timeout bubble up from the requests library.

We trust requests to respect the timeout so we mock its response.
"""

mock_session = mock.create_autospec(requests.Session)
mock_session.get.side_effect = requests.exceptions.Timeout()
http = RequestsWrapper({'persist_connections': True}, {'timeout': 0.08}, session=mock_session)

with pytest.raises(requests.exceptions.Timeout):
http.get('https://foobar.com')

assert 'timeout' in mock_session.get.call_args.kwargs, mock_session.get.call_args.kwargs
assert mock_session.get.call_args.kwargs['timeout'] == (0.08, 0.08)


class TestLogger:
def test_default(self, caplog):
Expand Down Expand Up @@ -174,10 +195,3 @@ def test_instance_override(self, caplog):
expected_message = 'Sending GET request to https://www.google.com'
for _, _, message in caplog.record_tuples:
assert message != expected_message


class TestIntegration:
def test_session_timeout(self):
http = RequestsWrapper({'persist_connections': True}, {'timeout': 0.08})
with pytest.raises(requests.exceptions.Timeout):
http.get('https://httpbin.org/delay/0.10')
Loading