def setUp(self): self.api = PowerDnsRestApiClient(u"wrong-password") self.zone = u"{0}-rest.zone.".format(type(self).__name__.lower()) self.nameservers = [ u"ns1.{0}".format(self.zone), u"ns2.{0}".format(self.zone), ]
class TestWrongApiPassword(unittest.TestCase): def setUp(self): self.api = PowerDnsRestApiClient(u"wrong-password") self.zone = u"{0}-rest.zone.".format(type(self).__name__.lower()) self.nameservers = [ u"ns1.{0}".format(self.zone), u"ns2.{0}".format(self.zone), ] def test_create_zone(self): with self.assertRaises(PowerDnsRestApiException) as context: self.api.add_zone(self.zone, self.nameservers)
def setUp(self): self.api = PowerDnsRestApiClient(u"pdnsapi") self.zone = u"{0}.zone.".format(type(self).__name__.lower()) self.nameservers = [ u"ns1.{0}".format(self.zone), u"ns2.{0}".format(self.zone) ] try: # It's possible that failed test left old zone remains self.api.del_zone(self.zone) except Exception: pass
class TestApiZone(unittest.TestCase): def setUp(self): self.api = PowerDnsRestApiClient(u"pdnsapi") self.zone = u"{0}.zone.".format(type(self).__name__.lower()) self.nameservers = [ u"ns1.{0}".format(self.zone), u"ns2.{0}".format(self.zone) ] try: # It's possible that failed test left old zone remains self.api.del_zone(self.zone) except Exception: pass def tearDown(self): try: self.api.del_zone(self.zone) except Exception: pass def test_add_zone(self): self.assertTrue(self.api.add_zone(self.zone, self.nameservers)) def test_delete_zone(self): self.api.add_zone(self.zone, self.nameservers) self.assertTrue(self.api.del_zone(self.zone)) def test_delete_zone_invalid(self): with self.assertRaises(PowerDnsRestApiException) as context: self.api.del_zone(u"nonexisting.") def test_delete_zone_invalid2(self): with self.assertRaises(PowerDnsRestApiException) as context: self.api.del_zone(u"nonexisting") def test_get_zone(self): from pypdnsrest.dnszone import DNSZone self.api.add_zone(self.zone, self.nameservers) self.assertIsInstance(self.api.get_zone(self.zone), DNSZone) def test_get_zones(self): self.assertIsInstance(self.api.get_zones(), list) def test_add_invalid(self): with self.assertRaises(TypeError) as context: self.api.add_zone(self.zone, int(1)) def test_add_invalid2(self): with self.assertRaises(ValueError) as context: self.api.add_zone(self.zone, []) def test_add_invalid3(self): with self.assertRaises(TypeError) as context: self.api.add_zone(self.zone, [None]) def test_add_invalid4(self): with self.assertRaises(ValueError) as context: self.api.add_zone(self.zone, [""]) def test_add_invalid5(self): with self.assertRaises(TypeError) as context: self.api.add_zone(self.zone, [int(1)])
class TestApiParsers(unittest.TestCase): def setUp(self): self.api = PowerDnsRestApiClient(u"pdnsapi") self.zone = u"{0}.zone.".format(type(self).__name__.lower()) self.nameservers = [ u"ns1.{0}".format(self.zone), u"ns2.{0}".format(self.zone) ] try: # It's possible that failed test left old zone remains self.api.del_zone(self.zone) except Exception: pass def tearDown(self): try: self.api.del_zone(self.zone) except Exception: pass def test_parser(self): from pypdnsrest.parsers import ARecordParser self.assertTrue(self.api.add_parser(ARecordParser())) def test_parser2(self): from pypdnsrest.parsers import ARecordParser self.api.add_parser(ARecordParser()) self.assertFalse(self.api.add_parser(ARecordParser())) def test_parser_invalid(self): with self.assertRaises(TypeError) as context: self.api.add_parser(int(1)) def test_parser_invalid2(self): self.api.add_parser(SoaRecordParser()) self.api.add_zone(self.zone, self.nameservers) with self.assertRaises(ValueError) as context: self.api.get_zone(self.zone)
log = logging.getLogger(__name__) if __name__ == "__main__": # Example: import sys logging.basicConfig(stream=sys.stdout, # level=logging.DEBUG ) from pypdnsrest.client import PowerDnsRestApiClient zone = u"example.org." api = PowerDnsRestApiClient(u"pdnsapi") try: api.del_zone(zone) except: pass api.add_zone(zone, [ "ns1.{0}".format(zone), "ns2.{0}".format(zone), ]) # Add SOA record from pypdnsrest.dnsrecords import DNSSoaRecord from pypdnsrest.dnsrecords import DNSSoaRecordData
class TestApiRecords(unittest.TestCase): def setUp(self): self.api = PowerDnsRestApiClient(u"pdnsapi") self.zone = u"{0}.zone.".format(type(self).__name__.lower()) self.nameservers = [ u"ns1.{0}".format(self.zone), u"ns2.{0}".format(self.zone), ] try: # It's possible that failed test left old zone remains self.api.del_zone(self.zone) except: pass self.api.add_zone(self.zone, self.nameservers) def tearDown(self): self.api.del_zone(self.zone) def test_add_record_soa(self): from pypdnsrest.dnsrecords import DNSSoaRecord from pypdnsrest.dnsrecords import DNSSoaRecordData soadata = DNSSoaRecordData(u"ns1.{0}".format(self.zone), u"admin.{0}".format(self.zone), 1) rec = DNSSoaRecord(self.zone) rec.set_data(soadata) self.assertTrue(self.api.add_record(self.zone, rec)) def test_add_record_a(self): from ipaddress import IPv4Address from pypdnsrest.dnsrecords import DNSARecord rec = DNSARecord(self.zone) rec.set_data(IPv4Address(u"192.168.101.1")) self.assertTrue(self.api.add_record(self.zone, rec)) def test_add_record_aaaa(self): from ipaddress import IPv6Address from pypdnsrest.dnsrecords import DNSAaaaRecord rec = DNSAaaaRecord(self.zone) rec.set_data(IPv6Address(u"fd12:3456:789a:bcde:f012:3456:789a:bcde")) self.assertTrue(self.api.add_record(self.zone, rec)) def test_2nameservers(self): from pypdnsrest.dnsrecords import DNSNsRecord rec = DNSNsRecord(self.zone) rec.set_data("ns1.{0}".format(self.zone)) self.api.add_record(self.zone, rec) rec = DNSNsRecord(self.zone) rec.set_data("ns2.{0}".format(self.zone)) self.assertTrue(self.api.add_record(self.zone, rec)) def test_add_record_invalid(self): with self.assertRaises(TypeError) as context: self.api.add_record(self.zone, u"invalid") def test_add_record_invalid2(self): from pypdnsrest.dnsrecords import DNSARecord rec = DNSARecord(self.zone) with self.assertRaises(InvalidDNSRecordException) as context: self.api.add_record(self.zone, rec) def test_delete_record(self): from ipaddress import IPv4Address from pypdnsrest.dnsrecords import DNSARecord rec = DNSARecord(self.zone) rec.set_data(IPv4Address(u"192.168.0.1")) self.api.add_record(self.zone, rec) self.assertTrue(self.api.del_record(self.zone, rec)) def test_delete_record_invalid(self): with self.assertRaises(TypeError) as context: self.api.del_record(self.zone, u"invalid") def test_delete_record_invalid2(self): from pypdnsrest.dnsrecords import DNSARecord rec = DNSARecord(self.zone) with self.assertRaises(InvalidDNSRecordException) as context: self.api.del_record(self.zone, rec)