|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# PowerDNS web api python client and interface (python-powerdns) |
| 4 | +# |
| 5 | +# Copyright (C) 2018 Denis Pompilio (jawa) <[email protected]> |
| 6 | +# |
| 7 | +# This file is part of python-powerdns |
| 8 | +# |
| 9 | +# This program is free software; you can redistribute it and/or |
| 10 | +# modify it under the terms of the MIT License. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# MIT License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the MIT License along with this |
| 18 | +# program; if not, see <https://opensource.org/licenses/MIT>. |
| 19 | + |
| 20 | +from datetime import date |
| 21 | +from unittest import TestCase |
| 22 | + |
| 23 | +from powerdns.client import PDNSApiClient |
| 24 | +from powerdns.interface import PDNSEndpoint, PDNSServer, PDNSZone |
| 25 | +from powerdns.interface import RRSet, Comment |
| 26 | +from powerdns.exceptions import PDNSError |
| 27 | + |
| 28 | +from . import API_CLIENT |
| 29 | + |
| 30 | + |
| 31 | +API = PDNSEndpoint(API_CLIENT) |
| 32 | +SERVER = API.servers[0] |
| 33 | +ZONE = API.servers[0].get_zone("test.outini.net.") |
| 34 | + |
| 35 | + |
| 36 | +class TestEndpoint(TestCase): |
| 37 | + |
| 38 | + def test_endpoint_attributes(self): |
| 39 | + self.assertIsInstance(API.api_client, PDNSApiClient) |
| 40 | + self.assertTrue(hasattr(API, "servers")) |
| 41 | + |
| 42 | + def test_endpoint_repr_and_str(self): |
| 43 | + api_client_repr = repr(API.api_client) |
| 44 | + api_repr = "PDNSEndpoint(%s)" % (api_client_repr,) |
| 45 | + self.assertEqual(repr(API), api_repr) |
| 46 | + self.assertEqual(str(API), str(API.api_client)) |
| 47 | + |
| 48 | + def test_endpoint_servers_list(self): |
| 49 | + self.assertIsInstance(API.servers, list) |
| 50 | + self.assertIsInstance(API.servers[0], PDNSServer) |
| 51 | + |
| 52 | + |
| 53 | +class TestServers(TestCase): |
| 54 | + |
| 55 | + def test_server_object(self): |
| 56 | + # server_repr = "PDNSServer(%s, %s)" |
| 57 | + # self.assertEqual(repr(SERVER), server_repr) |
| 58 | + self.assertEqual(str(SERVER), "localhost") |
| 59 | + self.assertEqual(SERVER.sid, "localhost") |
| 60 | + self.assertTrue(isinstance(SERVER.version, str)) |
| 61 | + |
| 62 | + def test_server_config(self): |
| 63 | + self.assertIsInstance(SERVER.config, list) |
| 64 | + self.assertIsInstance(SERVER.config[0], dict) |
| 65 | + |
| 66 | + def test_server_zones(self): |
| 67 | + self.assertIsInstance(SERVER.zones, list) |
| 68 | + self.assertIsInstance(SERVER.zones[0], PDNSZone) |
| 69 | + |
| 70 | + def test_server_create_zone(self): |
| 71 | + zone_name = "test.outini.net." |
| 72 | + serial = date.today().strftime("%Y%m%d00") |
| 73 | + timers = "28800 7200 604800 86400" |
| 74 | + soa = "ns01.test.outini.net. admin.outini.net. %s %s" % (serial, timers) |
| 75 | + comments = [Comment("test comment", "admin")] |
| 76 | + soa_r = RRSet(name=zone_name, rtype="SOA", ttl=86400, |
| 77 | + records=[(soa, False)], comments=comments) |
| 78 | + zone_data = dict(name=zone_name, |
| 79 | + kind="Master", |
| 80 | + rrsets=[soa_r], |
| 81 | + nameservers=["ns01.test.outini.net.", |
| 82 | + "ns02.test.outini.net."]) |
| 83 | + |
| 84 | + zone = SERVER.create_zone(**zone_data) |
| 85 | + self.assertIsInstance(zone, PDNSZone) |
| 86 | + with self.assertRaises(PDNSError): |
| 87 | + SERVER.create_zone(**zone_data) |
| 88 | + |
| 89 | + def test_server_get_zone(self): |
| 90 | + self.assertIs(SERVER.get_zone("nonexistent"), None) |
| 91 | + self.assertIsInstance(SERVER.get_zone("test.outini.net."), PDNSZone) |
| 92 | + |
| 93 | + def test_server_suggest_zone(self): |
| 94 | + zone = SERVER.suggest_zone("a.b.c.test.outini.net.") |
| 95 | + self.assertIsInstance(zone, PDNSZone) |
| 96 | + self.assertEqual(zone.name, "test.outini.net.") |
| 97 | + |
| 98 | + |
| 99 | +# class TestZones(TestCase): |
| 100 | +# |
| 101 | +# def test_zone_object(self): |
| 102 | +# # zone_repr = "PDNSZone(%s, %s)" |
| 103 | +# # self.assertEqual(repr(ZONE), zone_repr) |
| 104 | +# self.assertEqual(str(ZONE), "test.outini.net.") |
| 105 | +# print(dir(ZONE)) |
| 106 | + |
| 107 | + |
| 108 | +class TestRRSetRecords(TestCase): |
| 109 | + |
| 110 | + def test_dict_correct(self): |
| 111 | + rrset = RRSet("test", "TXT", [{"content": "foo"}, |
| 112 | + {"content": "bar", "disabled": False}, |
| 113 | + {"content": "baz", "disabled": True}]) |
| 114 | + |
| 115 | + self.assertEqual(rrset["records"][0], |
| 116 | + {"content": "foo", "disabled": False}) |
| 117 | + self.assertEqual(rrset["records"][1], |
| 118 | + {"content": "bar", "disabled": False}) |
| 119 | + self.assertEqual(rrset["records"][2], |
| 120 | + {"content": "baz", "disabled": True}) |
| 121 | + |
| 122 | + def test_dict_additional_key(self): |
| 123 | + with self.assertRaises(ValueError): |
| 124 | + RRSet("test", "TXT", [{"content": "baz", |
| 125 | + "disabled": False, |
| 126 | + "foo": "bar"}]) |
| 127 | + |
| 128 | + def test_dict_missing_key(self): |
| 129 | + with self.assertRaises(ValueError): |
| 130 | + RRSet("test", "TXT", [{"content": "baz", |
| 131 | + "disabled": False, |
| 132 | + "foo": "bar"}]) |
0 commit comments