pvl.hosts.config: support defaults for included hosts
authorTero Marttila <tero.marttila@aalto.fi>
Mon, 02 Mar 2015 18:25:50 +0200
changeset 692 34f25380d0e7
parent 691 760bab5a959f
child 693 570b0fd72cf8
pvl.hosts.config: support defaults for included hosts
pvl/hosts/config.py
pvl/hosts/tests.py
--- a/pvl/hosts/config.py	Mon Mar 02 18:17:47 2015 +0200
+++ b/pvl/hosts/config.py	Mon Mar 02 18:25:50 2015 +0200
@@ -231,7 +231,7 @@
         includes = list(parse_config_includes(options, path, includes))
 
         # within our domain context
-        for host in apply_hosts_files(options, includes, parent=name):
+        for host in apply_hosts_files(options, includes, parent=name, defaults=section):
             yield host
     else:
         includes = None
@@ -252,6 +252,10 @@
             for host in apply_hosts_configs(options, path, section_name, config[section_name], parent=name, defaults=section):
                 yield host
 
+    elif includes:
+        # includes-only zone
+        pass
+
     elif parent:
         # this is a host section
         log.debug("%s: %s@%s", path, name, parent)
@@ -267,12 +271,9 @@
 
             raise HostConfigError(path, "{parent}: {name}: {error}".format(parent=parent, name=name, error=error))
 
-    elif includes:
-        # includes-only zone
-        pass
-
     elif section:
         raise HostConfigError(path, "Top-level hosts are only allowed in included confs")
+
     else:
         # empty file
         log.info("%s: skip empty conf", path)
--- a/pvl/hosts/tests.py	Mon Mar 02 18:17:47 2015 +0200
+++ b/pvl/hosts/tests.py	Mon Mar 02 18:25:50 2015 +0200
@@ -29,13 +29,14 @@
     def assertHostsEqual(self, hosts, expected):
         hosts = list(hosts)
 
-        for host, expect in zip(hosts, expected):
+        for host, expect in itertools.izip_longest(hosts, expected):
+            self.assertIsNotNone(host, expect)
+            self.assertIsNotNone(expect, host)
+            
             host_str, attrs = expect
 
             self.assertHostEqual(host, host_str, attrs)
 
-        self.assertEqual(len(hosts), len(expected))
- 
     def testApplyHostConfigDict(self):
         host = config.apply_host('foo', 'test', {
             'ethernet.eth0': '00:11:22:33:44:55',
@@ -167,6 +168,21 @@
                 )),
         ])
 
+    def testApplyIncludesDefaults(self):
+        self.assertHostsEqual(config.apply_hosts_config(self.options, ConfFile('test', """
+boot.next-server = boot.lan
+
+include = etc/hosts/test
+        """)), [
+                ('bar@test', dict(
+                    ip          = ipaddr.IPAddress('192.0.2.2'),
+                )),
+                ('foo@test', dict(
+                    ip          = ipaddr.IPAddress('192.0.2.1'),
+                )),
+        ])
+
+
     def testApplyIncludePath(self):
         self.options.hosts_include = 'etc/hosts'
         self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/zones/forward/test']), [