pvl.hosts.tests: support multiple values for record name/type in assertZoneEquals; test multi-alias hosts
authorTero Marttila <tero.marttila@aalto.fi>
Mon, 02 Mar 2015 13:30:15 +0200
changeset 688 dfc5fcb6a06c
parent 687 f99f9e3d02cf
child 689 c258e3ff6d32
pvl.hosts.tests: support multiple values for record name/type in assertZoneEquals; test multi-alias hosts
pvl/hosts/tests.py
--- a/pvl/hosts/tests.py	Mon Mar 02 13:26:34 2015 +0200
+++ b/pvl/hosts/tests.py	Mon Mar 02 13:30:15 2015 +0200
@@ -175,14 +175,24 @@
 
 class TestZoneMixin(object):
     def assertZoneEquals(self, rrs, expected):
+        """
+            Tests that the given list of ZoneRecords is equal to the given {(rr.name, rr.type): str(rr.data)} dict.
+
+            Multiple records for the same name/type are gathered as a list. XXX: ordering
+        """
+
         gather = { }
 
         for rr in rrs:
             key = (rr.name.lower(), rr.type.upper())
-
-            self.assertNotIn(key, gather)
-
-            gather[key] = rr.data
+            value = '\t'.join(rr.data)
+            
+            if key not in gather:
+                gather[key] = value
+            elif not isinstance(gather[key], list):
+                gather[key] = [gather[key], value]
+            else:
+                gather[key].append(value)
 
         self.assertDictEqual(gather, expected)
 
@@ -202,8 +212,8 @@
         )
 
         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
-            ('host', 'A'): ['192.0.2.1'],
-            ('host', 'AAAA'): ['2001:db8::c000:201'],
+            ('host', 'A'): '192.0.2.1',
+            ('host', 'AAAA'): '2001:db8::c000:201',
         })
     
     def testHostAlias(self):
@@ -215,9 +225,9 @@
         self.assertEquals(h.alias, ['test', '*.test'])
 
         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
-            ('host', 'A'): ['192.0.2.1'],
-            ('test', 'CNAME'): ['host'],
-            ('*.test', 'CNAME'): ['host'],
+            ('host', 'A'): '192.0.2.1',
+            ('test', 'CNAME'): 'host',
+            ('*.test', 'CNAME'): 'host',
         })
 
     def testHostAlias46(self):
@@ -229,10 +239,10 @@
         )
 
         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
-            ('host', 'A'): ['192.0.2.1'],
-            ('host', 'AAAA'): ['2001:db8::c000:201'],
-            ('test4', 'A'): ['192.0.2.1'],
-            ('test6', 'AAAA'): ['2001:db8::c000:201'],
+            ('host', 'A'): '192.0.2.1',
+            ('host', 'AAAA'): '2001:db8::c000:201',
+            ('test4', 'A'): '192.0.2.1',
+            ('test6', 'AAAA'): '2001:db8::c000:201',
         })
 
     def testHostAlias4Missing(self):
@@ -270,7 +280,7 @@
         )
 
         self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
-            ('host', 'CNAME'): ['host.example.net.'],
+            ('host', 'CNAME'): 'host.example.net.',
         })
 
     def testHostForwardAlias(self):
@@ -280,8 +290,8 @@
         )
 
         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
-            ('host', 'CNAME'): ['host.example.net.'],
-            ('test', 'CNAME'): ['host'],
+            ('host', 'CNAME'): 'host.example.net.',
+            ('test', 'CNAME'): 'host',
         })
 
     def testHostLocation(self):
@@ -293,8 +303,8 @@
         self.assertEquals(h.location, ('test', 'domain'))
 
         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
-            ('host', 'A'): ['192.0.2.1'],
-            ('test', 'CNAME'): ['host'],
+            ('host', 'A'): '192.0.2.1',
+            ('test', 'CNAME'): 'host',
         })
 
     def testHostLocationDomain(self):
@@ -306,8 +316,8 @@
         self.assertEquals(h.location, ('test', 'bar.domain'))
 
         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
-            ('host.foo', 'A'): ['192.0.2.1'],
-            ('test.bar', 'CNAME'): ['host.foo'],
+            ('host.foo', 'A'): '192.0.2.1',
+            ('test.bar', 'CNAME'): 'host.foo',
         })
 
     def testHostLocationDomainOutOfOrigin(self):
