Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 50 additions & 59 deletions airflow/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ def test_service_checks_cannot_connect(aggregator):
'json_resp, expected_healthy_status, expected_healthy_value',
[({'status': 'OK'}, AgentCheck.OK, 1), ({'status': 'KO'}, AgentCheck.CRITICAL, 0), ({}, AgentCheck.CRITICAL, 0)],
)
def test_service_checks_healthy_exp(aggregator, json_resp, expected_healthy_status, expected_healthy_value):
def test_service_checks_healthy_exp(aggregator, mock_http, json_resp, expected_healthy_status, expected_healthy_value):
instance = common.FULL_CONFIG['instances'][0]
check = AirflowCheck('airflow', common.FULL_CONFIG, [instance])

with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value=None):
mock_session = mock.MagicMock()
with mock.patch('datadog_checks.base.utils.http.requests.Session', return_value=mock_session):
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [json_resp]
mock_session.get.return_value = mock_resp
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [json_resp]
mock_http.get.return_value = mock_resp

check.check(None)
with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value=None):
check.check(None)

tags = ['key:my-tag', 'url:http://localhost:8080']

Expand All @@ -54,65 +52,60 @@ def test_service_checks_healthy_exp(aggregator, json_resp, expected_healthy_stat
],
)
def test_service_checks_healthy_stable(
aggregator, metadb_status, scheduler_status, expected_healthy_status, expected_healthy_value
aggregator, mock_http, metadb_status, scheduler_status, expected_healthy_status, expected_healthy_value
): # Stable is only defined in the context of Airflow 2
instance = common.FULL_CONFIG['instances'][0]
check = AirflowCheck('airflow', common.FULL_CONFIG, [instance])

with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value='2.6.2'):
mock_session = mock.MagicMock()
with mock.patch('datadog_checks.base.utils.http.requests.Session', return_value=mock_session):
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': metadb_status}, 'scheduler': {'status': scheduler_status}},
{'status': 'OK'},
]
mock_session.get.return_value = mock_resp
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': metadb_status}, 'scheduler': {'status': scheduler_status}},
{'status': 'OK'},
]
mock_http.get.return_value = mock_resp

check.check(None)
with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value='2.6.2'):
check.check(None)

tags = ['key:my-tag', 'url:http://localhost:8080']

aggregator.assert_service_check('airflow.healthy', expected_healthy_status, tags=tags, count=1)
aggregator.assert_metric('airflow.healthy', expected_healthy_value, tags=tags, count=1)


def test_dag_total_tasks(aggregator, task_instance):
def test_dag_total_tasks(aggregator, mock_http, task_instance):
instance = common.FULL_CONFIG['instances'][0]
check = AirflowCheck('airflow', common.FULL_CONFIG, [instance])

with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value='2.6.2'):
req = mock.MagicMock()
with mock.patch('datadog_checks.base.utils.http.requests.Session', return_value=req):
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': 'healthy'}, 'scheduler': {'status': 'healthy'}},
task_instance,
]
req.get.return_value = mock_resp
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': 'healthy'}, 'scheduler': {'status': 'healthy'}},
task_instance,
]
mock_http.get.return_value = mock_resp

check.check(None)
with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value='2.6.2'):
check.check(None)

aggregator.assert_metric('airflow.dag.task.total_running', value=1, count=1)


def test_dag_task_ongoing_duration(aggregator, task_instance):
def test_dag_task_ongoing_duration(aggregator, mock_http, task_instance):
instance = common.FULL_CONFIG['instances'][0]
check = AirflowCheck('airflow', common.FULL_CONFIG, [instance])

mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': 'healthy'}, 'scheduler': {'status': 'healthy'}},
]
mock_http.get.return_value = mock_resp

with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value='2.6.2'):
req = mock.MagicMock()
with mock.patch('datadog_checks.base.utils.http.requests.Session', return_value=req):
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': 'healthy'}, 'scheduler': {'status': 'healthy'}},
]
req.get.return_value = mock_resp
with mock.patch(
'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances',
return_value=task_instance.get('task_instances'),
):
check.check(None)
with mock.patch(
'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances',
return_value=task_instance.get('task_instances'),
):
check.check(None)

