Skip to content

Commit

Permalink
Fix #2977: Drop aiodns<1.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Sep 14, 2019
1 parent 58bfa07 commit fdf3b6a
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 86 deletions.
31 changes: 0 additions & 31 deletions aiohttp/resolver.py
Expand Up @@ -52,10 +52,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self._loop = get_running_loop()
self._resolver = aiodns.DNSResolver(*args, loop=self._loop, **kwargs)

if not hasattr(self._resolver, 'gethostbyname'):
# aiodns 1.1 is not available, fallback to DNSResolver.query
self.resolve = self._resolve_with_query # type: ignore

async def resolve(self, host: str, port: int=0,
family: int=socket.AF_INET) -> List[Dict[str, Any]]:
try:
Expand All @@ -76,33 +72,6 @@ async def resolve(self, host: str, port: int=0,

return hosts

async def _resolve_with_query(
self, host: str, port: int=0,
family: int=socket.AF_INET) -> List[Dict[str, Any]]:
if family == socket.AF_INET6:
qtype = 'AAAA'
else:
qtype = 'A'

try:
resp = await self._resolver.query(host, qtype)
except aiodns.error.DNSError as exc:
msg = exc.args[1] if len(exc.args) >= 1 else "DNS lookup failed"
raise OSError(msg) from exc

hosts = []
for rr in resp:
hosts.append(
{'hostname': host,
'host': rr.host, 'port': port,
'family': family, 'proto': 0,
'flags': socket.AI_NUMERICHOST})

if not hosts:
raise OSError("DNS lookup failed")

return hosts

async def close(self) -> None:
return self._resolver.cancel()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -134,7 +134,7 @@ def read(f):
install_requires=install_requires,
extras_require={
'speedups': [
'aiodns',
'aiodns>=1.1',
'Brotli',
'cchardet',
],
Expand Down
54 changes: 0 additions & 54 deletions tests/test_resolver.py
Expand Up @@ -56,17 +56,6 @@ async def test_async_resolver_positive_lookup(loop) -> None:
socket.AF_INET)


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
async def test_async_resolver_query_positive_lookup(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
del mock().gethostbyname
mock().query.return_value = fake_query_result(['127.0.0.1'])
resolver = AsyncResolver()
real = await resolver.resolve('www.python.org')
ipaddress.ip_address(real[0]['host'])
mock().query.assert_called_with('www.python.org', 'A')


@pytest.mark.skipif(not gethostbyname, reason="aiodns 1.1 required")
async def test_async_resolver_multiple_replies(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
Expand All @@ -78,17 +67,6 @@ async def test_async_resolver_multiple_replies(loop) -> None:
assert len(ips) > 3, "Expecting multiple addresses"


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
async def test_async_resolver_query_multiple_replies(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
del mock().gethostbyname
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']
mock().query.return_value = fake_query_result(ips)
resolver = AsyncResolver()
real = await resolver.resolve('www.google.com')
ips = [ipaddress.ip_address(x['host']) for x in real]


@pytest.mark.skipif(not gethostbyname, reason="aiodns 1.1 required")
async def test_async_resolver_negative_lookup(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
Expand All @@ -98,26 +76,6 @@ async def test_async_resolver_negative_lookup(loop) -> None:
await resolver.resolve('doesnotexist.bla')


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
async def test_async_resolver_query_negative_lookup(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
del mock().gethostbyname
mock().query.side_effect = aiodns.error.DNSError()
resolver = AsyncResolver()
with pytest.raises(OSError):
await resolver.resolve('doesnotexist.bla')


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
async def test_async_resolver_no_hosts_in_query(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
del mock().gethostbyname
mock().query.return_value = fake_query_result([])
resolver = AsyncResolver()
with pytest.raises(OSError):
await resolver.resolve('doesnotexist.bla')


@pytest.mark.skipif(not gethostbyname, reason="aiodns 1.1 required")
async def test_async_resolver_no_hosts_in_gethostbyname(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
Expand Down Expand Up @@ -193,18 +151,6 @@ async def test_async_resolver_ipv6_positive_lookup(loop) -> None:
socket.AF_INET6)


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
async def test_async_resolver_query_ipv6_positive_lookup(loop) -> None:
with patch('aiodns.DNSResolver') as mock:
del mock().gethostbyname
mock().query.return_value = fake_query_result(['::1'])
resolver = AsyncResolver()
real = await resolver.resolve('www.python.org',
family=socket.AF_INET6)
ipaddress.ip_address(real[0]['host'])
mock().query.assert_called_with('www.python.org', 'AAAA')


async def test_async_resolver_aiodns_not_present(loop, monkeypatch) -> None:
monkeypatch.setattr("aiohttp.resolver.aiodns", None)
with pytest.raises(RuntimeError):
Expand Down

0 comments on commit fdf3b6a

Please sign in to comment.