pvl.hosts.dhcp: test and document hosts on multiple networks
authorTero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 15:05:18 +0200
changeset 483 19d084bb4afd
parent 482 41622bb9b95c
child 484 099dee149e74
pvl.hosts.dhcp: test and document hosts on multiple networks
README
etc/hosts/dhcp2
pvl/hosts/dhcp.py
pvl/hosts/tests.py
--- a/README	Thu Feb 26 15:05:04 2015 +0200
+++ b/README	Thu Feb 26 15:05:18 2015 +0200
@@ -109,3 +109,29 @@
         filename debian/wheezy/pxelinux.0;
     }
 
+=== DHCP hosts in multiple subnets/domains ===
+A host with different interfaces in multiple domains must specify unique interface names:
+
+    [foo.dhcp]
+        [[asdf]]
+            ip              = 10.1.0.1
+            ethernet.eth1   = 00:11:22:33:44:55
+
+    [bar.dhcp]
+        [[asdf]]
+            ip              = 10.2.0.1
+            ethernet.eth2   = 55:44:33:22:11:00
+
+    $ bin/pvl.hosts-dhcp etc/hosts/dhcp2 
+    host asdf-eth1 {
+        option host-name asdf;
+        hardware ethernet 00:11:22:33:44:55;
+        fixed-address 10.1.0.1;
+    }
+
+    host asdf-eth2 {
+        option host-name asdf;
+        hardware ethernet 55:44:33:22:11:00;
+        fixed-address 10.2.0.1;
+    }
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/hosts/dhcp2	Thu Feb 26 15:05:18 2015 +0200
@@ -0,0 +1,9 @@
+[foo.dhcp]
+    [[asdf]]
+        ip              = 10.1.0.1
+        ethernet.eth1   = 00:11:22:33:44:55
+
+[bar.dhcp]
+    [[asdf]]
+        ip              = 10.2.0.1
+        ethernet.eth2   = 55:44:33:22:11:00
--- a/pvl/hosts/dhcp.py	Thu Feb 26 15:05:04 2015 +0200
+++ b/pvl/hosts/dhcp.py	Thu Feb 26 15:05:18 2015 +0200
@@ -57,9 +57,8 @@
 
         yield ('host', name), list(dhcp_host_options(host, ethernet)), dict(comment=comment)
     
-def apply_hosts_dhcp (options, hosts):
+def dhcp_hosts (hosts):
     """
-        Generate dhcp.conf output lines for the set of hosts.
 
         Verifies that there are no dupliate hosts.
     """
@@ -69,12 +68,20 @@
     for host in hosts:
         for block, items, opts in dhcp_host(host):
             if block in blocks:
-                raise HostDHCPError(host, "dupliate host block {block}: {other}".format(block=block, other=blocks[block]))
-
-            blocks[block] = block
+                raise HostDHCPError(host, "hosts on multiple networks must use unique ethernet.XXX=... naming: {other}".format(block=block, other=blocks[block]))
 
-            for line in pvl.dhcp.config.build_block(block, items, **opts):
-                yield line
-            
-            yield ''
+            blocks[block] = host
 
+            yield block, items, opts
+
+def apply_hosts_dhcp (options, 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 ''
+
--- a/pvl/hosts/tests.py	Thu Feb 26 15:05:04 2015 +0200
+++ b/pvl/hosts/tests.py	Thu Feb 26 15:05:18 2015 +0200
@@ -495,5 +495,69 @@
             ], None)
         ])
 
+    def testHosts(self):
+        hosts = [
+                Host.build('foo', 'test',
+                        ip          = '192.0.2.1',
+                        ethernet    = '00:11:22:33:44:55',
+                ),
+                Host.build('bar', 'test',
+                        ip          = '192.0.2.2',
+                        ethernet    = '01:23:45:67:89:ab',
+                ),
+        ]
+
+        self.assertBlocksEqual(list(dhcp.dhcp_hosts(hosts)), [
+            (('host', 'foo'), [
+                ('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):
+        hosts = [
+                Host.build('foo', 'test1',
+                        ethernet    = '00:11:22:33:44:55',
+                ),
+                Host.build('foo', 'test2',
+                        ethernet    = '01:23:45:67:89:ab',
+                ),
+        ]
+        
+        with self.assertRaises(dhcp.HostDHCPError):
+            list(dhcp.dhcp_hosts(hosts))
+
+    def testHostMultinet(self):
+        hosts = [
+                Host.build('foo', 'test1',
+                    ip              = '192.0.1.1',
+                    ethernet        = { 'eth1': '00:11:22:33:44:55' },
+                ),
+                Host.build('foo', 'test2',
+                    ip              = '192.0.2.1',
+                    ethernet        = { 'eth2': '01:23:45:67:89:ab' },
+                ),
+        ]
+        
+        self.assertBlocksEqual(list(dhcp.dhcp_hosts(hosts)), [
+                (('host', 'foo-eth1'), [
+                    ('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),
+        ])
+
+
 if __name__ == '__main__':
     unittest.main()