aggregator.assert_metric(
'airflow.dag.task.ongoing_duration',
Expand Down Expand Up @@ -141,23 +134,21 @@ def test_dag_task_ongoing_duration(aggregator, task_instance):
),
],
)
def test_config_collect_ongoing_duration(collect_ongoing_duration, should_call_method):
def test_config_collect_ongoing_duration(mock_http, collect_ongoing_duration, should_call_method):
instance = {**common.FULL_CONFIG['instances'][0], 'collect_ongoing_duration': collect_ongoing_duration}
check = AirflowCheck('airflow', common.FULL_CONFIG, [instance])

mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': 'healthy'}, 'scheduler': {'status': 'healthy'}},
]
mock_http.get.return_value = mock_resp

with mock.patch('datadog_checks.airflow.airflow.AirflowCheck._get_version', return_value='2.6.2'):
req = mock.MagicMock()
with mock.patch('datadog_checks.base.utils.http.requests.Session', return_value=req):
mock_resp = mock.MagicMock(status_code=200)
mock_resp.json.side_effect = [
{'metadatabase': {'status': 'healthy'}, 'scheduler': {'status': 'healthy'}},
]
req.get.return_value = mock_resp

with mock.patch(
'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances'
) as mock_get_all_task_instances:
check.check(None)

# Assert method calls
mock_get_all_task_instances.assert_has_calls(should_call_method, any_order=False)
with mock.patch(
'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances'
) as mock_get_all_task_instances:
check.check(None)

