pvl.hosts.dhcp: update for pvl.dhcp.config
authorTero Marttila <terom@paivola.fi>
Sun, 01 Mar 2015 22:39:05 +0200
changeset 669 83e9bff09a0b
parent 668 794f943c835d
child 670 b95ad8c8bb4e
pvl.hosts.dhcp: update for pvl.dhcp.config
bin/pvl.hosts-dhcp
pvl/hosts/dhcp.py
pvl/hosts/tests.py
--- a/bin/pvl.hosts-dhcp	Sun Mar 01 22:38:02 2015 +0200
+++ b/bin/pvl.hosts-dhcp	Sun Mar 01 22:39:05 2015 +0200
@@ -22,8 +22,12 @@
 
     # process
     try:
-        for line in pvl.hosts.dhcp.apply_hosts_dhcp(hosts):
-            print line
+        for block in pvl.hosts.dhcp.dhcp_hosts(hosts):
+            for line in pvl.dhcp.config.build_block(block):
+                print line
+            
+            print ''
+
     except pvl.hosts.HostError as error:
         log.error("%s", error)
         return 3
--- a/pvl/hosts/dhcp.py	Sun Mar 01 22:38:02 2015 +0200
+++ b/pvl/hosts/dhcp.py	Sun Mar 01 22:39:05 2015 +0200
@@ -6,7 +6,7 @@
 
 def dhcp_host_options (host, ethernet):
     """
-        Yield specific dhcp.conf host { ... } parameters for build_block()
+        Yield specific dhcp.conf host { ... } items.
     """
 
     yield 'option', 'host-name', host.name
@@ -36,7 +36,7 @@
 
 def dhcp_host (host):
     """
-        Yield (block, items, **opts) tuples for pvl.dhcp.config.build_block().
+        Yield pvl.dhcp.config.Block's
     """
 
     if set(host.ethernet) == set([None]) :
@@ -55,7 +55,7 @@
     for index, ethernet in host.ethernet.iteritems() :
         name = host_fmt.format(host=host, index=index)
 
-        yield ('host', name), list(dhcp_host_options(host, ethernet)), dict(comment=comment)
+        yield pvl.dhcp.config.Block(('host', name), list(dhcp_host_options(host, ethernet)), comment=comment)
     
 def dhcp_hosts (hosts):
     """
@@ -66,22 +66,10 @@
     blocks = { }
 
     for host in hosts:
-        for block, items, opts in dhcp_host(host):
-            if block in blocks:
-                raise HostDHCPError(host, "hosts on multiple networks must use unique ethernet.XXX=... naming: {other}".format(block=block, other=blocks[block]))
-
-            blocks[block] = host
-
-            yield block, items, opts
+        for block in dhcp_host(host):
+            if block.key in blocks:
+                raise HostDHCPError(host, "dhcp {block} conflict with {other}; hosts on multiple networks must use unique ethernet.XXX=... naming".format(block=block, other=blocks[block.key]))
 
-def apply_hosts_dhcp (hosts):
-    """
-        Generate dhcp.conf output lines for the set of hosts.
-    """
-    
-    for block, items, opts in dhcp_hosts(hosts):
-        for line in pvl.dhcp.config.build_block(block, items, **opts):
-            yield line
-        
-        yield ''
+            blocks[block.key] = host
 
+            yield block
--- a/pvl/hosts/tests.py	Sun Mar 01 22:38:02 2015 +0200
+++ b/pvl/hosts/tests.py	Sun Mar 01 22:39:05 2015 +0200
@@ -1,4 +1,5 @@
 import ipaddr
+import itertools
 import pvl.args
 import unittest
 
@@ -557,15 +558,19 @@
         })
 
 class TestDhcp(unittest.TestCase):
-    def assertBlocksEqual(self, blockdefs, expected):
-        for (_block, _items, _opts), (block, items, opts) in zip(blockdefs, expected):
-            self.assertEqual(_block, block)
-            self.assertItemsEqual(_items, items)
+    def assertBlockEqual(self, block, (key, items, blocks)):
+        self.assertEqual(block.key, key)
+        self.assertItemsEqual(block.items, items)
 
-            if opts is not None:
-                self.assertEqual(_opts, opts)
-        
-        self.assertEqual(len(blockdefs), len(expected))
+        for _block, expect_block in itertools.izip_longest(block.blocks, blocks):
+            self.assertBlockEqual(_block, expect_block)
+
+    def assertBlocksEqual(self, blocks, expected):
+        for _block, block in itertools.izip_longest(blocks, expected):
+            self.assertIsNotNone(_block, block)
+            self.assertIsNotNone(block, _block)
+
+            self.assertBlockEqual(_block, block)
     
     def testHost(self):
         host = Host.build('foo', 'test',
@@ -576,10 +581,10 @@
 
         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
             (('host', 'foo'), [
-                ('option', 'host-name', "foo"),
-                ('fixed-address', '192.0.2.1'),
-                ('hardware', 'ethernet', '00:11:22:33:44:55'),
-                ], dict(comment="Owner: foo"))
+                    ('option', 'host-name', "foo"),
+                    ('fixed-address', '192.0.2.1'),
+                    ('hardware', 'ethernet', '00:11:22:33:44:55'),
+            ], [])
         ])
 
     def testHostStatic(self):
@@ -600,7 +605,7 @@
             (('host', 'foo'), [
                 ('option', 'host-name', "foo"),
                 ('hardware', 'ethernet', '00:11:22:33:44:55'),
-            ], None)
+            ], [])
         ])
 
     def testHostBoot(self):
@@ -625,17 +630,17 @@
                 ('hardware', 'ethernet', '00:11:22:33:44:55'),
                 ('next-server', 'boot.lan'),
                 ('filename', 'debian/wheezy/pxelinux.0'),
-            ], None),
+            ], []),
             (('host', 'foo2'), [
                 ('option', 'host-name', "foo2"),
                 ('hardware', 'ethernet', '00:11:22:33:44:55'),
                 ('next-server', 'boot.lan'),
-            ], None),
+            ], []),
             (('host', 'foo3'), [
                 ('option', 'host-name', "foo3"),
                 ('hardware', 'ethernet', '00:11:22:33:44:55'),
                 ('filename', 'debian/wheezy/pxelinux.0'),
-            ], None),
+            ], []),
         ])
 
     def testHosts(self):
@@ -655,12 +660,12 @@
                 ('option', 'host-name', "foo"),
                 ('fixed-address', '192.0.2.1'),
                 ('hardware', 'ethernet', '00:11:22:33:44:55'),
-            ], None),
+            ], []),
             (('host', 'bar'), [
                 ('option', 'host-name', "bar"),
                 ('fixed-address', '192.0.2.2'),
                 ('hardware', 'ethernet', '01:23:45:67:89:ab'),
-            ], None),
+            ], []),
         ])
 
     def testHostConflict(self):
@@ -693,12 +698,12 @@
                     ('option', 'host-name', "foo"),
                     ('fixed-address', '192.0.1.1'),
                     ('hardware', 'ethernet', '00:11:22:33:44:55'),
-                ], None),
+                ], []),
                 (('host', 'foo-eth2'), [
                     ('option', 'host-name', "foo"),
                     ('fixed-address', '192.0.2.1'),
                     ('hardware', 'ethernet', '01:23:45:67:89:ab'),
-                ], None),
+                ], []),
         ])