~hopem/charm-helpers/fix-ssl-disable

« back to all changes in this revision

Viewing changes to tests/contrib/network/test_ip.py

  • Committer: Jorge Niedbalski
  • Date: 2015-02-26 23:04:21 UTC
  • Revision ID: jorge.niedbalski@canonical.com-20150226230421-xe4dhoccwc1inqr1
Moves host<->ip translation logic to charmhelpers/contrib/network/ip.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import netifaces
6
6
 
7
7
import charmhelpers.contrib.network.ip as net_ip
8
 
from mock import patch
 
8
from mock import patch, MagicMock
 
9
 
9
10
import nose.tools
 
11
import six
 
12
 
 
13
if not six.PY3:
 
14
    builtin_open = '__builtin__.open'
 
15
    builtin_import = '__builtin__.__import__'
 
16
else:
 
17
    builtin_open = 'builtins.open'
 
18
    builtin_import = 'builtins.__import__'
10
19
 
11
20
DUMMY_ADDRESSES = {
12
21
    'lo': {
82
91
"""
83
92
 
84
93
 
 
94
class FakeAnswer(object):
 
95
    def __init__(self, ip):
 
96
        self.ip = ip
 
97
 
 
98
    def __str__(self):
 
99
        return self.ip
 
100
 
 
101
 
 
102
class FakeResolver(object):
 
103
    def __init__(self, ip):
 
104
        self.ip = ip
 
105
 
 
106
    def query(self, hostname, query_type):
 
107
        if self.ip == '':
 
108
            return []
 
109
        else:
 
110
            return [FakeAnswer(self.ip)]
 
111
 
 
112
 
 
113
class FakeReverse(object):
 
114
    def from_address(self, address):
 
115
        return '156.94.189.91.in-addr.arpa'
 
116
 
 
117
 
 
118
class FakeDNSName(object):
 
119
    def __init__(self, dnsname):
 
120
        pass
 
121
 
 
122
 
 
123
class FakeDNS(object):
 
124
    def __init__(self, ip):
 
125
        self.resolver = FakeResolver(ip)
 
126
        self.reversename = FakeReverse()
 
127
        self.name = MagicMock()
 
128
        self.name.Name = FakeDNSName
 
129
 
 
130
 
85
131
class IPTest(unittest.TestCase):
86
132
 
87
133
    def mock_ifaddresses(self, iface):
501
547
 
502
548
        with nose.tools.assert_raises(Exception):
503
549
            net_ip.get_iface_from_addr('1.2.3.4')
 
550
 
 
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'))
 
554
 
 
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')
 
561
 
 
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,
 
566
                                              ns_query):
 
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')
 
573
 
 
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,
 
579
                                                ns_query, *args):
 
580
        ns_query.return_value = []
 
581
        fake_dns = FakeDNS(None)
 
582
 
 
583
        def r():
 
584
            raise Exception()
 
585
 
 
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')
 
590
 
 
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')
 
597
 
 
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')
 
605
 
 
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')
 
612
 
 
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')
 
620
 
 
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)
 
627
 
 
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)
 
634
 
 
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')
 
641
 
 
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')
 
648
 
 
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')
 
653
 
 
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')
 
658
 
 
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')
 
663
 
 
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,
 
668
                                                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')
 
672
 
 
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)