pvl/hosts/tests.py
author Tero Marttila <tero.marttila@aalto.fi>
Wed, 25 Feb 2015 14:52:53 +0200
changeset 464 f1d3dbf04ca3
parent 463 2cbdb2435487
child 465 133f14810eb5
permissions -rw-r--r--
pvl.hosts.zone: fix reverse= to be IPv4-only
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     1
import ipaddr
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     2
import unittest
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     3
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
     4
from pvl.hosts import config, host, zone
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     5
from StringIO import StringIO
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     6
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     7
class Options(object):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     8
    hosts_charset   = 'utf-8'
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     9
    hosts_domain    = None
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    10
    hosts_include   = None
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    11
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    12
class ConfFile(StringIO):
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    13
    def __init__(self, name, buffer):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    14
        StringIO.__init__(self, buffer)
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    15
        self.name = name
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    16
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    17
class TestConfig(unittest.TestCase):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    18
    def setUp(self):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    19
        self.options = Options()
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    20
461
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    21
    def assertHostEqual(self, host, host_str, attrs):
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    22
        self.assertEquals(str(host), host_str)
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    23
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    24
        for attr, value in attrs.iteritems():
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    25
            self.assertEquals(getattr(host, attr), value)
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    26
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    27
    def assertHostsEqual(self, hosts, expected):
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    28
        for host, expect in zip(hosts, expected):
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    29
            host_str, attrs = expect
441
f058fff1f272 pvl.hosts.hosts: fix sort_key()
Tero Marttila <tero.marttila@aalto.fi>
parents: 440
diff changeset
    30
461
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    31
            self.assertHostEqual(host, host_str, attrs)
441
f058fff1f272 pvl.hosts.hosts: fix sort_key()
Tero Marttila <tero.marttila@aalto.fi>
parents: 440
diff changeset
    32
 
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    33
    def testApplyHostsFileError(self):
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    34
        with self.assertRaises(config.HostConfigError):
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    35
            list(config.apply_hosts(self.options, ['nonexistant']))
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    36
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    37
    def testApplyHosts(self):
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    38
        conf_file = ConfFile('test', """
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    39
[foo]
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    40
    ip = 127.0.0.1
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    41
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    42
[bar]
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    43
    ip = 127.0.0.2
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    44
        """)
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    45
        expected = [
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    46
                ('foo@test', dict(ip=ipaddr.IPAddress('127.0.0.1'))),
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    47
                ('bar@test', dict(ip=ipaddr.IPAddress('127.0.0.2'))),
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    48
        ]
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    49
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    50
        self.assertHostsEqual(config.apply_hosts_file(self.options, conf_file), expected)
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    51
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    52
    def testApply(self):
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    53
        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    54
                ('foo@test', dict(
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    55
                    ip          = ipaddr.IPAddress('127.0.0.1'),
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    56
                    ethernet    = {None: '00:11:22:33:44:55'},
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    57
                )),
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    58
                ('bar@test', dict(
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    59
                    ip          = ipaddr.IPAddress('127.0.0.2'),
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    60
                    ethernet    = {None: '01:23:45:67:89:ab'},
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    61
                )),
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    62
        ])
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    63
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    64
    def testApplyHostsExpand(self):
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
    65
        self.assertHostsEqual(config.apply_host_config(self.options, 'asdf', 'asdf{1-3}', ip='10.100.100.$'), [
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    66
                ('asdf1@asdf', dict(ip=ipaddr.IPAddress('10.100.100.1'))),
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    67
                ('asdf2@asdf', dict(ip=ipaddr.IPAddress('10.100.100.2'))),
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    68
                ('asdf3@asdf', dict(ip=ipaddr.IPAddress('10.100.100.3'))),
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    69
        ])
461
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    70
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    71
    def testApplyHostConfigDict(self):
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    72
        host = config.apply_host(self.options, 'foo', 'test', {
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    73
            'ethernet.eth0': '00:11:22:33:44:55',
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    74
        })
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    75
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    76
        self.assertHostEqual(host, 'foo@test', dict(
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    77
                ethernet    = { 'eth0': '00:11:22:33:44:55' }
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    78
        ))
