pvl/hosts/tests.py
changeset 740 74352351d6f5
parent 739 5149c39f3dfc
equal deleted inserted replaced
739:5149c39f3dfc 740:74352351d6f5
     1 import ipaddr
     1 import ipaddress
     2 import itertools
     2 import itertools
     3 import pvl.args
     3 import pvl.args
     4 import unittest
     4 import unittest
     5 
     5 
     6 from pvl.hosts import config, dhcp, zone
     6 from pvl.hosts import config, dhcp, zone
   120                 ('asdf.test2@', dict()),
   120                 ('asdf.test2@', dict()),
   121         ])
   121         ])
   122 
   122 
   123     def testApplyHostExpand(self):
   123     def testApplyHostExpand(self):
   124         self.assertHostsEqual(config.apply_hosts('test', 'asdf{1-3}', 
   124         self.assertHostsEqual(config.apply_hosts('test', 'asdf{1-3}', 
   125                 { 'ip': '10.100.100.$' }
   125                 { 'ip': u'10.100.100.$' }
   126         ), [
   126         ), [
   127                 ('asdf1@test', dict(ip4=ipaddr.IPAddress('10.100.100.1'))),
   127                 ('asdf1@test', dict(ip4=ipaddress.ip_address(u'10.100.100.1'))),
   128                 ('asdf2@test', dict(ip4=ipaddr.IPAddress('10.100.100.2'))),
   128                 ('asdf2@test', dict(ip4=ipaddress.ip_address(u'10.100.100.2'))),
   129                 ('asdf3@test', dict(ip4=ipaddr.IPAddress('10.100.100.3'))),
   129                 ('asdf3@test', dict(ip4=ipaddress.ip_address(u'10.100.100.3'))),
   130         ])
   130         ])
   131 
   131 
   132     def testApplyHostsFileError(self):
   132     def testApplyHostsFileError(self):
   133         with self.assertRaises(config.HostConfigError):
   133         with self.assertRaises(config.HostConfigError):
   134             list(config.apply_hosts_files(self.options, ['nonexistant']))
   134             list(config.apply_hosts_files(self.options, ['nonexistant']))
   141 [bar]
   141 [bar]
   142     ip = 127.0.0.2
   142     ip = 127.0.0.2
   143         """)
   143         """)
   144         
   144         
   145         self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), [
   145         self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), [
   146                 ('foo@test', dict(ip4=ipaddr.IPAddress('127.0.0.1'))),
   146                 ('foo@test', dict(ip4=ipaddress.ip_address(u'127.0.0.1'))),
   147                 ('bar@test', dict(ip4=ipaddr.IPAddress('127.0.0.2'))),
   147                 ('bar@test', dict(ip4=ipaddress.ip_address(u'127.0.0.2'))),
   148         ])
   148         ])
   149 
   149 
   150     def testApplyHostsConfigNested(self):
   150     def testApplyHostsConfigNested(self):
   151         conf_file = ConfFile('test', """
   151         conf_file = ConfFile('test', """
   152 [asdf]
   152 [asdf]
   157     [[bar]]
   157     [[bar]]
   158         ip = 127.0.0.2
   158         ip = 127.0.0.2
   159         """)
   159         """)
   160 
   160 
   161         self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), [
   161         self.assertHostsEqual(config.apply_hosts_config(self.options, conf_file), [
   162                 ('foo@asdf.test', dict(ip4=ipaddr.IPAddress('127.0.0.1'))),
   162                 ('foo@asdf.test', dict(ip4=ipaddress.ip_address(u'127.0.0.1'))),
   163                 ('bar@quux.test', dict(ip4=ipaddr.IPAddress('127.0.0.2'))),
   163                 ('bar@quux.test', dict(ip4=ipaddress.ip_address(u'127.0.0.2'))),
   164         ])
   164         ])
   165 
   165 
   166     def testHostsConfigDdefaults(self):
   166     def testHostsConfigDdefaults(self):
   167         hosts = config.apply_hosts_config(self.options, ConfFile('test', """
   167         hosts = config.apply_hosts_config(self.options, ConfFile('test', """
   168 boot.next-server = boot.lan
   168 boot.next-server = boot.lan
   173     boot.filename = /pxelinux.0
   173     boot.filename = /pxelinux.0
   174         """))
   174         """))
   175         
   175         
   176         self.assertHostsEqual(hosts, [
   176         self.assertHostsEqual(hosts, [
   177                 ('foo@test', dict(
   177                 ('foo@test', dict(
   178                     ip4         = ipaddr.IPAddress('192.0.2.1'),
   178                     ip4         = ipaddress.ip_address(u'192.0.2.1'),
   179                     ethernet    = { 'eth0': '00:11:22:33:44:55' },
   179                     ethernet    = { 'eth0': '00:11:22:33:44:55' },
   180                     extensions  = dict(
   180                     extensions  = dict(
   181                         dhcp        = { 'next_server': 'boot.lan', 'filename': '/pxelinux.0' }
   181                         dhcp        = { 'next_server': 'boot.lan', 'filename': '/pxelinux.0' }
   182                     ),
   182                     ),
   183                 )),
   183                 )),
   186 
   186 
   187  
   187  
   188     def testApplyIncludes(self):
   188     def testApplyIncludes(self):
   189         self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/test']), [
   189         self.assertHostsEqual(config.apply_hosts_files(self.options, ['etc/hosts/test']), [
   190                 ('bar@test', dict(
   190                 ('bar@test', dict(
   191                     ip4         = ipaddr.IPAddress('192.0.2.2'),
   191                     ip4         = ipaddress.ip_address(u'192.0.2.2'),
   192                 )),
   192                 )),
   193                 ('foo@test', dict(
   193                 ('foo@test', dict(
   194                     ip4         = ipaddr.IPAddress('192.0.2.1'),
   194                     ip4         = ipaddress.ip_address(u'192.0.2.1'),
   195                 )),
   195                 )),
   196         ])
   196         ])
   197 
   197 
   198     def testApplyIncludesDefaults(self):
   198     def testApplyIncludesDefaults(self):
   199         self.assertHostsEqual(config.apply_hosts_config(self.options, ConfFile('test', """
   199         self.assertHostsEqual(config.apply_hosts_config(self.options, ConfFile('test', """
   200 boot.next-server = boot.lan
   200 boot.next-server = boot.lan
   201 
   201 
   202 include = etc/hosts/test
   202 include = etc/hosts/test
   203         """)), [
   203         """)), [
   204                 ('bar@test', dict(
   204                 ('bar@test', dict(
   205                     ip4         = ipaddr.IPAddress('192.0.2.2'),
   205                     ip4         = ipaddress.ip_address(u'192.0.2.2'),
   206                 )),
   206                 )),
   207                 ('foo@test', dict(
   207                 ('foo@test', dict(
   208                     ip4         = ipaddr.IPAddress('192.0.2.1'),
   208                     ip4         = ipaddress.ip_address(u'192.0.2.1'),
   209                 )),
   209                 )),
   210         ])
   210         ])
   211 
   211 
   212 
   212 
   213     def testApplyIncludePath(self):
   213     def testApplyIncludePath(self):
   218             include_trace   = include_trace,
   218             include_trace   = include_trace,
   219         ))
   219         ))
   220 
   220 
   221         self.assertHostsEqual(hosts, [
   221         self.assertHostsEqual(hosts, [
   222                 ('quux@asdf.test', dict(
   222                 ('quux@asdf.test', dict(
   223                     ip4         = ipaddr.IPAddress('192.0.2.5'),
   223                     ip4         = ipaddress.ip_address(u'192.0.2.5'),
   224                 )),
   224                 )),
   225                 ('bar@test', dict(
   225                 ('bar@test', dict(
   226                     ip4         = ipaddr.IPAddress('192.0.2.2'),
   226                     ip4         = ipaddress.ip_address(u'192.0.2.2'),
   227                 )),
   227                 )),
   228                 ('foo@test', dict(
   228                 ('foo@test', dict(
   229                     ip4         = ipaddr.IPAddress('192.0.2.1'),
   229                     ip4         = ipaddress.ip_address(u'192.0.2.1'),
   230                 )),
   230                 )),
   231         ])
   231         ])
   232 
   232 
   233         self.assertEqual(include_trace, [
   233         self.assertEqual(include_trace, [
   234             'etc/zones/forward/test',
   234             'etc/zones/forward/test',
   240         ])
   240         ])
   241 
   241 
   242     def testApply(self):
   242     def testApply(self):
   243         self.assertHostsEqual(config.apply(self.options, ['etc/hosts/example.com']), [
   243         self.assertHostsEqual(config.apply(self.options, ['etc/hosts/example.com']), [
   244                 ('foo@example.com', dict(
   244                 ('foo@example.com', dict(
   245                     ip4         = ipaddr.IPAddress('192.0.2.1'),
   245                     ip4         = ipaddress.ip_address(u'192.0.2.1'),
   246                     ethernet    = {None: '00:11:22:33:44:55'},
   246                     ethernet    = {None: '00:11:22:33:44:55'},
   247                 )),
   247                 )),
   248                 ('bar@example.com', dict(
   248                 ('bar@example.com', dict(
   249                     ip4         = ipaddr.IPAddress('192.0.2.2'),
   249                     ip4         = ipaddress.ip_address(u'192.0.2.2'),
   250                     ethernet    = {None: '01:23:45:67:89:ab'},
   250                     ethernet    = {None: '01:23:45:67:89:ab'},
   251                 )),
   251                 )),
   252         ])
   252         ])
   253 
   253 
   254 class TestZoneMixin(object):
   254 class TestZoneMixin(object):
   276 
   276 
   277 
   277 
   278 class TestForwardZone(TestZoneMixin, unittest.TestCase):
   278 class TestForwardZone(TestZoneMixin, unittest.TestCase):
   279     def testHostOutOfOrigin(self):
   279     def testHostOutOfOrigin(self):
   280         h = Host.build('host', 'domain', 
   280         h = Host.build('host', 'domain', 
   281                 ip  = '10.0.0.1',
   281                 ip  = u'10.0.0.1',
   282         )
   282         )
   283 
   283 
   284         self.assertZoneEquals(zone.host_forward(h, 'test'), { })
   284         self.assertZoneEquals(zone.host_forward(h, 'test'), { })
   285 
   285 
   286     def testHostIP(self):
   286     def testHostIP(self):
   287         h = Host.build('host', 'domain',
   287         h = Host.build('host', 'domain',
   288                 ip  = '192.0.2.1',
   288                 ip  = u'192.0.2.1',
   289                 ip6 = '2001:db8::192.0.2.1',
   289                 ip6 = u'2001:db8::192.0.2.1',
   290         )
   290         )
   291 
   291 
   292         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
   292         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
   293             ('host', 'A'): '192.0.2.1',
   293             ('host', 'A'): '192.0.2.1',
   294             ('host', 'AAAA'): '2001:db8::c000:201',
   294             ('host', 'AAAA'): '2001:db8::c000:201',
   295         })
   295         })
   296     
   296     
   297     def testHostAlias(self):
   297     def testHostAlias(self):
   298         h = Host.build('host', 'domain',
   298         h = Host.build('host', 'domain',
   299                 ip      = '192.0.2.1',
   299                 ip      = u'192.0.2.1',
   300                 alias   = 'test *.test',
   300                 alias   = 'test *.test',
   301         )
   301         )
   302 
   302 
   303         self.assertEquals(h.alias, ['test', '*.test'])
   303         self.assertEquals(h.alias, ['test', '*.test'])
   304 
   304 
   308             ('*.test', 'CNAME'): 'host',
   308             ('*.test', 'CNAME'): 'host',
   309         })
   309         })
   310 
   310 
   311     def testHostAlias46(self):
   311     def testHostAlias46(self):
   312         h = Host.build('host', 'domain',
   312         h = Host.build('host', 'domain',
   313                 ip      = '192.0.2.1',
   313                 ip      = u'192.0.2.1',
   314                 ip6     = '2001:db8::192.0.2.1',
   314                 ip6     = u'2001:db8::192.0.2.1',
   315                 alias4  = 'test4',
   315                 alias4  = 'test4',
   316                 alias6  = 'test6',
   316                 alias6  = 'test6',
   317         )
   317         )
   318 
   318 
   319         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
   319         self.assertZoneEquals(zone.host_forward(h, 'domain'), {
   323             ('test6', 'AAAA'): '2001:db8::c000:201',
   323             ('test6', 'AAAA'): '2001:db8::c000:201',
   324         })
   324         })
   325 
   325 
   326     def testHostAlias4Missing(self):
   326     def testHostAlias4Missing(self):
   327         h = Host.build('host', 'domain',
   327         h = Host.build('host', 'domain',
   328                 ip6     = '2001:db8::192.0.2.1',
   328                 ip6     = u'2001:db8::192.0.2.1',
   329                 alias4  = 'test4',
   329                 alias4  = 'test4',
   330                 alias6  = 'test6',
   330                 alias6  = 'test6',
   331         )
   331         )
   332 
   332 
   333         with self.assertRaises(zone.HostZoneError):
   333         with self.assertRaises(zone.HostZoneError):
   334             self.assertZoneEquals(zone.host_forward(h, 'domain'), { })
   334             self.assertZoneEquals(zone.host_forward(h, 'domain'), { })
   335 
   335 
   336     def testHostAlias6Missing(self):
   336     def testHostAlias6Missing(self):
   337         h = Host.build('host', 'domain',
   337         h = Host.build('host', 'domain',
   338                 ip      = '192.0.2.1',
   338                 ip      = u'192.0.2.1',
   339                 alias4  = 'test4',
   339                 alias4  = 'test4',
   340                 alias6  = 'test6',
   340                 alias6  = 'test6',
   341         )
   341         )
   342 
   342 
   343         with self.assertRaises(zone.HostZoneError):
   343         with self.assertRaises(zone.HostZoneError):
   344             self.assertZoneEquals(zone.host_forward(h, 'domain'), { })
   344             self.assertZoneEquals(zone.host_forward(h, 'domain'), { })
   345 
   345 
   346     def testHostFQDN(self):
   346     def testHostFQDN(self):
   347         h = Host.build('host.example.net', None,
   347         h = Host.build('host.example.net', None,
   348                 ip          = '192.0.2.3',
   348                 ip          = u'192.0.2.3',
   349         )
   349         )
   350 
   350 
   351         self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
   351         self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
   352 
   352 
   353         })
   353         })
   372             ('test', 'CNAME'): 'host',
   372             ('test', 'CNAME'): 'host',
   373         })
   373         })
   374 
   374 
   375     def testHostLocation(self):
   375     def testHostLocation(self):
   376         h = Host.build('host', 'domain',
   376         h = Host.build('host', 'domain',
   377                 ip          = '192.0.2.1',
   377                 ip          = u'192.0.2.1',
   378                 location    = 'test',
   378                 location    = 'test',
   379         )
   379         )
   380 
   380 
   381         self.assertEquals(h.location, ('test', 'domain'))
   381         self.assertEquals(h.location, ('test', 'domain'))
   382 
   382 
   385             ('test', 'CNAME'): 'host',
   385             ('test', 'CNAME'): 'host',
   386         })
   386         })
   387 
   387 
   388     def testHostLocationDomain(self):
   388     def testHostLocationDomain(self):
   389         h = Host.build('host', 'foo.domain',
   389         h = Host.build('host', 'foo.domain',
   390                 ip          = '192.0.2.1',
   390                 ip          = u'192.0.2.1',
   391                 location    = 'test@bar.domain',
   391                 location    = 'test@bar.domain',
   392         )
   392         )
   393 
   393 
   394         self.assertEquals(h.location, ('test', 'bar.domain'))
   394         self.assertEquals(h.location, ('test', 'bar.domain'))
   395 
   395 
   398             ('test.bar', 'CNAME'): 'host.foo',
   398             ('test.bar', 'CNAME'): 'host.foo',
   399         })
   399         })
   400 
   400 
   401     def testHostLocationDomainOutOfOrigin(self):
   401     def testHostLocationDomainOutOfOrigin(self):
   402         h = Host.build('host', 'foo.domain',
   402         h = Host.build('host', 'foo.domain',
   403                 ip          = '192.0.2.1',
   403                 ip          = u'192.0.2.1',
   404                 location    = 'test@bar.domain',
   404                 location    = 'test@bar.domain',
   405         )
   405         )
   406 
   406 
   407         self.assertEquals(h.location, ('test', 'bar.domain'))
   407         self.assertEquals(h.location, ('test', 'bar.domain'))
   408 
   408 
   417         #})
   417         #})
   418 
   418 
   419     def testHostsForward(self):
   419     def testHostsForward(self):
   420         hosts = [
   420         hosts = [
   421                 Host.build('foo', 'domain',
   421                 Host.build('foo', 'domain',
   422                     ip      = '192.0.2.1',
   422                     ip      = u'192.0.2.1',
   423                     ip6     = '2001:db8::192.0.2.1',
   423                     ip6     = u'2001:db8::192.0.2.1',
   424                     alias   = 'test',
   424                     alias   = 'test',
   425                 ),
   425                 ),
   426                 Host.build('bar', 'domain',
   426                 Host.build('bar', 'domain',
   427                     ip      = '192.0.2.2',
   427                     ip      = u'192.0.2.2',
   428                 ),
   428                 ),
   429                 Host.build('quux', 'example',
   429                 Host.build('quux', 'example',
   430                     ip      = '192.0.2.3',
   430                     ip      = u'192.0.2.3',
   431                 ),
   431                 ),
   432         ]
   432         ]
   433                 
   433                 
   434         rrs = zone.apply_hosts_forward(hosts, 'domain', add_origin=True)
   434         rrs = zone.apply_hosts_forward(hosts, 'domain', add_origin=True)
   435     
   435     
   446         })
   446         })
   447 
   447 
   448     def testHostsMultiAlias(self):
   448     def testHostsMultiAlias(self):
   449         hosts = [
   449         hosts = [
   450                 Host.build('foo', 'domain',
   450                 Host.build('foo', 'domain',
   451                     ip      = '192.0.2.1',
   451                     ip      = u'192.0.2.1',
   452                     alias4  = 'test',
   452                     alias4  = 'test',
   453                 ),
   453                 ),
   454                 Host.build('bar', 'domain',
   454                 Host.build('bar', 'domain',
   455                     ip      = '192.0.2.2',
   455                     ip      = u'192.0.2.2',
   456                     alias4  = 'test',
   456                     alias4  = 'test',
   457                 )
   457                 )
   458         ]
   458         ]
   459 
   459 
   460         self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=False), {
   460         self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=False), {
   464         })
   464         })
   465 
   465 
   466     def testHostsConflict(self):
   466     def testHostsConflict(self):
   467         hosts = [
   467         hosts = [
   468                 Host.build('foo', 'domain',
   468                 Host.build('foo', 'domain',
   469                     ip      = '192.0.2.1',
   469                     ip      = u'192.0.2.1',
   470                 ),
   470                 ),
   471                 Host.build('foo', 'domain',
   471                 Host.build('foo', 'domain',
   472                     ip      = '192.0.2.2',
   472                     ip      = u'192.0.2.2',
   473                 )
   473                 )
   474         ]
   474         ]
   475         
   475         
   476         with self.assertRaises(zone.HostZoneError):
   476         with self.assertRaises(zone.HostZoneError):
   477             self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=True), { })
   477             self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=True), { })
   478 
   478 
   479     def testHostsAliasConflict(self):
   479     def testHostsAliasConflict(self):
   480         hosts = [
   480         hosts = [
   481                 Host.build('foo', 'domain',
   481                 Host.build('foo', 'domain',
   482                     ip          = '192.0.2.1',
   482                     ip          = u'192.0.2.1',
   483                 ),
   483                 ),
   484                 Host.build('bar', 'domain',
   484                 Host.build('bar', 'domain',
   485                     ip          = '192.0.2.2',
   485                     ip          = u'192.0.2.2',
   486                     alias       = 'foo',
   486                     alias       = 'foo',
   487                 )
   487                 )
   488         ]
   488         ]
   489         
   489         
   490         # with A first
   490         # with A first
   496             self.assertZoneEquals(zone.apply_hosts_forward(reversed(hosts), 'domain'), { })
   496             self.assertZoneEquals(zone.apply_hosts_forward(reversed(hosts), 'domain'), { })
   497 
   497 
   498     def testHostsAlias4Conflict(self):
   498     def testHostsAlias4Conflict(self):
   499         hosts = [
   499         hosts = [
   500                 Host.build('foo', 'domain',
   500                 Host.build('foo', 'domain',
   501                     ip          = '192.0.2.1',
   501                     ip          = u'192.0.2.1',
   502                 ),
   502                 ),
   503                 Host.build('bar', 'domain',
   503                 Host.build('bar', 'domain',
   504                     ip          = '192.0.2.2',
   504                     ip          = u'192.0.2.2',
   505                     alias4      = 'foo',
   505                     alias4      = 'foo',
   506                 )
   506                 )
   507         ]
   507         ]
   508         
   508         
   509         with self.assertRaises(zone.HostZoneError):
   509         with self.assertRaises(zone.HostZoneError):
   511     
   511     
   512 
   512 
   513 class TestReverseZone(TestZoneMixin, unittest.TestCase):
   513 class TestReverseZone(TestZoneMixin, unittest.TestCase):
   514     def testHostIP(self):
   514     def testHostIP(self):
   515         h = Host.build('host', 'domain',
   515         h = Host.build('host', 'domain',
   516                 ip  = '192.0.2.1',
   516                 ip  = u'192.0.2.1',
   517                 ip6 = '2001:db8::192.0.2.1',
   517                 ip6 = u'2001:db8::192.0.2.1',
   518         )
   518         )
   519 
   519 
   520         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
   520         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.2.0/24'))), {
   521             ('1', 'PTR'): 'host.domain.',
   521             ('1', 'PTR'): 'host.domain.',
   522         })
   522         })
   523 
   523 
   524         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
   524         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'2001:db8::/64'))), {
   525             ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.domain.',
   525             ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.domain.',
   526         })
   526         })
   527 
   527 
   528     def testHostIP4(self):
   528     def testHostIP4(self):
   529         h = Host.build('host', 'domain',
   529         h = Host.build('host', 'domain',
   530                 ip  = '192.0.2.1',
   530                 ip  = u'192.0.2.1',
   531         )
   531         )
   532 
   532 
   533         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
   533         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.2.0/24'))), {
   534             ('1', 'PTR'): 'host.domain.',
   534             ('1', 'PTR'): 'host.domain.',
   535         })
   535         })
   536         
   536         
   537         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.0.0/16'))), {
   537         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.0.0/16'))), {
   538             ('1.2', 'PTR'): 'host.domain.',
   538             ('1.2', 'PTR'): 'host.domain.',
   539         })
   539         })
   540         
   540         
   541         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.0.0/12'))), {
   541         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.0.0/12'))), {
   542             ('1.2.0', 'PTR'): 'host.domain.',
   542             ('1.2.0', 'PTR'): 'host.domain.',
   543         })
   543         })
   544 
   544 
   545         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
   545         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'2001:db8::/64'))), {
   546 
   546 
   547         })
   547         })
   548 
   548 
   549     def testHostIP6(self):
   549     def testHostIP6(self):
   550         h = Host.build('host', 'domain',
   550         h = Host.build('host', 'domain',
   551                 ip6 = '2001:db8::192.0.2.1',
   551                 ip6 = u'2001:db8::192.0.2.1',
   552         )
   552         )
   553 
   553 
   554         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
   554         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.2.0/24'))), {
   555         })
   555         })
   556 
   556 
   557         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
   557         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'2001:db8::/64'))), {
   558             ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.domain.',
   558             ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.domain.',
   559         })
   559         })
   560 
   560 
   561     def testHostIPOutOfPrefix(self):
   561     def testHostIPOutOfPrefix(self):
   562         h = Host.build('host', 'domain',
   562         h = Host.build('host', 'domain',
   563                 ip  = '192.0.2.1',
   563                 ip  = u'192.0.2.1',
   564                 ip6 = '2001:db8::192.0.2.1',
   564                 ip6 = u'2001:db8::192.0.2.1',
   565         )
   565         )
   566 
   566 
   567         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.1.0/24'))), {
   567         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.1.0/24'))), {
   568 
   568 
   569         })
   569         })
   570 
   570 
   571         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8:1::/64'))), {
   571         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'2001:db8:1::/64'))), {
   572 
   572 
   573         })
   573         })
   574 
   574 
   575     def testHostFQDN(self):
   575     def testHostFQDN(self):
   576         h = Host.build('host.example.net', None,
   576         h = Host.build('host.example.net', None,
   577                 ip          = '192.0.2.3',
   577                 ip          = u'192.0.2.3',
   578                 ip6         = '2001:db8::192.0.2.3',
   578                 ip6         = u'2001:db8::192.0.2.3',
   579         )
   579         )
   580 
   580 
   581         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
   581         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.2.0/24'))), {
   582             ('3', 'PTR'): 'host.example.net.',
   582             ('3', 'PTR'): 'host.example.net.',
   583 
   583 
   584         })
   584         })
   585         
   585         
   586         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
   586         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'2001:db8::/64'))), {
   587             ('3.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.example.net.',
   587             ('3.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): 'host.example.net.',
   588         })
   588         })
   589 
   589 
   590     def testHostDelegate(self):
   590     def testHostDelegate(self):
   591         h = Host.build('host', 'example.com',
   591         h = Host.build('host', 'example.com',
   592                 ip      = '192.0.2.1',
   592                 ip      = u'192.0.2.1',
   593                 ip6     = '2001:db8::192.0.2.1',
   593                 ip6     = u'2001:db8::192.0.2.1',
   594                 forward = '',
   594                 forward = '',
   595                 reverse = '1.0/28.2.0.192.in-addr.arpa',
   595                 reverse = '1.0/28.2.0.192.in-addr.arpa',
   596         )
   596         )
   597 
   597 
   598         self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
   598         self.assertZoneEquals(zone.host_forward(h, 'example.com'), {
   599 
   599 
   600         })
   600         })
   601 
   601 
   602         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), {
   602         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'192.0.2.0/24'))), {
   603             ('1', 'CNAME'): '1.0/28.2.0.192.in-addr.arpa.',
   603             ('1', 'CNAME'): '1.0/28.2.0.192.in-addr.arpa.',
   604         })
   604         })
   605         
   605         
   606         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), {
   606         self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddress.ip_network(u'2001:db8::/64'))), {
   607 
   607 
   608         })
   608         })
   609 
   609 
   610     def testHosts(self):
   610     def testHosts(self):
   611         hosts = [
   611         hosts = [
   612                 Host.build('foo', 'domain',
   612                 Host.build('foo', 'domain',
   613                     ip      = '192.0.2.1',
   613                     ip      = u'192.0.2.1',
   614                 ),
   614                 ),
   615                 Host.build('bar', 'domain',
   615                 Host.build('bar', 'domain',
   616                     ip      = '192.0.2.2',
   616                     ip      = u'192.0.2.2',
   617                 )
   617                 )
   618         ]
   618         ]
   619         
   619         
   620         self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddr.IPNetwork('192.0.2.1/24')), {
   620         self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddress.ip_network(u'192.0.2.0/24')), {
   621             ('1', 'PTR'): 'foo.domain.',
   621             ('1', 'PTR'): 'foo.domain.',
   622             ('2', 'PTR'): 'bar.domain.',
   622             ('2', 'PTR'): 'bar.domain.',
   623         })
   623         })
   624         
   624         
   625         # in ip order
   625         # in ip order
   626         self.assertZoneEquals(zone.apply_hosts_reverse(reversed(hosts), ipaddr.IPNetwork('192.0.2.1/24')), {
   626         self.assertZoneEquals(zone.apply_hosts_reverse(reversed(hosts), ipaddress.ip_network(u'192.0.2.0/24')), {
   627             ('1', 'PTR'): 'foo.domain.',
   627             ('1', 'PTR'): 'foo.domain.',
   628             ('2', 'PTR'): 'bar.domain.',
   628             ('2', 'PTR'): 'bar.domain.',
   629         })
   629         })
   630 
   630 
   631     def testHostsConflict(self):
   631     def testHostsConflict(self):
   632         hosts = [
   632         hosts = [
   633                 Host.build('foo', 'domain',
   633                 Host.build('foo', 'domain',
   634                     ip      = '192.0.2.1',
   634                     ip      = u'192.0.2.1',
   635                 ),
   635                 ),
   636                 Host.build('bar', 'domain',
   636                 Host.build('bar', 'domain',
   637                     ip      = '192.0.2.1',
   637                     ip      = u'192.0.2.1',
   638                 )
   638                 )
   639         ]
   639         ]
   640         
   640         
   641         with self.assertRaises(zone.HostZoneError):
   641         with self.assertRaises(zone.HostZoneError):
   642             self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddr.IPNetwork('192.0.2.1/24')), { })
   642             self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddress.ip_network(u'192.0.2.0/24')), { })
   643 
   643 
   644     def testHostsGenerateUnknown(self):
   644     def testHostsGenerateUnknown(self):
   645         hosts = [
   645         hosts = [
   646                 Host.build('foo', 'domain',
   646                 Host.build('foo', 'domain',
   647                     ip      = '192.0.2.1',
   647                     ip      = u'192.0.2.1',
   648                 ),
   648                 ),
   649                 Host.build('bar', 'domain',
   649                 Host.build('bar', 'domain',
   650                     ip      = '192.0.2.5',
   650                     ip      = u'192.0.2.5',
   651                 ),
   651                 ),
   652         ]
   652         ]
   653         
   653         
   654         self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddr.IPNetwork('192.0.2.1/29'),
   654         self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddress.ip_network(u'192.0.2.0/29'),
   655                 unknown_host = 'ufc',
   655                 unknown_host = 'ufc',
   656                 unknown_domain = 'domain',
   656                 unknown_domain = 'domain',
   657         ), {
   657         ), {
   658             ('1', 'PTR'): 'foo.domain.',
   658             ('1', 'PTR'): u'foo.domain.',
   659             ('2', 'PTR'): 'ufc.domain.',
   659             ('2', 'PTR'): u'ufc.domain.',
   660             ('3', 'PTR'): 'ufc.domain.',
   660             ('3', 'PTR'): u'ufc.domain.',
   661             ('4', 'PTR'): 'ufc.domain.',
   661             ('4', 'PTR'): u'ufc.domain.',
   662             ('5', 'PTR'): 'bar.domain.',
   662             ('5', 'PTR'): u'bar.domain.',
   663             ('6', 'PTR'): 'ufc.domain.',
   663             ('6', 'PTR'): u'ufc.domain.',
   664         })
   664         })
   665 
   665 
   666 class TestDhcp(unittest.TestCase):
   666 class TestDhcp(unittest.TestCase):
   667     def assertBlockEqual(self, block, (key, items, blocks)):
   667     def assertBlockEqual(self, block, (key, items, blocks)):
   668         self.assertEqual(block.key, key)
   668         self.assertEqual(block.key, key)
   683 
   683 
   684             self.assertBlockEqual(_block, block)
   684             self.assertBlockEqual(_block, block)
   685     
   685     
   686     def testHost(self):
   686     def testHost(self):
   687         host = Host.build('foo', 'test',
   687         host = Host.build('foo', 'test',
   688                 ip          = '192.0.2.1',
   688                 ip          = u'192.0.2.1',
   689                 ethernet    = '00:11:22:33:44:55',
   689                 ethernet    = '00:11:22:33:44:55',
   690                 owner       = 'foo',
   690                 owner       = 'foo',
   691         )
   691         )
   692 
   692 
   693         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
   693         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
   698             ], [])
   698             ], [])
   699         ])
   699         ])
   700 
   700 
   701     def testHostFQDN(self):
   701     def testHostFQDN(self):
   702         host = Host.build('foo.test', 'test',
   702         host = Host.build('foo.test', 'test',
   703                 ip          = '192.0.2.1',
   703                 ip          = u'192.0.2.1',
   704                 ethernet    = '00:11:22:33:44:55',
   704                 ethernet    = '00:11:22:33:44:55',
   705         )
   705         )
   706 
   706 
   707         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
   707         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
   708             (('host', 'foo.test'), [
   708             (('host', 'foo.test'), [
   712             ], [])
   712             ], [])
   713         ])
   713         ])
   714 
   714 
   715     def testHostStatic(self):
   715     def testHostStatic(self):
   716         host = Host.build('foo', 'test',
   716         host = Host.build('foo', 'test',
   717                 ip          = '192.0.2.1',
   717                 ip          = u'192.0.2.1',
   718         )
   718         )
   719 
   719 
   720         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
   720         self.assertBlocksEqual(list(dhcp.dhcp_host(host)), [
   721 
   721 
   722         ])
   722         ])
   779         ])
   779         ])
   780     
   780     
   781     def testHosts(self):
   781     def testHosts(self):
   782         hosts = [
   782         hosts = [
   783                 Host.build('foo', 'test',
   783                 Host.build('foo', 'test',
   784                         ip          = '192.0.2.1',
   784                         ip          = u'192.0.2.1',
   785                         ethernet    = '00:11:22:33:44:55',
   785                         ethernet    = '00:11:22:33:44:55',
   786                 ),
   786                 ),
   787                 Host.build('bar', 'test',
   787                 Host.build('bar', 'test',
   788                         ip          = '192.0.2.2',
   788                         ip          = u'192.0.2.2',
   789                         ethernet    = '01:23:45:67:89:ab',
   789                         ethernet    = '01:23:45:67:89:ab',
   790                 ),
   790                 ),
   791         ]
   791         ]
   792 
   792 
   793         self.assertBlocksEqual(list(dhcp.dhcp_hosts(hosts)), [
   793         self.assertBlocksEqual(list(dhcp.dhcp_hosts(hosts)), [
   817             list(dhcp.dhcp_hosts(hosts))
   817             list(dhcp.dhcp_hosts(hosts))
   818 
   818 
   819     def testHostMultinet(self):
   819     def testHostMultinet(self):
   820         hosts = [
   820         hosts = [
   821                 Host.build('foo', 'test1',
   821                 Host.build('foo', 'test1',
   822                     ip              = '192.0.1.1',
   822                     ip              = u'192.0.1.1',
   823                     ethernet        = { 'eth1': '00:11:22:33:44:55' },
   823                     ethernet        = { 'eth1': '00:11:22:33:44:55' },
   824                 ),
   824                 ),
   825                 Host.build('foo', 'test2',
   825                 Host.build('foo', 'test2',
   826                     ip              = '192.0.2.1',
   826                     ip              = u'192.0.2.1',
   827                     ethernet        = { 'eth2': '01:23:45:67:89:ab' },
   827                     ethernet        = { 'eth2': '01:23:45:67:89:ab' },
   828                 ),
   828                 ),
   829         ]
   829         ]
   830         
   830         
   831         self.assertBlocksEqual(list(dhcp.dhcp_hosts(hosts)), [
   831         self.assertBlocksEqual(list(dhcp.dhcp_hosts(hosts)), [