Skip to content

Commit 9cb68c8

Browse files
committed
tests: implement basic tests
1 parent 0585dd9 commit 9cb68c8

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed

tests/__init__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
import powerdns
21+
22+
from logging import Logger
23+
from unittest import TestCase
24+
25+
26+
PDNS_KEY = "MySupErS3cureK3y"
27+
PDNS_API = "http://172.17.0.2:8081/api/v1"
28+
29+
API_CLIENT = powerdns.PDNSApiClient(api_endpoint=PDNS_API,
30+
api_key=PDNS_KEY,
31+
verify=False)
32+
33+
34+
class TestLogger(TestCase):
35+
36+
def test_logger_creation(self):
37+
self.assertIsInstance(powerdns.basic_logger("test", 2, 1), Logger)

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

tests/test_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 unittest import TestCase
21+
22+
from . import API_CLIENT, PDNS_API, PDNS_KEY
23+
24+
25+
class TestClient(TestCase):
26+
27+
def test_client_repr_and_str(self):
28+
repr_str = "PDNSApiClient('%s', '%s', verify=False, timeout=None)" % (
29+
PDNS_API, PDNS_KEY
30+
)
31+
self.assertEqual(repr(API_CLIENT), repr_str)
32+
self.assertEqual(str(API_CLIENT), PDNS_API)
33+
34+
def test_client_full_uri(self):
35+
self.assertIsInstance(API_CLIENT.get(PDNS_API + "/servers"), list)

tests/test_exceptions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 unittest import TestCase
21+
from powerdns import exceptions
22+
23+
24+
class TestExceptions(TestCase):
25+
26+
def test_exception_pdns_error(self):
27+
exc = exceptions.PDNSError("/fake-url", 404, "Not found.")
28+
self.assertEqual(exc.url, "/fake-url")
29+
self.assertEqual(exc.status_code, 404)
30+
self.assertEqual(exc.message, "Not found.")
31+
self.assertEqual(repr(exc), 'PDNSError("/fake-url", 404, "Not found.")')
32+
self.assertEqual(str(exc), 'code=404 /fake-url: Not found.')
33+
34+
def test_exception_pdns_canonical_error(self):
35+
self.assertTrue(issubclass(exceptions.PDNSCanonicalError, SyntaxError))
36+
exc = exceptions.PDNSCanonicalError("fake-name.tld")
37+
self.assertEqual(repr(exc), "PDNSCanonicalError('fake-name.tld')")
38+
self.assertEqual(str(exc), "fake-name.tld")
39+
self.assertEqual(exc.name, "fake-name.tld")
40+
self.assertEqual(exc.message, "'fake-name.tld' is not canonical")

tests/test_interface.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)