test/dhcp.py
changeset 7 0f9cae2d7147
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/dhcp.py	Sun Jul 12 00:51:08 2009 +0300
@@ -0,0 +1,166 @@
+#!/usr/bin/env python2.5
+"""
+    Test dhcp_conf
+"""
+
+import dhcp_conf as dhcpc
+import test_conf, dhcp, addr
+
+import unittest
+
+class TestDHCPConf (test_conf._TestConfBase) :
+    def assert_stmt (self, stmt, line) :
+        """
+            Formats the given Statement, and compares the output against the given line.
+
+            Note that the dhcpc.Statement doesn't have a working fmt_lines implementation.
+        """
+        
+        self.assertEqual(stmt._fmt_data(), line)
+    
+    def test_comment (self) :
+        self.assert_obj(dhcpc.Comment("foo bar"),                           [ "# foo bar" ])
+
+    def test_section (self) :
+        self.assert_obj(dhcpc.Section(comment="test"),                      [ "# test" ])
+
+        self.assert_obj(dhcpc.Section(params=[
+                dhcpc.Parameter("param0"), None
+            ], comment="foo"), [
+                "# foo",
+                "param0;",
+            ])
+
+    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 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;" ])
+        self.assert_obj(dhcpc.Parameter("param1", comment="testing"),       [ "# testing", "param1;" ])
+    
+    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")
+            ])
+        ], comment="foo"),  [
+            "# foo",
+            "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;",
+        ])
+    
+class TestDHCP (_TestConfObj) :
+    def test_host (self) :
+        self.assert_obj(dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4"), comment="foo"), [
+                "# foo",
+                "host testhost {",
+                "\thardware ethernet 12:34:56:78:90:ab;",
+                "\tfixed-address 1.2.3.4;",
+                "}"
+            ])
+
+    def test_subnet (self) :
+        self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24"), comment="bar"), [
+                "# bar",
+                "subnet 1.2.3.0 netmask 255.255.255.0 {",
+                "\toption routers 1.2.3.1;",
+                "}"
+            ])
+
+        self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24"), router_idx=10, range=(20, 30), unknown_clients='allow'), [
+                "subnet 1.2.3.0 netmask 255.255.255.0 {",
+                "\toption routers 1.2.3.10;",
+                "\trange 1.2.3.20 1.2.3.30;",
+                "\tallow unknown-clients;",
+                "}"
+            ])
+    
+    def test_config (self) :
+        self.assert_obj(dhcp.Config(
+                settings        = { 'foo-setting': 'someval' }, 
+                options         = { 'bar-opt': ['one', 'two'] },
+                shared_network  = "FOO-NET",
+                subnets         = [
+                        dhcp.Subnet(addr.Network("1.2.3.0/24"))
+                    ],
+                hosts           = [
+                        dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4"))
+                    ],
+            ), [
+                "foo-setting someval;",
+                "option bar-opt one, two;",
+                "shared-network FOO-NET {",
+                "\tsubnet 1.2.3.0 netmask 255.255.255.0 {",
+                "\t\toption routers 1.2.3.1;",
+                "\t}",
+                "}",
+                "host testhost {",
+                "\thardware ethernet 12:34:56:78:90:ab;",
+                "\tfixed-address 1.2.3.4;",
+                "}"
+            ])
+
+if __name__ == '__main__' :
+    unittest.main()
+