class Servers(ApiTestCase): def test_flush(self): r = self.session.put( self.url( "/api/v1/servers/localhost/cache/flush?domain=example.org.")) self.assert_success_json(r) data = r.json() self.assertIn('count', data) @unittest.skipIf(not is_recursor(), "Not applicable") def test_flush_count(self): sdig("ns1.example.com", 'A') r = self.session.put( self.url( "/api/v1/servers/localhost/cache/flush?domain=ns1.example.com." )) self.assert_success_json(r) data = r.json() self.assertIn('count', data) self.assertEqual(1, data['count']) @unittest.skipIf(not is_recursor(), "Not applicable") def test_flush_subtree(self): sdig("ns1.example.com", 'A') sdig("ns2.example.com", 'A') r = self.session.put( self.url( "/api/v1/servers/localhost/cache/flush?domain=example.com.&subtree=false" )) self.assert_success_json(r) data = r.json() self.assertIn('count', data) self.assertEqual(1, data['count']) r = self.session.put( self.url( "/api/v1/servers/localhost/cache/flush?domain=example.com.&subtree=true" )) self.assert_success_json(r) data = r.json() self.assertIn('count', data) self.assertEqual(2, data['count']) def test_flush_root(self): r = self.session.put( self.url("/api/v1/servers/localhost/cache/flush?domain=.")) self.assert_success_json(r) data = r.json() self.assertIn('count', data) self.assertEqual(data['result'], 'Flushed cache.') def test_flush_no_domain(self): r = self.session.put(self.url("/api/v1/servers/localhost/cache/flush")) self.assertEqual(r.status_code, 422) def test_flush_unqualified(self): r = self.session.put( self.url("/api/v1/servers/localhost/cache/flush?domain=bar")) self.assertEqual(r.status_code, 422)
def test_read_metrics(self): if is_recursor(): res = self.session.get(self.url("/metrics"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) self.assertEqual(res.status_code, 200) # print(res.text) found = False for line in res.text.splitlines(): if line[0] == "#": continue if line.split(" ")[0] == "pdns_recursor_uptime": found = True self.assertTrue(found,"pdns_recursor_uptime is missing")
def test_list_zones(self): r = self.session.get(self.url("/servers/localhost/zones")) self.assert_success_json(r) domains = r.json() example_com = [domain for domain in domains if domain['name'] in ('example.com', 'example.com.')] self.assertEquals(len(example_com), 1) example_com = example_com[0] required_fields = ['id', 'url', 'name', 'kind'] if is_auth(): required_fields = required_fields + ['masters', 'last_check', 'notified_serial', 'serial', 'account'] elif is_recursor(): required_fields = required_fields + ['recursion_desired', 'servers'] for field in required_fields: self.assertIn(field, example_com)
def test_servers_localhost(self): r = self.session.get(self.url("/api/v1/servers/localhost")) self.assert_success_json(r) data = r.json() for k in ('id', 'type', 'version', 'daemon_type', 'url', 'zones_url', 'config_url'): self.assertIn(k, data) self.assertEquals(data['id'], 'localhost') self.assertEquals(data['type'], 'Server') # or 'recursor' for recursors if is_auth(): daemon_type = 'authoritative' elif is_recursor(): daemon_type = 'recursor' else: raise RuntimeError('Unknown daemon type') self.assertEquals(data['daemon_type'], daemon_type)
def test_servers_localhost(self): r = self.session.get(self.url("/api/v1/servers/localhost")) self.assert_success_json(r) data = r.json() for k in ('id', 'type', 'version', 'daemon_type', 'url', 'zones_url', 'config_url'): self.assertIn(k, data) self.assertEqual(data['id'], 'localhost') self.assertEqual(data['type'], 'Server') # or 'recursor' for recursors if is_auth(): daemon_type = 'authoritative' elif is_recursor(): daemon_type = 'recursor' else: raise RuntimeError('Unknown daemon type') self.assertEqual(data['daemon_type'], daemon_type)
import json import unittest from test_helper import ApiTestCase, is_recursor @unittest.skipIf(not is_recursor(), "Only applicable to recursors") class RecursorConfig(ApiTestCase): def test_config_allow_from_get(self): r = self.session.get( self.url("/api/v1/servers/localhost/config/allow-from")) self.assert_success_json(r) def test_config_allow_from_replace(self): payload = {'value': ["127.0.0.1"]} r = self.session.put( self.url("/api/v1/servers/localhost/config/allow-from"), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assert_success_json(r) data = r.json() self.assertIn("value", data) self.assertEqual(len(data["value"]), 1) self.assertEqual("127.0.0.1/32", data["value"][0]) def test_config_allow_from_replace_error(self): """Test the error case, should return 422.""" payload = {'value': ["abcdefgh"]} r = self.session.put( self.url("/api/v1/servers/localhost/config/allow-from"), data=json.dumps(payload), headers={'content-type': 'application/json'})
self.assertEquals(len(r.json()), 1) # FIXME test disarmed for now (should be 4) def test_search_rr_case_insensitive(self): name = 'search-rr-insenszone.name' self.create_zone(name=name) r = self.session.get( self.url("/servers/localhost/search-data?q=rr-insensZONE")) self.assert_success_json(r) print r.json() # should return zone, SOA, ns1, ns2 self.assertEquals(len(r.json()), 1) # FIXME test disarmed for now (should be 4) @unittest.skipIf(not is_recursor(), "Not applicable") class RecursorZones(ApiTestCase): def create_zone(self, name=None, kind=None, rd=False, servers=None): if name is None: name = unique_zone_name() if servers is None: servers = [] payload = { 'name': name, 'kind': kind, 'servers': servers, 'recursion_desired': rd } r = self.session.post(self.url("/servers/localhost/zones"), data=json.dumps(payload), headers={'content-type': 'application/json'})
'kind': 'Native', 'soa_edit_api': '', 'soa_edit': '' } r = self.session.put( self.url("/servers/localhost/zones/" + zone_id), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assert_success_json(r) data = r.json() for k in payload.keys(): self.assertIn(k, data) self.assertEquals(data[k], payload[k]) @unittest.skipIf(not is_recursor(), "Not applicable") class RecursorZones(ApiTestCase): def create_zone(self, name=None, kind=None, rd=False, servers=None): if name is None: name = unique_zone_name() if servers is None: servers = [] payload = { 'name': name, 'kind': kind, 'servers': servers, 'recursion_desired': rd } r = self.session.post( self.url("/servers/localhost/zones"),
import json import unittest from test_helper import ApiTestCase, is_recursor @unittest.skipIf(not is_recursor(), "Only applicable to recursors") class RecursorConfig(ApiTestCase): def test_config_allow_from_get(self): r = self.session.get(self.url("/api/v1/servers/localhost/config/allow-from")) self.assert_success_json(r) def test_config_allow_from_replace(self): payload = {'value': ["127.0.0.1"]} r = self.session.put( self.url("/api/v1/servers/localhost/config/allow-from"), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assert_success_json(r) data = r.json() self.assertEquals("127.0.0.1/32", data["value"][0]) def test_config_allow_from_replace_error(self): """Test the error case, should return 422.""" payload = {'value': ["abcdefgh"]} r = self.session.put( self.url("/api/v1/servers/localhost/config/allow-from"), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assertEquals(r.status_code, 422) data = r.json()
class Servers(ApiTestCase): def test_list_servers(self): r = self.session.get(self.url("/api/v1/servers")) self.assert_success_json(r) lst = r.json() self.assertEqual(len(lst), 1) # only localhost allowed in there data = lst[0] for k in ('id', 'daemon_type', 'url'): self.assertIn(k, data) self.assertEqual(data['id'], 'localhost') def test_servers_localhost(self): r = self.session.get(self.url("/api/v1/servers/localhost")) self.assert_success_json(r) data = r.json() for k in ('id', 'type', 'version', 'daemon_type', 'url', 'zones_url', 'config_url'): self.assertIn(k, data) self.assertEqual(data['id'], 'localhost') self.assertEqual(data['type'], 'Server') # or 'recursor' for recursors if is_auth(): daemon_type = 'authoritative' elif is_recursor(): daemon_type = 'recursor' else: raise RuntimeError('Unknown daemon type') self.assertEqual(data['daemon_type'], daemon_type) def test_read_config(self): r = self.session.get(self.url("/api/v1/servers/localhost/config")) self.assert_success_json(r) data = dict([(r['name'], r['value']) for r in r.json()]) self.assertIn('daemon', data) def test_read_statistics(self): r = self.session.get(self.url("/api/v1/servers/localhost/statistics")) self.assert_success_json(r) data = r.json() self.assertIn('uptime', [e['name'] for e in data]) print(data) if is_auth(): qtype_stats, respsize_stats, queries_stats, rcode_stats = None, None, None, None for elem in data: if elem['type'] == 'MapStatisticItem' and elem[ 'name'] == 'response-by-qtype': qtype_stats = elem['value'] elif elem['type'] == 'MapStatisticItem' and elem[ 'name'] == 'response-sizes': respsize_stats = elem['value'] elif elem['type'] == 'RingStatisticItem' and elem[ 'name'] == 'queries': queries_stats = elem['value'] elif elem['type'] == 'MapStatisticItem' and elem[ 'name'] == 'response-by-rcode': rcode_stats = elem['value'] self.assertIn('A', [e['name'] for e in qtype_stats]) self.assertIn('80', [e['name'] for e in respsize_stats]) self.assertIn('example.com/A', [e['name'] for e in queries_stats]) self.assertIn('No Error', [e['name'] for e in rcode_stats]) else: qtype_stats, respsize_stats, rcode_stats = None, None, None for elem in data: if elem['type'] == 'MapStatisticItem' and elem[ 'name'] == 'response-by-qtype': qtype_stats = elem['value'] elif elem['type'] == 'MapStatisticItem' and elem[ 'name'] == 'response-sizes': respsize_stats = elem['value'] elif elem['type'] == 'MapStatisticItem' and elem[ 'name'] == 'response-by-rcode': rcode_stats = elem['value'] self.assertIn('A', [e['name'] for e in qtype_stats]) self.assertIn('60', [e['name'] for e in respsize_stats]) self.assertIn('80', [e['name'] for e in respsize_stats]) self.assertIn('No Error', [e['name'] for e in rcode_stats]) def test_read_one_statistic(self): r = self.session.get( self.url("/api/v1/servers/localhost/statistics?statistic=uptime")) self.assert_success_json(r) data = r.json() self.assertIn('uptime', [e['name'] for e in data]) def test_read_one_non_existent_statistic(self): r = self.session.get( self.url( "/api/v1/servers/localhost/statistics?statistic=uptimeAAAA")) self.assertEqual(r.status_code, 422) self.assertIn("Unknown statistic name", r.json()['error']) def test_read_metrics(self): if is_recursor(): res = self.session.get(self.url("/metrics"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) self.assertEqual(res.status_code, 200) # print(res.text) found = False for line in res.text.splitlines(): if line[0] == "#": continue if line.split(" ")[0] == "pdns_recursor_uptime": found = True self.assertTrue(found, "pdns_recursor_uptime is missing") @unittest.skipIf(is_auth(), "Not applicable") def test_read_statistics_using_password(self): r = requests.get(self.url("/api/v1/servers/localhost/statistics"), auth=('admin', self.server_web_password)) self.assertEqual(r.status_code, requests.codes.ok) self.assert_success_json(r) @unittest.skipIf(is_recursor(), "Not applicable") @unittest.skipIf(is_auth_lmdb(), "No autoprimary management in LMDB yet") def test_autoprimaries(self): # verify that we have zero autoprimaries res = self.session.get( self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) self.assertEqual(res.status_code, requests.codes.ok) self.assertEqual(res.json(), []) # add one payload = {'ip': '192.0.2.1', 'nameserver': 'ns.example.com'} res = self.session.post( self.url("/api/v1/servers/localhost/autoprimaries"), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assertEqual(res.status_code, 201) # check that it's there res = self.session.get( self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) self.assertEqual(res.status_code, requests.codes.ok) self.assertEqual(res.json(), [{ 'account': '', 'ip': '192.0.2.1', 'nameserver': 'ns.example.com' }]) # add another one, this time with an account field payload = { 'ip': '192.0.2.2', 'nameserver': 'ns.example.org', 'account': 'test' } res = self.session.post( self.url("/api/v1/servers/localhost/autoprimaries"), data=json.dumps(payload), headers={'content-type': 'application/json'}) self.assertEqual(res.status_code, 201) # check that both are there res = self.session.get( self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) self.assertEqual(res.status_code, requests.codes.ok) self.assertEqual(len(res.json()), 2) self.assertEqual(sorted(res.json(), key=operator.itemgetter('ip')), [{ 'account': '', 'ip': '192.0.2.1', 'nameserver': 'ns.example.com' }, { 'account': 'test', 'ip': '192.0.2.2', 'nameserver': 'ns.example.org' }]) # remove one res = self.session.delete(self.url( "/api/v1/servers/localhost/autoprimaries/192.0.2.2/ns.example.org" ), headers={ 'content-type': 'application/json"' }) self.assertEqual(res.status_code, 204) # check that we are back to just one res = self.session.get( self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) self.assertEqual(res.status_code, requests.codes.ok) self.assertEqual(res.json(), [{ 'account': '', 'ip': '192.0.2.1', 'nameserver': 'ns.example.com' }])