Skip to content

Commit

Permalink
Bump the dnspython version to 2.4.2 on Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentClarret committed Nov 6, 2023
1 parent 3b5473e commit b4e9c41
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 37 deletions.
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/16150.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bump the dnspython version to 2.4.2 on Python 3
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ cryptography==41.0.5; python_version > '3.0'
ddtrace==0.32.2; sys_platform == 'win32' and python_version < '3.0'
ddtrace==0.53.2; sys_platform != 'win32' and python_version < '3.0'
ddtrace==1.11.2; python_version > '3.0'
dnspython==1.16.0
dnspython==1.16.0; python_version < '3.0'
dnspython==2.4.2; python_version > '3.0'
enum34==1.1.10; python_version < '3.0'
foundationdb==6.3.24; python_version > '3.0'
futures==3.4.0; python_version < '3.0'
Expand Down
1 change: 1 addition & 0 deletions dns_check/changelog.d/16150.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bump the dnspython version to 2.4.2 on Python 3
8 changes: 6 additions & 2 deletions dns_check/datadog_checks/dns_check/dns_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List, Optional # noqa: F401

import dns.resolver
from six import PY3

from datadog_checks.base import AgentCheck, ConfigurationError
from datadog_checks.base.utils.time import get_precise_time
Expand Down Expand Up @@ -78,7 +79,10 @@ def check(self, _):
raise AssertionError("Expected an NXDOMAIN, got a result.")
else:
answer = resolver.query(self.hostname, rdtype=self.record_type) # dns.resolver.Answer
assert answer.rrset.items[0].to_text()

items = list(answer.rrset.items.keys()) if PY3 else answer.rrset.items
assert items[0].to_text()

if self.resolves_as_ips:
self._check_answer(answer)

Expand All @@ -105,7 +109,7 @@ def _check_answer(self, answer):

assert len(self.resolves_as_ips) == number_of_results
result_ips = []
for rip in answer.rrset.items:
for rip in answer.rrset.items.keys() if PY3 else answer.rrset.items:
result = rip.to_text().lower()
if result.endswith('.'):
result = result[:-1]
Expand Down
3 changes: 2 additions & 1 deletion dns_check/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ dynamic = [

[project.optional-dependencies]
deps = [
"dnspython==1.16.0",
"dnspython==1.16.0; python_version < '3.0'",
"dnspython==2.4.2; python_version > '3.0'",
]

[project.urls]
Expand Down
20 changes: 0 additions & 20 deletions dns_check/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,6 @@
'nameserver': '127.0.0.1',
}

CONFIG_INVALID = [
# invalid hostname
({'name': 'invalid_hostname', 'hostname': 'example'}, "DNS resolution of example has failed"),
# invalid nameserver
(
{'name': 'invalid_nameserver', 'hostname': 'www.example.org', 'nameserver': '0.0.0.0'},
"DNS resolution of www.example.org timed out",
),
# invalid record type
(
{'name': 'invalid_rcrd_type', 'hostname': 'www.example.org', 'record_type': 'FOO'},
"DNS resolution of www.example.org has failed",
),
# valid domain when NXDOMAIN is expected
(
{'name': 'valid_domain_for_nxdomain_type', 'hostname': 'example.com', 'record_type': 'NXDOMAIN'},
"DNS resolution of example.com has failed",
),
]

E2E_METADATA = {'docker_platform': 'windows' if using_windows_containers() else 'linux'}


Expand Down
10 changes: 5 additions & 5 deletions dns_check/tests/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under Simplified BSD License (see LICENSE)

from dns.resolver import NXDOMAIN
from six import PY3


class MockDNSAnswer:
Expand All @@ -13,12 +14,11 @@ class MockRrset:
def __init__(self, address):
addresses = [x.strip().lower() for x in address.split(',')]
if len(addresses) > 1:
items = []
for address in addresses:
items.append(MockDNSAnswer.MockItem(address))
self.items = items
items = [MockDNSAnswer.MockItem(address) for address in addresses]
else:
self.items = [MockDNSAnswer.MockItem(address)]
items = [MockDNSAnswer.MockItem(address)]

self.items = {item: None for item in items} if PY3 else items

class MockItem:
def __init__(self, address):
Expand Down
33 changes: 25 additions & 8 deletions dns_check/tests/test_dns_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,28 @@ def test_instance_timeout(mocked_query, mocked_time, aggregator):
aggregator.assert_all_metrics_covered()


def test_invalid_config(aggregator):
for instance, message in common.CONFIG_INVALID:
integration = DNSCheck('dns_check', {}, [instance])
integration.check(instance)
aggregator.assert_service_check(DNSCheck.SERVICE_CHECK_NAME, status=DNSCheck.CRITICAL, count=1, message=message)

# Assert coverage for this check on this instance
aggregator.assert_all_metrics_covered()
@pytest.mark.parametrize(
'instance, message',
[
pytest.param(
{'name': 'invalid_hostname', 'hostname': 'example'},
"DNS resolution of example has failed",
id='invalid hostname',
),
pytest.param(
{'name': 'invalid_rcrd_type', 'hostname': 'www.example.org', 'record_type': 'FOO'},
"DNS resolution of www.example.org has failed",
id='invalid record type',
),
pytest.param(
{'name': 'valid_domain_for_nxdomain_type', 'hostname': 'example.com', 'record_type': 'NXDOMAIN'},
"DNS resolution of example.com has failed",
id='valid domain when NXDOMAIN is expected',
),
],
)
def test_invalid_config(aggregator, instance, message):
integration = DNSCheck('dns_check', {}, [instance])
integration.check(instance)
aggregator.assert_service_check(DNSCheck.SERVICE_CHECK_NAME, status=DNSCheck.CRITICAL, count=1, message=message)
aggregator.assert_all_metrics_covered()

0 comments on commit b4e9c41

Please sign in to comment.