pvl/hosts/tests.py
author Tero Marttila <tero.marttila@aalto.fi>
Tue, 24 Feb 2015 17:35:36 +0200
changeset 440 1d755df7bf97
child 441 f058fff1f272
permissions -rw-r--r--
pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
import ipaddr
import unittest

from pvl.hosts import config
from StringIO import StringIO

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

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

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

    def testApplyHostsError(self):
        with self.assertRaises(config.HostConfigError):
            list(config.apply_hosts(self.options, ['nonexistant']))

    def testApplyHosts(self):
        expected = [
            ('foo', 'test', ipaddr.IPAddress('127.0.0.1')),
            ('bar', 'test', ipaddr.IPAddress('127.0.0.2')),
        ]

        for expect, host in zip(expected, config.apply_hosts_file(self.options, NamedStringIO('test', """
[foo]
    ip = 127.0.0.1

[bar]
    ip = 127.0.0.2
"""
        ))):
            hostname, domain, ip = expect

            self.assertEquals(str(host), hostname)
            self.assertEquals(host.domain, domain)
            self.assertEquals(host.ip, ip)
 
if __name__ == '__main__':
    unittest.main()