pvl.hosts.zone: fix and test --unknown-host
authorTero Marttila <tero.marttila@aalto.fi>
Wed, 25 Feb 2015 15:49:11 +0200
changeset 474 51983fcda6b1
parent 473 68fd85d850ab
child 475 a76571e27c6f
pvl.hosts.zone: fix and test --unknown-host
pvl/hosts/tests.py
pvl/hosts/zone.py
--- a/pvl/hosts/tests.py	Wed Feb 25 15:40:58 2015 +0200
+++ b/pvl/hosts/tests.py	Wed Feb 25 15:49:11 2015 +0200
@@ -341,6 +341,14 @@
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
             ('1', 'PTR'): ['host.domain.'],
         })
+        
+        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.0.0/16'))), {
+            ('1.2', 'PTR'): ['host.domain.'],
+        })
+        
+        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.0.0/12'))), {
+            ('1.2.0', 'PTR'): ['host.domain.'],
+        })
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
 
@@ -392,6 +400,39 @@
 
         })
 
+    def testHostsConflict(self):
+        hosts = [
+                host.Host.build('foo', 'domain',
+                    ip      = '192.0.2.1',
+                ),
+                host.Host.build('bar', 'domain',
+                    ip      = '192.0.2.1',
+                )
+        ]
+        
+        with self.assertRaises(zone.HostZoneError):
+            self.assertZoneEquals(zone.apply_hosts_reverse(self.options, hosts, ipaddr.IPNetwork('192.0.2.1/24')), { })
+
+    def testHostsGenerateUnknown(self):
+        hosts = [
+                host.Host.build('foo', 'domain',
+                    ip      = '192.0.2.1',
+                ),
+                host.Host.build('bar', 'domain',
+                    ip      = '192.0.2.5',
+                ),
+        ]
+        
+        self.options.unknown_host = 'ufc'
+        self.options.hosts_domain = 'domain'
+        self.assertZoneEquals(zone.apply_hosts_reverse(self.options, hosts, ipaddr.IPNetwork('192.0.2.1/29')), {
+            ('1', 'PTR'): ['foo.domain.'],
+            ('2', 'PTR'): ['ufc.domain.'],
+            ('3', 'PTR'): ['ufc.domain.'],
+            ('4', 'PTR'): ['ufc.domain.'],
+            ('5', 'PTR'): ['bar.domain.'],
+            ('6', 'PTR'): ['ufc.domain.'],
+        })
 
 if __name__ == '__main__':
     unittest.main()
--- a/pvl/hosts/zone.py	Wed Feb 25 15:40:58 2015 +0200
+++ b/pvl/hosts/zone.py	Wed Feb 25 15:49:11 2015 +0200
@@ -179,6 +179,9 @@
 
         Yields ZoneRecords in IPAddress-order
     """
+        
+    if options.unknown_host and not options.hosts_domain:
+        raise Exception("--unknown-host requires --hosts-domain")
     
     # collect data for records
     by_ip = dict()
@@ -204,13 +207,13 @@
         elif options.unknown_host:
             # synthesize a record
             label = pvl.dns.reverse_label(prefix, ip)
-            fqdn = pvl.dns.zone.fqdn(options.unknown_host, options.hosts_domain)
+            fqdn = pvl.dns.fqdn(options.unknown_host, options.hosts_domain)
 
             log.info("%s %s[%s]: unused PTR %s", options.unknown_host, ip, prefix, fqdn)
 
-            yield pvl.dns.zone.ZoneRecord.PTR(label, fqdn)
+            yield pvl.dns.ZoneRecord.PTR(label, fqdn)
 
-        else :
+        else:
             continue
 
 import pvl.args