# Assert method calls
mock_get_all_task_instances.assert_has_calls(should_call_method, any_order=False)
2 changes: 1 addition & 1 deletion appgate_sdp/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_check_appgate_sdp(dd_run_check, aggregator, instance, mock_http_respons
def test_emits_critical_service_check_when_service_is_down(dd_run_check, aggregator, instance, mock_http_response):
mock_http_response(status_code=404)
check = AppgateSDPCheck('appgate_sdp', {}, [instance])
with pytest.raises(Exception, match='requests.exceptions.HTTPError'):
with pytest.raises(Exception, match='HTTPStatusError'):
dd_run_check(check)
aggregator.assert_service_check('appgate_sdp.openmetrics.health', AppgateSDPCheck.CRITICAL)

Expand Down
2 changes: 1 addition & 1 deletion argo_workflows/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ def test_check_with_fixtures(dd_run_check, aggregator, instance, mock_http_respo
def test_emits_critical_service_check_when_service_is_down(dd_run_check, aggregator, instance, mock_http_response):
mock_http_response(status_code=404)
check = ArgoWorkflowsCheck('argo_workflows', {}, [instance])
with pytest.raises(Exception, match='requests.exceptions.HTTPError'):
with pytest.raises(Exception, match='HTTPStatusError'):
dd_run_check(check)
aggregator.assert_service_check('argo_workflows.openmetrics.health', ArgoWorkflowsCheck.CRITICAL)
55 changes: 26 additions & 29 deletions bentoml/tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# (C) Datadog, Inc. 2025-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from unittest.mock import Mock, patch
from unittest.mock import Mock

import pytest
import requests

from datadog_checks.base.constants import ServiceCheck
from datadog_checks.base.utils.http_exceptions import HTTPStatusError
from datadog_checks.bentoml import BentomlCheck
from datadog_checks.dev.utils import get_metadata_metrics

Expand All @@ -21,26 +21,19 @@
def test_bentoml_mock_metrics(dd_run_check, aggregator, mock_http_response):
mock_http_response(file_path=get_fixture_path('metrics.txt'))

with patch('datadog_checks.bentoml.check.BentomlCheck.http') as mock_http:
mock_response = type('MockResponse', (), {'status_code': 200})()
mock_http.get.return_value = mock_response
mock_http.get.return_value.raise_for_status = lambda: None

check = BentomlCheck('bentoml', {}, [OM_MOCKED_INSTANCE])
dd_run_check(check)
check = BentomlCheck('bentoml', {}, [OM_MOCKED_INSTANCE])
dd_run_check(check)

for metric in METRICS:
aggregator.assert_metric(metric)
for metric in METRICS:
aggregator.assert_metric(metric)

for metric in ENDPOINT_METRICS:
aggregator.assert_metric(metric, value=1, tags=['test:tag', 'status_code:200'])
for metric in ENDPOINT_METRICS:
aggregator.assert_metric(metric, value=1, tags=['test:tag', 'status_code:200'])

aggregator.assert_all_metrics_covered()
assert mock_http.get.call_count == 2
aggregator.assert_metrics_using_metadata(get_metadata_metrics())
aggregator.assert_all_metrics_covered()
aggregator.assert_metric_has_tag('bentoml.service.request.count', 'bentoml_endpoint:/summarize')
aggregator.assert_service_check('bentoml.openmetrics.health', ServiceCheck.OK)
aggregator.assert_all_metrics_covered()
aggregator.assert_metrics_using_metadata(get_metadata_metrics())
aggregator.assert_metric_has_tag('bentoml.service.request.count', 'bentoml_endpoint:/summarize')
aggregator.assert_service_check('bentoml.openmetrics.health', ServiceCheck.OK)


def test_bentoml_mock_invalid_endpoint(dd_run_check, aggregator, mock_http_response):
Expand All @@ -53,21 +46,25 @@ def test_bentoml_mock_invalid_endpoint(dd_run_check, aggregator, mock_http_respo


def test_bentoml_mock_valid_endpoint_invalid_health(dd_run_check, aggregator, mock_http_response):
mock_http_response(file_path=get_fixture_path('metrics.txt'))
session_get_mock = mock_http_response(file_path=get_fixture_path('metrics.txt'))
metrics_response = session_get_mock.return_value

_err = Mock()
_err.status_code = 500
_http_err = requests.HTTPError("500 Internal Server Error")
_http_err.response = _err
_http_err = HTTPStatusError("500 Internal Server Error", response=_err)
_err.raise_for_status.side_effect = _http_err

with patch('datadog_checks.bentoml.check.BentomlCheck.http') as mock_http:
mock_http.get.return_value = _err
def dispatch(url, **_):
if '/livez' in url or '/readyz' in url:
return _err
return metrics_response

check = BentomlCheck('bentoml', {}, [OM_MOCKED_INSTANCE])
dd_run_check(check)
session_get_mock.side_effect = dispatch

check = BentomlCheck('bentoml', {}, [OM_MOCKED_INSTANCE])
dd_run_check(check)

for metric in ENDPOINT_METRICS:
aggregator.assert_metric(metric, value=0, tags=['test:tag', 'status_code:500'])
for metric in ENDPOINT_METRICS:
aggregator.assert_metric(metric, value=0, tags=['test:tag', 'status_code:500'])

aggregator.assert_service_check('bentoml.openmetrics.health', ServiceCheck.OK)
aggregator.assert_service_check('bentoml.openmetrics.health', ServiceCheck.OK)
2 changes: 1 addition & 1 deletion celery/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_emits_critical_openemtrics_service_check_when_service_is_down(
"""
mock_http_response(status_code=404)
check = CeleryCheck("celery", {}, [instance])
with pytest.raises(Exception, match="requests.exceptions.HTTPError"):
with pytest.raises(Exception, match="HTTPStatusError"):
dd_run_check(check)

aggregator.assert_all_metrics_covered()
Expand Down
60 changes: 23 additions & 37 deletions consul/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
import logging

import mock
import pytest

from datadog_checks.consul import ConsulCheck
Expand Down Expand Up @@ -155,25 +154,25 @@ def test_get_nodes_with_service_critical(aggregator):
aggregator.assert_metric('consul.catalog.services_count', value=1, tags=expected_tags)


def test_consul_request(aggregator, instance, mocker):
def test_consul_request(aggregator, instance, mocker, mock_http):
consul_check = ConsulCheck(common.CHECK_NAME, {}, [consul_mocks.MOCK_CONFIG])
mocker.patch("datadog_checks.base.utils.serialization.json.loads")
with mock.patch("datadog_checks.consul.consul.requests.Session.get") as mock_requests_get:

consul_check.consul_request("foo")
url = "{}/{}".format(instance["url"], "foo")
aggregator.assert_service_check("consul.can_connect", ConsulCheck.OK, tags=["url:{}".format(url)], count=1)

aggregator.reset()
mock_http.get.side_effect = Exception("message")
with pytest.raises(Exception):
consul_check.consul_request("foo")
url = "{}/{}".format(instance["url"], "foo")
aggregator.assert_service_check("consul.can_connect", ConsulCheck.OK, tags=["url:{}".format(url)], count=1)

aggregator.reset()
mock_requests_get.side_effect = Exception("message")
with pytest.raises(Exception):
consul_check.consul_request("foo")
aggregator.assert_service_check(
"consul.can_connect",
ConsulCheck.CRITICAL,
tags=["url:{}".format(url)],
count=1,
message="Consul request to {} failed: message".format(url),
)
aggregator.assert_service_check(
"consul.can_connect",
ConsulCheck.CRITICAL,
tags=["url:{}".format(url)],
count=1,
message="Consul request to {} failed: message".format(url),
)


def test_service_checks(aggregator):
Expand Down Expand Up @@ -648,26 +647,13 @@ def test_network_latency_node_name(
),
],
)
def test_config(test_case, extra_config, expected_http_kwargs, mocker):
def test_config(test_case, extra_config, expected_http_kwargs):
instance = extra_config
check = ConsulCheck(common.CHECK_NAME, {}, instances=[instance])
mocker.patch("datadog_checks.base.utils.serialization.json.loads")

with mock.patch('datadog_checks.base.utils.http.requests.Session') as session:
mock_session = mock.MagicMock()
session.return_value = mock_session
mock_session.get.return_value = mock.MagicMock(status_code=200)

check.check(None)

http_wargs = {
'auth': mock.ANY,
'cert': mock.ANY,
'headers': mock.ANY,
'proxies': mock.ANY,
'timeout': mock.ANY,
'verify': mock.ANY,
'allow_redirects': mock.ANY,
}
http_wargs.update(expected_http_kwargs)
mock_session.get.assert_called_with('/v1/status/leader', **http_wargs)
for key, value in expected_http_kwargs.items():
if key == 'headers':
for h_key, h_value in value.items():
assert check.http.get_header(h_key) == h_value
else:
assert check.http.options[key] == value
21 changes: 2 additions & 19 deletions couch/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from copy import deepcopy
from unittest.mock import MagicMock

import mock
import pytest

from datadog_checks.couch import CouchDb
Expand All @@ -30,24 +29,8 @@ def test_config(test_case, extra_config, expected_http_kwargs):
instance.update(extra_config)
check = CouchDb(common.CHECK_NAME, {}, instances=[instance])

r = mock.MagicMock()
with mock.patch('datadog_checks.base.utils.http.requests.Session', return_value=r):
r.get.return_value = mock.MagicMock(status_code=200, content='{}')

check.check(instance)

http_wargs = {
'auth': mock.ANY,
'cert': mock.ANY,
'headers': mock.ANY,
'proxies': mock.ANY,
'timeout': mock.ANY,
'verify': mock.ANY,
'allow_redirects': mock.ANY,
}
http_wargs.update(expected_http_kwargs)

r.get.assert_called_with('http://{}:5984/_all_dbs/'.format(common.HOST), **http_wargs)
for key, value in expected_http_kwargs.items():
assert check.http.options[key] == value


def test_new_version_system_metrics(load_test_data):
Expand Down
Loading
Loading