pvl.hosts.config: fix apply_host override error handling
authorTero Marttila <tero.marttila@aalto.fi>
Tue, 24 Feb 2015 19:24:06 +0200
changeset 451 d302b4957b07
parent 450 1d86e3909678
child 452 d10f2b2188bb
pvl.hosts.config: fix apply_host override error handling
pvl/hosts/config.py
pvl/hosts/tests.py
--- a/pvl/hosts/config.py	Tue Feb 24 19:18:45 2015 +0200
+++ b/pvl/hosts/config.py	Tue Feb 24 19:24:06 2015 +0200
@@ -123,10 +123,13 @@
         else:
             f = fields
         
-        if instance:
+        if not instance and field not in f:
+            f[field] = value
+
+        elif field not in f:
+            f[field] = {instance: value}
+        elif isinstance(f[field], dict):
             f.setdefault(field, {})[instance] = value
-        elif field not in f:
-            f[field] = value
         else:
             raise HostConfigError(None, "{name}: override instanced {field} value: {value}".format(name=name, field=field, value=value))
 
--- a/pvl/hosts/tests.py	Tue Feb 24 19:18:45 2015 +0200
+++ b/pvl/hosts/tests.py	Tue Feb 24 19:24:06 2015 +0200
@@ -27,13 +27,7 @@
             for attr, value in attrs.iteritems():
                 self.assertEquals(getattr(host, attr), value)
  
-    def testApply(self):
-        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [
-                ('foo@test', dict(ip=ipaddr.IPAddress('127.0.0.1'))),
-                ('bar@test', dict(ip=ipaddr.IPAddress('127.0.0.2'))),
-        ])
-
-    def testApplyHostsError(self):
+    def testApplyHostsFileError(self):
         with self.assertRaises(config.HostConfigError):
             list(config.apply_hosts(self.options, ['nonexistant']))
 
@@ -52,12 +46,31 @@
 
         self.assertHostsEqual(config.apply_hosts_file(self.options, conf_file), expected)
 
+    def testApply(self):
+        self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [
+                ('foo@test', dict(
+                    ip          = ipaddr.IPAddress('127.0.0.1'),
+                    ethernet    = {None: '00:11:22:33:44:55'},
+                )),
+                ('bar@test', dict(
+                    ip          = ipaddr.IPAddress('127.0.0.2'),
+                    ethernet    = {None: '01:23:45:67:89:ab'},
+                )),
+        ])
+
     def testApplyHostsExpand(self):
         self.assertHostsEqual(config.apply_host_config(self.options, 'asdf', 'asdf{1-3}', ip='10.100.100.$'), [
                 ('asdf1@asdf', dict(ip=ipaddr.IPAddress('10.100.100.1'))),
                 ('asdf2@asdf', dict(ip=ipaddr.IPAddress('10.100.100.2'))),
                 ('asdf3@asdf', dict(ip=ipaddr.IPAddress('10.100.100.3'))),
         ])
+    
+    def testApplyHostsConfigError(self):
+        with self.assertRaises(config.HostConfigError):
+            config.apply_host(self.options, 'foo', 'test', {
+                'ethernet': 'foo',
+                'ethernet.eth0': 'bar',
+            })
 
 if __name__ == '__main__':
     unittest.main()