]> git.ipfire.org Git - thirdparty/pdns.git/blame - regression-tests.api/test_Servers.py
Make sure we can install unsigned packages.
[thirdparty/pdns.git] / regression-tests.api / test_Servers.py
CommitLineData
c563cbe5
CH
1import requests
2import unittest
c1374bdb 3from test_helper import ApiTestCase, is_auth, is_recursor
1a152698
CH
4
5
6class Servers(ApiTestCase):
7
c1374bdb 8 def test_list_servers(self):
46d06a12 9 r = self.session.get(self.url("/api/v1/servers"))
c1374bdb 10 self.assert_success_json(r)
1a152698
CH
11 lst = r.json()
12 self.assertEquals(len(lst), 1) # only localhost allowed in there
13 data = lst[0]
14 for k in ('id', 'daemon_type', 'url'):
15 self.assertIn(k, data)
16 self.assertEquals(data['id'], 'localhost')
17
c1374bdb 18 def test_servers_localhost(self):
46d06a12 19 r = self.session.get(self.url("/api/v1/servers/localhost"))
c1374bdb 20 self.assert_success_json(r)
1a152698
CH
21 data = r.json()
22 for k in ('id', 'type', 'version', 'daemon_type', 'url', 'zones_url', 'config_url'):
23 self.assertIn(k, data)
24 self.assertEquals(data['id'], 'localhost')
25 self.assertEquals(data['type'], 'Server')
26 # or 'recursor' for recursors
c1374bdb 27 if is_auth():
7c876c30 28 daemon_type = 'authoritative'
c1374bdb 29 elif is_recursor():
7c876c30 30 daemon_type = 'recursor'
c1374bdb
CH
31 else:
32 raise RuntimeError('Unknown daemon type')
7c876c30 33 self.assertEquals(data['daemon_type'], daemon_type)
1a152698 34
c1374bdb 35 def test_read_config(self):
46d06a12 36 r = self.session.get(self.url("/api/v1/servers/localhost/config"))
c1374bdb 37 self.assert_success_json(r)
a45303b8
CH
38 data = dict([(r['name'], r['value']) for r in r.json()])
39 self.assertIn('daemon', data)
40
c1374bdb 41 def test_read_statistics(self):
46d06a12 42 r = self.session.get(self.url("/api/v1/servers/localhost/statistics"))
c1374bdb 43 self.assert_success_json(r)
118d3b31
CH
44 data = r.json()
45 self.assertIn('uptime', [e['name'] for e in data])
46 if is_auth():
541bb91b 47 print(data)
87798d5e 48 qtype_stats, respsize_stats, queries_stats, rcode_stats = None, None, None, None
118d3b31 49 for elem in data:
87798d5e 50 if elem['type'] == 'MapStatisticItem' and elem['name'] == 'response-by-qtype':
118d3b31
CH
51 qtype_stats = elem['value']
52 elif elem['type'] == 'MapStatisticItem' and elem['name'] == 'response-sizes':
53 respsize_stats = elem['value']
54 elif elem['type'] == 'RingStatisticItem' and elem['name'] == 'queries':
55 queries_stats = elem['value']
87798d5e
TB
56 elif elem['type'] == 'MapStatisticItem' and elem['name'] == 'response-by-rcode':
57 rcode_stats = elem['value']
118d3b31
CH
58 self.assertIn('A', [e['name'] for e in qtype_stats])
59 self.assertIn('60', [e['name'] for e in respsize_stats])
60 self.assertIn('example.com/A', [e['name'] for e in queries_stats])
87798d5e 61 self.assertIn('No Error', [e['name'] for e in rcode_stats])
5376a5d7
RG
62
63 def test_read_one_statistic(self):
64 r = self.session.get(self.url("/api/v1/servers/localhost/statistics?statistic=uptime"))
65 self.assert_success_json(r)
66 data = r.json()
67 self.assertIn('uptime', [e['name'] for e in data])
68
69 def test_read_one_non_existent_statistic(self):
70 r = self.session.get(self.url("/api/v1/servers/localhost/statistics?statistic=uptimeAAAA"))
71 self.assertEquals(r.status_code, 422)
72 self.assertIn("Unknown statistic name", r.json()['error'])
8b4030fb 73
8b4030fb
GC
74 def test_read_metrics(self):
75 if is_recursor():
76 res = self.session.get(self.url("/metrics"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0)
77 self.assertEqual(res.status_code, 200)
78 # print(res.text)
79 found = False
80 for line in res.text.splitlines():
81 if line[0] == "#":
82 continue
5773b1c6 83 if line.split(" ")[0] == "pdns_recursor_uptime":
8b4030fb 84 found = True
5773b1c6 85 self.assertTrue(found,"pdns_recursor_uptime is missing")
921f6274 86
c563cbe5
CH
87 @unittest.skipIf(is_auth(), "Not applicable")
88 def test_read_statistics_using_password(self):
89 r = requests.get(self.url("/api/v1/servers/localhost/statistics"), auth=('admin', self.server_web_password))
90 self.assertEquals(r.status_code, requests.codes.ok)
91 self.assert_success_json(r)