test_dhcp.py
author Tero Marttila <terom@fixme.fi>
Thu, 02 Apr 2009 20:19:18 +0300
changeset 1 2223ade4f259
parent 0 257003279747
child 2 e66102ab7048
permissions -rwxr-xr-x
continue the overengineering effort, we are now able to generate dhcpd.conf files
#!/usr/bin/env python2.5
"""
    Test conf_dhcp
"""

import dhcp_conf as dhcpc, conf, addr

import unittest
from cStringIO import StringIO

class TestConfDHCP (unittest.TestCase) :
    def assert_stmt (self, stmt, line) :
        """
            Formats the given Statement, and compares the output against the given line
        """
        
        self.assertEqual(stmt._fmt_data(), line)
    
    def test_statement (self) :
        self.assert_stmt(dhcpc.Statement("stmt0"),                           "stmt0")
        self.assert_stmt(dhcpc.Statement("stmt1", [ "this", "that" ]),       "stmt1 this, that")
        self.assert_stmt(dhcpc.Statement("stmt2", dhcpc.Literal("...")),     "stmt2 ...")
        self.assert_stmt(dhcpc.Statement("stmt3", u"quux"),                  "stmt3 quux")
        self.assert_stmt(dhcpc.Statement("stmt4", "bar"),                    "stmt4 bar")
        self.assert_stmt(dhcpc.Statement("stmt5", 1),                        "stmt5 1")
        self.assert_stmt(dhcpc.Statement("stmt6", 1, None, 2),               "stmt6 1 2")
 
    def assert_obj (self, obj, lines) :
        """
            Formats the given conf.Object and compares the output against the given lines
        """

        self.assertEqual(list(obj.fmt_lines()), lines)
    
    def test_literal (self) :
        self.assert_obj(dhcpc.Literal("///"),                               [ "///" ])

    def test_parameter (self) :
        self.assert_obj(dhcpc.Parameter("param0", "this", 13, "that"),      [ "param0 this 13 that;" ])
    
    def test_declaration (self) :
        self.assert_obj(dhcpc.Declaration("decl0", ["arg0", "arg1"], [
            dhcpc.Parameter("param0"),
            None
        ], [
            dhcpc.Declaration("decl0.0", params=[
                dhcpc.Parameter("param0.0.1", "value")
            ])
        ]),  [
            
            "decl0 arg0 arg1 {",
            "\tparam0;",
            "\tdecl0.0 {",
            "\t\tparam0.0.1 value;",
            "\t}",
            "}",
        ])
    
    def test_shared_network (self) :
        self.assert_obj(dhcpc.SharedNetwork("net0", params=[
            dhcpc.Parameter("param0")
        ]), [
            "shared-network net0 {",
            "\tparam0;",
            "}"
        ])

    def test_subnet (self) :
        self.assert_obj(dhcpc.Subnet(addr.Network("194.197.235.0/24"), params=[
            dhcpc.Parameter("param0")
        ]), [
            "subnet 194.197.235.0 netmask 255.255.255.0 {",
            "\tparam0;",
            "}"
        ])

    def test_group (self) :
        self.assert_obj(dhcpc.Group(decls=[
            dhcpc.Declaration("decl0.0", params=[
                dhcpc.Parameter("param0.0.1", "value")
            ])
        ]), [
            "group {",
            "\tdecl0.0 {",
            "\t\tparam0.0.1 value;",
            "\t}",
            "}"
        ])
    
    def test_host (self) :
        self.assert_obj(dhcpc.Host("test-hostname", params=[
            dhcpc.Parameter("param0")
        ]), [
            "host test-hostname {",
            "\tparam0;",
            "}"
        ])

    def test_option (self) :
        self.assert_obj(dhcpc.Option("foo", "example.com"), [
            "option foo example.com;",
        ])
    
if __name__ == '__main__' :
    unittest.main()