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 testApply(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(self.options, ['etc/hosts/test'])):
hostname, domain, ip = expect
self.assertEquals(str(host), hostname)
self.assertEquals(host.domain, domain)
self.assertEquals(host.ip, ip)
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()