From: Christian Hofstaedtler Date: Tue, 7 Jan 2014 16:12:54 +0000 (+0100) Subject: Add initial testsuite for Auth. REST API X-Git-Tag: rec-3.6.0-rc1~217^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1a1526982444c9505b90896aed4c75c40359a53f;p=pdns Add initial testsuite for Auth. REST API --- diff --git a/.travis.yml b/.travis.yml index 19b9cab58..b4d71c28f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ before_script: - sudo /sbin/ip addr add 10.0.3.0/24 dev lo - sudo rm /etc/apt/sources.list.d/travis_ci_zeromq3-source.list - sudo apt-get update - - sudo apt-get install libboost-all-dev libtolua-dev bc libcdb-dev libnet-dns-perl unbound-host ldnsutils dnsutils bind9utils libtool libcdb-dev xmlto dblatex links asciidoc ruby-json ruby-sqlite3 rubygems libcurl4-openssl-dev ruby1.9.1 socat time libzmq1 libzmq-dev pkg-config daemontools authbind liblua5.1-posix1 libopendbx1-dev libopendbx1-sqlite3 + - sudo apt-get install libboost-all-dev libtolua-dev bc libcdb-dev libnet-dns-perl unbound-host ldnsutils dnsutils bind9utils libtool libcdb-dev xmlto dblatex links asciidoc ruby-json ruby-sqlite3 rubygems libcurl4-openssl-dev ruby1.9.1 socat time libzmq1 libzmq-dev pkg-config daemontools authbind liblua5.1-posix1 libopendbx1-dev libopendbx1-sqlite3 python-virtualenv - sudo update-alternatives --set ruby /usr/bin/ruby1.9.1 - sudo touch /etc/authbind/byport/53 - sudo chmod 755 /etc/authbind/byport/53 @@ -40,6 +40,8 @@ script: - ./stop.sh - sleep 3 - ./clean.sh + - cd ../regression-tests.api + - ./runtests - cd ../regression-tests - touch tests/verify-dnssec-zone/allow-missing - ./start-test-stop 5300 bind-both diff --git a/regression-tests.api/.gitignore b/regression-tests.api/.gitignore new file mode 100644 index 000000000..b5d3e23b1 --- /dev/null +++ b/regression-tests.api/.gitignore @@ -0,0 +1,7 @@ +/.venv +/*.xml +/*.pid +/pdns.sqlite3 +/*.controlsocket +/named.conf +/*.pyc diff --git a/regression-tests.api/requirements.txt b/regression-tests.api/requirements.txt new file mode 100644 index 000000000..9dbd8898e --- /dev/null +++ b/regression-tests.api/requirements.txt @@ -0,0 +1,2 @@ +requests==2.1.0 +nose==1.3.0 diff --git a/regression-tests.api/runtests b/regression-tests.api/runtests new file mode 100755 index 000000000..9680de884 --- /dev/null +++ b/regression-tests.api/runtests @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +if [ ! -d .venv ]; then + virtualenv .venv +fi +. .venv/bin/activate +python -V +pip install -r requirements.txt + +set -e +set -x +exec ./runtests.py diff --git a/regression-tests.api/runtests.py b/regression-tests.api/runtests.py new file mode 100755 index 000000000..c17909d13 --- /dev/null +++ b/regression-tests.api/runtests.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# +# Shell-script style. + +import os +import subprocess +import sys +import tempfile + +SQLITE_DB = 'pdns.sqlite3' +WEBPORT = '5556' +WEBPASSWORD = '12345' + +NAMED_CONF_TPL = """ +# Generated by runtests.py +options { directory "../regression-tests/zones/"; }; +zone "example.com" { type master; file "example.com"; }; +""" + +# Prepare sqlite DB with a single zone. +subprocess.check_call(["rm", "-f", SQLITE_DB]) +subprocess.check_call(["make", "-C", "../pdns", "zone2sql"]) + +with open('../modules/gsqlite3backend/no-dnssec.schema.sqlite3.sql', 'r') as schema_file: + subprocess.check_call(["sqlite3", SQLITE_DB], stdin=schema_file) +with open('../modules/gsqlite3backend/dnssec.schema.sqlite3.sql', 'r') as schema_file: + subprocess.check_call(["sqlite3", SQLITE_DB], stdin=schema_file) + +with open('named.conf', 'w') as named_conf: + named_conf.write(NAMED_CONF_TPL) +with tempfile.TemporaryFile() as tf: + p = subprocess.Popen(["../pdns/zone2sql", "--transactions", "--gsqlite", "--named-conf=named.conf"], stdout=tf) + p.communicate() + if p.returncode != 0: + raise Exception("zone2sql failed") + tf.seek(0, os.SEEK_SET) # rewind + subprocess.check_call(["sqlite3", SQLITE_DB], stdin=tf) + + +# Now run pdns and the tests. +print "Launching pdns_server and running tests..." +pdnsargs = ("--daemon=no --local-port=5300 --socket-dir=./ --no-shuffle --launch=gsqlite3 --gsqlite3-dnssec --send-root-referral --allow-2136-from=127.0.0.0/8 --experimental-rfc2136=yes --cache-ttl=0 --no-config --gsqlite3-database="+SQLITE_DB+" --experimental-json-interface=yes --webserver=yes --webserver-port="+WEBPORT+" --webserver-address=127.0.0.1 --query-logging --webserver-password="+WEBPASSWORD).split() +pdns = subprocess.Popen(["../pdns/pdns_server"] + pdnsargs, close_fds=True) + +rc = 0 +test_env = {} +test_env.update(os.environ) +test_env.update({'WEBPORT': WEBPORT, 'WEBPASSWORD': WEBPASSWORD}) + +try: + print "" + p = subprocess.check_call(["nosetests", "--with-xunit"], env=test_env) +except subprocess.CalledProcessError as ex: + rc = ex.returncode +finally: + pdns.terminate() + pdns.wait() + +sys.exit(rc) diff --git a/regression-tests.api/test_Basics.py b/regression-tests.api/test_Basics.py new file mode 100644 index 000000000..ece0e3e81 --- /dev/null +++ b/regression-tests.api/test_Basics.py @@ -0,0 +1,10 @@ +import unittest +import requests +from test_helper import ApiTestCase + + +class TestBasics(ApiTestCase): + + def test_Unauth(self): + r = requests.get(self.url("/servers/localhost")) + self.assertEquals(r.status_code, requests.codes.unauthorized) diff --git a/regression-tests.api/test_Servers.py b/regression-tests.api/test_Servers.py new file mode 100644 index 000000000..561be5a49 --- /dev/null +++ b/regression-tests.api/test_Servers.py @@ -0,0 +1,34 @@ +import unittest +import requests +from test_helper import ApiTestCase + + +class Servers(ApiTestCase): + + def test_ListServers(self): + r = self.session.get(self.url("/servers")) + self.assertSuccessJson(r) + lst = r.json() + self.assertEquals(len(lst), 1) # only localhost allowed in there + data = lst[0] + for k in ('id', 'daemon_type', 'url'): + self.assertIn(k, data) + self.assertEquals(data['id'], 'localhost') + + def test_ServersLocalhost(self): + r = self.session.get(self.url("/servers/localhost")) + self.assertSuccessJson(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 + self.assertEquals(data['daemon_type'], 'authoritative') + + def test_ReadConfig(self): + r = self.session.get(self.url("/servers/localhost/config")) + self.assertSuccessJson(r) + data = dict(r.json()) + self.assertIn('version', data) + print data diff --git a/regression-tests.api/test_Zones.py b/regression-tests.api/test_Zones.py new file mode 100644 index 000000000..046f9e2cd --- /dev/null +++ b/regression-tests.api/test_Zones.py @@ -0,0 +1,18 @@ +import unittest +import requests +from test_helper import ApiTestCase + + +class Servers(ApiTestCase): + + def test_ListZones(self): + r = self.session.get(self.url("/servers/localhost/zones")) + self.assertSuccessJson(r) + data = r.json() + self.assertIn('domains', data) + domains = data['domains'] + example_com = [domain for domain in domains if domain['name'] == u'example.com'] + self.assertEquals(len(example_com), 1) + example_com = example_com[0] + for k in ('name', 'masters', 'kind', 'last_check', 'notified_serial', 'serial'): + self.assertIn(k, example_com) diff --git a/regression-tests.api/test_helper.py b/regression-tests.api/test_helper.py new file mode 100644 index 000000000..846f33e64 --- /dev/null +++ b/regression-tests.api/test_helper.py @@ -0,0 +1,19 @@ +import requests +import urlparse +import unittest +import os + +class ApiTestCase(unittest.TestCase): + + def setUp(self): + # TODO: config + self.server_url = 'http://127.0.0.1:%s/' % (os.environ.get('WEBPORT','5580')) + self.session = requests.Session() + self.session.auth = ('admin', os.environ.get('WEBPASSWORD','changeme')) + + def url(self, relative_url): + return urlparse.urljoin(self.server_url, relative_url) + + def assertSuccessJson(self, result): + result.raise_for_status() + self.assertEquals(result.headers['Content-Type'], 'application/json')