pvl/hosts/tests.py
changeset 447 6f0357759e9b
parent 441 f058fff1f272
child 449 a19438b781d5
--- a/pvl/hosts/tests.py	Tue Feb 24 18:06:32 2015 +0200
+++ b/pvl/hosts/tests.py	Tue Feb 24 18:22:07 2015 +0200
@@ -9,7 +9,7 @@
     hosts_domain    = None
     hosts_include   = None
 
-class NamedStringIO(StringIO):
+class ConfFile(StringIO):
     def __init__(self, name, buffer):
         StringIO.__init__(self, buffer)
         self.name = name
@@ -18,42 +18,46 @@
     def setUp(self):
         self.options = Options()
 
-    def testApply(self):
-        expected = [
-            ('foo', 'test', ipaddr.IPAddress('127.0.0.1')),
-            ('bar', 'test', ipaddr.IPAddress('127.0.0.2')),
-        ]
+    def assertHostsEqual(self, hosts, expected):
+        for host, expect in zip(hosts, expected):
+            host_str, attrs = expect
 
-        for expect, host in zip(expected, config.apply(self.options, ['etc/hosts/test'])):
-            hostname, domain, ip = expect
+            self.assertEquals(str(host), host_str)
 
-            self.assertEquals(str(host), hostname)
-            self.assertEquals(host.domain, domain)
-            self.assertEquals(host.ip, ip)
+            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):
         with self.assertRaises(config.HostConfigError):
             list(config.apply_hosts(self.options, ['nonexistant']))
 
     def testApplyHosts(self):
-        expected = [
-            ('foo', 'test', ipaddr.IPAddress('127.0.0.1')),
-            ('bar', 'test', ipaddr.IPAddress('127.0.0.2')),
-        ]
-
-        for expect, host in zip(expected, config.apply_hosts_file(self.options, NamedStringIO('test', """
+        conf_file = ConfFile('test', """
 [foo]
     ip = 127.0.0.1
 
 [bar]
     ip = 127.0.0.2
-"""
-        ))):
-            hostname, domain, ip = expect
+        """)
+        expected = [
+                ('foo@test', dict(ip=ipaddr.IPAddress('127.0.0.1'))),
+                ('bar@test', dict(ip=ipaddr.IPAddress('127.0.0.2'))),
+        ]
 
-            self.assertEquals(str(host), hostname)
-            self.assertEquals(host.domain, domain)
-            self.assertEquals(host.ip, ip)
- 
+        self.assertHostsEqual(config.apply_hosts_file(self.options, conf_file), expected)
+
+    def testApplyHostsExpand(self):
+        self.assertHostsEqual(config.apply_host_config(self.options, 'etc/hosts/asdf', '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'))),
+        ])
+
 if __name__ == '__main__':
     unittest.main()