pvl/hosts/tests.py
author Tero Marttila <tero.marttila@aalto.fi>
Wed, 25 Feb 2015 14:29:40 +0200
changeset 462 6d699c76d75d
parent 461 e3bddc5eeff5
child 463 2cbdb2435487
permissions -rw-r--r--
pvl.hosts.zone: test resolve() and handle FQDN's strictly
import ipaddr
import unittest

from pvl.hosts import config, zone
from StringIO import StringIO

class Options(object):
    hosts_charset   = 'utf-8'
    hosts_domain    = None
    hosts_include   = None

class ConfFile(StringIO):
    def __init__(self, name, buffer):
        StringIO.__init__(self, buffer)
        self.name = name

class TestConfig(unittest.TestCase):
    def setUp(self):
        self.options = Options()

    def assertHostEqual(self, host, host_str, attrs):
        self.assertEquals(str(host), host_str)

        for attr, value in attrs.iteritems():
            self.assertEquals(getattr(host, attr), value)

    def assertHostsEqual(self, hosts, expected):
        for host, expect in zip(hosts, expected):
            host_str, attrs = expect

            self.assertHostEqual(host, host_str, attrs)
 
    def testApplyHostsFileError(self):
        with self.assertRaises(config.HostConfigError):
            list(config.apply_hosts(self.options, ['nonexistant']))

    def testApplyHosts(self):
        conf_file = ConfFile('test', """
[foo]
    ip = 127.0.0.1

[bar]
    ip = 127.0.0.2
        """)
        expected = [
                ('foo@test', dict(ip=ipaddr.IPAddress('127.0.0.1'))),
                ('bar@test', dict(ip=ipaddr.IPAddress('127.0.0.2'))),
        ]

        self.assertHostsEqual(config.apply_hosts_file(self.options, conf_file), expected)

    def testApply(self):
        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [
                ('foo@test', dict(
                    ip          = ipaddr.IPAddress('127.0.0.1'),
                    ethernet    = {None: '00:11:22:33:44:55'},
                )),
                ('bar@test', dict(
                    ip          = ipaddr.IPAddress('127.0.0.2'),
                    ethernet    = {None: '01:23:45:67:89:ab'},
                )),
        ])

    def testApplyHostsExpand(self):
        self.assertHostsEqual(config.apply_host_config(self.options, 'asdf', 'asdf{1-3}', ip='10.100.100.$'), [
                ('asdf1@asdf', dict(ip=ipaddr.IPAddress('10.100.100.1'))),
                ('asdf2@asdf', dict(ip=ipaddr.IPAddress('10.100.100.2'))),
                ('asdf3@asdf', dict(ip=ipaddr.IPAddress('10.100.100.3'))),
        ])

    def testApplyHostConfigDict(self):
        host = config.apply_host(self.options, 'foo', 'test', {
            'ethernet.eth0': '00:11:22:33:44:55',
        })

        self.assertHostEqual(host, 'foo@test', dict(
                ethernet    = { 'eth0': '00:11:22:33:44:55' }
        ))
   
    def testApplyHostsConfigError(self):
        with self.assertRaises(config.HostConfigError):
            config.apply_host(self.options, 'foo', 'test', {
                'ethernet': 'foo',
                'ethernet.eth0': 'bar',
            })

class TestForwardZone(unittest.TestCase):
    def setUp(self):
        self.options = Options()
        self.options.add_origin = False

    def testResolve(self):
        self.assertEquals(zone.resolve(None, None, 'host'), 'host.')
        self.assertEquals(zone.resolve(None, 'domain', 'host'), 'host.domain.')

        with self.assertRaises(zone.HostZoneError):
            zone.resolve('origin', 'domain', 'host')

        self.assertEquals(zone.resolve('domain', 'domain', 'host'), 'host')
        self.assertEquals(zone.resolve('origin', 'domain.origin', 'host'), 'host.domain')
        
        with self.assertRaises(zone.HostZoneError):
            zone.resolve('origin', 'domainorigin', 'host')

        with self.assertRaises(zone.HostZoneError):
            zone.resolve('origin', None, 'host.domain')

if __name__ == '__main__':
    unittest.main()