diff --git a/arangodb/tests/test_arangodb.py b/arangodb/tests/test_arangodb.py index 14ec1ce25b8c1..3790a3e599304 100644 --- a/arangodb/tests/test_arangodb.py +++ b/arangodb/tests/test_arangodb.py @@ -9,7 +9,7 @@ from requests import HTTPError from datadog_checks.arangodb import ArangodbCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from .common import METRICS @@ -54,7 +54,7 @@ def test_check(instance, dd_run_check, aggregator, tag_condition, base_tags): def mock_requests_get(session, url, *args, **kwargs): fixture = url.rsplit('/', 1)[-1] - return MockResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', tag_condition, fixture)) + return MockHTTPResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', tag_condition, fixture)) with mock.patch('requests.Session.get', side_effect=mock_requests_get, autospec=True): dd_run_check(check) diff --git a/avi_vantage/tests/conftest.py b/avi_vantage/tests/conftest.py index 8de057987afac..46c029fde49d9 100644 --- a/avi_vantage/tests/conftest.py +++ b/avi_vantage/tests/conftest.py @@ -4,14 +4,14 @@ import json import os from typing import Any, AnyStr +from unittest.mock import MagicMock from urllib.parse import urlparse -import mock import pytest +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run, get_docker_hostname, get_here from datadog_checks.dev.conditions import CheckDockerLogs -from datadog_checks.dev.http import MockResponse HERE = get_here() @@ -57,7 +57,7 @@ def _get_metrics(metrics_folder=NO_TENANT_METRICS_FOLDER, endpoint=None): @pytest.fixture -def mock_client(): +def mock_client(mock_http): def mock_get(url: AnyStr, *__: Any, **___: Any): parsed = urlparse(url) resource = [part for part in parsed.path.split('/') if len(part) > 0][-1] @@ -69,20 +69,21 @@ def mock_get(url: AnyStr, *__: Any, **___: Any): path['tenant=admin%2Ctenant_a%2Ctenant_b'] = MULTIPLE_TENANTS_METRICS_FOLDER if query_params: - return MockResponse( + return MockHTTPResponse( file_path=os.path.join(HERE, 'compose', 'fixtures', path[query_params], f'{resource}_metrics') ) - return MockResponse( + return MockHTTPResponse( file_path=os.path.join(HERE, 'compose', 'fixtures', NO_TENANT_METRICS_FOLDER, f'{resource}_metrics') ) def mock_post(url: AnyStr, *__: Any, **___: Any): - return mock.MagicMock(status_code=200, content=b'{"results": []}') + return MockHTTPResponse(json_data={"results": []}) - with mock.patch('datadog_checks.base.utils.http.RequestsWrapper.get', side_effect=mock_get): - with mock.patch('datadog_checks.base.utils.http.RequestsWrapper.post', new=mock_post): - yield + mock_http.session = MagicMock(cookies={}) + mock_http.get.side_effect = mock_get + mock_http.post.side_effect = mock_post + yield @pytest.fixture(scope='session') diff --git a/cert_manager/tests/test_cert_manager.py b/cert_manager/tests/test_cert_manager.py index ee1097015556e..28a6b9e1e7e6e 100644 --- a/cert_manager/tests/test_cert_manager.py +++ b/cert_manager/tests/test_cert_manager.py @@ -6,8 +6,8 @@ import mock import pytest +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.cert_manager import CertManagerCheck -from datadog_checks.dev.http import MockResponse from .common import ACME_METRICS, CERT_METRICS, CONTROLLER_METRICS, MOCK_INSTANCE @@ -32,7 +32,7 @@ def test_check(aggregator, dd_run_check): check = CertManagerCheck('cert_manager', {}, [MOCK_INSTANCE]) def mock_requests_get(url, *args, **kwargs): - return MockResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', 'cert_manager.txt')) + return MockHTTPResponse(file_path=os.path.join(os.path.dirname(__file__), 'fixtures', 'cert_manager.txt')) with mock.patch('requests.Session.get', side_effect=mock_requests_get, autospec=True): dd_run_check(check) diff --git a/citrix_hypervisor/tests/conftest.py b/citrix_hypervisor/tests/conftest.py index 9670622329d2e..26537a41a4815 100644 --- a/citrix_hypervisor/tests/conftest.py +++ b/citrix_hypervisor/tests/conftest.py @@ -3,11 +3,10 @@ # Licensed under a 3-clause BSD style license (see LICENSE) import os -import mock import pytest +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run -from datadog_checks.dev.http import MockResponse from . import common @@ -38,17 +37,17 @@ def mock_requests_get(url, *args, **kwargs): print(url_parts) if url_parts[0] == 'wrong': - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) json_file = f"rrd_updates_{url_parts[0]}.json" if url_parts[1] == "rrd_updates" else f"{url_parts[1]}.json" path = os.path.join(common.HERE, 'fixtures', 'standalone', json_file) if not os.path.exists(path): - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) - return MockResponse(file_path=path) + return MockHTTPResponse(file_path=path) @pytest.fixture -def mock_responses(): - with mock.patch('requests.Session.get', side_effect=mock_requests_get): - yield +def mock_responses(mock_http): + mock_http.get.side_effect = mock_requests_get + yield diff --git a/datadog_checks_base/datadog_checks/base/utils/http_testing.py b/datadog_checks_base/datadog_checks/base/utils/http_testing.py index 5be3651809ad6..428b370bfd130 100644 --- a/datadog_checks_base/datadog_checks/base/utils/http_testing.py +++ b/datadog_checks_base/datadog_checks/base/utils/http_testing.py @@ -2,6 +2,7 @@ # All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) import json +import re from collections.abc import Mapping from datetime import timedelta from http.client import responses as http_responses @@ -119,6 +120,31 @@ def ok(self) -> bool: def reason(self) -> str: return http_responses.get(self.status_code, '') + @property + def links(self) -> dict[str, dict[str, str]]: + """Parse Link header into a dict keyed by rel, matching requests.Response.links.""" + header = self.headers.get('link', '').strip().strip("'\"") + result: dict[str, dict[str, str]] = {} + if not header: + return result + # Split on ", <" to avoid breaking URLs that contain commas (matches requests behavior) + for val in re.split(', *<', header): + try: + url, params_str = val.split(';', 1) + except ValueError: + url, params_str = val, '' + link: dict[str, str] = {'url': url.strip("<> '\"")} + for param in params_str.split(';'): + try: + key, value = param.split('=') + except ValueError: + break + link[key.strip(" '\"")] = value.strip(" '\"") + key = link.get('rel') or link.get('url') + if key: + result[key] = link + return result + def json(self, **kwargs: Any) -> Any: return json.loads(self.text, **kwargs) diff --git a/datadog_checks_base/tests/base/checks/prometheus/test_prometheus.py b/datadog_checks_base/tests/base/checks/prometheus/test_prometheus.py index 479a85e38bf1a..bc45101f2d8ab 100644 --- a/datadog_checks_base/tests/base/checks/prometheus/test_prometheus.py +++ b/datadog_checks_base/tests/base/checks/prometheus/test_prometheus.py @@ -1963,11 +1963,14 @@ def test_text_filter_input(): def test_ssl_verify_not_raise_warning(caplog, mocked_prometheus_check, text_data): - from datadog_checks.dev.http import MockResponse + from datadog_checks.base.utils.http_testing import MockHTTPResponse check = mocked_prometheus_check - with caplog.at_level(logging.DEBUG), mock.patch('requests.Session.get', return_value=MockResponse('httpbin.org')): + with ( + caplog.at_level(logging.DEBUG), + mock.patch('requests.Session.get', return_value=MockHTTPResponse(content='httpbin.org')), + ): resp = check.poll('https://httpbin.org/get') assert 'httpbin.org' in resp.content.decode('utf-8') @@ -1978,12 +1981,15 @@ def test_ssl_verify_not_raise_warning(caplog, mocked_prometheus_check, text_data def test_ssl_verify_not_raise_warning_cert_false(caplog, mocked_prometheus_check, text_data): - from datadog_checks.dev.http import MockResponse + from datadog_checks.base.utils.http_testing import MockHTTPResponse check = mocked_prometheus_check check.ssl_ca_cert = False - with caplog.at_level(logging.DEBUG), mock.patch('requests.Session.get', return_value=MockResponse('httpbin.org')): + with ( + caplog.at_level(logging.DEBUG), + mock.patch('requests.Session.get', return_value=MockHTTPResponse(content='httpbin.org')), + ): resp = check.poll('https://httpbin.org/get') assert 'httpbin.org' in resp.content.decode('utf-8') diff --git a/datadog_checks_base/tests/base/utils/http/test_http_testing.py b/datadog_checks_base/tests/base/utils/http/test_http_testing.py index 93d166002bfba..ebaccc59efd07 100644 --- a/datadog_checks_base/tests/base/utils/http/test_http_testing.py +++ b/datadog_checks_base/tests/base/utils/http/test_http_testing.py @@ -83,19 +83,6 @@ def test_mock_response_normalize_leading_newline_with_indent(): assert response.text == "line one\nline two\n" -def test_mock_response_ok_property(): - assert MockHTTPResponse(status_code=200).ok is True - assert MockHTTPResponse(status_code=399).ok is True - assert MockHTTPResponse(status_code=400).ok is False - assert MockHTTPResponse(status_code=500).ok is False - - -def test_mock_response_reason_property(): - assert MockHTTPResponse(status_code=200).reason == 'OK' - assert MockHTTPResponse(status_code=404).reason == 'Not Found' - assert MockHTTPResponse(status_code=999).reason == '' - - def test_mock_response_headers_case_insensitive(): response = MockHTTPResponse(headers={'Content-Type': 'text/plain', 'X-Custom': 'val'}) @@ -105,16 +92,6 @@ def test_mock_response_headers_case_insensitive(): assert response.headers.get('cOnTeNt-tYpE') == 'text/plain' -def test_mock_response_headers_delete_and_pop(): - response = MockHTTPResponse(headers={'Content-Type': 'text/plain', 'X-Custom': 'val'}) - - del response.headers['Content-Type'] - assert 'content-type' not in response.headers - - assert response.headers.pop('X-Custom') == 'val' - assert response.headers.pop('X-Custom', 'gone') == 'gone' - - def test_mock_response_headers_update_and_setdefault(): response = MockHTTPResponse(headers={'Content-Type': 'text/plain'}) @@ -131,31 +108,42 @@ def test_mock_response_headers_update_and_setdefault(): assert response.headers['x-iter'] == 'iter_val' -def test_mock_response_headers_update_with_non_dict_mapping(): - from collections.abc import Mapping +def test_mock_response_links_standard(): + response = MockHTTPResponse(headers={'link': '; rel=next; type="text/plain"'}) + assert 'next' in response.links + assert response.links['next']['url'] == 'http://example.com/page2' + assert response.links['next']['type'] == 'text/plain' + + +def test_mock_response_links_multiple(): + response = MockHTTPResponse( + headers={'link': '; rel=next, ; rel=prev'} + ) + assert len(response.links) == 2 + assert response.links['next']['url'] == 'http://example.com/page2' + assert response.links['prev']['url'] == 'http://example.com/page1' + - class CustomHeaders(Mapping): - def __init__(self, d): - self._d = d +def test_mock_response_links_empty(): + assert MockHTTPResponse().links == {} + assert MockHTTPResponse(headers={'link': ''}).links == {} - def __getitem__(self, key): - return self._d[key] - def __iter__(self): - return iter(self._d) +def test_mock_response_links_no_rel_keys_by_url(): + response = MockHTTPResponse(headers={'link': '; type="text/plain"'}) + assert 'http://example.com/page2' in response.links - def __len__(self): - return len(self._d) - response = MockHTTPResponse(headers={'A': '1'}) - response.headers.update(CustomHeaders({'B': '2'})) - assert response.headers['b'] == '2' - assert response.headers['a'] == '1' +def test_mock_response_links_url_with_comma(): + response = MockHTTPResponse(headers={'link': '; rel=next'}) + assert response.links['next']['url'] == 'http://example.com/path?a=1,2' -def test_mock_response_url(): - assert MockHTTPResponse(url='http://example.com').url == 'http://example.com' - assert MockHTTPResponse().url == '' +def test_mock_response_links_cleared_after_header_pop(): + response = MockHTTPResponse(headers={'link': '; rel=next'}) + assert 'next' in response.links + response.headers.pop('link') + assert response.links == {} def test_mock_response_raw_readable(): diff --git a/ecs_fargate/tests/conftest.py b/ecs_fargate/tests/conftest.py index 8c092686f9825..9176123642a01 100644 --- a/ecs_fargate/tests/conftest.py +++ b/ecs_fargate/tests/conftest.py @@ -6,8 +6,8 @@ import pytest +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import get_here -from datadog_checks.dev.http import MockResponse from datadog_checks.ecs_fargate import FargateCheck HERE = get_here() @@ -83,38 +83,38 @@ def mocked_requests_get_linux(*args, **kwargs): # v2 if args[0].endswith("/metadata"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata.json')) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata.json')) elif args[0].endswith("/stats"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', LINUX_STATS_FIXTURE)) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', LINUX_STATS_FIXTURE)) else: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) def mocked_requests_get_linux_v4(*args, **kwargs): if args[0].endswith("/task"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata_v4.json')) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata_v4.json')) elif args[0].endswith("/task/stats"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', LINUX_STATS_FIXTURE_V4)) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', LINUX_STATS_FIXTURE_V4)) else: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) def mocked_requests_get_windows(*args, **kwargs): if args[0].endswith("/metadata"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata.json')) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata.json')) elif args[0].endswith("/stats"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', WINDOWS_STATS_FIXTURE)) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', WINDOWS_STATS_FIXTURE)) else: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) def mocked_requests_get_sys_delta(*args, **kwargs): if args[0].endswith("/metadata"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata.json')) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'metadata.json')) elif args[0].endswith("/stats"): - return MockResponse(file_path=os.path.join(HERE, 'fixtures', 'stats_wrong_system_delta.json')) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'stats_wrong_system_delta.json')) else: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) def mocked_get_tags(entity, _): diff --git a/ecs_fargate/tests/test_unit.py b/ecs_fargate/tests/test_unit.py index 813e2a5768d51..6406cfc8ce15d 100644 --- a/ecs_fargate/tests/test_unit.py +++ b/ecs_fargate/tests/test_unit.py @@ -8,7 +8,7 @@ import mock import pytest -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.ecs_fargate import FargateCheck from .conftest import ( @@ -42,7 +42,8 @@ def test_failing_check(check, aggregator, dd_run_check): Testing fargate metadata endpoint error. """ with mock.patch( - 'datadog_checks.ecs_fargate.ecs_fargate.requests.Session.get', return_value=MockResponse('{}', status_code=500) + 'datadog_checks.ecs_fargate.ecs_fargate.requests.Session.get', + return_value=MockHTTPResponse('{}', status_code=500), ): dd_run_check(check) @@ -55,7 +56,8 @@ def test_invalid_response_check(check, aggregator, dd_run_check): Testing invalid fargate metadata payload. """ with mock.patch( - 'datadog_checks.ecs_fargate.ecs_fargate.requests.Session.get', return_value=MockResponse('{}', status_code=200) + 'datadog_checks.ecs_fargate.ecs_fargate.requests.Session.get', + return_value=MockHTTPResponse('{}', status_code=200), ): dd_run_check(check) diff --git a/elastic/tests/test_unit.py b/elastic/tests/test_unit.py index 551133e47a4fd..2e5f2370a0eaf 100644 --- a/elastic/tests/test_unit.py +++ b/elastic/tests/test_unit.py @@ -8,7 +8,7 @@ import pytest from datadog_checks.base import ConfigurationError, is_affirmative -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.elastic import ESCheck from datadog_checks.elastic.elastic import AuthenticationError, get_value_from_path from datadog_checks.elastic.metrics import INDEX_STATS_METRICS, stats_for_version @@ -135,7 +135,7 @@ def test_get_template_metrics(aggregator, instance, mock_http_response): def test_get_template_metrics_raise_exception(aggregator, instance): with mock.patch( 'requests.Session.get', - return_value=MockResponse(status_code=403), + return_value=MockHTTPResponse(status_code=403), ): check = ESCheck('elastic', {}, instances=[instance]) # Make sure we do not throw an exception and move on @@ -152,7 +152,7 @@ def test_get_value_from_path(): def test__get_data_throws_authentication_error(instance): with mock.patch( 'requests.Session.get', - return_value=MockResponse(status_code=400), + return_value=MockHTTPResponse(status_code=400), ): check = ESCheck('elastic', {}, instances=[instance]) @@ -163,7 +163,7 @@ def test__get_data_throws_authentication_error(instance): def test__get_data_creates_critical_service_alert(aggregator, instance): with mock.patch( 'requests.Session.get', - return_value=MockResponse(status_code=500), + return_value=MockHTTPResponse(status_code=500), ): check = ESCheck('elastic', {}, instances=[instance]) @@ -174,7 +174,7 @@ def test__get_data_creates_critical_service_alert(aggregator, instance): check.SERVICE_CHECK_CONNECT_NAME, status=check.CRITICAL, tags=check._config.service_check_tags, - message="Error 500 Server Error: None for url: None when hitting test.com", + message="Error 500 Server Error when hitting test.com", ) @@ -194,7 +194,7 @@ def test__get_data_creates_critical_service_alert(aggregator, instance): def test_disable_legacy_sc_tags(aggregator, es_instance): with mock.patch( 'requests.Session.get', - return_value=MockResponse(status_code=500), + return_value=MockHTTPResponse(status_code=500), ): check = ESCheck('elastic', {}, instances=[es_instance]) @@ -210,7 +210,7 @@ def test_disable_legacy_sc_tags(aggregator, es_instance): check.SERVICE_CHECK_CONNECT_NAME, status=check.CRITICAL, tags=expected_tags, - message="Error 500 Server Error: None for url: None when hitting test.com", + message="Error 500 Server Error when hitting test.com", ) diff --git a/fly_io/tests/conftest.py b/fly_io/tests/conftest.py index 959a905ee23dd..6de661bd9eac3 100644 --- a/fly_io/tests/conftest.py +++ b/fly_io/tests/conftest.py @@ -7,14 +7,13 @@ from pathlib import Path from urllib.parse import urlparse -import mock import pytest -import requests +from datadog_checks.base.utils.http_exceptions import HTTPStatusError +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run from datadog_checks.dev.conditions import CheckDockerLogs, CheckEndpoints from datadog_checks.dev.fs import get_here -from datadog_checks.dev.http import MockResponse from .common import COMPOSE_FILE, INSTANCE, LAB_INSTANCE, USE_FLY_LAB @@ -98,17 +97,13 @@ def call(method, url, file='response', headers=None, params=None): response = mock_responses(method, url, file=file, headers=headers, params=params) if response is not None: return response - http_response = requests.models.Response() - http_response.status_code = 404 - http_response.reason = "Not Found" - http_response.url = url - raise requests.exceptions.HTTPError(response=http_response) + raise HTTPStatusError('404 Client Error', response=MockHTTPResponse(status_code=404, url=url)) yield call @pytest.fixture -def mock_http_get(request, monkeypatch, mock_http_call): +def mock_http_get(request, mock_http, mock_http_call): param = request.param if hasattr(request, 'param') and request.param is not None else {} http_error = param.pop('http_error', {}) @@ -117,15 +112,13 @@ def get(url, *args, **kwargs): url = get_url_path(url) if http_error and url in http_error: return http_error[url] - mock_status_code = mock.MagicMock(return_value=200) if "/metrics" in url: filepath = os.path.join(get_here(), 'fixtures', 'output.txt') - return MockResponse(file_path=filepath) + return MockHTTPResponse(file_path=filepath) headers = kwargs.get('headers') params = kwargs.get('params') - mock_json = mock.MagicMock(return_value=mock_http_call(method, url, headers=headers, params=params)) - return mock.MagicMock(json=mock_json, status_code=mock_status_code) + json_data = mock_http_call(method, url, headers=headers, params=params) + return MockHTTPResponse(json_data=json_data) - mock_get = mock.MagicMock(side_effect=get) - monkeypatch.setattr('requests.Session.get', mock_get) - return mock_get + mock_http.get.side_effect = get + return mock_http.get diff --git a/fly_io/tests/test_unit.py b/fly_io/tests/test_unit.py index 6098d20713619..4917810f7feb2 100644 --- a/fly_io/tests/test_unit.py +++ b/fly_io/tests/test_unit.py @@ -8,7 +8,7 @@ import pytest from datadog_checks.base.constants import ServiceCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.fly_io import FlyIoCheck @@ -94,11 +94,11 @@ def test_rest_api_app_metrics(dd_run_check, aggregator, instance, caplog): ('mock_http_get'), [ pytest.param( - {'http_error': {'/v1/apps': MockResponse(status_code=500)}}, + {'http_error': {'/v1/apps': MockHTTPResponse(status_code=500)}}, id='500', ), pytest.param( - {'http_error': {'/v1/apps': MockResponse(status_code=404)}}, + {'http_error': {'/v1/apps': MockHTTPResponse(status_code=404)}}, id='404', ), ], @@ -107,7 +107,7 @@ def test_rest_api_app_metrics(dd_run_check, aggregator, instance, caplog): @pytest.mark.usefixtures('mock_http_get') def test_rest_api_exception(dd_run_check, instance, aggregator): check = FlyIoCheck('fly_io', {}, [instance]) - with pytest.raises(Exception, match=r'requests.exceptions.HTTPError'): + with pytest.raises(Exception, match=r'HTTPStatusError'): dd_run_check(check) aggregator.assert_metric("fly_io.machines_api.up", value=0) @@ -124,7 +124,9 @@ def test_rest_api_exception(dd_run_check, instance, aggregator): pytest.param( { 'http_error': { - '/v1/apps/example-app-1/machines': MockResponse(json_data=[{'state': 'started', 'config': None}]) + '/v1/apps/example-app-1/machines': MockHTTPResponse( + json_data=[{'state': 'started', 'config': None}] + ) } }, id='malformed response', @@ -156,7 +158,7 @@ def test_bad_response_exception(dd_run_check, instance, aggregator, caplog): ('mock_http_get'), [ pytest.param( - {'http_error': {'/v1/apps/example-app-1/volumes': MockResponse(status_code=404)}}, + {'http_error': {'/v1/apps/example-app-1/volumes': MockHTTPResponse(status_code=404)}}, id='http error', ), ], @@ -169,8 +171,9 @@ def test_http_error_exception(dd_run_check, instance, aggregator, caplog): dd_run_check(check) assert ( - "Encountered a RequestException in '_collect_volumes_for_app' []: " - "404 Client Error: None for url: None" in caplog.text + "Encountered a RequestException in '_collect_volumes_for_app'" + " []: " + "404 Client Error" in caplog.text ) for metric in MOCKED_PROMETHEUS_METRICS: @@ -218,20 +221,28 @@ def test_external_host_tags(instance, datadog_agent, dd_run_check): ('mock_http_get, log_lines'), [ pytest.param( - {'http_error': {'/v1/apps/example-app-2': MockResponse(status_code=404)}}, - ['RequestException in \'_get_app_status\' []: 404'], + {'http_error': {'/v1/apps/example-app-2': MockHTTPResponse(status_code=404)}}, + [ + "RequestException in '_get_app_status'" + " []:" + " 404 Client Error" + ], id='one app', ), pytest.param( { 'http_error': { - '/v1/apps/example-app-1': MockResponse(status_code=404), - '/v1/apps/example-app-2': MockResponse(status_code=500), + '/v1/apps/example-app-1': MockHTTPResponse(status_code=404), + '/v1/apps/example-app-2': MockHTTPResponse(status_code=500), } }, [ - 'RequestException in \'_get_app_status\' []: 404', - 'RequestException in \'_get_app_status\' []: 500', + "RequestException in '_get_app_status'" + " []:" + " 404 Client Error", + "RequestException in '_get_app_status'" + " []:" + " 500 Server Error", ], id='two apps', ), diff --git a/harbor/tests/conftest.py b/harbor/tests/conftest.py index 632ef1b7571e6..f0f83528653ad 100644 --- a/harbor/tests/conftest.py +++ b/harbor/tests/conftest.py @@ -5,11 +5,10 @@ import pytest import requests -from mock import patch +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run from datadog_checks.dev.conditions import CheckDockerLogs, WaitFor -from datadog_checks.dev.http import MockResponse from datadog_checks.harbor import HarborCheck from datadog_checks.harbor.api import HarborAPI from datadog_checks.harbor.common import ( @@ -104,9 +103,10 @@ def harbor_api(harbor_check, admin_instance, patch_requests): @pytest.fixture -def patch_requests(): - with patch("requests.Session.request", side_effect=mocked_requests): - yield +def patch_requests(mock_http): + mock_http.get.side_effect = mocked_requests + mock_http.post.side_effect = mocked_requests + yield def get_docker_compose_file(): @@ -115,7 +115,7 @@ def get_docker_compose_file(): return os.path.join(HERE, 'compose', harbor_folder, 'docker-compose.yml') -def mocked_requests(_, url, **kwargs): +def mocked_requests(url, **kwargs): def match(url, *candidates_url): for c in candidates_url: if url == c.format(base_url=URL): @@ -123,24 +123,24 @@ def match(url, *candidates_url): return False if match(url, LOGIN_URL): - return MockResponse() + return MockHTTPResponse() elif match(url, HEALTH_URL): - return MockResponse(json_data=HEALTH_FIXTURE) + return MockHTTPResponse(json_data=HEALTH_FIXTURE) elif match(url, PING_URL): - return MockResponse('Pong') + return MockHTTPResponse('Pong') elif match(url, CHARTREPO_HEALTH_URL): - return MockResponse(json_data=CHARTREPO_HEALTH_FIXTURE) + return MockHTTPResponse(json_data=CHARTREPO_HEALTH_FIXTURE) elif match(url, PROJECTS_URL): - return MockResponse(json_data=PROJECTS_FIXTURE) + return MockHTTPResponse(json_data=PROJECTS_FIXTURE) elif match(url, REGISTRIES_URL): - return MockResponse(json_data=REGISTRIES_FIXTURE) + return MockHTTPResponse(json_data=REGISTRIES_FIXTURE) elif match(url, REGISTRIES_PING_URL): - return MockResponse() + return MockHTTPResponse() elif match(url, VOLUME_INFO_URL): if HARBOR_VERSION < VERSION_2_2: - return MockResponse(json_data=VOLUME_INFO_PRE_2_2_FIXTURE) - return MockResponse(json_data=VOLUME_INFO_FIXTURE) + return MockHTTPResponse(json_data=VOLUME_INFO_PRE_2_2_FIXTURE) + return MockHTTPResponse(json_data=VOLUME_INFO_FIXTURE) elif match(url, SYSTEM_INFO_URL): - return MockResponse(json_data=SYSTEM_INFO_FIXTURE) + return MockHTTPResponse(json_data=SYSTEM_INFO_FIXTURE) - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) diff --git a/harbor/tests/test_unit.py b/harbor/tests/test_unit.py index 92b588db4fd04..86dbd046db17e 100644 --- a/harbor/tests/test_unit.py +++ b/harbor/tests/test_unit.py @@ -3,10 +3,10 @@ # Licensed under a 3-clause BSD style license (see LICENSE) import pytest from mock import MagicMock -from requests import HTTPError from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_exceptions import HTTPStatusError +from datadog_checks.base.utils.http_testing import MockHTTPResponse from .common import HARBOR_COMPONENTS @@ -53,11 +53,11 @@ def test_submit_read_only_status(aggregator, harbor_check, harbor_api): def test_api__make_get_request(harbor_api): harbor_api.http = MagicMock() - harbor_api.http.get = MagicMock(return_value=MockResponse(json_data={'json': True})) + harbor_api.http.get = MagicMock(return_value=MockHTTPResponse(json_data={'json': True})) assert harbor_api._make_get_request('{base_url}/api/path') == {"json": True} - harbor_api.http.get = MagicMock(return_value=MockResponse(status_code=500)) - with pytest.raises(HTTPError): + harbor_api.http.get = MagicMock(return_value=MockHTTPResponse(status_code=500)) + with pytest.raises(HTTPStatusError): harbor_api._make_get_request('{base_url}/api/path') @@ -66,7 +66,9 @@ def test_api__make_paginated_get_request(harbor_api): paginated_result = [[expected_result[i], expected_result[i + 1]] for i in range(0, len(expected_result) - 1, 2)] values = [] for r in paginated_result: - values.append(MockResponse(json_data=r, headers={'link': 'Link: ; rel=next; type="text/plain"'})) + values.append( + MockHTTPResponse(json_data=r, headers={'link': 'Link: ; rel=next; type="text/plain"'}) + ) values[-1].headers.pop('link') harbor_api.http = MagicMock() @@ -77,9 +79,9 @@ def test_api__make_paginated_get_request(harbor_api): def test_api__make_post_request(harbor_api): harbor_api.http = MagicMock() - harbor_api.http.post = MagicMock(return_value=MockResponse(json_data={'json': True})) + harbor_api.http.post = MagicMock(return_value=MockHTTPResponse(json_data={'json': True})) assert harbor_api._make_post_request('{base_url}/api/path') == {"json": True} - harbor_api.http.post = MagicMock(return_value=MockResponse(status_code=500)) - with pytest.raises(HTTPError): + harbor_api.http.post = MagicMock(return_value=MockHTTPResponse(status_code=500)) + with pytest.raises(HTTPStatusError): harbor_api._make_post_request('{base_url}/api/path') diff --git a/hdfs_datanode/tests/conftest.py b/hdfs_datanode/tests/conftest.py index d8248b94a20a1..8017fb19a6de0 100644 --- a/hdfs_datanode/tests/conftest.py +++ b/hdfs_datanode/tests/conftest.py @@ -6,13 +6,12 @@ from copy import deepcopy import pytest -from mock import patch +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run -from datadog_checks.dev.http import MockResponse from datadog_checks.hdfs_datanode import HDFSDataNode -from .common import FIXTURE_DIR, HERE, INSTANCE_INTEGRATION, TEST_PASSWORD, TEST_USERNAME +from .common import FIXTURE_DIR, HERE, INSTANCE_INTEGRATION @pytest.fixture(scope="session") @@ -36,37 +35,26 @@ def instance(): @pytest.fixture -def mocked_request(): - with patch('requests.Session.get', new=requests_get_mock): - yield +def mocked_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield @pytest.fixture -def mocked_metadata_request(): - with patch('requests.Session.get', new=requests_metadata_mock): - yield +def mocked_metadata_request(mock_http): + mock_http.get.side_effect = requests_metadata_mock + yield @pytest.fixture -def mocked_auth_request(): - with patch('requests.Session.get', new=requests_auth_mock): - yield +def mocked_auth_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield -def requests_get_mock(*args, **kwargs): - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_datanode_jmx.json')) +def requests_get_mock(url, *args, **kwargs): + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_datanode_jmx.json')) -def requests_metadata_mock(*args, **kwargs): - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_datanode_info_jmx.json')) - - -def requests_auth_mock(*args, **kwargs): - # Make sure we're passing in authentication - assert 'auth' in kwargs, "Error, missing authentication" - - # Make sure we've got the correct username and password - assert kwargs['auth'] == (TEST_USERNAME, TEST_PASSWORD), "Incorrect username or password" - - # Return mocked request.get(...) - return requests_get_mock(*args, **kwargs) +def requests_metadata_mock(url, *args, **kwargs): + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_datanode_info_jmx.json')) diff --git a/hdfs_namenode/tests/conftest.py b/hdfs_namenode/tests/conftest.py index f6a0da366dfab..fae3b8bd6f61c 100644 --- a/hdfs_namenode/tests/conftest.py +++ b/hdfs_namenode/tests/conftest.py @@ -4,10 +4,9 @@ import os import pytest -from mock import patch +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run -from datadog_checks.dev.http import MockResponse from datadog_checks.hdfs_namenode import HDFSNameNode from .common import ( @@ -17,8 +16,6 @@ NAME_SYSTEM_METADATA_URL, NAME_SYSTEM_STATE_URL, NAME_SYSTEM_URL, - TEST_PASSWORD, - TEST_USERNAME, ) @@ -43,32 +40,21 @@ def check(): @pytest.fixture -def mocked_request(): - with patch("requests.Session.get", new=requests_get_mock): - yield +def mocked_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield @pytest.fixture -def mocked_auth_request(): - with patch("requests.Session.get", new=requests_auth_mock): - yield +def mocked_auth_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield -def requests_get_mock(session, url, *args, **kwargs): +def requests_get_mock(url, *args, **kwargs): if url == NAME_SYSTEM_STATE_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem_state.json')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem_state.json')) elif url == NAME_SYSTEM_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem.json')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem.json')) elif url == NAME_SYSTEM_METADATA_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem_info.json')) - - -def requests_auth_mock(*args, **kwargs): - # Make sure we're passing in authentication - assert 'auth' in kwargs, "Error, missing authentication" - - # Make sure we've got the correct username and password - assert kwargs['auth'] == (TEST_USERNAME, TEST_PASSWORD), "Incorrect username or password" - - # Return mocked request.get(...) - return requests_get_mock(*args, **kwargs) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'hdfs_namesystem_info.json')) diff --git a/kubelet/tests/test_kubelet.py b/kubelet/tests/test_kubelet.py index 6ffe23ceaaeb7..459de309f5199 100644 --- a/kubelet/tests/test_kubelet.py +++ b/kubelet/tests/test_kubelet.py @@ -15,7 +15,7 @@ from datadog_checks.base.checks.kubelet_base.base import KubeletCredentials from datadog_checks.base.errors import SkipInstanceError from datadog_checks.base.utils.date import parse_rfc3339 -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.kubelet import KubeletCheck, PodListUtils # Skip the whole tests module on Windows @@ -857,7 +857,7 @@ def test_report_container_state_metrics(monkeypatch, tagger): monkeypatch.setattr( check, 'perform_kubelet_query', - mock.Mock(return_value=MockResponse(file_path=os.path.join(HERE, 'fixtures', 'pods_crashed.json'))), + mock.Mock(return_value=MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'pods_crashed.json'))), ) monkeypatch.setattr(check, 'compute_pod_expiration_datetime', mock.Mock(return_value=None)) monkeypatch.setattr(check, 'gauge', mock.Mock()) @@ -1010,7 +1010,7 @@ def test_pod_expiration(monkeypatch, aggregator, tagger): monkeypatch.setattr( check, 'perform_kubelet_query', - mock.Mock(return_value=MockResponse(file_path=os.path.join(HERE, 'fixtures', 'pods_expired.json'))), + mock.Mock(return_value=MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'pods_expired.json'))), ) monkeypatch.setattr( check, 'compute_pod_expiration_datetime', mock.Mock(return_value=parse_rfc3339("2019-02-18T16:00:06Z")) diff --git a/mapreduce/tests/conftest.py b/mapreduce/tests/conftest.py index dea2ab20b4951..754b33b47d2a1 100644 --- a/mapreduce/tests/conftest.py +++ b/mapreduce/tests/conftest.py @@ -5,11 +5,10 @@ from copy import deepcopy import pytest -from mock import patch +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run from datadog_checks.dev.conditions import WaitFor -from datadog_checks.dev.http import MockResponse from datadog_checks.mapreduce import MapReduceCheck from .common import ( @@ -21,8 +20,6 @@ MR_JOB_COUNTERS_URL, MR_JOBS_URL, MR_TASKS_URL, - TEST_PASSWORD, - TEST_USERNAME, YARN_APPS_URL_BASE, setup_mapreduce, ) @@ -51,55 +48,41 @@ def instance(): @pytest.fixture -def mocked_request(): - with patch("requests.Session.get", new=requests_get_mock): - yield +def mocked_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield @pytest.fixture -def mocked_auth_request(): - with patch("requests.Session.get", new=requests_auth_mock): - yield +def mocked_auth_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield def get_custom_hosts(): return [(host, '127.0.0.1') for host in MOCKED_E2E_HOSTS] -def requests_get_mock(session, *args, **kwargs): - url = args[0] - # The parameter that creates the query params (kwargs) is an unordered dict, - # so the query params can be in any order +def requests_get_mock(url, *args, **kwargs): if url.startswith(YARN_APPS_URL_BASE): query = url[len(YARN_APPS_URL_BASE) :] if query in ["?states=RUNNING&applicationTypes=MAPREDUCE", "?applicationTypes=MAPREDUCE&states=RUNNING"]: - return MockResponse(file_path=os.path.join(HERE, "fixtures", "apps_metrics")) + return MockHTTPResponse(file_path=os.path.join(HERE, "fixtures", "apps_metrics")) else: raise Exception( "Apps URL must have the two query parameters: states=RUNNING and applicationTypes=MAPREDUCE" ) if url == MR_JOBS_URL: - return MockResponse(file_path=os.path.join(HERE, "fixtures", "job_metrics")) + return MockHTTPResponse(file_path=os.path.join(HERE, "fixtures", "job_metrics")) if url == MR_JOB_COUNTERS_URL: - return MockResponse(file_path=os.path.join(HERE, "fixtures", "job_counter_metrics")) + return MockHTTPResponse(file_path=os.path.join(HERE, "fixtures", "job_counter_metrics")) if url == MR_TASKS_URL: - return MockResponse(file_path=os.path.join(HERE, "fixtures", "task_metrics")) + return MockHTTPResponse(file_path=os.path.join(HERE, "fixtures", "task_metrics")) if url == CLUSTER_INFO_URL: - return MockResponse(file_path=os.path.join(HERE, "fixtures", "cluster_info")) + return MockHTTPResponse(file_path=os.path.join(HERE, "fixtures", "cluster_info")) raise Exception("There is no mock request for {}".format(url)) - - -def requests_auth_mock(session, *args, **kwargs): - # Make sure we're passing in authentication - assert 'auth' in kwargs, "Error, missing authentication" - - # Make sure we've got the correct username and password - assert kwargs['auth'] == (TEST_USERNAME, TEST_PASSWORD), "Incorrect username or password" - - # Return mocked request.get(...) - return requests_get_mock(session, *args, **kwargs) diff --git a/nginx/tests/common.py b/nginx/tests/common.py index 023d7d1c81b08..bcb25ffd0b747 100644 --- a/nginx/tests/common.py +++ b/nginx/tests/common.py @@ -3,8 +3,8 @@ # Licensed under a 3-clause BSD style license (see LICENSE) import os +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import get_docker_hostname -from datadog_checks.dev.http import MockResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.nginx.metrics import COUNT_METRICS, METRICS_SEND_AS_COUNT, METRICS_SEND_AS_HISTOGRAM @@ -115,4 +115,4 @@ def mock_http_responses(url, **_params): raise Exception("url `{url}` not registered".format(url=url)) with open(os.path.join(HERE, 'fixtures', metrics_file)) as f: - return MockResponse(content=f.read(), headers={"content-type": "application/json"}) + return MockHTTPResponse(content=f.read(), headers={"content-type": "application/json"}) diff --git a/nvidia_nim/tests/test_unit.py b/nvidia_nim/tests/test_unit.py index c6ea3eb977340..d593cc99a9a1f 100644 --- a/nvidia_nim/tests/test_unit.py +++ b/nvidia_nim/tests/test_unit.py @@ -7,7 +7,7 @@ import pytest from datadog_checks.base.constants import ServiceCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.nvidia_nim import NvidiaNIMCheck @@ -20,8 +20,8 @@ def test_check_nvidia_nim(dd_run_check, aggregator, datadog_agent, instance): with mock.patch( 'requests.Session.get', side_effect=[ - MockResponse(file_path=get_fixture_path("nim_metrics.txt")), - MockResponse(file_path=get_fixture_path("nim_version.json")), + MockHTTPResponse(file_path=get_fixture_path("nim_metrics.txt")), + MockHTTPResponse(file_path=get_fixture_path("nim_version.json")), ], ): dd_run_check(check) diff --git a/octopus_deploy/tests/conftest.py b/octopus_deploy/tests/conftest.py index 7ec3897c84a13..acb3c2c45d7db 100644 --- a/octopus_deploy/tests/conftest.py +++ b/octopus_deploy/tests/conftest.py @@ -5,16 +5,16 @@ import json import os from pathlib import Path +from unittest.mock import MagicMock from urllib.parse import urlparse -import mock import pytest -import requests +from datadog_checks.base.utils.http_exceptions import HTTPStatusError +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run from datadog_checks.dev.conditions import CheckDockerLogs, CheckEndpoints from datadog_checks.dev.fs import get_here -from datadog_checks.dev.http import MockResponse from .constants import COMPOSE_FILE, INSTANCE, LAB_INSTANCE, USE_OCTOPUS_LAB @@ -88,8 +88,9 @@ } -# https://docs.python.org/3/library/unittest.mock-examples.html#coping-with-mutable-arguments -class CopyingMock(mock.MagicMock): +class _CopyingMock(MagicMock): + """Deep-copy args at record time so mutable params aren't modified after the call.""" + def __call__(self, /, *args, **kwargs): args = copy.deepcopy(args) kwargs = copy.deepcopy(kwargs) @@ -181,38 +182,31 @@ def call(method, url, file='response', headers=None, params=None): response = mock_responses(method, url, file=file, headers=headers, params=params) if response is not None: return response - http_response = requests.models.Response() - http_response.status_code = 404 - http_response.reason = "Not Found" - http_response.url = url - raise requests.exceptions.HTTPError(response=http_response) + raise HTTPStatusError('404 Client Error', response=MockHTTPResponse(status_code=404, url=url)) yield call @pytest.fixture -def mock_http_get(request, monkeypatch, mock_http_call): +def mock_http_get(request, mock_http, mock_http_call): param = request.param if hasattr(request, 'param') and request.param is not None else {} http_error = param.pop('http_error', {}) data = param.pop('mock_data', {}) elapsed_total_seconds = param.pop('elapsed_total_seconds', {}) def get(url, *args, **kwargs): - args = copy.deepcopy(args) kwargs = copy.deepcopy(kwargs) method = 'GET' url = get_url_path(url) if http_error and url in http_error: return http_error[url] if data and url in data: - return MockResponse(json_data=data[url], status_code=200) + return MockHTTPResponse(json_data=data[url]) headers = kwargs.get('headers') params = kwargs.get('params') - mock_elapsed = mock.MagicMock(total_seconds=mock.MagicMock(return_value=elapsed_total_seconds.get(url, 0.0))) - mock_json = mock.MagicMock(return_value=mock_http_call(method, url, headers=headers, params=params)) - mock_status_code = mock.MagicMock(return_value=200) - return CopyingMock(elapsed=mock_elapsed, json=mock_json, status_code=mock_status_code) - - mock_get = CopyingMock(side_effect=get) - monkeypatch.setattr('requests.Session.get', mock_get) - return mock_get + json_data = mock_http_call(method, url, headers=headers, params=params) + return MockHTTPResponse(json_data=json_data, elapsed_seconds=elapsed_total_seconds.get(url, 0.0)) + + copying_mock = _CopyingMock(side_effect=get) + mock_http.get = copying_mock + return copying_mock diff --git a/octopus_deploy/tests/test_unit.py b/octopus_deploy/tests/test_unit.py index 5cb5cd76c3002..e93af9c65c74b 100644 --- a/octopus_deploy/tests/test_unit.py +++ b/octopus_deploy/tests/test_unit.py @@ -9,7 +9,7 @@ import mock import pytest -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.octopus_deploy import OctopusDeployCheck @@ -33,7 +33,7 @@ pytest.param( { 'http_error': { - '/api/spaces': MockResponse(status_code=500), + '/api/spaces': MockHTTPResponse(status_code=500), } }, pytest.raises(Exception, match=r'Could not connect to octopus API.*'), @@ -1038,10 +1038,10 @@ def test_empty_include(get_current_datetime, dd_run_check, aggregator): pytest.param( { 'http_error': { - '/api/Spaces-1/tasks': MockResponse(status_code=500), + '/api/Spaces-1/tasks': MockHTTPResponse(status_code=500), } }, - 'Failed to access endpoint: api/Spaces-1/tasks: 500 Server Error: None for url: None', + 'Failed to access endpoint: api/Spaces-1/tasks: 500 Server Error', id='http error', ), ], @@ -1101,10 +1101,10 @@ def test_server_node_metrics(get_current_datetime, dd_run_check, aggregator, ins pytest.param( { 'http_error': { - '/api/octopusservernodes': MockResponse(status_code=500), + '/api/octopusservernodes': MockHTTPResponse(status_code=500), } }, - 'Failed to access endpoint: api/octopusservernodes: 500 Server Error: None for url: None', + 'Failed to access endpoint: api/octopusservernodes: 500 Server Error', id='http error', ), ], @@ -1545,10 +1545,10 @@ def test_environments_discovery_include_invalid(get_current_datetime, dd_run_che pytest.param( { 'http_error': { - '/api/Spaces-1/environments': MockResponse(status_code=500), + '/api/Spaces-1/environments': MockHTTPResponse(status_code=500), } }, - 'Failed to access endpoint: api/Spaces-1/environments: 500 Server Error: None for url: None', + 'Failed to access endpoint: api/Spaces-1/environments: 500 Server Error', id='http error', ), ], @@ -1651,10 +1651,10 @@ def test_environments_metrics_http_failure( pytest.param( { 'http_error': { - '/api/Spaces-1/releases/Releases-3': MockResponse(status_code=500), + '/api/Spaces-1/releases/Releases-3': MockHTTPResponse(status_code=500), } }, - 'Failed to access endpoint: api/Spaces-1/releases/Releases-3: 500 Server Error: None for url: None', + 'Failed to access endpoint: api/Spaces-1/releases/Releases-3: 500 Server Error', id='http error', ), ], @@ -1879,10 +1879,10 @@ def test_deployment_metrics_releases_http_failure( pytest.param( { 'http_error': { - '/api/Spaces-1/deployments/Deployments-18': MockResponse(status_code=500), + '/api/Spaces-1/deployments/Deployments-18': MockHTTPResponse(status_code=500), } }, - 'Failed to access endpoint: api/Spaces-1/deployments/Deployments-18: 500 Server Error: None for url: None', + 'Failed to access endpoint: api/Spaces-1/deployments/Deployments-18: 500 Server Error', id='http error', ), ], @@ -2111,10 +2111,10 @@ def test_deployment_metrics_deployments_http_failure( pytest.param( { 'http_error': { - '/api/Spaces-1/environments': MockResponse(status_code=500), + '/api/Spaces-1/environments': MockHTTPResponse(status_code=500), } }, - 'Failed to access endpoint: api/Spaces-1/environments: 500 Server Error: None for url: None', + 'Failed to access endpoint: api/Spaces-1/environments: 500 Server Error', id='http error', ), ], diff --git a/openstack_controller/tests/conftest.py b/openstack_controller/tests/conftest.py index e6b933bb66871..f4ee07975972d 100644 --- a/openstack_controller/tests/conftest.py +++ b/openstack_controller/tests/conftest.py @@ -14,10 +14,10 @@ import yaml import tests.configs as configs +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run from datadog_checks.dev.conditions import CheckDockerLogs from datadog_checks.dev.fs import get_here -from datadog_checks.dev.http import MockResponse from datadog_checks.openstack_controller import OpenStackControllerCheck from .endpoints import IRONIC_ENDPOINTS, NOVA_ENDPOINTS @@ -1000,7 +1000,7 @@ def get(url, *args, **kwargs): if http_error and url in http_error: return http_error[url] if data and url in data: - return MockResponse(json_data=data[url], status_code=200) + return MockHTTPResponse(json_data=data[url], status_code=200) headers = kwargs.get('headers') params = kwargs.get('params') mock_elapsed = mock.MagicMock(total_seconds=mock.MagicMock(return_value=elapsed_total_seconds.get(url, 0.0))) @@ -1024,6 +1024,7 @@ def post(url, *args, **kwargs): url = get_url_path(url) if http_error and url in http_error: return http_error[url] + headers = None if url == '/identity/v3/auth/tokens': data = kwargs['json'] file = data.get('auth', {}).get('scope', 'unscoped') @@ -1038,7 +1039,7 @@ def post(url, *args, **kwargs): json_data = mock_http_call(method, url) if replace and url in replace: json_data = replace[url](json_data) - return MockResponse(json_data=json_data, status_code=200, headers=headers) + return MockHTTPResponse(json_data=json_data, status_code=200, headers=headers) mock_post = mock.MagicMock(side_effect=post) monkeypatch.setattr('requests.Session.post', mock_post) diff --git a/openstack_controller/tests/test_unit_auth.py b/openstack_controller/tests/test_unit_auth.py index 24b8e74fe9492..9725dc2c06060 100644 --- a/openstack_controller/tests/test_unit_auth.py +++ b/openstack_controller/tests/test_unit_auth.py @@ -8,7 +8,7 @@ import pytest import tests.configs as configs -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse pytestmark = [ pytest.mark.unit, @@ -20,14 +20,14 @@ ('mock_http_post', 'connection_authorize', 'instance'), [ pytest.param( - {'http_error': {'/identity/v3/auth/tokens': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/auth/tokens': MockHTTPResponse(status_code=500)}}, None, configs.REST, id='api rest', ), pytest.param( None, - {'http_error': MockResponse(status_code=500)}, + {'http_error': MockHTTPResponse(status_code=500)}, configs.SDK, id='api sdk', ), diff --git a/openstack_controller/tests/test_unit_cinder.py b/openstack_controller/tests/test_unit_cinder.py index 994d2f629acc2..be4c728926220 100644 --- a/openstack_controller/tests/test_unit_cinder.py +++ b/openstack_controller/tests/test_unit_cinder.py @@ -10,7 +10,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog @@ -148,12 +148,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/volume/v3/': MockResponse(status_code=500)}}, + {'http_error': {'/volume/v3/': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/volume/v3/': MockResponse(status_code=500)}}, + {'http_error': {'/volume/v3/': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), diff --git a/openstack_controller/tests/test_unit_glance.py b/openstack_controller/tests/test_unit_glance.py index 20b6235afce3e..6157af5b0c2de 100644 --- a/openstack_controller/tests/test_unit_glance.py +++ b/openstack_controller/tests/test_unit_glance.py @@ -11,7 +11,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog from tests.metrics import ( @@ -133,12 +133,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/image': MockResponse(status_code=500)}}, + {'http_error': {'/image': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/image': MockResponse(status_code=500)}}, + {'http_error': {'/image': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -214,7 +214,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ('mock_http_get', 'connection_image', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/image/v2/images': MockResponse(status_code=500)}}, + {'http_error': {'/image/v2/images': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -222,7 +222,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ), pytest.param( None, - {'http_error': {'images': MockResponse(status_code=500)}}, + {'http_error': {'images': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', diff --git a/openstack_controller/tests/test_unit_heat.py b/openstack_controller/tests/test_unit_heat.py index cef6d072d5d72..2516cfd80d7fd 100644 --- a/openstack_controller/tests/test_unit_heat.py +++ b/openstack_controller/tests/test_unit_heat.py @@ -11,7 +11,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog from tests.metrics import ( @@ -104,12 +104,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/heat-api': MockResponse(status_code=500)}}, + {'http_error': {'/heat-api': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/heat-api': MockResponse(status_code=500)}}, + {'http_error': {'/heat-api': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -187,8 +187,8 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): pytest.param( { 'http_error': { - '/heat-api/v1/1e6e233e637d4d55a50a62b63398ad15/stacks': MockResponse(status_code=500), - '/heat-api/v1/6e39099cccde4f809b003d9e0dd09304/stacks': MockResponse(status_code=500), + '/heat-api/v1/1e6e233e637d4d55a50a62b63398ad15/stacks': MockHTTPResponse(status_code=500), + '/heat-api/v1/6e39099cccde4f809b003d9e0dd09304/stacks': MockHTTPResponse(status_code=500), } }, None, @@ -201,8 +201,8 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): { 'http_error': { 'stacks': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, diff --git a/openstack_controller/tests/test_unit_ironic.py b/openstack_controller/tests/test_unit_ironic.py index 013ce3230658a..34340fe5cb4c1 100644 --- a/openstack_controller/tests/test_unit_ironic.py +++ b/openstack_controller/tests/test_unit_ironic.py @@ -11,7 +11,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog from tests.metrics import ( @@ -677,12 +677,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/baremetal': MockResponse(status_code=500)}}, + {'http_error': {'/baremetal': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/baremetal': MockResponse(status_code=500)}}, + {'http_error': {'/baremetal': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -758,7 +758,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ('mock_http_get', 'connection_baremetal', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/baremetal/v1/nodes/detail': MockResponse(status_code=500)}}, + {'http_error': {'/baremetal/v1/nodes/detail': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -766,7 +766,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ), pytest.param( None, - {'http_error': {'nodes': MockResponse(status_code=500)}}, + {'http_error': {'nodes': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1220,7 +1220,7 @@ def test_pagination_invalid_no_exception(aggregator, openstack_controller_check, ('mock_http_get', 'connection_baremetal', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/baremetal/v1/conductors': MockResponse(status_code=500)}}, + {'http_error': {'/baremetal/v1/conductors': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1228,7 +1228,7 @@ def test_pagination_invalid_no_exception(aggregator, openstack_controller_check, ), pytest.param( None, - {'http_error': {'conductors': MockResponse(status_code=500)}}, + {'http_error': {'conductors': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', diff --git a/openstack_controller/tests/test_unit_keystone.py b/openstack_controller/tests/test_unit_keystone.py index bc6eab0ca4cb3..90978efec1bda 100644 --- a/openstack_controller/tests/test_unit_keystone.py +++ b/openstack_controller/tests/test_unit_keystone.py @@ -11,7 +11,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog @@ -409,12 +409,12 @@ def test_region_id_in_tags(aggregator, dd_run_check, instance, openstack_control ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/identity': MockResponse(status_code=500)}}, + {'http_error': {'/identity': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/identity': MockResponse(status_code=500)}}, + {'http_error': {'/identity': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -500,7 +500,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/regions': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/regions': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -508,7 +508,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ), pytest.param( None, - {'http_error': {'regions': MockResponse(status_code=500)}}, + {'http_error': {'regions': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -575,7 +575,7 @@ def test_regions_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/domains': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/domains': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -583,7 +583,7 @@ def test_regions_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'domains': MockResponse(status_code=500)}}, + {'http_error': {'domains': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -670,7 +670,7 @@ def test_domains_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/projects': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/projects': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -678,7 +678,7 @@ def test_domains_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'projects': MockResponse(status_code=500)}}, + {'http_error': {'projects': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -829,7 +829,7 @@ def test_projects_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/users': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/users': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -837,7 +837,7 @@ def test_projects_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'users': MockResponse(status_code=500)}}, + {'http_error': {'users': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1024,7 +1024,7 @@ def test_users_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/groups': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/groups': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1032,7 +1032,7 @@ def test_users_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'groups': MockResponse(status_code=500)}}, + {'http_error': {'groups': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1063,7 +1063,7 @@ def test_groups_exception(aggregator, check, dd_run_check, mock_http_get, connec pytest.param( { 'http_error': { - '/identity/v3/groups/89b36a4c32c44b0ea8856b6357f101ea/users': MockResponse(status_code=500) + '/identity/v3/groups/89b36a4c32c44b0ea8856b6357f101ea/users': MockHTTPResponse(status_code=500) } }, None, @@ -1073,7 +1073,7 @@ def test_groups_exception(aggregator, check, dd_run_check, mock_http_get, connec ), pytest.param( None, - {'http_error': {'group_users': {'89b36a4c32c44b0ea8856b6357f101ea': MockResponse(status_code=500)}}}, + {'http_error': {'group_users': {'89b36a4c32c44b0ea8856b6357f101ea': MockHTTPResponse(status_code=500)}}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1199,7 +1199,7 @@ def test_groups_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/services': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/services': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1207,7 +1207,7 @@ def test_groups_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'services': MockResponse(status_code=500)}}, + {'http_error': {'services': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1344,7 +1344,7 @@ def test_services_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/registered_limits': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/registered_limits': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1352,7 +1352,7 @@ def test_services_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'registered_limits': MockResponse(status_code=500)}}, + {'http_error': {'registered_limits': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1388,7 +1388,7 @@ def test_registered_limits_exception(aggregator, check, dd_run_check, mock_http_ ('mock_http_get', 'connection_identity', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/identity/v3/limits': MockResponse(status_code=500)}}, + {'http_error': {'/identity/v3/limits': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1396,7 +1396,7 @@ def test_registered_limits_exception(aggregator, check, dd_run_check, mock_http_ ), pytest.param( None, - {'http_error': {'limits': MockResponse(status_code=500)}}, + {'http_error': {'limits': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', diff --git a/openstack_controller/tests/test_unit_neutron.py b/openstack_controller/tests/test_unit_neutron.py index 21713a956edc6..75afdb10d2340 100644 --- a/openstack_controller/tests/test_unit_neutron.py +++ b/openstack_controller/tests/test_unit_neutron.py @@ -11,7 +11,7 @@ import tests.configs as configs import tests.metrics as metrics from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog @@ -181,12 +181,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/networking': MockResponse(status_code=500)}}, + {'http_error': {'/networking': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/networking': MockResponse(status_code=500)}}, + {'http_error': {'/networking': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -262,7 +262,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ('mock_http_get', 'connection_network', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/networking/v2.0/agents': MockResponse(status_code=500)}}, + {'http_error': {'/networking/v2.0/agents': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -270,7 +270,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): ), pytest.param( None, - {'http_error': {'agents': MockResponse(status_code=500)}}, + {'http_error': {'agents': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -488,7 +488,7 @@ def test_disable_quotas_collect_for_all_projects(aggregator, dd_run_check, insta pytest.param( { 'http_error': { - '/networking/v2.0/networks': MockResponse(status_code=500), + '/networking/v2.0/networks': MockHTTPResponse(status_code=500), } }, None, @@ -501,8 +501,8 @@ def test_disable_quotas_collect_for_all_projects(aggregator, dd_run_check, insta { 'http_error': { 'networks': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -1348,8 +1348,8 @@ def test_networks_pagination( pytest.param( { 'http_error': { - '/networking/v2.0/quotas/1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '/networking/v2.0/quotas/6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '/networking/v2.0/quotas/1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '/networking/v2.0/quotas/6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } }, None, @@ -1362,8 +1362,8 @@ def test_networks_pagination( { 'http_error': { 'quotas': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, diff --git a/openstack_controller/tests/test_unit_nova.py b/openstack_controller/tests/test_unit_nova.py index ab81ef4cf237a..abd1b3c5d211a 100644 --- a/openstack_controller/tests/test_unit_nova.py +++ b/openstack_controller/tests/test_unit_nova.py @@ -15,7 +15,7 @@ import tests.configs as configs import tests.metrics as metrics from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog @@ -441,12 +441,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/compute/v2.1': MockResponse(status_code=500)}}, + {'http_error': {'/compute/v2.1': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/compute/v2.1': MockResponse(status_code=500)}}, + {'http_error': {'/compute/v2.1': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -524,7 +524,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): pytest.param( { 'http_error': { - '/compute/v2.1/limits': MockResponse(status_code=500), + '/compute/v2.1/limits': MockHTTPResponse(status_code=500), } }, None, @@ -537,8 +537,8 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): { 'http_error': { 'limits': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -842,7 +842,7 @@ def test_limits_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_compute', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/compute/v2.1/os-services': MockResponse(status_code=500)}}, + {'http_error': {'/compute/v2.1/os-services': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -850,7 +850,7 @@ def test_limits_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'services': MockResponse(status_code=500)}}, + {'http_error': {'services': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -916,7 +916,7 @@ def test_services_metrics(aggregator, check, dd_run_check, metrics): ('mock_http_get', 'connection_compute', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/compute/v2.1/flavors/detail': MockResponse(status_code=500)}}, + {'http_error': {'/compute/v2.1/flavors/detail': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -924,7 +924,7 @@ def test_services_metrics(aggregator, check, dd_run_check, metrics): ), pytest.param( None, - {'http_error': {'flavors': MockResponse(status_code=500)}}, + {'http_error': {'flavors': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1195,7 +1195,7 @@ def test_flavors_metrics(aggregator, check, dd_run_check): ('mock_http_get', 'connection_compute', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/compute/v2.1/os-hypervisors/detail': MockResponse(status_code=500)}}, + {'http_error': {'/compute/v2.1/os-hypervisors/detail': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1203,7 +1203,7 @@ def test_flavors_metrics(aggregator, check, dd_run_check): ), pytest.param( None, - {'http_error': {'hypervisors': MockResponse(status_code=500)}}, + {'http_error': {'hypervisors': MockHTTPResponse(status_code=500)}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1232,7 +1232,7 @@ def test_hypervisors_exception(aggregator, check, dd_run_check, mock_http_get, c ('mock_http_get', 'connection_compute', 'instance', 'api_type'), [ pytest.param( - {'http_error': {'/compute/v2.1/os-hypervisors/1/uptime': MockResponse(status_code=500)}}, + {'http_error': {'/compute/v2.1/os-hypervisors/1/uptime': MockHTTPResponse(status_code=500)}}, None, configs.REST, ApiType.REST, @@ -1240,7 +1240,7 @@ def test_hypervisors_exception(aggregator, check, dd_run_check, mock_http_get, c ), pytest.param( None, - {'http_error': {'hypervisor_uptime': {1: MockResponse(status_code=500)}}}, + {'http_error': {'hypervisor_uptime': {1: MockHTTPResponse(status_code=500)}}}, configs.SDK, ApiType.SDK, id='api sdk', @@ -1566,8 +1566,8 @@ def test_disable_diagnostics_collect_for_all_servers(aggregator, dd_run_check, i pytest.param( { 'http_error': { - '/compute/v2.1/os-quota-sets/1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '/compute/v2.1/os-quota-sets/6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '/compute/v2.1/os-quota-sets/1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '/compute/v2.1/os-quota-sets/6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } }, None, @@ -1580,8 +1580,8 @@ def test_disable_diagnostics_collect_for_all_servers(aggregator, dd_run_check, i { 'http_error': { 'quota_sets': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -1692,7 +1692,7 @@ def test_quota_sets_metrics_excluding_demo_project(aggregator, check, dd_run_che pytest.param( { 'http_error': { - '/compute/v2.1/servers/detail': MockResponse(status_code=500), + '/compute/v2.1/servers/detail': MockHTTPResponse(status_code=500), } }, None, @@ -1705,8 +1705,8 @@ def test_quota_sets_metrics_excluding_demo_project(aggregator, check, dd_run_che { 'http_error': { 'servers': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -2143,7 +2143,7 @@ def test_servers_metrics_excluding_dev_servers(aggregator, check, dd_run_check, pytest.param( { 'http_error': { - '/compute/v2.1/flavors/c1': MockResponse(status_code=500), + '/compute/v2.1/flavors/c1': MockHTTPResponse(status_code=500), } }, None, @@ -2156,7 +2156,7 @@ def test_servers_metrics_excluding_dev_servers(aggregator, check, dd_run_check, pytest.param( { 'http_error': { - '/compute/v2.1/flavors/c1': MockResponse(status_code=500), + '/compute/v2.1/flavors/c1': MockHTTPResponse(status_code=500), } }, None, @@ -2171,7 +2171,7 @@ def test_servers_metrics_excluding_dev_servers(aggregator, check, dd_run_check, { 'http_error': { 'flavors': { - 'c1': MockResponse(status_code=500), + 'c1': MockHTTPResponse(status_code=500), } } }, @@ -2186,7 +2186,7 @@ def test_servers_metrics_excluding_dev_servers(aggregator, check, dd_run_check, { 'http_error': { 'flavors': { - 'c1': MockResponse(status_code=500), + 'c1': MockHTTPResponse(status_code=500), } } }, @@ -2307,7 +2307,7 @@ def test_server_disable_flavors( pytest.param( { 'http_error': { - '/compute/v2.1/servers/5102fbbf-7156-48dc-8355-af7ab992266f/diagnostics': MockResponse( + '/compute/v2.1/servers/5102fbbf-7156-48dc-8355-af7ab992266f/diagnostics': MockHTTPResponse( status_code=500 ), } @@ -2322,7 +2322,7 @@ def test_server_disable_flavors( pytest.param( { 'http_error': { - '/compute/v2.1/servers/5102fbbf-7156-48dc-8355-af7ab992266f/diagnostics': MockResponse( + '/compute/v2.1/servers/5102fbbf-7156-48dc-8355-af7ab992266f/diagnostics': MockHTTPResponse( status_code=500 ), } @@ -2339,7 +2339,7 @@ def test_server_disable_flavors( { 'http_error': { 'server_diagnostics': { - '5102fbbf-7156-48dc-8355-af7ab992266f': MockResponse(status_code=500), + '5102fbbf-7156-48dc-8355-af7ab992266f': MockHTTPResponse(status_code=500), } } }, @@ -2354,7 +2354,7 @@ def test_server_disable_flavors( { 'http_error': { 'server_diagnostics': { - '5102fbbf-7156-48dc-8355-af7ab992266f': MockResponse(status_code=500), + '5102fbbf-7156-48dc-8355-af7ab992266f': MockHTTPResponse(status_code=500), } } }, diff --git a/openstack_controller/tests/test_unit_octavia.py b/openstack_controller/tests/test_unit_octavia.py index a685e261fc3a6..f369f34e84080 100644 --- a/openstack_controller/tests/test_unit_octavia.py +++ b/openstack_controller/tests/test_unit_octavia.py @@ -10,7 +10,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog @@ -496,12 +496,12 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, ('mock_http_get', 'instance'), [ pytest.param( - {'http_error': {'/load-balancer': MockResponse(status_code=500)}}, + {'http_error': {'/load-balancer': MockHTTPResponse(status_code=500)}}, configs.REST, id='api rest', ), pytest.param( - {'http_error': {'/load-balancer': MockResponse(status_code=500)}}, + {'http_error': {'/load-balancer': MockHTTPResponse(status_code=500)}}, configs.SDK, id='api sdk', ), @@ -579,7 +579,7 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): pytest.param( { 'http_error': { - '/load-balancer/v2/lbaas/loadbalancers': MockResponse(status_code=500), + '/load-balancer/v2/lbaas/loadbalancers': MockHTTPResponse(status_code=500), } }, None, @@ -592,8 +592,8 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): { 'http_error': { 'load_balancers': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -859,7 +859,7 @@ def test_loadbalancers_pagination( pytest.param( { 'http_error': { - '/load-balancer/v2/lbaas/listeners': MockResponse(status_code=500), + '/load-balancer/v2/lbaas/listeners': MockHTTPResponse(status_code=500), } }, None, @@ -872,8 +872,8 @@ def test_loadbalancers_pagination( { 'http_error': { 'listeners': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -1523,7 +1523,7 @@ def test_listeners_pagination( pytest.param( { 'http_error': { - '/load-balancer/v2/lbaas/pools': MockResponse(status_code=500), + '/load-balancer/v2/lbaas/pools': MockHTTPResponse(status_code=500), } }, None, @@ -1536,8 +1536,8 @@ def test_listeners_pagination( { 'http_error': { 'pools': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -1707,7 +1707,7 @@ def test_pools_pagination( pytest.param( { 'http_error': { - '/load-balancer/v2/lbaas/pools/d0335b34-3115-4b3b-9a1a-7e2363ebfee3/members': MockResponse( + '/load-balancer/v2/lbaas/pools/d0335b34-3115-4b3b-9a1a-7e2363ebfee3/members': MockHTTPResponse( status_code=500 ), } @@ -1722,7 +1722,7 @@ def test_pools_pagination( { 'http_error': { 'pool_members': { - 'd0335b34-3115-4b3b-9a1a-7e2363ebfee3': MockResponse(status_code=500), + 'd0335b34-3115-4b3b-9a1a-7e2363ebfee3': MockHTTPResponse(status_code=500), } } }, @@ -1882,7 +1882,7 @@ def test_pool_members_metrics(aggregator, check, dd_run_check): pytest.param( { 'http_error': { - '/load-balancer/v2/lbaas/healthmonitors': MockResponse(status_code=500), + '/load-balancer/v2/lbaas/healthmonitors': MockHTTPResponse(status_code=500), } }, None, @@ -1895,8 +1895,8 @@ def test_pool_members_metrics(aggregator, check, dd_run_check): { 'http_error': { 'health_monitors': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -2052,7 +2052,7 @@ def test_healthmonitors_metrics(aggregator, check, dd_run_check): pytest.param( { 'http_error': { - '/load-balancer/v2/lbaas/quotas': MockResponse(status_code=500), + '/load-balancer/v2/lbaas/quotas': MockHTTPResponse(status_code=500), } }, None, @@ -2065,8 +2065,8 @@ def test_healthmonitors_metrics(aggregator, check, dd_run_check): { 'http_error': { 'quotas': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, @@ -2343,7 +2343,7 @@ def test_quotas_metrics(aggregator, check, dd_run_check): pytest.param( { 'http_error': { - '/load-balancer/v2/octavia/amphorae': MockResponse(status_code=500), + '/load-balancer/v2/octavia/amphorae': MockHTTPResponse(status_code=500), } }, None, @@ -2356,8 +2356,8 @@ def test_quotas_metrics(aggregator, check, dd_run_check): { 'http_error': { 'amphorae': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, diff --git a/openstack_controller/tests/test_unit_swift.py b/openstack_controller/tests/test_unit_swift.py index e58f9c17ed862..d1ce57f296cd6 100644 --- a/openstack_controller/tests/test_unit_swift.py +++ b/openstack_controller/tests/test_unit_swift.py @@ -11,7 +11,7 @@ import tests.configs as configs from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.openstack_controller.api.type import ApiType from tests.common import remove_service_from_catalog from tests.metrics import ( @@ -106,8 +106,8 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, pytest.param( { 'http_error': { - '/v1/AUTH_1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '/v1/AUTH_6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '/v1/AUTH_1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '/v1/AUTH_6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } }, configs.REST, @@ -116,8 +116,8 @@ def test_not_in_catalog(aggregator, check, dd_run_check, caplog, mock_http_post, pytest.param( { 'http_error': { - '/v1/AUTH_1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '/v1/AUTH_6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '/v1/AUTH_1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '/v1/AUTH_6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } }, configs.SDK, @@ -197,8 +197,8 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): pytest.param( { 'http_error': { - '/v1/AUTH_1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '/v1/AUTH_6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '/v1/AUTH_1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '/v1/AUTH_6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), }, }, None, @@ -211,8 +211,8 @@ def test_response_time(aggregator, check, dd_run_check, mock_http_get): { 'http_error': { 'containers': { - '1e6e233e637d4d55a50a62b63398ad15': MockResponse(status_code=500), - '6e39099cccde4f809b003d9e0dd09304': MockResponse(status_code=500), + '1e6e233e637d4d55a50a62b63398ad15': MockHTTPResponse(status_code=500), + '6e39099cccde4f809b003d9e0dd09304': MockHTTPResponse(status_code=500), } } }, diff --git a/powerdns_recursor/tests/test_metadata.py b/powerdns_recursor/tests/test_metadata.py index 36ff99eebd981..e5f82358b469d 100644 --- a/powerdns_recursor/tests/test_metadata.py +++ b/powerdns_recursor/tests/test_metadata.py @@ -6,7 +6,7 @@ import pytest import requests -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.powerdns_recursor import PowerDNSRecursorCheck from . import common @@ -30,13 +30,13 @@ def test_metadata_unit(datadog_agent): check.log.debug.assert_called_with('Error collecting PowerDNS Recursor version: %s', '') datadog_agent.reset() - with mock.patch('requests.Session.get', return_value=MockResponse()): + with mock.patch('requests.Session.get', return_value=MockHTTPResponse()): check._collect_metadata(config_obj) datadog_agent.assert_metadata_count(0) check.log.debug.assert_called_with("Couldn't find the PowerDNS Recursor Server version header") datadog_agent.reset() - with mock.patch('requests.Session.get', return_value=MockResponse(headers={'Server': 'wrong_stuff'})): + with mock.patch('requests.Session.get', return_value=MockHTTPResponse(headers={'Server': 'wrong_stuff'})): check._collect_metadata(config_obj) datadog_agent.assert_metadata_count(0) check.log.debug.assert_called_with( diff --git a/proxmox/tests/conftest.py b/proxmox/tests/conftest.py index 6d0411b86f136..d54d7b7f0cfd1 100644 --- a/proxmox/tests/conftest.py +++ b/proxmox/tests/conftest.py @@ -7,10 +7,10 @@ from pathlib import Path from urllib.parse import urlparse -import mock import pytest -import requests +from datadog_checks.base.utils.http_exceptions import HTTPStatusError +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.fs import get_here from .common import INSTANCE @@ -83,17 +83,13 @@ def call(method, url, file='response', headers=None, params=None): response = mock_responses(method, url, file=file, headers=headers, params=params) if response is not None: return response - http_response = requests.models.Response() - http_response.status_code = 404 - http_response.reason = "Not Found" - http_response.url = url - raise requests.exceptions.HTTPError(response=http_response) + raise HTTPStatusError('404 Client Error', response=MockHTTPResponse(status_code=404, url=url)) yield call @pytest.fixture -def mock_http_get(request, monkeypatch, mock_http_call): +def mock_http_get(request, mock_http, mock_http_call): param = request.param if hasattr(request, 'param') and request.param is not None else {} http_error = param.pop('http_error', {}) @@ -102,12 +98,10 @@ def get(url, *args, **kwargs): url = get_url_path(url) if http_error and url in http_error: return http_error[url] - mock_status_code = mock.MagicMock(return_value=200) headers = kwargs.get('headers') params = kwargs.get('params') - mock_json = mock.MagicMock(return_value=mock_http_call(method, url, headers=headers, params=params)) - return mock.MagicMock(json=mock_json, status_code=mock_status_code) + json_data = mock_http_call(method, url, headers=headers, params=params) + return MockHTTPResponse(json_data=json_data) - mock_get = mock.MagicMock(side_effect=get) - monkeypatch.setattr('requests.Session.get', mock_get) - return mock_get + mock_http.get.side_effect = get + return mock_http.get diff --git a/proxmox/tests/test_unit.py b/proxmox/tests/test_unit.py index 8bec8ae7d26b0..23802f60b76f5 100644 --- a/proxmox/tests/test_unit.py +++ b/proxmox/tests/test_unit.py @@ -9,7 +9,7 @@ import mock import pytest -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.proxmox import ProxmoxCheck @@ -58,11 +58,11 @@ def test_no_tags(dd_run_check, aggregator, instance): ('mock_http_get'), [ pytest.param( - {'http_error': {'/api2/json/version': MockResponse(status_code=500)}}, + {'http_error': {'/api2/json/version': MockHTTPResponse(status_code=500)}}, id='500', ), pytest.param( - {'http_error': {'/api2/json/version': MockResponse(status_code=404)}}, + {'http_error': {'/api2/json/version': MockHTTPResponse(status_code=404)}}, id='404', ), ], @@ -71,7 +71,7 @@ def test_no_tags(dd_run_check, aggregator, instance): @pytest.mark.usefixtures('mock_http_get') def test_api_down(dd_run_check, aggregator, instance): check = ProxmoxCheck('proxmox', {}, [instance]) - with pytest.raises(Exception, match=r'requests.exceptions.HTTPError'): + with pytest.raises(Exception, match=r'HTTPStatusError'): dd_run_check(check) aggregator.assert_metric( @@ -270,7 +270,7 @@ def test_resource_up_metrics(dd_run_check, aggregator, instance): pytest.param( { 'http_error': { - '/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': MockResponse(status_code=500) + '/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': MockHTTPResponse(status_code=500) } }, id='500', @@ -278,7 +278,7 @@ def test_resource_up_metrics(dd_run_check, aggregator, instance): pytest.param( { 'http_error': { - '/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': MockResponse(status_code=404) + '/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': MockHTTPResponse(status_code=404) } }, id='404', @@ -286,7 +286,7 @@ def test_resource_up_metrics(dd_run_check, aggregator, instance): pytest.param( { 'http_error': { - '/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': MockResponse( + '/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': MockHTTPResponse( status_code=200, json_data={"data": None, "message": "No QEMU guest agent configured\n"} ) } @@ -489,7 +489,7 @@ def test_perf_metrics(dd_run_check, aggregator, instance): ('mock_http_get'), [ pytest.param( - {'http_error': {'/api2/json/cluster/metrics/export': MockResponse(status_code=501)}}, + {'http_error': {'/api2/json/cluster/metrics/export': MockHTTPResponse(status_code=501)}}, id='501', ), ], diff --git a/rabbitmq/tests/test_openmetrics.py b/rabbitmq/tests/test_openmetrics.py index 36990aa17f8e4..2000235d73d61 100644 --- a/rabbitmq/tests/test_openmetrics.py +++ b/rabbitmq/tests/test_openmetrics.py @@ -10,7 +10,7 @@ from datadog_checks.base.errors import ConfigurationError from datadog_checks.base.types import ServiceCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.rabbitmq import RabbitMQ @@ -203,7 +203,7 @@ def mock_http_responses(url, **_params): ): 'detailed-only-metrics.txt', }[parsed.path + (f"?{parsed.query}" if parsed.query else "")] with open(os.path.join(OM_RESPONSE_FIXTURES, fname)) as fh: - return MockResponse(content=fh.read()) + return MockHTTPResponse(content=fh.read()) @pytest.mark.parametrize( diff --git a/sonarqube/tests/test_unit.py b/sonarqube/tests/test_unit.py index 3582574044079..a469dfafe6c2b 100644 --- a/sonarqube/tests/test_unit.py +++ b/sonarqube/tests/test_unit.py @@ -6,7 +6,7 @@ import mock import requests -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from .common import HERE from .metrics import WEB_METRICS @@ -27,10 +27,10 @@ def test_service_check_critical(aggregator, dd_run_check, sonarqube_check, web_i def test_service_check_ok_version_empty(aggregator, dd_run_check, sonarqube_check, web_instance): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version_empty')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version_empty')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance) global_tags = ['endpoint:{}'.format(web_instance['web_endpoint'])] @@ -44,10 +44,10 @@ def test_service_check_ok_version_empty(aggregator, dd_run_check, sonarqube_chec def test_service_check_ok(aggregator, dd_run_check, sonarqube_check, web_instance): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance) global_tags = ['endpoint:{}'.format(web_instance['web_endpoint'])] @@ -61,10 +61,10 @@ def test_service_check_ok(aggregator, dd_run_check, sonarqube_check, web_instanc def test_service_check_ok_and_config_none(aggregator, dd_run_check, sonarqube_check, web_instance_config_none): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_config_none) global_tags = ['endpoint:{}'.format(web_instance_config_none['web_endpoint'])] @@ -80,10 +80,10 @@ def test_service_check_ok_and_exclude_metrics( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_and_exclude_metrics) global_tags = ['endpoint:{}'.format(web_instance_and_exclude_metrics['web_endpoint'])] @@ -99,11 +99,11 @@ def test_service_check_ok_with_autodiscovery_only_include( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_with_autodiscovery_only_include) global_tags = ['endpoint:{}'.format(web_instance_with_autodiscovery_only_include['web_endpoint'])] @@ -119,10 +119,10 @@ def test_service_check_ok_with_autodiscovery_only_include_metrics_empty( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_empty')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component_empty')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_empty')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component_empty')), ] check = sonarqube_check(web_instance_with_autodiscovery_only_include) global_tags = ['endpoint:{}'.format(web_instance_with_autodiscovery_only_include['web_endpoint'])] @@ -138,12 +138,12 @@ def test_service_check_ok_with_autodiscovery_include_all_and_exclude( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_with_autodiscovery_include_all_and_exclude) global_tags = ['endpoint:{}'.format(web_instance_with_autodiscovery_include_all_and_exclude['web_endpoint'])] @@ -162,12 +162,12 @@ def test_service_check_ok_with_autodiscovery_include_all_and_limit( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search_with_tmp_p2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_with_autodiscovery_include_all_and_limit) global_tags = ['endpoint:{}'.format(web_instance_with_autodiscovery_include_all_and_limit['web_endpoint'])] @@ -184,12 +184,12 @@ def test_service_check_ok_with_component_and_autodiscovery( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_with_component_and_autodiscovery) global_tags = ['endpoint:{}'.format(web_instance_with_component_and_autodiscovery['web_endpoint'])] @@ -206,11 +206,11 @@ def test_service_check_ok_with_autodiscovery_config_none( ): with mock.patch('datadog_checks.sonarqube.check.SonarqubeCheck.http') as mock_http: mock_http.get.side_effect = [ - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), - MockResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'version')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_1')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'metrics_search_p_2')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'components_search')), + MockHTTPResponse(file_path=os.path.join(HERE, 'api_responses', 'measures_component')), ] check = sonarqube_check(web_instance_with_autodiscovery_config_none) global_tags = ['endpoint:{}'.format(web_instance_with_autodiscovery_config_none['web_endpoint'])] diff --git a/spark/datadog_checks/spark/spark.py b/spark/datadog_checks/spark/spark.py index 43db18e3d84fd..222423e79edb8 100644 --- a/spark/datadog_checks/spark/spark.py +++ b/spark/datadog_checks/spark/spark.py @@ -1,6 +1,7 @@ # (C) Datadog, Inc. 2018-present # All rights reserved # Licensed under a 3-clause BSD style license (see LICENSE) +from json import JSONDecodeError as StdJSONDecodeError from urllib.parse import urljoin, urlparse, urlsplit, urlunsplit from bs4 import BeautifulSoup @@ -455,7 +456,7 @@ def _describe_app(self, property, running_apps, addl_tags): continue try: yield (response.json(), [f'app_name:{app_name}'] + addl_tags) - except JSONDecodeError: + except (JSONDecodeError, StdJSONDecodeError): self.log.debug( 'Skipping metrics for %s from app %s due to unparsable JSON payload.', property, app_name ) @@ -723,7 +724,7 @@ def _rest_request_to_json(self, address, object_path, service_name, tags, *args, try: response_json = response.json() - except JSONDecodeError as e: + except (JSONDecodeError, StdJSONDecodeError) as e: response_text = response.text.strip() if response_text and 'spark is starting up' in response_text.lower(): # Handle startup message based on retry configuration diff --git a/spark/tests/test_spark.py b/spark/tests/test_spark.py index 43bae42035523..6735156576c36 100644 --- a/spark/tests/test_spark.py +++ b/spark/tests/test_spark.py @@ -14,7 +14,7 @@ import urllib3 from requests import ConnectionError, RequestException -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.spark import SparkCheck @@ -161,13 +161,13 @@ def __hash__(self): CERTIFICATE_DIR = os.path.join(os.path.dirname(__file__), 'certificate') DEFAULT_RESPONSES = { - '/jobs': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')), - '/stages': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')), - '/executors': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')), - '/storage/rdd': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')), - '/streaming/statistics': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')), - '/metrics/json': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')), - '/api/v1/version': MockResponse(file_path=os.path.join(FIXTURE_DIR, 'version')), + '/jobs': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')), + '/stages': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')), + '/executors': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')), + '/storage/rdd': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')), + '/streaming/statistics': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')), + '/metrics/json': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')), + '/api/v1/version': MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'version')), } @@ -182,9 +182,9 @@ def yarn_requests_get_mock(session, url, *args, **kwargs): arg_url = Url(url) if arg_url == YARN_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'yarn_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'yarn_apps')) elif arg_url == YARN_SPARK_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) return get_default_mock(url) @@ -203,100 +203,100 @@ def mesos_requests_get_mock(session, url, *args, **kwargs): arg_url = Url(url) if arg_url == MESOS_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'mesos_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'mesos_apps')) elif arg_url == MESOS_SPARK_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) elif arg_url == MESOS_SPARK_JOB_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) elif arg_url == MESOS_SPARK_STAGE_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) elif arg_url == MESOS_SPARK_EXECUTOR_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) elif arg_url == MESOS_SPARK_RDD_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) elif arg_url == MESOS_SPARK_STREAMING_STATISTICS_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) elif arg_url == MESOS_SPARK_METRICS_JSON_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) def driver_requests_get_mock(session, url, *args, **kwargs): arg_url = Url(url) if arg_url == DRIVER_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) elif arg_url == DRIVER_SPARK_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) elif arg_url == DRIVER_SPARK_JOB_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) elif arg_url == DRIVER_SPARK_STAGE_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) elif arg_url == DRIVER_SPARK_EXECUTOR_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) elif arg_url == DRIVER_SPARK_RDD_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) elif arg_url == DRIVER_SPARK_STREAMING_STATISTICS_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) elif arg_url == DRIVER_SPARK_METRICS_JSON_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) def standalone_requests_get_mock(session, url, *args, **kwargs): arg_url = Url(url) if arg_url == STANDALONE_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_apps')) elif arg_url == STANDALONE_APP_HTML_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_app')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_app')) elif arg_url == STANDALONE_SPARK_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps')) elif arg_url == STANDALONE_SPARK_JOB_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) elif arg_url == STANDALONE_SPARK_STAGE_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) elif arg_url == STANDALONE_SPARK_EXECUTOR_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) elif arg_url == STANDALONE_SPARK_RDD_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) elif arg_url == STANDALONE_SPARK_STREAMING_STATISTICS_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) elif arg_url == STANDALONE_SPARK_METRICS_JSON_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) def standalone_requests_pre20_get_mock(session, url, *args, **kwargs): arg_url = Url(url) if arg_url == STANDALONE_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_apps')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_apps')) elif arg_url == STANDALONE_APP_HTML_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_app')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_standalone_app')) elif arg_url == STANDALONE_SPARK_APP_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps_pre20')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'spark_apps_pre20')) elif arg_url == STANDALONE_SPARK_JOB_URL: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) elif arg_url == STANDALONE_SPARK_STAGE_URL: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) elif arg_url == STANDALONE_SPARK_EXECUTOR_URL: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) elif arg_url == STANDALONE_SPARK_RDD_URL: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) elif arg_url == STANDALONE_SPARK_STREAMING_STATISTICS_URL: - return MockResponse(status_code=404) + return MockHTTPResponse(status_code=404) elif arg_url == STANDALONE_SPARK_JOB_URL_PRE20: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'job_metrics')) elif arg_url == STANDALONE_SPARK_STAGE_URL_PRE20: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'stage_metrics')) elif arg_url == STANDALONE_SPARK_EXECUTOR_URL_PRE20: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'executor_metrics')) elif arg_url == STANDALONE_SPARK_RDD_URL_PRE20: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'rdd_metrics')) elif arg_url == STANDALONE_SPARK_STREAMING_STATISTICS_URL_PRE20: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'streaming_statistics')) elif arg_url == VERSION_PATH: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'version')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'version')) elif arg_url == STANDALONE_SPARK_METRICS_JSON_URL_PRE20: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'metrics_json')) def proxy_with_warning_page_mock(session, url, *args, **kwargs): @@ -314,7 +314,7 @@ def proxy_with_warning_page_mock(session, url, *args, **kwargs): url_parts[4] = urlencode(query) with open(os.path.join(FIXTURE_DIR, 'html_warning_page'), 'r') as f: body = f.read().replace('$REDIRECT_URL$', urlunparse(url_parts)) - return MockResponse(body, cookies={'proxy_cookie': 'foo'}) + return MockHTTPResponse(body, cookies={'proxy_cookie': 'foo'}) CHECK_NAME = 'spark' @@ -1190,10 +1190,10 @@ def test_do_not_crash_on_version_collection_failure(): @pytest.mark.unit def test_driver_startup_message_default_retries(aggregator, caplog): """Default behavior (startup_wait_retries=3): retry 3 times then raise.""" - from simplejson import JSONDecodeError + from json import JSONDecodeError check = SparkCheck('spark', {}, [DRIVER_CONFIG]) - response = MockResponse(content="Spark is starting up. Please wait a while until it's ready.") + response = MockHTTPResponse(content="Spark is starting up. Please wait a while until it's ready.") with caplog.at_level(logging.DEBUG): with mock.patch.object(check, '_rest_request', return_value=response): @@ -1222,12 +1222,12 @@ def test_driver_startup_message_default_retries(aggregator, caplog): @pytest.mark.parametrize("retries_value", [0, -1, -5]) def test_driver_startup_message_disabled(aggregator, retries_value): """When startup_wait_retries<=0, treat startup messages as errors immediately.""" - from simplejson import JSONDecodeError + from json import JSONDecodeError config = DRIVER_CONFIG.copy() config['startup_wait_retries'] = retries_value check = SparkCheck('spark', {}, [config]) - response = MockResponse(content="Spark is starting up. Please wait a while until it's ready.") + response = MockHTTPResponse(content="Spark is starting up. Please wait a while until it's ready.") with mock.patch.object(check, '_rest_request', return_value=response): with pytest.raises(JSONDecodeError): @@ -1243,12 +1243,12 @@ def test_driver_startup_message_disabled(aggregator, retries_value): @pytest.mark.unit def test_driver_startup_message_limited_retries(aggregator, caplog): """When startup_wait_retries>0, retry N times then raise.""" - from simplejson import JSONDecodeError + from json import JSONDecodeError config = DRIVER_CONFIG.copy() config['startup_wait_retries'] = 3 check = SparkCheck('spark', {}, [config]) - response = MockResponse(content="Spark is starting up. Please wait a while until it's ready.") + response = MockHTTPResponse(content="Spark is starting up. Please wait a while until it's ready.") with caplog.at_level(logging.DEBUG): with mock.patch.object(check, '_rest_request', return_value=response): @@ -1280,8 +1280,8 @@ def test_driver_startup_retry_counter_resets_on_success(caplog): config = DRIVER_CONFIG.copy() config['startup_wait_retries'] = 2 check = SparkCheck('spark', {}, [config]) - startup_response = MockResponse(content="Spark is starting up. Please wait a while until it's ready.") - success_response = MockResponse(json_data=[{"id": "app_001", "name": "TestApp"}]) + startup_response = MockHTTPResponse(content="Spark is starting up. Please wait a while until it's ready.") + success_response = MockHTTPResponse(json_data=[{"id": "app_001", "name": "TestApp"}]) with caplog.at_level(logging.DEBUG): with mock.patch.object(check, '_rest_request', return_value=startup_response): @@ -1359,7 +1359,7 @@ def test_do_not_crash_on_single_app_failure(): ids=["driver", "yarn", "mesos", "standalone", "standalone_pre_20"], ) def test_no_running_apps(aggregator, dd_run_check, instance, service_check, caplog): - with mock.patch('requests.Session.get', return_value=MockResponse("{}")): + with mock.patch('requests.Session.get', return_value=MockHTTPResponse("{}")): with caplog.at_level(logging.WARNING): dd_run_check(SparkCheck('spark', {}, [instance])) @@ -1378,9 +1378,11 @@ def test_no_running_apps(aggregator, dd_run_check, instance, service_check, capl @pytest.mark.parametrize( "mock_response", [ - pytest.param(MockResponse(content=""), id="Invalid JSON"), # this triggers json parsing error, - pytest.param(MockResponse(status_code=404), id="property not found"), - pytest.param(MockResponse(status_code=500), id="Spark internal server error"), # reported by users in the wild + pytest.param(MockHTTPResponse(content=""), id="Invalid JSON"), # this triggers json parsing error, + pytest.param(MockHTTPResponse(status_code=404), id="property not found"), + pytest.param( + MockHTTPResponse(status_code=500), id="Spark internal server error" + ), # reported by users in the wild ], ) @pytest.mark.parametrize( @@ -1412,7 +1414,7 @@ def get_without_json(session, url, *args, **kwargs): if arg_url == property_url: return mock_response elif arg_url == YARN_SPARK_APP_URL: - return MockResponse( + return MockHTTPResponse( json_data=[ { "id": SPARK_APP_ID, diff --git a/torchserve/tests/conftest.py b/torchserve/tests/conftest.py index b02a64195e086..022191fb25b3a 100644 --- a/torchserve/tests/conftest.py +++ b/torchserve/tests/conftest.py @@ -7,10 +7,10 @@ import pytest +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import EnvVars, TempDir, docker_run, get_here from datadog_checks.dev._env import get_state, save_state from datadog_checks.dev.conditions import CheckEndpoints, WaitFor -from datadog_checks.dev.http import MockResponse from datadog_checks.torchserve import TorchserveCheck from .common import ( @@ -135,7 +135,7 @@ def _mock_http_responses(url, **_params): pytest.fail(f"url `{url}` not registered") with open(os.path.join(HERE, 'fixtures', metrics_file)) as f: - return MockResponse(content=f.read()) + return MockHTTPResponse(content=f.read()) return _mock_http_responses diff --git a/torchserve/tests/management/test_unit.py b/torchserve/tests/management/test_unit.py index 15cd3b694faff..1e099ff64b3fe 100644 --- a/torchserve/tests/management/test_unit.py +++ b/torchserve/tests/management/test_unit.py @@ -4,7 +4,7 @@ import pytest from datadog_checks.base import AgentCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from ..conftest import mock_http_responses @@ -39,7 +39,7 @@ def custom_mock_http_responses(url, **_params): 'http://torchserve:8081/models/linear_regression_1_1/all', 'http://torchserve:8081/models/linear_regression_2_2/all', ): - return MockResponse(status_code=500) + return MockHTTPResponse(status_code=500) return mock_http_responses()(url) diff --git a/twistlock/tests/test_twistlock.py b/twistlock/tests/test_twistlock.py index c6bef536c290c..7b9442ab0e793 100644 --- a/twistlock/tests/test_twistlock.py +++ b/twistlock/tests/test_twistlock.py @@ -8,8 +8,8 @@ import pytest from datadog_checks.base import AgentCheck +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import get_here -from datadog_checks.dev.http import MockResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.twistlock import TwistlockCheck @@ -38,7 +38,7 @@ def mock_get_factory(fixture_group): def mock_get(session, url, *args, **kwargs): split_url = url.split('/') path = split_url[-1] - return MockResponse(file_path=os.path.join(HERE, 'fixtures', fixture_group, '{}.json'.format(path))) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', fixture_group, '{}.json'.format(path))) return mock_get @@ -97,8 +97,8 @@ def mock_get(url, *args, **kwargs): path = split_url[-1] if path != 'images': - return MockResponse(file_path=os.path.join(HERE, 'fixtures', fixture_group, '{}.json'.format(path))) - return MockResponse(file_path=os.path.join(HERE, 'fixtures', 'empty_images.json')) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', fixture_group, '{}.json'.format(path))) + return MockHTTPResponse(file_path=os.path.join(HERE, 'fixtures', 'empty_images.json')) with mock.patch('requests.Session.get', side_effect=mock_get): check.check(instance) @@ -110,7 +110,7 @@ def test_err_response(aggregator, instance): with pytest.raises(Exception, match='^Error in response'): with mock.patch( - 'requests.Session.get', return_value=MockResponse('{"err": "invalid credentials"}'), autospec=True + 'requests.Session.get', return_value=MockHTTPResponse('{"err": "invalid credentials"}'), autospec=True ): check.check(instance) diff --git a/vault/tests/test_vault.py b/vault/tests/test_vault.py index cf90dddea9d2a..0fd5d4a72bc6b 100644 --- a/vault/tests/test_vault.py +++ b/vault/tests/test_vault.py @@ -8,7 +8,7 @@ import pytest import requests -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.vault import Vault from datadog_checks.vault.common import DEFAULT_API_VERSION from datadog_checks.vault.errors import ApiUnreachable @@ -78,11 +78,11 @@ def test_service_check_connect_ok_all_tags(self, aggregator, dd_run_check, globa def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={'ha_enabled': False, 'is_self': True, 'leader_address': '', 'leader_cluster_address': ''} ) elif url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -135,7 +135,7 @@ def test_service_check_500_fail(self, aggregator, dd_run_check, global_tags): instance.update(INSTANCES['main']) c = Vault(Vault.CHECK_NAME, {}, [instance]) - with mock.patch('requests.Session.get', return_value=MockResponse(status_code=500)): + with mock.patch('requests.Session.get', return_value=MockHTTPResponse(status_code=500)): with pytest.raises( Exception, match=r'^The Vault endpoint `{}.+?` returned 500$'.format(re.escape(instance['api_url'])) ): @@ -171,11 +171,11 @@ def test_service_check_unsealed_ok_all_tags(self, aggregator, dd_run_check, glob def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={'ha_enabled': False, 'is_self': True, 'leader_address': '', 'leader_cluster_address': ''} ) elif url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -214,7 +214,7 @@ def test_service_check_unsealed_fail(self, aggregator, dd_run_check, use_openmet def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -255,11 +255,11 @@ def test_service_check_initialized_ok_all_tags(self, aggregator, dd_run_check, g def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={'ha_enabled': False, 'is_self': True, 'leader_address': '', 'leader_cluster_address': ''} ) elif url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -298,7 +298,7 @@ def test_service_check_initialized_fail(self, aggregator, dd_run_check, use_open def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -329,11 +329,11 @@ def test_disable_legacy_cluster_tag(self, aggregator, dd_run_check, global_tags) def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={'ha_enabled': False, 'is_self': True, 'leader_address': '', 'leader_cluster_address': ''} ) elif url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -370,7 +370,7 @@ def test_replication_dr_mode(self, aggregator, dd_run_check, use_openmetrics): def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -407,7 +407,7 @@ def test_replication_dr_mode_collect_secondary(self, aggregator, dd_run_check, u def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -453,7 +453,7 @@ def mock_requests_get(session, url, *args, **kwargs): else: replication_dr_mode = 'secondary' - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -506,7 +506,7 @@ def test_event_leader_change(self, aggregator, dd_run_check, cluster, use_openme def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={ 'ha_enabled': False, 'is_self': True, @@ -547,7 +547,7 @@ def test_leader_change_not_self(self, aggregator, dd_run_check, use_openmetrics) def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={ 'ha_enabled': False, 'is_self': False, @@ -573,7 +573,7 @@ def test_is_leader_metric_true(self, aggregator, dd_run_check, use_openmetrics): def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={ 'ha_enabled': False, 'is_self': True, @@ -599,7 +599,7 @@ def test_is_leader_metric_false(self, aggregator, dd_run_check, use_openmetrics) def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse( + return MockHTTPResponse( json_data={ 'ha_enabled': False, 'is_self': False, @@ -626,7 +626,7 @@ def test_sys_health_non_standard_status_codes(self, aggregator, dd_run_check, st def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/health': - return MockResponse( + return MockHTTPResponse( json_data={ 'cluster_id': '9e25ccdb-09ea-8bd8-0521-34cf3ef7a4cc', 'cluster_name': 'vault-cluster-f5f44063', @@ -660,7 +660,7 @@ def test_sys_leader_non_standard_status_codes(self, aggregator, dd_run_check, us def mock_requests_get(session, url, *args, **kwargs): if url == instance['api_url'] + '/sys/leader': - return MockResponse(json_data={'errors': ["Vault is sealed"]}, status_code=503) + return MockHTTPResponse(json_data={'errors': ["Vault is sealed"]}, status_code=503) return requests_get(url, *args, **kwargs) with mock.patch('requests.Session.get', side_effect=mock_requests_get, autospec=True): diff --git a/vllm/tests/test_unit.py b/vllm/tests/test_unit.py index 1fa5454eedf65..e1f6dba11bc9d 100644 --- a/vllm/tests/test_unit.py +++ b/vllm/tests/test_unit.py @@ -7,7 +7,7 @@ import pytest from datadog_checks.base.constants import ServiceCheck -from datadog_checks.dev.http import MockResponse +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev.utils import get_metadata_metrics from datadog_checks.vllm import vLLMCheck @@ -19,8 +19,8 @@ def test_check_vllm(dd_run_check, aggregator, datadog_agent, instance): check.check_id = "test:123" mock_responses = [ - MockResponse(file_path=get_fixture_path("vllm_metrics.txt")), - MockResponse(file_path=get_fixture_path("vllm_version.json")), + MockHTTPResponse(file_path=get_fixture_path("vllm_metrics.txt")), + MockHTTPResponse(file_path=get_fixture_path("vllm_version.json")), ] with mock.patch('requests.Session.get', side_effect=mock_responses): @@ -43,8 +43,8 @@ def test_check_vllm_w_ray_prefix(dd_run_check, aggregator, datadog_agent, ray_in check.check_id = "test:123" mock_responses = [ - MockResponse(file_path=get_fixture_path("ray_vllm_metrics.txt")), - MockResponse(file_path=get_fixture_path("vllm_version.json")), + MockHTTPResponse(file_path=get_fixture_path("ray_vllm_metrics.txt")), + MockHTTPResponse(file_path=get_fixture_path("vllm_version.json")), ] with mock.patch('requests.Session.get', side_effect=mock_responses): diff --git a/vsphere/tests/common.py b/vsphere/tests/common.py index 562b990e34dbb..54eb7b5de9c06 100644 --- a/vsphere/tests/common.py +++ b/vsphere/tests/common.py @@ -8,8 +8,8 @@ import mock from pyVmomi import vim, vmodl +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.base.utils.time import get_current_datetime -from datadog_checks.dev.http import MockResponse from datadog_checks.vsphere.api_rest import VSphereRestAPI HERE = os.path.abspath(os.path.dirname(__file__)) @@ -1091,7 +1091,7 @@ def __init__(self): def get(self, url, *args, **kwargs): if '/api/' in url: - return MockResponse({}, 404) + return MockHTTPResponse(json_data={}, status_code=404) parsed_url = urlparse(url) path_and_args = parsed_url.path + "?" + parsed_url.query if parsed_url.query else parsed_url.path path_parts = path_and_args.split('/') @@ -1101,7 +1101,7 @@ def get(self, url, *args, **kwargs): if re.match(r'.*/category/id:.*$', url): parts = url.split('_') num = parts[len(parts) - 1] - return MockResponse( + return MockHTTPResponse( json_data={ "value": { "name": "my_cat_name_{}".format(num), @@ -1116,7 +1116,7 @@ def get(self, url, *args, **kwargs): elif re.match(r'.*/tagging/tag/id:.*$', url): parts = url.split('_') num = parts[len(parts) - 1] - return MockResponse( + return MockHTTPResponse( json_data={ "value": { "category_id": "cat_id_{}".format(num), @@ -1132,7 +1132,7 @@ def get(self, url, *args, **kwargs): def post(self, url, *args, **kwargs): if '/api/' in url: - return MockResponse({}, 404) + return MockHTTPResponse(json_data={}, status_code=404) assert kwargs['headers']['Content-Type'] == 'application/json' parsed_url = urlparse(url) path_and_args = parsed_url.path + "?" + parsed_url.query if parsed_url.query else parsed_url.path @@ -1141,12 +1141,12 @@ def post(self, url, *args, **kwargs): if subpath in self.exceptions: raise self.exceptions[subpath] if re.match(r'.*/session$', url): - return MockResponse( + return MockHTTPResponse( json_data={"value": "dummy-token"}, status_code=200, ) elif re.match(r'.*/tagging/tag-association\?~action=list-attached-tags-on-objects$', url): - return MockResponse( + return MockHTTPResponse( json_data={ "value": [ {"object_id": {"id": "vm1", "type": "VirtualMachine"}, "tag_ids": ["tag_id_1", "tag_id_2"]}, @@ -1173,7 +1173,7 @@ def get(self, url, *args, **kwargs): if re.match(r'.*/category/.*$', url): parts = url.split('_') num = parts[len(parts) - 1] - return MockResponse( + return MockHTTPResponse( json_data={ 'name': 'my_cat_name_{}'.format(num), 'description': 'VM category description', @@ -1186,7 +1186,7 @@ def get(self, url, *args, **kwargs): elif re.match(r'.*/tagging/tag/.*$', url): parts = url.split('_') num = parts[len(parts) - 1] - return MockResponse( + return MockHTTPResponse( json_data={ 'category_id': 'cat_id_{}'.format(num), 'name': 'my_tag_name_{}'.format(num), @@ -1207,12 +1207,12 @@ def post(self, url, *args, **kwargs): if subpath in self.exceptions: raise self.exceptions[subpath] if re.match(r'.*/session$', url): - return MockResponse( + return MockHTTPResponse( json_data="dummy-token", status_code=200, ) elif re.match(r'.*/tagging/tag-association\?action=list-attached-tags-on-objects$', url): - return MockResponse( + return MockHTTPResponse( json_data=[ {'tag_ids': ['tag_id_1', 'tag_id_2'], 'object_id': {'id': 'vm1', 'type': 'VirtualMachine'}}, {'tag_ids': ['tag_id_2'], 'object_id': {'id': 'ds1', 'type': 'Datastore'}}, diff --git a/yarn/tests/conftest.py b/yarn/tests/conftest.py index 1ca370584a43b..b435fcf773a73 100644 --- a/yarn/tests/conftest.py +++ b/yarn/tests/conftest.py @@ -10,9 +10,9 @@ from mock import patch from requests.exceptions import SSLError +from datadog_checks.base.utils.http_testing import MockHTTPResponse from datadog_checks.dev import docker_run from datadog_checks.dev.conditions import CheckEndpoints -from datadog_checks.dev.http import MockResponse from datadog_checks.yarn import YarnCheck from datadog_checks.yarn.yarn import YARN_APPS_PATH, YARN_CLUSTER_METRICS_PATH, YARN_NODES_PATH, YARN_SCHEDULER_PATH @@ -20,8 +20,6 @@ FIXTURE_DIR, HERE, INSTANCE_INTEGRATION, - TEST_PASSWORD, - TEST_USERNAME, YARN_APPS_URL, YARN_CLUSTER_METRICS_URL, YARN_NODES_URL, @@ -56,56 +54,36 @@ def instance(): @pytest.fixture -def mocked_request(): - with patch("requests.Session.get", new=requests_get_mock): - yield +def mocked_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield @pytest.fixture -def mocked_auth_request(): - def requests_auth_get(session, *args, **kwargs): - # Make sure we're passing in authentication - assert 'auth' in kwargs, 'Missing "auth" argument in requests.Session.get(...) call' - - # Make sure we've got the correct username and password - assert kwargs['auth'] == ( - TEST_USERNAME, - TEST_PASSWORD, - ), "Incorrect username or password in requests.Session.get" - - # Return mocked request.get(...) - return requests_get_mock(session, *args, **kwargs) - - with patch("requests.Session.get", new=requests_auth_get): - yield +def mocked_auth_request(mock_http): + mock_http.get.side_effect = requests_get_mock + yield @pytest.fixture def mocked_bad_cert_request(): - """ - Mock request.Session.get to an endpoint with a badly configured ssl cert - """ + """Keep requests.Session.get patch — tests verify=True vs verify=False which requires the real HTTP wrapper.""" def requests_bad_cert_get(session, *args, **kwargs): - # Make sure we're passing in the 'verify' argument - assert 'verify' in kwargs, 'Missing "verify" argument in requests.Session.get(...) call' - - if kwargs['verify']: + if kwargs.get('verify', True): raise SSLError("certificate verification failed for {}".format(args[0])) - - # Return the actual response - return requests_get_mock(session, *args, **kwargs) + return requests_get_mock(args[0], *args[1:], **kwargs) with patch("requests.Session.get", new=requests_bad_cert_get): yield -def requests_get_mock(session, *args, **kwargs): - if args[0] == YARN_CLUSTER_METRICS_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'cluster_metrics')) - elif args[0] == YARN_APPS_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'apps_metrics')) - elif args[0] == YARN_NODES_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'nodes_metrics')) - elif args[0] == YARN_SCHEDULER_URL: - return MockResponse(file_path=os.path.join(FIXTURE_DIR, 'scheduler_metrics')) +def requests_get_mock(url, *args, **kwargs): + if url == YARN_CLUSTER_METRICS_URL: + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'cluster_metrics')) + elif url == YARN_APPS_URL: + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'apps_metrics')) + elif url == YARN_NODES_URL: + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'nodes_metrics')) + elif url == YARN_SCHEDULER_URL: + return MockHTTPResponse(file_path=os.path.join(FIXTURE_DIR, 'scheduler_metrics'))