502
548
with nose.tools.assert_raises(Exception):
503
549
net_ip.get_iface_from_addr('1.2.3.4')
551
def test_is_ip(self):
552
self.assertTrue(net_ip.is_ip('10.0.0.1'))
553
self.assertFalse(net_ip.is_ip('www.ubuntu.com'))
555
@patch('charmhelpers.contrib.network.ip.apt_install')
556
def test_get_host_ip_with_hostname(self, apt_install):
557
fake_dns = FakeDNS('10.0.0.1')
558
with patch(builtin_import, side_effect=[fake_dns]):
559
ip = net_ip.get_host_ip('www.ubuntu.com')
560
self.assertEquals(ip, '10.0.0.1')
562
@patch('charmhelpers.contrib.network.ip.ns_query')
563
@patch('charmhelpers.contrib.network.ip.socket.gethostbyname')
564
@patch('charmhelpers.contrib.network.ip.apt_install')
565
def test_get_host_ip_with_hostname_no_dns(self, apt_install, socket,
567
ns_query.return_value = []
568
fake_dns = FakeDNS(None)
569
socket.return_value = '10.0.0.1'
570
with patch(builtin_import, side_effect=[fake_dns]):
571
ip = net_ip.get_host_ip('www.ubuntu.com')
572
self.assertEquals(ip, '10.0.0.1')
574
@patch('charmhelpers.contrib.network.ip.log')
575
@patch('charmhelpers.contrib.network.ip.ns_query')
576
@patch('charmhelpers.contrib.network.ip.socket.gethostbyname')
577
@patch('charmhelpers.contrib.network.ip.apt_install')
578
def test_get_host_ip_with_hostname_fallback(self, apt_install, socket,
580
ns_query.return_value = []
581
fake_dns = FakeDNS(None)
586
socket.side_effect = r
587
with patch(builtin_import, side_effect=[fake_dns]):
588
ip = net_ip.get_host_ip('www.ubuntu.com', fallback='127.0.0.1')
589
self.assertEquals(ip, '127.0.0.1')
591
@patch('charmhelpers.contrib.network.ip.apt_install')
592
def test_get_host_ip_with_ip(self, apt_install):
593
fake_dns = FakeDNS('5.5.5.5')
594
with patch(builtin_import, side_effect=[fake_dns]):
595
ip = net_ip.get_host_ip('4.2.2.1')
596
self.assertEquals(ip, '4.2.2.1')
598
@patch('charmhelpers.contrib.network.ip.apt_install')
599
def test_ns_query_trigger_apt_install(self, apt_install):
600
fake_dns = FakeDNS('5.5.5.5')
601
with patch(builtin_import, side_effect=[ImportError, fake_dns]):
602
nsq = net_ip.ns_query('5.5.5.5')
603
apt_install.assert_called_with('python-dnspython')
604
self.assertEquals(nsq, '5.5.5.5')
606
@patch('charmhelpers.contrib.network.ip.apt_install')
607
def test_ns_query_ptr_record(self, apt_install):
608
fake_dns = FakeDNS('127.0.0.1')
609
with patch(builtin_import, side_effect=[fake_dns]):
610
nsq = net_ip.ns_query('127.0.0.1')
611
self.assertEquals(nsq, '127.0.0.1')
613
@patch('charmhelpers.contrib.network.ip.apt_install')
614
def test_ns_query_a_record(self, apt_install):
615
fake_dns = FakeDNS('127.0.0.1')
616
fake_dns_name = FakeDNSName('www.somedomain.tld')
617
with patch(builtin_import, side_effect=[fake_dns]):
618
nsq = net_ip.ns_query(fake_dns_name)
619
self.assertEquals(nsq, '127.0.0.1')
621
@patch('charmhelpers.contrib.network.ip.apt_install')
622
def test_ns_query_blank_record(self, apt_install):
623
fake_dns = FakeDNS(None)
624
with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
625
nsq = net_ip.ns_query(None)
626
self.assertEquals(nsq, None)
628
@patch('charmhelpers.contrib.network.ip.apt_install')
629
def test_ns_query_lookup_fail(self, apt_install):
630
fake_dns = FakeDNS('')
631
with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
632
nsq = net_ip.ns_query('nonexistant')
633
self.assertEquals(nsq, None)
635
@patch('charmhelpers.contrib.network.ip.apt_install')
636
def test_get_hostname_with_ip(self, apt_install):
637
fake_dns = FakeDNS('www.ubuntu.com')
638
with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
639
hn = net_ip.get_hostname('4.2.2.1')
640
self.assertEquals(hn, 'www.ubuntu.com')
642
@patch('charmhelpers.contrib.network.ip.apt_install')
643
def test_get_hostname_with_ip_not_fqdn(self, apt_install):
644
fake_dns = FakeDNS('packages.ubuntu.com')
645
with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
646
hn = net_ip.get_hostname('4.2.2.1', fqdn=False)
647
self.assertEquals(hn, 'packages')
649
@patch('charmhelpers.contrib.network.ip.apt_install')
650
def test_get_hostname_with_hostname(self, apt_install):
651
hn = net_ip.get_hostname('www.ubuntu.com')
652
self.assertEquals(hn, 'www.ubuntu.com')
654
@patch('charmhelpers.contrib.network.ip.apt_install')
655
def test_get_hostname_with_hostname_trailingdot(self, apt_install):
656
hn = net_ip.get_hostname('www.ubuntu.com.')
657
self.assertEquals(hn, 'www.ubuntu.com')
659
@patch('charmhelpers.contrib.network.ip.apt_install')
660
def test_get_hostname_with_hostname_not_fqdn(self, apt_install):
661
hn = net_ip.get_hostname('packages.ubuntu.com', fqdn=False)
662
self.assertEquals(hn, 'packages')
664
@patch('charmhelpers.contrib.network.ip.apt_install')
665
def test_get_hostname_trigger_apt_install(self, apt_install):
666
fake_dns = FakeDNS('www.ubuntu.com')
667
with patch(builtin_import, side_effect=[ImportError, fake_dns,
669
hn = net_ip.get_hostname('4.2.2.1')
670
apt_install.assert_called_with('python-dnspython')
671
self.assertEquals(hn, 'www.ubuntu.com')
673
@patch('charmhelpers.contrib.network.ip.ns_query')
674
@patch('charmhelpers.contrib.network.ip.apt_install')
675
def test_get_hostname_lookup_fail(self, apt_install, ns_query):
676
fake_dns = FakeDNS('www.ubuntu.com')
677
ns_query.return_value = []
678
with patch(builtin_import, side_effect=[fake_dns, fake_dns]):
679
hn = net_ip.get_hostname('4.2.2.1')
680
self.assertEquals(hn, None)