pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
authorTero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 18:19:09 +0200
changeset 511 99043eab9140
parent 510 368a568412ed
child 512 dafac309813e
pvl.hosts.config: change nested domains to be relative to parent domain, not absolute
pvl/hosts/config.py
pvl/hosts/tests.py
--- a/pvl/hosts/config.py	Thu Feb 26 18:07:03 2015 +0200
+++ b/pvl/hosts/config.py	Thu Feb 26 18:19:09 2015 +0200
@@ -230,7 +230,12 @@
 
     if config.sections:
         # this is a top-level section that includes hosts
-        log.info("%s: @%s", path, name)
+        if parent:
+            log.info("%s: @%s@%s", path, name, parent)
+
+            name = pvl.dns.join(name, parent)
+        else:
+            log.info("%s: @%s", path, name)
 
         # recurse until we hit a scalar-only section representing a host
         for section_name in config.sections:
@@ -314,6 +319,8 @@
         # use directory name
         parent = os.path.basename(path.rstrip('/'))
 
+        log.info("%s: @%s", path, parent)
+
     for name in os.listdir(path):
         file_path = os.path.join(path, name)
 
--- a/pvl/hosts/tests.py	Thu Feb 26 18:07:03 2015 +0200
+++ b/pvl/hosts/tests.py	Thu Feb 26 18:19:09 2015 +0200
@@ -99,7 +99,7 @@
         with self.assertRaises(config.HostConfigError):
             list(config.apply_hosts_files(self.options, ['nonexistant']))
 
-    def testApplyHostsFile(self):
+    def testApplyHostsConfig(self):
         conf_file = ConfFile('test', """
 [foo]
     ip = 127.0.0.1
@@ -107,12 +107,27 @@
 [bar]
     ip = 127.0.0.2
         """)
-        expected = [
+        
+        self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), [
                 ('foo@test', dict(ip=ipaddr.IPAddress('127.0.0.1'))),
                 ('bar@test', dict(ip=ipaddr.IPAddress('127.0.0.2'))),
-        ]
+        ])
 
-        self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), expected)
+    def testApplyHostsConfigNested(self):
+        conf_file = ConfFile('test', """
+[asdf]
+    [[foo]]
+        ip = 127.0.0.1
+
+[quux]
+    [[bar]]
+        ip = 127.0.0.2
+        """)
+
+        self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), [
+                ('foo@asdf.test', dict(ip=ipaddr.IPAddress('127.0.0.1'))),
+                ('bar@quux.test', dict(ip=ipaddr.IPAddress('127.0.0.2'))),
+        ])
 
     def testApplyIncludes(self):
         self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/includes.test']), [
@@ -129,6 +144,9 @@
 
     def testApplyDirectory(self):
         self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/included.test/']), [
+                ('quux@asdf.included.test', dict(
+                    ip          = ipaddr.IPAddress('192.0.2.5'),
+                )),
                 ('foo@included.test', dict(
                     ip          = ipaddr.IPAddress('192.0.2.1'),
                 )),