pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
authorTero Marttila <tero.marttila@aalto.fi>
Wed, 25 Feb 2015 14:41:51 +0200
changeset 463 2cbdb2435487
parent 462 6d699c76d75d
child 464 f1d3dbf04ca3
pvl.hosts.zone: fix pvl.dns.fqdn() and basic tests for host_forward/reverse
pvl/hosts/tests.py
pvl/hosts/zone.py
--- a/pvl/hosts/tests.py	Wed Feb 25 14:29:40 2015 +0200
+++ b/pvl/hosts/tests.py	Wed Feb 25 14:41:51 2015 +0200
@@ -1,7 +1,7 @@
 import ipaddr
 import unittest
 
-from pvl.hosts import config, zone
+from pvl.hosts import config, host, zone
 from StringIO import StringIO
 
 class Options(object):
@@ -89,6 +89,18 @@
         self.options = Options()
         self.options.add_origin = False
 
+    def assertZoneEquals(self, rrs, expected):
+        gather = { }
+
+        for rr in rrs:
+            key = (rr.name.lower(), rr.type.upper())
+
+            self.assertNotIn(key, gather)
+
+            gather[key] = rr.data
+
+        self.assertDictEqual(gather, expected)
+
     def testResolve(self):
         self.assertEquals(zone.resolve(None, None, 'host'), 'host.')
         self.assertEquals(zone.resolve(None, 'domain', 'host'), 'host.domain.')
@@ -105,5 +117,37 @@
         with self.assertRaises(zone.HostZoneError):
             zone.resolve('origin', None, 'host.domain')
 
+    def testHostForwardOutOfOrigin(self):
+        h = host.Host('host', 'domain', ip=ipaddr.IPAddress('10.0.0.1'))
+
+        self.assertZoneEquals(zone.host_forward(h, 'test'), { })
+
+    def testHostForwardIP(self):
+        h = host.Host.build('host', 'domain',
+                ip  = '192.0.2.1',
+                ip6 = '2001:db8::192.0.2.1',
+        )
+
+        self.assertZoneEquals(zone.host_forward(h, 'domain'), {
+            ('host', 'A'): ['192.0.2.1'],
+            ('host', 'AAAA'): ['2001:db8::c000:201'],
+        })
+
+    def testHostDelegate(self):
+        h = host.Host.build('host', 'example.com',
+                ip      = '192.0.2.1',
+                ip6     = '2001:db8::192.0.2.1',
+                forward = 'host.example.net',
+                reverse = '1.0/28.2.0.192.in-addr.arpa',
+        )
+
+        self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
+            ('host', 'CNAME'): ['host.example.net.'],
+        })
+
+        self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
+            ('1', 'CNAME'): ['1.0/28.2.0.192.in-addr.arpa.'],
+        })
+
 if __name__ == '__main__':
     unittest.main()
--- a/pvl/hosts/zone.py	Wed Feb 25 14:29:40 2015 +0200
+++ b/pvl/hosts/zone.py	Wed Feb 25 14:41:51 2015 +0200
@@ -49,7 +49,7 @@
         return
 
     if host.forward:
-        forward = pvl.dns.zone.fqdn(host.forward)
+        forward = pvl.dns.fqdn(host.forward)
 
         log.info("%s: forward: %s", host, forward)
 
@@ -108,7 +108,7 @@
     label = pvl.dns.reverse_label(prefix, ip)
    
     if host.reverse :
-        alias = pvl.dns.zone.fqdn(host.reverse)
+        alias = pvl.dns.fqdn(host.reverse)
         
         log.info("%s %s[%s]: CNAME %s", host, prefix, ip, alias)