@@ -320,7 +330,7 @@
 
         with self.assertRaises(zone.HostZoneError):
             self.assertZoneEquals(zone.host_forward(h, 'foo.domain'), {
-                ('host', 'A'): ['192.0.2.1'],
+                ('host', 'A'): '192.0.2.1',
             })
         
         # TODO
@@ -351,10 +361,28 @@
         self.assertEquals(unicode(rd), '$ORIGIN\tdomain.')
 
         self.assertZoneEquals(rrs, {
-            ('foo', 'A'): ['192.0.2.1'],
-            ('foo', 'AAAA'): ['2001:db8::c000:201'],
-            ('test', 'CNAME'): ['foo'],
-            ('bar', 'A'): ['192.0.2.2'],
+            ('foo', 'A'): '192.0.2.1',
+            ('foo', 'AAAA'): '2001:db8::c000:201',
+            ('test', 'CNAME'): 'foo',
+            ('bar', 'A'): '192.0.2.2',
+        })
+
+    def testHostsMultiAlias(self):
+        hosts = [
+                Host.build('foo', 'domain',
+                    ip      = '192.0.2.1',
+                    alias4  = 'test',
+                ),
+                Host.build('bar', 'domain',
+                    ip      = '192.0.2.2',
+                    alias4  = 'test',
+                )
+        ]
+
+        self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=False), {
+            ('foo', 'A'): '192.0.2.1',
+            ('bar', 'A'): '192.0.2.2',
+            ('test', 'A'): ['192.0.2.1', '192.0.2.2'],
         })
 
     def testHostsConflict(self):
@@ -412,11 +440,11 @@
         )
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
-            ('1', 'PTR'): ['host.domain.'],
+            ('1', 'PTR'): 'host.domain.',
         })
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
-            ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): ['host.domain.'],
+            ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.domain.',
         })
 
     def testHostIP4(self):
@@ -425,15 +453,15 @@
         )
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
-            ('1', 'PTR'): ['host.domain.'],
+            ('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.'],
+            ('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.'],
+            ('1.2.0', 'PTR'): 'host.domain.',
         })
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
@@ -449,7 +477,7 @@
         })
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
-            ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): ['host.domain.'],
+            ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.domain.',
         })
 
     def testHostIPOutOfPrefix(self):
@@ -473,12 +501,12 @@
         )
 
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
-            ('3', 'PTR'): ['host.example.net.'],
+            ('3', 'PTR'): 'host.example.net.',
 
         })
         
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
-            ('3.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): ['host.example.net.'],
+            ('3.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.example.net.',
         })
 
     def testHostDelegate(self):
@@ -494,7 +522,7 @@
         })
 
         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.'],
+            ('1', 'CNAME'): '1.0/28.2.0.192.in-addr.arpa.',
         })
         
         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
@@ -512,14 +540,14 @@
         ]
         
         self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddr.IPNetwork('192.0.2.1/24')), {
-            ('1', 'PTR'): ['foo.domain.'],
-            ('2', 'PTR'): ['bar.domain.'],
+            ('1', 'PTR'): 'foo.domain.',
+            ('2', 'PTR'): 'bar.domain.',
         })
         
         # in ip order
         self.assertZoneEquals(zone.apply_hosts_reverse(reversed(hosts), ipaddr.IPNetwork('192.0.2.1/24')), {
-            ('1', 'PTR'): ['foo.domain.'],
-            ('2', 'PTR'): ['bar.domain.'],
+            ('1', 'PTR'): 'foo.domain.',
+            ('2', 'PTR'): 'bar.domain.',
         })
 
     def testHostsConflict(self):
@@ -549,12 +577,12 @@
                 unknown_host = 'ufc',
                 unknown_domain = 'domain',
         ), {
-            ('1', 'PTR'): ['foo.domain.'],
-            ('2', 'PTR'): ['ufc.domain.'],
-            ('3', 'PTR'): ['ufc.domain.'],
-            ('4', 'PTR'): ['ufc.domain.'],
-            ('5', 'PTR'): ['bar.domain.'],
-            ('6', 'PTR'): ['ufc.domain.'],
+            ('1', 'PTR'): 'foo.domain.',
+            ('2', 'PTR'): 'ufc.domain.',
+            ('3', 'PTR'): 'ufc.domain.',
+            ('4', 'PTR'): 'ufc.domain.',
+            ('5', 'PTR'): 'bar.domain.',
+            ('6', 'PTR'): 'ufc.domain.',
         })
 
 class TestDhcp(unittest.TestCase):