pvl/hosts/tests.py
author Tero Marttila <tero.marttila@aalto.fi>
Wed, 25 Feb 2015 14:23:04 +0200
changeset 461 e3bddc5eeff5
parent 451 d302b4957b07
child 462 6d699c76d75d
permissions -rw-r--r--
pvl.hosts: test config.apply_host()
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 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',
            })

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