e3bddc5eeff5 pvl.hosts: test config.apply_host()
Tero Marttila <tero.marttila@aalto.fi>
parents: 451
diff changeset
    79
   
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    80
    def testApplyHostsConfigError(self):
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    81
        with self.assertRaises(config.HostConfigError):
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    82
            config.apply_host(self.options, 'foo', 'test', {
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    83
                'ethernet': 'foo',
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    84
                'ethernet.eth0': 'bar',
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    85
            })
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    86
462
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
    87
class TestForwardZone(unittest.TestCase):
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
    88
    def setUp(self):
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
    89
        self.options = Options()
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
    90
        self.options.add_origin = False
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
    91
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    92
    def assertZoneEquals(self, rrs, expected):
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    93
        gather = { }
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    94
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    95
        for rr in rrs:
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    96
            key = (rr.name.lower(), rr.type.upper())
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    97
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    98
            self.assertNotIn(key, gather)
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
    99
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   100
            gather[key] = rr.data
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   101
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   102
        self.assertDictEqual(gather, expected)
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   103
462
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   104
    def testResolve(self):
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   105
        self.assertEquals(zone.resolve(None, None, 'host'), 'host.')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   106
        self.assertEquals(zone.resolve(None, 'domain', 'host'), 'host.domain.')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   107
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   108
        with self.assertRaises(zone.HostZoneError):
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   109
            zone.resolve('origin', 'domain', 'host')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   110
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   111
        self.assertEquals(zone.resolve('domain', 'domain', 'host'), 'host')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   112
        self.assertEquals(zone.resolve('origin', 'domain.origin', 'host'), 'host.domain')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   113
        
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   114
        with self.assertRaises(zone.HostZoneError):
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   115
            zone.resolve('origin', 'domainorigin', 'host')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   116
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   117
        with self.assertRaises(zone.HostZoneError):
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   118
            zone.resolve('origin', None, 'host.domain')
6d699c76d75d pvl.hosts.zone: test resolve() and handle FQDN's strictly
Tero Marttila <tero.marttila@aalto.fi>
parents: 461
diff changeset
   119
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   120
    def testHostForwardOutOfOrigin(self):
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   121
        h = host.Host('host', 'domain', ip=ipaddr.IPAddress('10.0.0.1'))
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   122
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   123
        self.assertZoneEquals(zone.host_forward(h, 'test'), { })
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   124
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   125
    def testHostIP(self):
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   126
        h = host.Host.build('host', 'domain',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   127
                ip  = '192.0.2.1',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   128
                ip6 = '2001:db8::192.0.2.1',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   129
        )
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   130
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   131
        self.assertZoneEquals(zone.host_forward(h, 'domain'), {
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   132
            ('host', 'A'): ['192.0.2.1'],
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   133
            ('host', 'AAAA'): ['2001:db8::c000:201'],
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   134
        })
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   135
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   136
        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   137
            ('1', 'PTR'): ['host.domain.'],
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   138
        })
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   139
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   140
        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   141
            ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): ['host.domain.'],
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   142
        })
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   143
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   144
    def testHostDelegate(self):
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   145
        h = host.Host.build('host', 'example.com',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   146
                ip      = '192.0.2.1',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   147
                ip6     = '2001:db8::192.0.2.1',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   148
                forward = 'host.example.net',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   149
                reverse = '1.0/28.2.0.192.in-addr.arpa',
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   150
        )
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   151
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   152
        self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   153
            ('host', 'CNAME'): ['host.example.net.'],
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   154
        })
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   155
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   156
        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   157
            ('1', 'CNAME'): ['1.0/28.2.0.192.in-addr.arpa.'],
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   158
        })
464
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   159
        
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   160
        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   161
f1d3dbf04ca3 pvl.hosts.zone: fix reverse= to be IPv4-only
Tero Marttila <tero.marttila@aalto.fi>
parents: 463
diff changeset
   162
        })
463
2cbdb2435487 pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
Tero Marttila <tero.marttila@aalto.fi>
parents: 462
diff changeset
   163
440
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   164
if __name__ == '__main__':
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
   165
    unittest.main()