Merge lp:~niedbalski/charm-helpers/ip-host-translation into lp:charm-helpers
- ip-host-translation
- Merge into devel
Proposed by
Jorge Niedbalski
Status: | Merged |
---|---|
Merged at revision: | 326 |
Proposed branch: | lp:~niedbalski/charm-helpers/ip-host-translation |
Merge into: | lp:charm-helpers |
Diff against target: |
539 lines (+272/-177) 4 files modified
charmhelpers/contrib/network/ip.py (+84/-1) charmhelpers/contrib/openstack/utils.py (+6/-72) tests/contrib/network/test_ip.py (+178/-1) tests/contrib/openstack/test_openstack_utils.py (+4/-103) |
To merge this branch: | bzr merge lp:~niedbalski/charm-helpers/ip-host-translation |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Billy Olsen | Approve | ||
charmers | Pending | ||
Review via email:
|
Commit message
Description of the change
- Moved out all the host<->ip translation logic from contrib.openstack to contrib.network module.
- Re-factored the get_host_ip method to also check for the socket.
- Added tests for covering the changes.
To post a comment you must log in.
- 325. By Jorge Niedbalski
-
Minor lints
- 326. By Jorge Niedbalski
-
Minor lints
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'charmhelpers/contrib/network/ip.py' | |||
2 | --- charmhelpers/contrib/network/ip.py 2015-01-22 06:06:03 +0000 | |||
3 | +++ charmhelpers/contrib/network/ip.py 2015-02-26 23:24:37 +0000 | |||
4 | @@ -17,13 +17,16 @@ | |||
5 | 17 | import glob | 17 | import glob |
6 | 18 | import re | 18 | import re |
7 | 19 | import subprocess | 19 | import subprocess |
8 | 20 | import six | ||
9 | 21 | import socket | ||
10 | 20 | 22 | ||
11 | 21 | from functools import partial | 23 | from functools import partial |
12 | 22 | 24 | ||
13 | 23 | from charmhelpers.core.hookenv import unit_get | 25 | from charmhelpers.core.hookenv import unit_get |
14 | 24 | from charmhelpers.fetch import apt_install | 26 | from charmhelpers.fetch import apt_install |
15 | 25 | from charmhelpers.core.hookenv import ( | 27 | from charmhelpers.core.hookenv import ( |
17 | 26 | log | 28 | log, |
18 | 29 | WARNING, | ||
19 | 27 | ) | 30 | ) |
20 | 28 | 31 | ||
21 | 29 | try: | 32 | try: |
22 | @@ -365,3 +368,83 @@ | |||
23 | 365 | return True | 368 | return True |
24 | 366 | 369 | ||
25 | 367 | return False | 370 | return False |
26 | 371 | |||
27 | 372 | |||
28 | 373 | def is_ip(address): | ||
29 | 374 | """ | ||
30 | 375 | Returns True if address is a valid IP address. | ||
31 | 376 | """ | ||
32 | 377 | try: | ||
33 | 378 | # Test to see if already an IPv4 address | ||
34 | 379 | socket.inet_aton(address) | ||
35 | 380 | return True | ||
36 | 381 | except socket.error: | ||
37 | 382 | return False | ||
38 | 383 | |||
39 | 384 | |||
40 | 385 | def ns_query(address): | ||
41 | 386 | try: | ||
42 | 387 | import dns.resolver | ||
43 | 388 | except ImportError: | ||
44 | 389 | apt_install('python-dnspython') | ||
45 | 390 | import dns.resolver | ||
46 | 391 | |||
47 | 392 | if isinstance(address, dns.name.Name): | ||
48 | 393 | rtype = 'PTR' | ||
49 | 394 | elif isinstance(address, six.string_types): | ||
50 | 395 | rtype = 'A' | ||
51 | 396 | else: | ||
52 | 397 | return None | ||
53 | 398 | |||
54 | 399 | answers = dns.resolver.query(address, rtype) | ||
55 | 400 | if answers: | ||
56 | 401 | return str(answers[0]) | ||
57 | 402 | return None | ||
58 | 403 | |||
59 | 404 | |||
60 | 405 | def get_host_ip(hostname, fallback=None): | ||
61 | 406 | """ | ||
62 | 407 | Resolves the IP for a given hostname, or returns | ||
63 | 408 | the input if it is already an IP. | ||
64 | 409 | """ | ||
65 | 410 | if is_ip(hostname): | ||
66 | 411 | return hostname | ||
67 | 412 | |||
68 | 413 | ip_addr = ns_query(hostname) | ||
69 | 414 | if not ip_addr: | ||
70 | 415 | try: | ||
71 | 416 | ip_addr = socket.gethostbyname(hostname) | ||
72 | 417 | except: | ||
73 | 418 | log("Failed to resolve hostname '%s'" % (hostname), | ||
74 | 419 | level=WARNING) | ||
75 | 420 | return fallback | ||
76 | 421 | return ip_addr | ||
77 | 422 | |||
78 | 423 | |||
79 | 424 | def get_hostname(address, fqdn=True): | ||
80 | 425 | """ | ||
81 | 426 | Resolves hostname for given IP, or returns the input | ||
82 | 427 | if it is already a hostname. | ||
83 | 428 | """ | ||
84 | 429 | if is_ip(address): | ||
85 | 430 | try: | ||
86 | 431 | import dns.reversename | ||
87 | 432 | except ImportError: | ||
88 | 433 | apt_install("python-dnspython") | ||
89 | 434 | import dns.reversename | ||
90 | 435 | |||
91 | 436 | rev = dns.reversename.from_address(address) | ||
92 | 437 | result = ns_query(rev) | ||
93 | 438 | if not result: | ||
94 | 439 | return None | ||
95 | 440 | else: | ||
96 | 441 | result = address | ||
97 | 442 | |||
98 | 443 | if fqdn: | ||
99 | 444 | # strip trailing . | ||
100 | 445 | if result.endswith('.'): | ||
101 | 446 | return result[:-1] | ||
102 | 447 | else: | ||
103 | 448 | return result | ||
104 | 449 | else: | ||
105 | 450 | return result.split('.')[0] | ||
106 | 368 | 451 | ||
107 | === modified file 'charmhelpers/contrib/openstack/utils.py' | |||
108 | --- charmhelpers/contrib/openstack/utils.py 2015-02-16 21:55:34 +0000 | |||
109 | +++ charmhelpers/contrib/openstack/utils.py 2015-02-26 23:24:37 +0000 | |||
110 | @@ -23,12 +23,13 @@ | |||
111 | 23 | import subprocess | 23 | import subprocess |
112 | 24 | import json | 24 | import json |
113 | 25 | import os | 25 | import os |
114 | 26 | import socket | ||
115 | 27 | import sys | 26 | import sys |
116 | 28 | 27 | ||
117 | 29 | import six | 28 | import six |
118 | 30 | import yaml | 29 | import yaml |
119 | 31 | 30 | ||
120 | 31 | from charmhelpers.contrib.network import ip | ||
121 | 32 | |||
122 | 32 | from charmhelpers.core.hookenv import ( | 33 | from charmhelpers.core.hookenv import ( |
123 | 33 | config, | 34 | config, |
124 | 34 | log as juju_log, | 35 | log as juju_log, |
125 | @@ -421,77 +422,10 @@ | |||
126 | 421 | else: | 422 | else: |
127 | 422 | zap_disk(block_device) | 423 | zap_disk(block_device) |
128 | 423 | 424 | ||
200 | 424 | 425 | is_ip = ip.is_ip | |
201 | 425 | def is_ip(address): | 426 | ns_query = ip.ns_query |
202 | 426 | """ | 427 | get_host_ip = ip.get_host_ip |
203 | 427 | Returns True if address is a valid IP address. | 428 | get_hostname = ip.get_hostname |
133 | 428 | """ | ||
134 | 429 | try: | ||
135 | 430 | # Test to see if already an IPv4 address | ||
136 | 431 | socket.inet_aton(address) | ||
137 | 432 | return True | ||
138 | 433 | except socket.error: | ||
139 | 434 | return False | ||
140 | 435 | |||
141 | 436 | |||
142 | 437 | def ns_query(address): | ||
143 | 438 | try: | ||
144 | 439 | import dns.resolver | ||
145 | 440 | except ImportError: | ||
146 | 441 | apt_install('python-dnspython') | ||
147 | 442 | import dns.resolver | ||
148 | 443 | |||
149 | 444 | if isinstance(address, dns.name.Name): | ||
150 | 445 | rtype = 'PTR' | ||
151 | 446 | elif isinstance(address, six.string_types): | ||
152 | 447 | rtype = 'A' | ||
153 | 448 | else: | ||
154 | 449 | return None | ||
155 | 450 | |||
156 | 451 | answers = dns.resolver.query(address, rtype) | ||
157 | 452 | if answers: | ||
158 | 453 | return str(answers[0]) | ||
159 | 454 | return None | ||
160 | 455 | |||
161 | 456 | |||
162 | 457 | def get_host_ip(hostname): | ||
163 | 458 | """ | ||
164 | 459 | Resolves the IP for a given hostname, or returns | ||
165 | 460 | the input if it is already an IP. | ||
166 | 461 | """ | ||
167 | 462 | if is_ip(hostname): | ||
168 | 463 | return hostname | ||
169 | 464 | |||
170 | 465 | return ns_query(hostname) | ||
171 | 466 | |||
172 | 467 | |||
173 | 468 | def get_hostname(address, fqdn=True): | ||
174 | 469 | """ | ||
175 | 470 | Resolves hostname for given IP, or returns the input | ||
176 | 471 | if it is already a hostname. | ||
177 | 472 | """ | ||
178 | 473 | if is_ip(address): | ||
179 | 474 | try: | ||
180 | 475 | import dns.reversename | ||
181 | 476 | except ImportError: | ||
182 | 477 | apt_install('python-dnspython') | ||
183 | 478 | import dns.reversename | ||
184 | 479 | |||
185 | 480 | rev = dns.reversename.from_address(address) | ||
186 | 481 | result = ns_query(rev) | ||
187 | 482 | if not result: | ||
188 | 483 | return None | ||
189 | 484 | else: | ||
190 | 485 | result = address | ||
191 | 486 | |||
192 | 487 | if fqdn: | ||
193 | 488 | # strip trailing . | ||
194 | 489 | if result.endswith('.'): | ||
195 | 490 | return result[:-1] | ||
196 | 491 | else: | ||
197 | 492 | return result | ||
198 | 493 | else: | ||
199 | 494 | return result.split('.')[0] | ||
204 | 495 | 429 | ||
205 | 496 | 430 | ||
206 | 497 | def get_matchmaker_map(mm_file='/etc/oslo/matchmaker_ring.json'): | 431 | def get_matchmaker_map(mm_file='/etc/oslo/matchmaker_ring.json'): |
207 | 498 | 432 | ||
208 | === modified file 'tests/contrib/network/test_ip.py' | |||
209 | --- tests/contrib/network/test_ip.py 2014-11-25 15:07:02 +0000 | |||
210 | +++ tests/contrib/network/test_ip.py 2015-02-26 23:24:37 +0000 | |||
211 | @@ -5,8 +5,17 @@ | |||
212 | 5 | import netifaces | 5 | import netifaces |
213 | 6 | 6 | ||
214 | 7 | import charmhelpers.contrib.network.ip as net_ip | 7 | import charmhelpers.contrib.network.ip as net_ip |
216 | 8 | from mock import patch | 8 | from mock import patch, MagicMock |
217 | 9 | |||
218 | 9 | import nose.tools | 10 | import nose.tools |
219 | 11 | import six | ||
220 | 12 | |||
221 | 13 | if not six.PY3: | ||
222 | 14 | builtin_open = '__builtin__.open' | ||
223 | 15 | builtin_import = '__builtin__.__import__' | ||
224 | 16 | else: | ||
225 | 17 | builtin_open = 'builtins.open' | ||
226 | 18 | builtin_import = 'builtins.__import__' | ||
227 | 10 | 19 | ||
228 | 11 | DUMMY_ADDRESSES = { | 20 | DUMMY_ADDRESSES = { |
229 | 12 | 'lo': { | 21 | 'lo': { |
230 | @@ -82,6 +91,43 @@ | |||
231 | 82 | """ | 91 | """ |
232 | 83 | 92 | ||
233 | 84 | 93 | ||
234 | 94 | class FakeAnswer(object): | ||
235 | 95 | def __init__(self, ip): | ||
236 | 96 | self.ip = ip | ||
237 | 97 | |||
238 | 98 | def __str__(self): | ||
239 | 99 | return self.ip | ||
240 | 100 | |||
241 | 101 | |||
242 | 102 | class FakeResolver(object): | ||
243 | 103 | def __init__(self, ip): | ||
244 | 104 | self.ip = ip | ||
245 | 105 | |||
246 | 106 | def query(self, hostname, query_type): | ||
247 | 107 | if self.ip == '': | ||
248 | 108 | return [] | ||
249 | 109 | else: | ||
250 | 110 | return [FakeAnswer(self.ip)] | ||
251 | 111 | |||
252 | 112 | |||
253 | 113 | class FakeReverse(object): | ||
254 | 114 | def from_address(self, address): | ||
255 | 115 | return '156.94.189.91.in-addr.arpa' | ||
256 | 116 | |||
257 | 117 | |||
258 | 118 | class FakeDNSName(object): | ||
259 | 119 | def __init__(self, dnsname): | ||
260 | 120 | pass | ||
261 | 121 | |||
262 | 122 | |||
263 | 123 | class FakeDNS(object): | ||
264 | 124 | def __init__(self, ip): | ||
265 | 125 | self.resolver = FakeResolver(ip) | ||
266 | 126 | self.reversename = FakeReverse() | ||
267 | 127 | self.name = MagicMock() | ||
268 | 128 | self.name.Name = FakeDNSName | ||
269 | 129 | |||
270 | 130 | |||
271 | 85 | class IPTest(unittest.TestCase): | 131 | class IPTest(unittest.TestCase): |
272 | 86 | 132 | ||
273 | 87 | def mock_ifaddresses(self, iface): | 133 | def mock_ifaddresses(self, iface): |
274 | @@ -501,3 +547,134 @@ | |||
275 | 501 | 547 | ||
276 | 502 | with nose.tools.assert_raises(Exception): | 548 | with nose.tools.assert_raises(Exception): |
277 | 503 | net_ip.get_iface_from_addr('1.2.3.4') | 549 | net_ip.get_iface_from_addr('1.2.3.4') |
278 | 550 | |||
279 | 551 | def test_is_ip(self): | ||
280 | 552 | self.assertTrue(net_ip.is_ip('10.0.0.1')) | ||
281 | 553 | self.assertFalse(net_ip.is_ip('www.ubuntu.com')) | ||
282 | 554 | |||
283 | 555 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
284 | 556 | def test_get_host_ip_with_hostname(self, apt_install): | ||
285 | 557 | fake_dns = FakeDNS('10.0.0.1') | ||
286 | 558 | with patch(builtin_import, side_effect=[fake_dns]): | ||
287 | 559 | ip = net_ip.get_host_ip('www.ubuntu.com') | ||
288 | 560 | self.assertEquals(ip, '10.0.0.1') | ||
289 | 561 | |||
290 | 562 | @patch('charmhelpers.contrib.network.ip.ns_query') | ||
291 | 563 | @patch('charmhelpers.contrib.network.ip.socket.gethostbyname') | ||
292 | 564 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
293 | 565 | def test_get_host_ip_with_hostname_no_dns(self, apt_install, socket, | ||
294 | 566 | ns_query): | ||
295 | 567 | ns_query.return_value = [] | ||
296 | 568 | fake_dns = FakeDNS(None) | ||
297 | 569 | socket.return_value = '10.0.0.1' | ||
298 | 570 | with patch(builtin_import, side_effect=[fake_dns]): | ||
299 | 571 | ip = net_ip.get_host_ip('www.ubuntu.com') | ||
300 | 572 | self.assertEquals(ip, '10.0.0.1') | ||
301 | 573 | |||
302 | 574 | @patch('charmhelpers.contrib.network.ip.log') | ||
303 | 575 | @patch('charmhelpers.contrib.network.ip.ns_query') | ||
304 | 576 | @patch('charmhelpers.contrib.network.ip.socket.gethostbyname') | ||
305 | 577 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
306 | 578 | def test_get_host_ip_with_hostname_fallback(self, apt_install, socket, | ||
307 | 579 | ns_query, *args): | ||
308 | 580 | ns_query.return_value = [] | ||
309 | 581 | fake_dns = FakeDNS(None) | ||
310 | 582 | |||
311 | 583 | def r(): | ||
312 | 584 | raise Exception() | ||
313 | 585 | |||
314 | 586 | socket.side_effect = r | ||
315 | 587 | with patch(builtin_import, side_effect=[fake_dns]): | ||
316 | 588 | ip = net_ip.get_host_ip('www.ubuntu.com', fallback='127.0.0.1') | ||
317 | 589 | self.assertEquals(ip, '127.0.0.1') | ||
318 | 590 | |||
319 | 591 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
320 | 592 | def test_get_host_ip_with_ip(self, apt_install): | ||
321 | 593 | fake_dns = FakeDNS('5.5.5.5') | ||
322 | 594 | with patch(builtin_import, side_effect=[fake_dns]): | ||
323 | 595 | ip = net_ip.get_host_ip('4.2.2.1') | ||
324 | 596 | self.assertEquals(ip, '4.2.2.1') | ||
325 | 597 | |||
326 | 598 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
327 | 599 | def test_ns_query_trigger_apt_install(self, apt_install): | ||
328 | 600 | fake_dns = FakeDNS('5.5.5.5') | ||
329 | 601 | with patch(builtin_import, side_effect=[ImportError, fake_dns]): | ||
330 | 602 | nsq = net_ip.ns_query('5.5.5.5') | ||
331 | 603 | apt_install.assert_called_with('python-dnspython') | ||
332 | 604 | self.assertEquals(nsq, '5.5.5.5') | ||
333 | 605 | |||
334 | 606 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
335 | 607 | def test_ns_query_ptr_record(self, apt_install): | ||
336 | 608 | fake_dns = FakeDNS('127.0.0.1') | ||
337 | 609 | with patch(builtin_import, side_effect=[fake_dns]): | ||
338 | 610 | nsq = net_ip.ns_query('127.0.0.1') | ||
339 | 611 | self.assertEquals(nsq, '127.0.0.1') | ||
340 | 612 | |||
341 | 613 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
342 | 614 | def test_ns_query_a_record(self, apt_install): | ||
343 | 615 | fake_dns = FakeDNS('127.0.0.1') | ||
344 | 616 | fake_dns_name = FakeDNSName('www.somedomain.tld') | ||
345 | 617 | with patch(builtin_import, side_effect=[fake_dns]): | ||
346 | 618 | nsq = net_ip.ns_query(fake_dns_name) | ||
347 | 619 | self.assertEquals(nsq, '127.0.0.1') | ||
348 | 620 | |||
349 | 621 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
350 | 622 | def test_ns_query_blank_record(self, apt_install): | ||
351 | 623 | fake_dns = FakeDNS(None) | ||
352 | 624 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
353 | 625 | nsq = net_ip.ns_query(None) | ||
354 | 626 | self.assertEquals(nsq, None) | ||
355 | 627 | |||
356 | 628 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
357 | 629 | def test_ns_query_lookup_fail(self, apt_install): | ||
358 | 630 | fake_dns = FakeDNS('') | ||
359 | 631 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
360 | 632 | nsq = net_ip.ns_query('nonexistant') | ||
361 | 633 | self.assertEquals(nsq, None) | ||
362 | 634 | |||
363 | 635 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
364 | 636 | def test_get_hostname_with_ip(self, apt_install): | ||
365 | 637 | fake_dns = FakeDNS('www.ubuntu.com') | ||
366 | 638 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
367 | 639 | hn = net_ip.get_hostname('4.2.2.1') | ||
368 | 640 | self.assertEquals(hn, 'www.ubuntu.com') | ||
369 | 641 | |||
370 | 642 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
371 | 643 | def test_get_hostname_with_ip_not_fqdn(self, apt_install): | ||
372 | 644 | fake_dns = FakeDNS('packages.ubuntu.com') | ||
373 | 645 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
374 | 646 | hn = net_ip.get_hostname('4.2.2.1', fqdn=False) | ||
375 | 647 | self.assertEquals(hn, 'packages') | ||
376 | 648 | |||
377 | 649 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
378 | 650 | def test_get_hostname_with_hostname(self, apt_install): | ||
379 | 651 | hn = net_ip.get_hostname('www.ubuntu.com') | ||
380 | 652 | self.assertEquals(hn, 'www.ubuntu.com') | ||
381 | 653 | |||
382 | 654 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
383 | 655 | def test_get_hostname_with_hostname_trailingdot(self, apt_install): | ||
384 | 656 | hn = net_ip.get_hostname('www.ubuntu.com.') | ||
385 | 657 | self.assertEquals(hn, 'www.ubuntu.com') | ||
386 | 658 | |||
387 | 659 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
388 | 660 | def test_get_hostname_with_hostname_not_fqdn(self, apt_install): | ||
389 | 661 | hn = net_ip.get_hostname('packages.ubuntu.com', fqdn=False) | ||
390 | 662 | self.assertEquals(hn, 'packages') | ||
391 | 663 | |||
392 | 664 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
393 | 665 | def test_get_hostname_trigger_apt_install(self, apt_install): | ||
394 | 666 | fake_dns = FakeDNS('www.ubuntu.com') | ||
395 | 667 | with patch(builtin_import, side_effect=[ImportError, fake_dns, | ||
396 | 668 | fake_dns]): | ||
397 | 669 | hn = net_ip.get_hostname('4.2.2.1') | ||
398 | 670 | apt_install.assert_called_with('python-dnspython') | ||
399 | 671 | self.assertEquals(hn, 'www.ubuntu.com') | ||
400 | 672 | |||
401 | 673 | @patch('charmhelpers.contrib.network.ip.ns_query') | ||
402 | 674 | @patch('charmhelpers.contrib.network.ip.apt_install') | ||
403 | 675 | def test_get_hostname_lookup_fail(self, apt_install, ns_query): | ||
404 | 676 | fake_dns = FakeDNS('www.ubuntu.com') | ||
405 | 677 | ns_query.return_value = [] | ||
406 | 678 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
407 | 679 | hn = net_ip.get_hostname('4.2.2.1') | ||
408 | 680 | self.assertEquals(hn, None) | ||
409 | 504 | 681 | ||
410 | === modified file 'tests/contrib/openstack/test_openstack_utils.py' | |||
411 | --- tests/contrib/openstack/test_openstack_utils.py 2014-12-03 13:27:17 +0000 | |||
412 | +++ tests/contrib/openstack/test_openstack_utils.py 2015-02-26 23:24:37 +0000 | |||
413 | @@ -595,107 +595,6 @@ | |||
414 | 595 | openstack.clean_storage('/dev/vdb') | 595 | openstack.clean_storage('/dev/vdb') |
415 | 596 | zap_disk.assert_called_with('/dev/vdb') | 596 | zap_disk.assert_called_with('/dev/vdb') |
416 | 597 | 597 | ||
417 | 598 | def test_is_ip(self): | ||
418 | 599 | self.assertTrue(openstack.is_ip('10.0.0.1')) | ||
419 | 600 | self.assertFalse(openstack.is_ip('www.ubuntu.com')) | ||
420 | 601 | |||
421 | 602 | @patch.object(openstack, 'apt_install') | ||
422 | 603 | def test_get_host_ip_with_hostname(self, apt_install): | ||
423 | 604 | fake_dns = FakeDNS('10.0.0.1') | ||
424 | 605 | with patch(builtin_import, side_effect=[fake_dns]): | ||
425 | 606 | ip = openstack.get_host_ip('www.ubuntu.com') | ||
426 | 607 | self.assertEquals(ip, '10.0.0.1') | ||
427 | 608 | |||
428 | 609 | @patch.object(openstack, 'apt_install') | ||
429 | 610 | def test_get_host_ip_with_ip(self, apt_install): | ||
430 | 611 | fake_dns = FakeDNS('5.5.5.5') | ||
431 | 612 | with patch(builtin_import, side_effect=[fake_dns]): | ||
432 | 613 | ip = openstack.get_host_ip('4.2.2.1') | ||
433 | 614 | self.assertEquals(ip, '4.2.2.1') | ||
434 | 615 | |||
435 | 616 | @patch.object(openstack, 'apt_install') | ||
436 | 617 | def test_ns_query_trigger_apt_install(self, apt_install): | ||
437 | 618 | fake_dns = FakeDNS('5.5.5.5') | ||
438 | 619 | with patch(builtin_import, side_effect=[ImportError, fake_dns]): | ||
439 | 620 | nsq = openstack.ns_query('5.5.5.5') | ||
440 | 621 | apt_install.assert_called_with('python-dnspython') | ||
441 | 622 | self.assertEquals(nsq, '5.5.5.5') | ||
442 | 623 | |||
443 | 624 | @patch.object(openstack, 'apt_install') | ||
444 | 625 | def test_ns_query_ptr_record(self, apt_install): | ||
445 | 626 | fake_dns = FakeDNS('127.0.0.1') | ||
446 | 627 | with patch(builtin_import, side_effect=[fake_dns]): | ||
447 | 628 | nsq = openstack.ns_query('127.0.0.1') | ||
448 | 629 | self.assertEquals(nsq, '127.0.0.1') | ||
449 | 630 | |||
450 | 631 | @patch.object(openstack, 'apt_install') | ||
451 | 632 | def test_ns_query_a_record(self, apt_install): | ||
452 | 633 | fake_dns = FakeDNS('127.0.0.1') | ||
453 | 634 | fake_dns_name = FakeDNSName('www.somedomain.tld') | ||
454 | 635 | with patch(builtin_import, side_effect=[fake_dns]): | ||
455 | 636 | nsq = openstack.ns_query(fake_dns_name) | ||
456 | 637 | self.assertEquals(nsq, '127.0.0.1') | ||
457 | 638 | |||
458 | 639 | @patch.object(openstack, 'apt_install') | ||
459 | 640 | def test_ns_query_blank_record(self, apt_install): | ||
460 | 641 | fake_dns = FakeDNS(None) | ||
461 | 642 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
462 | 643 | nsq = openstack.ns_query(None) | ||
463 | 644 | self.assertEquals(nsq, None) | ||
464 | 645 | |||
465 | 646 | @patch.object(openstack, 'apt_install') | ||
466 | 647 | def test_ns_query_lookup_fail(self, apt_install): | ||
467 | 648 | fake_dns = FakeDNS('') | ||
468 | 649 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
469 | 650 | nsq = openstack.ns_query('nonexistant') | ||
470 | 651 | self.assertEquals(nsq, None) | ||
471 | 652 | |||
472 | 653 | @patch.object(openstack, 'apt_install') | ||
473 | 654 | def test_get_hostname_with_ip(self, apt_install): | ||
474 | 655 | fake_dns = FakeDNS('www.ubuntu.com') | ||
475 | 656 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
476 | 657 | hn = openstack.get_hostname('4.2.2.1') | ||
477 | 658 | self.assertEquals(hn, 'www.ubuntu.com') | ||
478 | 659 | |||
479 | 660 | @patch.object(openstack, 'apt_install') | ||
480 | 661 | def test_get_hostname_with_ip_not_fqdn(self, apt_install): | ||
481 | 662 | fake_dns = FakeDNS('packages.ubuntu.com') | ||
482 | 663 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
483 | 664 | hn = openstack.get_hostname('4.2.2.1', fqdn=False) | ||
484 | 665 | self.assertEquals(hn, 'packages') | ||
485 | 666 | |||
486 | 667 | @patch.object(openstack, 'apt_install') | ||
487 | 668 | def test_get_hostname_with_hostname(self, apt_install): | ||
488 | 669 | hn = openstack.get_hostname('www.ubuntu.com') | ||
489 | 670 | self.assertEquals(hn, 'www.ubuntu.com') | ||
490 | 671 | |||
491 | 672 | @patch.object(openstack, 'apt_install') | ||
492 | 673 | def test_get_hostname_with_hostname_trailingdot(self, apt_install): | ||
493 | 674 | hn = openstack.get_hostname('www.ubuntu.com.') | ||
494 | 675 | self.assertEquals(hn, 'www.ubuntu.com') | ||
495 | 676 | |||
496 | 677 | @patch.object(openstack, 'apt_install') | ||
497 | 678 | def test_get_hostname_with_hostname_not_fqdn(self, apt_install): | ||
498 | 679 | hn = openstack.get_hostname('packages.ubuntu.com', fqdn=False) | ||
499 | 680 | self.assertEquals(hn, 'packages') | ||
500 | 681 | |||
501 | 682 | @patch.object(openstack, 'apt_install') | ||
502 | 683 | def test_get_hostname_trigger_apt_install(self, apt_install): | ||
503 | 684 | fake_dns = FakeDNS('www.ubuntu.com') | ||
504 | 685 | with patch(builtin_import, side_effect=[ImportError, fake_dns, fake_dns]): | ||
505 | 686 | hn = openstack.get_hostname('4.2.2.1') | ||
506 | 687 | apt_install.assert_called_with('python-dnspython') | ||
507 | 688 | self.assertEquals(hn, 'www.ubuntu.com') | ||
508 | 689 | |||
509 | 690 | @patch.object(openstack, 'ns_query') | ||
510 | 691 | @patch.object(openstack, 'apt_install') | ||
511 | 692 | def test_get_hostname_lookup_fail(self, apt_install, ns_query): | ||
512 | 693 | fake_dns = FakeDNS('www.ubuntu.com') | ||
513 | 694 | ns_query.return_value = [] | ||
514 | 695 | with patch(builtin_import, side_effect=[fake_dns, fake_dns]): | ||
515 | 696 | hn = openstack.get_hostname('4.2.2.1') | ||
516 | 697 | self.assertEquals(hn, None) | ||
517 | 698 | |||
518 | 699 | @patch('os.path.isfile') | 598 | @patch('os.path.isfile') |
519 | 700 | @patch(builtin_open) | 599 | @patch(builtin_open) |
520 | 701 | def test_get_matchmaker_map(self, _open, _isfile): | 600 | def test_get_matchmaker_map(self, _open, _isfile): |
521 | @@ -846,7 +745,8 @@ | |||
522 | 846 | 745 | ||
523 | 847 | openstack._git_clone_and_install_single(repo, branch) | 746 | openstack._git_clone_and_install_single(repo, branch) |
524 | 848 | mkdir.assert_called_with(dest_parent_dir) | 747 | mkdir.assert_called_with(dest_parent_dir) |
526 | 849 | install_remote.assert_called_with(repo, dest=dest_parent_dir, branch=branch) | 748 | install_remote.assert_called_with(repo, dest=dest_parent_dir, |
527 | 749 | branch=branch) | ||
528 | 850 | assert not _git_update_reqs.called | 750 | assert not _git_update_reqs.called |
529 | 851 | pip_install.assert_called_with(dest_dir) | 751 | pip_install.assert_called_with(dest_dir) |
530 | 852 | 752 | ||
531 | @@ -871,7 +771,8 @@ | |||
532 | 871 | 771 | ||
533 | 872 | openstack._git_clone_and_install_single(repo, branch, True) | 772 | openstack._git_clone_and_install_single(repo, branch, True) |
534 | 873 | mkdir.assert_called_with(dest_parent_dir) | 773 | mkdir.assert_called_with(dest_parent_dir) |
536 | 874 | install_remote.assert_called_with(repo, dest=dest_parent_dir, branch=branch) | 774 | install_remote.assert_called_with(repo, dest=dest_parent_dir, |
537 | 775 | branch=branch) | ||
538 | 875 | _git_update_reqs.assert_called_with(dest_dir, reqs_dir) | 776 | _git_update_reqs.assert_called_with(dest_dir, reqs_dir) |
539 | 876 | pip_install.assert_called_with(dest_dir) | 777 | pip_install.assert_called_with(dest_dir) |
540 | 877 | 778 |
LGTM approve