test_dhcp.py
changeset 2 e66102ab7048
parent 1 2223ade4f259
child 3 ff98fa9b84ce
--- a/test_dhcp.py	Thu Apr 02 20:19:18 2009 +0300
+++ b/test_dhcp.py	Thu Apr 02 20:54:37 2009 +0300
@@ -3,19 +3,42 @@
     Test conf_dhcp
 """
 
-import dhcp_conf as dhcpc, conf, addr
+import dhcp_conf as dhcpc, dhcp, addr
 
 import unittest
-from cStringIO import StringIO
 
-class TestConfDHCP (unittest.TestCase) :
+class _TestConfObj (unittest.TestCase) :
+    def assert_obj (self, obj, lines) :
+        """
+            Formats the given conf.Object and compares the output against the given lines
+        """
+        
+        for obj_line, line in zip(obj.fmt_lines(), lines) :
+            self.assertEqual(obj_line, line)
+
+class TestDHCPConf (_TestConfObj) :
     def assert_stmt (self, stmt, line) :
         """
-            Formats the given Statement, and compares the output against the given 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")
@@ -24,14 +47,7 @@
         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("///"),                               [ "///" ])
 
@@ -101,6 +117,55 @@
             "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")), [
+                "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")), [
+                "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()