tero@440: import ipaddr tero@440: import unittest tero@440: tero@463: from pvl.hosts import config, host, zone tero@440: from StringIO import StringIO tero@440: tero@440: class Options(object): tero@440: hosts_charset = 'utf-8' tero@440: hosts_domain = None tero@440: hosts_include = None tero@440: tero@447: class ConfFile(StringIO): tero@440: def __init__(self, name, buffer): tero@440: StringIO.__init__(self, buffer) tero@440: self.name = name tero@440: tero@440: class TestConfig(unittest.TestCase): tero@440: def setUp(self): tero@440: self.options = Options() tero@440: tero@461: def assertHostEqual(self, host, host_str, attrs): tero@461: self.assertEquals(str(host), host_str) tero@461: tero@461: for attr, value in attrs.iteritems(): tero@461: self.assertEquals(getattr(host, attr), value) tero@461: tero@447: def assertHostsEqual(self, hosts, expected): tero@447: for host, expect in zip(hosts, expected): tero@447: host_str, attrs = expect tero@441: tero@461: self.assertHostEqual(host, host_str, attrs) tero@441: tero@451: def testApplyHostsFileError(self): tero@440: with self.assertRaises(config.HostConfigError): tero@440: list(config.apply_hosts(self.options, ['nonexistant'])) tero@440: tero@440: def testApplyHosts(self): tero@447: conf_file = ConfFile('test', """ tero@440: [foo] tero@440: ip = 127.0.0.1 tero@440: tero@440: [bar] tero@440: ip = 127.0.0.2 tero@447: """) tero@447: expected = [ tero@447: ('foo@test', dict(ip=ipaddr.IPAddress('127.0.0.1'))), tero@447: ('bar@test', dict(ip=ipaddr.IPAddress('127.0.0.2'))), tero@447: ] tero@440: tero@447: self.assertHostsEqual(config.apply_hosts_file(self.options, conf_file), expected) tero@447: tero@451: def testApply(self): tero@451: self.assertHostsEqual(config.apply(self.options, ['etc/hosts/test']), [ tero@451: ('foo@test', dict( tero@451: ip = ipaddr.IPAddress('127.0.0.1'), tero@451: ethernet = {None: '00:11:22:33:44:55'}, tero@451: )), tero@451: ('bar@test', dict( tero@451: ip = ipaddr.IPAddress('127.0.0.2'), tero@451: ethernet = {None: '01:23:45:67:89:ab'}, tero@451: )), tero@451: ]) tero@451: tero@447: def testApplyHostsExpand(self): tero@449: self.assertHostsEqual(config.apply_host_config(self.options, 'asdf', 'asdf{1-3}', ip='10.100.100.$'), [ tero@447: ('asdf1@asdf', dict(ip=ipaddr.IPAddress('10.100.100.1'))), tero@447: ('asdf2@asdf', dict(ip=ipaddr.IPAddress('10.100.100.2'))), tero@447: ('asdf3@asdf', dict(ip=ipaddr.IPAddress('10.100.100.3'))), tero@447: ]) tero@461: tero@461: def testApplyHostConfigDict(self): tero@461: host = config.apply_host(self.options, 'foo', 'test', { tero@461: 'ethernet.eth0': '00:11:22:33:44:55', tero@461: }) tero@461: tero@461: self.assertHostEqual(host, 'foo@test', dict( tero@461: ethernet = { 'eth0': '00:11:22:33:44:55' } tero@461: )) tero@461: tero@451: def testApplyHostsConfigError(self): tero@451: with self.assertRaises(config.HostConfigError): tero@451: config.apply_host(self.options, 'foo', 'test', { tero@451: 'ethernet': 'foo', tero@451: 'ethernet.eth0': 'bar', tero@451: }) tero@447: tero@469: class TestZoneMixin(object): tero@463: def assertZoneEquals(self, rrs, expected): tero@463: gather = { } tero@463: tero@463: for rr in rrs: tero@463: key = (rr.name.lower(), rr.type.upper()) tero@463: tero@463: self.assertNotIn(key, gather) tero@463: tero@463: gather[key] = rr.data tero@463: tero@463: self.assertDictEqual(gather, expected) tero@463: tero@469: tero@469: class TestForwardZone(TestZoneMixin, unittest.TestCase): tero@469: def setUp(self): tero@469: self.options = Options() tero@469: self.options.add_origin = False tero@469: tero@462: def testResolve(self): tero@462: self.assertEquals(zone.resolve(None, None, 'host'), 'host.') tero@462: self.assertEquals(zone.resolve(None, 'domain', 'host'), 'host.domain.') tero@462: tero@462: with self.assertRaises(zone.HostZoneError): tero@462: zone.resolve('origin', 'domain', 'host') tero@462: tero@462: self.assertEquals(zone.resolve('domain', 'domain', 'host'), 'host') tero@462: self.assertEquals(zone.resolve('origin', 'domain.origin', 'host'), 'host.domain') tero@462: tero@462: with self.assertRaises(zone.HostZoneError): tero@462: zone.resolve('origin', 'domainorigin', 'host') tero@462: tero@462: with self.assertRaises(zone.HostZoneError): tero@462: zone.resolve('origin', None, 'host.domain') tero@462: tero@469: def testHostOutOfOrigin(self): tero@463: h = host.Host('host', 'domain', ip=ipaddr.IPAddress('10.0.0.1')) tero@463: tero@463: self.assertZoneEquals(zone.host_forward(h, 'test'), { }) tero@463: tero@464: def testHostIP(self): tero@463: h = host.Host.build('host', 'domain', tero@463: ip = '192.0.2.1', tero@463: ip6 = '2001:db8::192.0.2.1', tero@463: ) tero@463: tero@463: self.assertZoneEquals(zone.host_forward(h, 'domain'), { tero@463: ('host', 'A'): ['192.0.2.1'], tero@463: ('host', 'AAAA'): ['2001:db8::c000:201'], tero@463: }) tero@463: tero@464: self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), { tero@464: ('1', 'PTR'): ['host.domain.'], tero@464: }) tero@464: tero@464: self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), { tero@464: ('1.0.2.0.0.0.0.c.0.0.0.0.0.0.0.0', 'PTR'): ['host.domain.'], tero@464: }) tero@465: tero@466: def testHostAlias(self): tero@466: h = host.Host.build('host', 'domain', tero@466: ip = '192.0.2.1', tero@466: alias = 'test *.test', tero@466: ) tero@466: tero@466: self.assertEquals(h.alias, ['test', '*.test']) tero@466: tero@466: self.assertZoneEquals(zone.host_forward(h, 'domain'), { tero@466: ('host', 'A'): ['192.0.2.1'], tero@466: ('test', 'CNAME'): ['host'], tero@466: ('*.test', 'CNAME'): ['host'], tero@466: }) tero@466: tero@468: def testHostAlias46(self): tero@468: h = host.Host.build('host', 'domain', tero@468: ip = '192.0.2.1', tero@468: ip6 = '2001:db8::192.0.2.1', tero@468: alias4 = 'test4', tero@468: alias6 = 'test6', tero@468: ) tero@468: tero@468: self.assertZoneEquals(zone.host_forward(h, 'domain'), { tero@468: ('host', 'A'): ['192.0.2.1'], tero@468: ('host', 'AAAA'): ['2001:db8::c000:201'], tero@468: ('test4', 'A'): ['192.0.2.1'], tero@468: ('test6', 'AAAA'): ['2001:db8::c000:201'], tero@468: }) tero@468: tero@468: def testHostAlias4Missing(self): tero@468: h = host.Host.build('host', 'domain', tero@468: ip6 = '2001:db8::192.0.2.1', tero@468: alias4 = 'test4', tero@468: alias6 = 'test6', tero@468: ) tero@468: tero@468: with self.assertRaises(zone.HostZoneError): tero@468: self.assertZoneEquals(zone.host_forward(h, 'domain'), { }) tero@468: tero@468: def testHostAlias6Missing(self): tero@468: h = host.Host.build('host', 'domain', tero@468: ip = '192.0.2.1', tero@468: alias4 = 'test4', tero@468: alias6 = 'test6', tero@468: ) tero@468: tero@468: with self.assertRaises(zone.HostZoneError): tero@468: self.assertZoneEquals(zone.host_forward(h, 'domain'), { }) tero@468: tero@469: def testHostDelegate(self): tero@469: h = host.Host.build('host', 'example.com', tero@469: forward = 'host.example.net', tero@469: ) tero@469: tero@469: self.assertZoneEquals(zone.host_forward(h, 'example.com'), { tero@469: ('host', 'CNAME'): ['host.example.net.'], tero@469: }) tero@469: tero@467: def testHostForwardAlias(self): tero@467: h = host.Host.build('host', 'domain', tero@467: forward = 'host.example.net', tero@467: alias = 'test', tero@467: ) tero@467: tero@467: self.assertZoneEquals(zone.host_forward(h, 'domain'), { tero@467: ('host', 'CNAME'): ['host.example.net.'], tero@467: ('test', 'CNAME'): ['host'], tero@467: }) tero@466: tero@470: def testHostLocation(self): tero@470: h = host.Host.build('host', 'domain', tero@470: ip = '192.0.2.1', tero@470: location = 'test', tero@470: ) tero@470: tero@470: self.assertEquals(h.location, ('test', 'domain')) tero@470: tero@470: self.assertZoneEquals(zone.host_forward(h, 'domain'), { tero@470: ('host', 'A'): ['192.0.2.1'], tero@470: ('test', 'CNAME'): ['host'], tero@470: }) tero@470: tero@469: class TestReverseZone(TestZoneMixin, unittest.TestCase): tero@469: def setUp(self): tero@469: self.options = Options() tero@469: self.options.unknown_host = False tero@464: tero@463: def testHostDelegate(self): tero@463: h = host.Host.build('host', 'example.com', tero@463: ip = '192.0.2.1', tero@463: ip6 = '2001:db8::192.0.2.1', tero@465: forward = '', tero@463: reverse = '1.0/28.2.0.192.in-addr.arpa', tero@463: ) tero@463: tero@463: self.assertZoneEquals(zone.host_forward(h, 'example.com'), { tero@465: tero@463: }) tero@463: tero@463: self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('192.0.2.1/24'))), { tero@463: ('1', 'CNAME'): ['1.0/28.2.0.192.in-addr.arpa.'], tero@463: }) tero@464: tero@464: self.assertZoneEquals((rr for ip, rr in zone.host_reverse(h, ipaddr.IPNetwork('2001:db8::/64'))), { tero@464: tero@464: }) tero@463: tero@469: tero@440: if __name__ == '__main__': tero@440: unittest.main()