Skip to content

Commit

Permalink
[AGENT-10055] Correct checking instance type of get data response
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-datadog committed Aug 14, 2023
1 parent eb67d39 commit a00153b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
4 changes: 4 additions & 0 deletions elastic/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

***Fixed***:

* [AGENT-10055] Correct checking instance type of get data response ([#15554](https://github.com/DataDog/integrations-core/pull/15554))

## 6.0.0 / 2023-08-10

***Changed***:
Expand Down
4 changes: 2 additions & 2 deletions elastic/datadog_checks/elastic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
from .__about__ import __version__
from .elastic import ESCheck
from .elastic import ESCheck, AuthenticationError

__all__ = ['__version__', 'ESCheck']
__all__ = ['__version__', 'ESCheck', 'AuthenticationError']
2 changes: 1 addition & 1 deletion elastic/datadog_checks/elastic/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _get_data(self, url, send_sc=True, data=None):
resp.raise_for_status()
except Exception as e:
# this means we've hit a particular kind of auth error that means the config is broken
if resp and resp.status_code == 400:
if isinstance(resp, requests.Response) and resp.status_code == 400:
raise AuthenticationError("The ElasticSearch credentials are incorrect")

if send_sc:
Expand Down
32 changes: 31 additions & 1 deletion elastic/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from datadog_checks.base import ConfigurationError
from datadog_checks.dev.http import MockResponse
from datadog_checks.elastic import ESCheck
from datadog_checks.elastic import AuthenticationError, ESCheck
from datadog_checks.elastic.elastic import get_value_from_path

from .common import URL, get_fixture_path
Expand Down Expand Up @@ -145,3 +145,33 @@ def test_get_template_metrics_raise_exception(aggregator, instance):
def test_get_value_from_path():
value = get_value_from_path({"5": {"b": [0, 1, 2, {"a": ["foo"]}]}}, "5.b.3.a.0")
assert value == "foo"


def test__get_data_throws_authentication_error(instance):
with mock.patch(
'requests.get',
return_value=MockResponse(status_code=400),
):
check = ESCheck('elastic', {}, instances=[instance])

with pytest.raises(AuthenticationError):
check._get_data(url='test.com')


@mock.patch.dict('os.environ', {'DDEV_SKIP_GENERIC_TAGS_CHECK': 'true'})
def test__get_data_creates_critical_service_alert(aggregator, instance):
with mock.patch(
'requests.get',
return_value=MockResponse(status_code=500),
):
check = ESCheck('elastic', {}, instances=[instance])

with pytest.raises(Exception):
check._get_data(url='test.com')

aggregator.assert_service_check(
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",
)

0 comments on commit a00153b

Please sign in to comment.