pvl/hosts/tests.py
author Tero Marttila <tero.marttila@aalto.fi>
Tue, 24 Feb 2015 18:56:22 +0200
changeset 449 a19438b781d5
parent 447 6f0357759e9b
child 451 d302b4957b07
permissions -rw-r--r--
pvl.hosts.config: cleanup apply_host_config
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
 
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    30
    def testApply(self):
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    31
        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    32
                ('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
    33
                ('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
    34
        ])
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    35
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
    def testApplyHostsError(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
    37
        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
    38
            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
    39
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
    def testApplyHosts(self):
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    41
        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
    42
[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
    43
    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
    44
1d755df7bf97 pvl.hosts: refactor as a package; cleanup pvl.hosts.config with some basic tests
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    45
[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
    46
    ip = 127.0.0.2
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    47
        """)
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    48
        expected = [
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    49
                ('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
    50
                ('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
    51
        ]
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
    52
447
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    53
        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
    54
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    55
    def testApplyHostsExpand(self):
449
a19438b781d5 pvl.hosts.config: cleanup apply_host_config
Tero Marttila <tero.marttila@aalto.fi>
parents: 447
diff changeset
    56
        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
    57
                ('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
    58
                ('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
    59
                ('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
    60
        ])
6f0357759e9b pvl.hosts: fixup and document host expansion
Tero Marttila <tero.marttila@aalto.fi>
parents: 441
diff changeset
    61
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
    62
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
    63
    unittest.main()