pvl/dhcp/tests.py
author Tero Marttila <terom@paivola.fi>
Mon, 02 Mar 2015 01:29:56 +0200
changeset 682 60dbd952a15e
parent 679 31adba0f586d
child 685 668f934bb958
permissions -rw-r--r--
pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
666
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
import itertools
678
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
     2
import re
666
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     3
import unittest
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     4
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     5
from pvl.dhcp import config
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     6
from StringIO import StringIO
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     8
class File(StringIO):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     9
    @classmethod
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    10
    def lines (cls, *lines):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    11
        return cls('\n'.join(lines) + '\n')
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    12
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    13
    def __init__(self, buffer, name='test.file'):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    14
        StringIO.__init__(self, buffer)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    15
        self.name = name
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    16
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    17
class ConfigTest(unittest.TestCase):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    18
    def setUp(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    19
        self.parser = config.DHCPConfigParser(name='test')
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    20
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    21
    def assertLexEqual(self, lexed, expected):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    22
        self.assertEqual(list(lexed), expected)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    23
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    24
    def assertBlockEqual(self, block, (key, items, blocks)):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    25
        self.assertEqual(block.key, key)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    26
        self.assertEqual(block.items, items)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    27
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    28
        for _block, expect_block in itertools.izip_longest(block.blocks, blocks):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    29
            self.assertBlockEqual(_block, expect_block)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    30
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    31
    def _testLex(self, lines, expected):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    32
        lexed = [item for line in lines for item in self.parser.lex(line)]
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    33
        
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    34
        self.assertEqual(lexed, expected)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    35
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    36
    def _testParse(self, lines, expected):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    37
        for block, expect in itertools.izip_longest(self.parser.parse_lines(lines), expected):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    38
            self.assertIsNotNone(block, expect)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    39
            self.assertIsNotNone(expect, block)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    40
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    41
            self.assertBlockEqual(block, expect)
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    42
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    43
    def testLexerEmpty(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    44
        self._testLex([''], [])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    45
    
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    46
    def testLexerSingleToken(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    47
        self._testLex(['foo;'], [
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    48
            ('item', ('foo', )),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    49
        ])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    50
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    51
    def testLexerSingleTokenWhitespace(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    52
        self._testLex([' foo ;'], [
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    53
            ('item', ('foo', )),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    54
        ])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    55
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    56
    def testLexerSingleLine(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    57
        self._testLex(['foo { bar "quux"; } # ignore'], [
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    58
            ('open', ('foo', )),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    59
            ('item', ('bar', 'quux')),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    60
            ('close', None),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    61
        ])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    62
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    63
    def testLexerSingleTokenLines(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    64
        self._testLex(['foo {'], [('open', ('foo', ))])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    65
        self._testLex([' bar;'], [('item', ('bar', ))])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    66
        self._testLex(['}'], [('close', None)])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    67
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    68
    def testLexerSingleTokens(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    69
        self._testLex(['foo', '  {  ', 'bar', '', '"quux"', ';', '}'], [
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    70
            ('open', ('foo', )),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    71
            ('item', ('bar', 'quux')),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    72
            ('close', None),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    73
        ])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    74
    
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    75
    def testParse(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    76
        self._testParse(['foo {'], [])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    77
        self._testParse([' bar;', ' quux asdf;'], [])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    78
        self._testParse(['}'], [
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    79
            (('foo', ), [('bar', ), ('quux', 'asdf')], []),
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    80
        ])
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    81
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    82
    def testParseConf(self):
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    83
        self.assertBlockEqual(config.DHCPConfigParser.load(File("""
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    84
group {
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    85
    next-server boot.test;
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    86
    filename "/debian/wheezy/pxelinux.0";
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    87
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    88
    include "hosts/test.conf";
675
f1335a4301d0 pvl.dhcp.config: loosen up TOKEN even further to permit unquoted IP addresses and ethernet addresses
Tero Marttila <terom@paivola.fi>
parents: 666
diff changeset
    89
678
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
    90
    host foo {
675
f1335a4301d0 pvl.dhcp.config: loosen up TOKEN even further to permit unquoted IP addresses and ethernet addresses
Tero Marttila <terom@paivola.fi>
parents: 666
diff changeset
    91
        fixed-address       192.0.2.1;
f1335a4301d0 pvl.dhcp.config: loosen up TOKEN even further to permit unquoted IP addresses and ethernet addresses
Tero Marttila <terom@paivola.fi>
parents: 666
diff changeset
    92
        hardware ethernet   00:11:22:33:44:55;
f1335a4301d0 pvl.dhcp.config: loosen up TOKEN even further to permit unquoted IP addresses and ethernet addresses
Tero Marttila <terom@paivola.fi>
parents: 666
diff changeset
    93
    }
666
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
    94
}
682
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
    95
        """)), (None, [], [
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
    96
            (('group', ), [
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
    97
                ('next-server', 'boot.test'),
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
    98
                ('filename', "/debian/wheezy/pxelinux.0"),
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
    99
                ('include', "hosts/test.conf"),
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
   100
            ], [
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
   101
                (('host', 'foo'), [
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
   102
                    ('fixed-address', '192.0.2.1'),
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
   103
                    ('hardware', 'ethernet', '00:11:22:33:44:55'),
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
   104
                ], [])
60dbd952a15e pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
Tero Marttila <terom@paivola.fi>
parents: 679
diff changeset
   105
            ]),
675
f1335a4301d0 pvl.dhcp.config: loosen up TOKEN even further to permit unquoted IP addresses and ethernet addresses
Tero Marttila <terom@paivola.fi>
parents: 666
diff changeset
   106
        ]))
666
a8ddcbe894ff pvl.dhcp.config: refactor DHCPConfigParser to use shlex and yield Block objects, change build_block() to use Block; tests
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   107
678
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   108
class ConfigBuildTest(unittest.TestCase):
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   109
    def assertEqualWhitespace(self, value, expected):
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   110
        # normalize
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   111
        value = re.sub(r'\s+', ' ', value)
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   112
        expected = re.sub(r'\s+', ' ', expected)
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   113
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   114
        self.assertEqual(value, expected)
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   115
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   116
    def assertLinesEqual(self, lines, expected):
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   117
        for line, expect in itertools.izip_longest(lines, expected):
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   118
            self.assertEqualWhitespace(line, expect)
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   119
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   120
    def testBuildConf(self):
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   121
        self.assertLinesEqual(
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   122
                config.build_block(config.Block(('group', ), [
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   123
                        ('next-server', 'boot.test'),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   124
                        ('filename', "/debian/wheezy/pxelinux.0"),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   125
                        ('include', "hosts/test.conf"),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   126
                    ], [
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   127
                        config.Block(('host', 'foo'), [
679
31adba0f586d pvl.dhcp.config: make quoting context-sensitive, so that only certain items get special quoting..
Tero Marttila <terom@paivola.fi>
parents: 678
diff changeset
   128
                            ('option', 'host-name', "foo.test"),
678
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   129
                            ('fixed-address', '192.0.2.1'),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   130
                            ('hardware', 'ethernet', '00:11:22:33:44:55'),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   131
                        ]),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   132
                    ]
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   133
                )),
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   134
                """
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   135
group {
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   136
    next-server boot.test;
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   137
    filename "/debian/wheezy/pxelinux.0";
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   138
    include "hosts/test.conf";
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   139
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   140
    host foo {
679
31adba0f586d pvl.dhcp.config: make quoting context-sensitive, so that only certain items get special quoting..
Tero Marttila <terom@paivola.fi>
parents: 678
diff changeset
   141
        option host-name    "foo.test";
678
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   142
        fixed-address       192.0.2.1;
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   143
        hardware ethernet   00:11:22:33:44:55;
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   144
    }
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   145
}
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   146
                """.strip().splitlines()
5f280b922905 pvl.dhcp.tests: test build_block()
Tero Marttila <terom@paivola.fi>
parents: 675
diff changeset
   147
        )