pvl/hosts/tests.py
author Tero Marttila <tero.marttila@aalto.fi>
Tue, 24 Feb 2015 19:24:06 +0200
changeset 451 d302b4957b07
parent 449 a19438b781d5
child 461 e3bddc5eeff5
permissions -rw-r--r--
pvl.hosts.config: fix apply_host override error handling
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
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     4
from pvl.hosts import config
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
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    21
    def assertHostsEqual(self, hosts, expected):
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    22
        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
    23
            host_str, attrs = expect
441
f058fff1f272 pvl.hosts.hosts: fix sort_key()
Tero Marttila <tero.marttila@aalto.fi>
parents: 440
diff changeset
    24
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    25
            self.assertEquals(str(host), host_str)
441
f058fff1f272 pvl.hosts.hosts: fix sort_key()
Tero Marttila <tero.marttila@aalto.fi>
parents: 440
diff changeset
    26
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    27
            for attr, value in attrs.iteritems():
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    28
                self.assertEquals(getattr(host, attr), value)
441
f058fff1f272 pvl.hosts.hosts: fix sort_key()
Tero Marttila <tero.marttila@aalto.fi>
parents: 440
diff changeset
    29
 
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    30
    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
    31
        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
    32
            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
    33
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
    def testApplyHosts(self):
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    35
        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
    36
[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
    37
    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
    38
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
[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
    40
    ip = 127.0.0.2
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    41
        """)
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    42
        expected = [
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    43
                ('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
    44
                ('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
    45
        ]
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
    46
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    47
        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
    48
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    49
    def testApply(self):
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    50
        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
    51
                ('foo@test', dict(
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    52
                    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
    53
                    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
    54
                )),
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    55
                ('bar@test', dict(
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    56
                    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
    57
                    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
    58
                )),
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    59
        ])
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    60
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    61
    def testApplyHostsExpand(self):
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
    62
        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
    63
                ('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
    64
                ('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
    65
                ('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
    66
        ])
451
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    67
    
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    68
    def testApplyHostsConfigError(self):
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    69
        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
    70
            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
    71
                'ethernet': 'foo',
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    72
                'ethernet.eth0': 'bar',
d302b4957b07 pvl.hosts.config: fix apply_host override error handling
Tero Marttila <tero.marttila@aalto.fi>
parents: 449
diff changeset
    73
            })
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    74
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
    75
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
    76
    unittest.main()