9
9
10
10
try :
11
11
import aiodns
12
+ gethostbyname = hasattr (aiodns .DNSResolver , 'gethostbyname' )
12
13
except ImportError :
13
14
aiodns = None
15
+ gethostbyname = False
14
16
15
17
16
18
class FakeResult :
19
+ def __init__ (self , addresses ):
20
+ self .addresses = addresses
21
+
22
+
23
+ class FakeQueryResult :
17
24
def __init__ (self , host ):
18
25
self .host = host
19
26
20
27
21
28
@asyncio .coroutine
22
- def fake_result (result ):
23
- return [FakeResult (host = h )
29
+ def fake_result (addresses ):
30
+ return FakeResult (addresses = tuple (addresses ))
31
+
32
+
33
+ @asyncio .coroutine
34
+ def fake_query_result (result ):
35
+ return [FakeQueryResult (host = h )
24
36
for h in result ]
25
37
26
38
@@ -36,23 +48,36 @@ def fake(*args, **kwargs):
36
48
return fake
37
49
38
50
39
- @pytest .mark .skipif (aiodns is None , reason = "aiodns required" )
51
+ @pytest .mark .skipif (not gethostbyname , reason = "aiodns 1.1 required" )
40
52
@asyncio .coroutine
41
53
def test_async_resolver_positive_lookup (loop ):
42
- with patch ('aiodns.DNSResolver.query ' ) as mock_query :
43
- mock_query .return_value = fake_result (['127.0.0.1' ])
54
+ with patch ('aiodns.DNSResolver' ) as mock :
55
+ mock (). gethostbyname .return_value = fake_result (['127.0.0.1' ])
44
56
resolver = AsyncResolver (loop = loop )
45
57
real = yield from resolver .resolve ('www.python.org' )
46
58
ipaddress .ip_address (real [0 ]['host' ])
47
- mock_query .assert_called_with ('www.python.org' , 'A' )
59
+ mock ().gethostbyname .assert_called_with ('www.python.org' ,
60
+ socket .AF_INET )
48
61
49
62
50
63
@pytest .mark .skipif (aiodns is None , reason = "aiodns required" )
51
64
@asyncio .coroutine
65
+ def test_async_resolver_query_positive_lookup (loop ):
66
+ with patch ('aiodns.DNSResolver' ) as mock :
67
+ del mock ().gethostbyname
68
+ mock ().query .return_value = fake_query_result (['127.0.0.1' ])
69
+ resolver = AsyncResolver (loop = loop )
70
+ real = yield from resolver .resolve ('www.python.org' )
71
+ ipaddress .ip_address (real [0 ]['host' ])
72
+ mock ().query .assert_called_with ('www.python.org' , 'A' )
73
+
74
+
75
+ @pytest .mark .skipif (not gethostbyname , reason = "aiodns 1.1 required" )
76
+ @asyncio .coroutine
52
77
def test_async_resolver_multiple_replies (loop ):
53
- with patch ('aiodns.DNSResolver.query ' ) as mock_query :
78
+ with patch ('aiodns.DNSResolver' ) as mock :
54
79
ips = ['127.0.0.1' , '127.0.0.2' , '127.0.0.3' , '127.0.0.4' ]
55
- mock_query .return_value = fake_result (ips )
80
+ mock (). gethostbyname .return_value = fake_result (ips )
56
81
resolver = AsyncResolver (loop = loop )
57
82
real = yield from resolver .resolve ('www.google.com' )
58
83
ips = [ipaddress .ip_address (x ['host' ]) for x in real ]
@@ -61,9 +86,32 @@ def test_async_resolver_multiple_replies(loop):
61
86
62
87
@pytest .mark .skipif (aiodns is None , reason = "aiodns required" )
63
88
@asyncio .coroutine
64
- def test_async_negative_lookup (loop ):
65
- with patch ('aiodns.DNSResolver.query' ) as mock_query :
66
- mock_query .side_effect = aiodns .error .DNSError ()
89
+ def test_async_resolver_query_multiple_replies (loop ):
90
+ with patch ('aiodns.DNSResolver' ) as mock :
91
+ del mock ().gethostbyname
92
+ ips = ['127.0.0.1' , '127.0.0.2' , '127.0.0.3' , '127.0.0.4' ]
93
+ mock ().query .return_value = fake_query_result (ips )
94
+ resolver = AsyncResolver (loop = loop )
95
+ real = yield from resolver .resolve ('www.google.com' )
96
+ ips = [ipaddress .ip_address (x ['host' ]) for x in real ]
97
+
98
+
99
+ @pytest .mark .skipif (not gethostbyname , reason = "aiodns 1.1 required" )
100
+ @asyncio .coroutine
101
+ def test_async_resolver_negative_lookup (loop ):
102
+ with patch ('aiodns.DNSResolver' ) as mock :
103
+ mock ().gethostbyname .side_effect = aiodns .error .DNSError ()
104
+ resolver = AsyncResolver (loop = loop )
105
+ with pytest .raises (aiodns .error .DNSError ):
106
+ yield from resolver .resolve ('doesnotexist.bla' )
107
+
108
+
109
+ @pytest .mark .skipif (aiodns is None , reason = "aiodns required" )
110
+ @asyncio .coroutine
111
+ def test_async_resolver_query_negative_lookup (loop ):
112
+ with patch ('aiodns.DNSResolver' ) as mock :
113
+ del mock ().gethostbyname
114
+ mock ().query .side_effect = aiodns .error .DNSError ()
67
115
resolver = AsyncResolver (loop = loop )
68
116
with pytest .raises (aiodns .error .DNSError ):
69
117
yield from resolver .resolve ('doesnotexist.bla' )
@@ -122,16 +170,30 @@ def test_default_loop_for_async_resolver(loop):
122
170
assert resolver ._loop is loop
123
171
124
172
125
- @pytest .mark .skipif (aiodns is None , reason = "aiodns required" )
173
+ @pytest .mark .skipif (not gethostbyname , reason = "aiodns 1.1 required" )
126
174
@asyncio .coroutine
127
175
def test_async_resolver_ipv6_positive_lookup (loop ):
128
- with patch ('aiodns.DNSResolver.query' ) as mock_query :
129
- mock_query .return_value = fake_result (['::1' ])
176
+ with patch ('aiodns.DNSResolver' ) as mock :
177
+ mock ().gethostbyname .return_value = fake_result (['::1' ])
178
+ resolver = AsyncResolver (loop = loop )
179
+ real = yield from resolver .resolve ('www.python.org' ,
180
+ family = socket .AF_INET6 )
181
+ ipaddress .ip_address (real [0 ]['host' ])
182
+ mock ().gethostbyname .assert_called_with ('www.python.org' ,
183
+ socket .AF_INET6 )
184
+
185
+
186
+ @pytest .mark .skipif (aiodns is None , reason = "aiodns required" )
187
+ @asyncio .coroutine
188
+ def test_async_resolver_query_ipv6_positive_lookup (loop ):
189
+ with patch ('aiodns.DNSResolver' ) as mock :
190
+ del mock ().gethostbyname
191
+ mock ().query .return_value = fake_query_result (['::1' ])
130
192
resolver = AsyncResolver (loop = loop )
131
193
real = yield from resolver .resolve ('www.python.org' ,
132
194
family = socket .AF_INET6 )
133
195
ipaddress .ip_address (real [0 ]['host' ])
134
- mock_query .assert_called_with ('www.python.org' , 'AAAA' )
196
+ mock (). query .assert_called_with ('www.python.org' , 'AAAA' )
135
197
136
198
137
199
def test_async_resolver_aiodns_not_present (loop , monkeypatch ):
0 commit comments