]> git.ipfire.org Git - thirdparty/pdns.git/blame - regression-tests.api/runtests.py
Merge pull request #6136 from zeha/apitests
[thirdparty/pdns.git] / regression-tests.api / runtests.py
CommitLineData
1a152698
CH
1#!/usr/bin/env python
2#
3# Shell-script style.
4
5import os
a1e51a8a 6import requests
02945d9a 7import shutil
1a152698
CH
8import subprocess
9import sys
10import tempfile
a1e51a8a 11import time
1a152698
CH
12
13SQLITE_DB = 'pdns.sqlite3'
7992c698
CH
14WEBPORT = 5556
15DNSPORT = 5300
bbef8f04 16APIKEY = '1234567890abcdefghijklmnopq-key'
7cbc5255 17PDNSUTIL_CMD = ["../pdns/pdnsutil", "--config-dir=."]
1a152698
CH
18
19NAMED_CONF_TPL = """
20# Generated by runtests.py
21options { directory "../regression-tests/zones/"; };
22zone "example.com" { type master; file "example.com"; };
ccfabd0d 23zone "powerdnssec.org" { type master; file "powerdnssec.org"; };
86b3e4e8 24zone "cryptokeys.org" { type master; file "cryptokeys.org"; };
ccfabd0d
CH
25"""
26
27AUTH_CONF_TPL = """
28# Generated by runtests.py
a21e8566 29launch=gsqlite3,bind
ccfabd0d
CH
30gsqlite3-dnssec=on
31gsqlite3-database="""+SQLITE_DB+"""
32module-dir=../regression-tests/modules
a21e8566
CH
33bind-config=bindbackend.conf
34"""
35
36BINDBACKEND_CONF_TPL = """
37# Generated by runtests.py
1a152698
CH
38"""
39
41942bb3
CH
40ACL_LIST_TPL = """
41# Generated by runtests.py
42# local host
43127.0.0.1
44::1
45"""
46
02945d9a
CH
47REC_EXAMPLE_COM_CONF_TPL = """
48# Generated by runtests.py
49auth-zones+=example.com=../regression-tests/zones/example.com
50"""
51
52REC_CONF_TPL = """
53# Generated by runtests.py
54auth-zones=
55forward-zones=
56forward-zones-recurse=
d07bf7ff 57api-config-dir=%(conf_dir)s
02945d9a
CH
58include-dir=%(conf_dir)s
59"""
60
c1374bdb 61
02945d9a
CH
62def ensure_empty_dir(name):
63 if os.path.exists(name):
64 shutil.rmtree(name)
65 os.mkdir(name)
66
67
7992c698
CH
68def format_call_args(cmd):
69 return "$ '%s'" % ("' '".join(cmd))
70
71
72def run_check_call(cmd, *args, **kwargs):
73 print format_call_args(cmd)
74 subprocess.check_call(cmd, *args, **kwargs)
75
76
5f8fa6d4
CH
77wait = ('--wait' in sys.argv)
78if wait:
79 sys.argv.remove('--wait')
80
6754ef71
CH
81tests = [opt for opt in sys.argv if opt.startswith('--tests=')]
82if tests:
83 for opt in tests:
84 sys.argv.remove(opt)
85tests = [opt.split('=', 1)[1] for opt in tests]
86
7c876c30
CH
87daemon = (len(sys.argv) == 2) and sys.argv[1] or None
88if daemon not in ('authoritative', 'recursor'):
89 print "Usage: ./runtests (authoritative|recursor)"
90 sys.exit(2)
91
92daemon = sys.argv[1]
93
7992c698 94pdns_server = os.environ.get("PDNSSERVER", "../pdns/pdns_server")
bf6096f5 95pdns_recursor = os.environ.get("PDNSRECURSOR", "../pdns/recursordist/pdns_recursor")
7992c698
CH
96common_args = [
97 "--daemon=no", "--socket-dir=.", "--config-dir=.",
98 "--local-address=127.0.0.1", "--local-port="+str(DNSPORT),
99 "--webserver=yes", "--webserver-port="+str(WEBPORT), "--webserver-address=127.0.0.1", "--webserver-password=something",
100 "--api-key="+APIKEY
101]
bf6096f5 102
7c876c30
CH
103if daemon == 'authoritative':
104
ccfabd0d 105 # Prepare sqlite DB with some zones.
7992c698
CH
106 run_check_call(["rm", "-f", SQLITE_DB])
107 run_check_call(["make", "-C", "../pdns", "zone2sql"])
7c876c30 108
bf8abc96 109 with open('../modules/gsqlite3backend/schema.sqlite3.sql', 'r') as schema_file:
7992c698 110 run_check_call(["sqlite3", SQLITE_DB], stdin=schema_file)
7c876c30
CH
111
112 with open('named.conf', 'w') as named_conf:
113 named_conf.write(NAMED_CONF_TPL)
114 with tempfile.TemporaryFile() as tf:
115 p = subprocess.Popen(["../pdns/zone2sql", "--transactions", "--gsqlite", "--named-conf=named.conf"], stdout=tf)
116 p.communicate()
117 if p.returncode != 0:
118 raise Exception("zone2sql failed")
119 tf.seek(0, os.SEEK_SET) # rewind
7992c698 120 run_check_call(["sqlite3", SQLITE_DB], stdin=tf)
7c876c30 121
a21e8566
CH
122 with open('bindbackend.conf', 'w') as bindbackend_conf:
123 bindbackend_conf.write(BINDBACKEND_CONF_TPL)
124
125 with open('pdns.conf', 'w') as pdns_conf:
126 pdns_conf.write(AUTH_CONF_TPL)
ccfabd0d 127
7992c698
CH
128 run_check_call(PDNSUTIL_CMD + ["secure-zone", "powerdnssec.org"])
129 servercmd = [pdns_server] + common_args + ["--local-ipv6=", "--no-shuffle", "--dnsupdate=yes", "--cache-ttl=0", "--api=yes"]
7c876c30
CH
130
131else:
02945d9a
CH
132 conf_dir = 'rec-conf.d'
133 ensure_empty_dir(conf_dir)
41942bb3
CH
134 with open('acl.list', 'w') as acl_list:
135 acl_list.write(ACL_LIST_TPL)
02945d9a
CH
136 with open('recursor.conf', 'w') as recursor_conf:
137 recursor_conf.write(REC_CONF_TPL % locals())
138 with open(conf_dir+'/example.com..conf', 'w') as conf_file:
139 conf_file.write(REC_EXAMPLE_COM_CONF_TPL)
7c876c30 140
7992c698 141 servercmd = [pdns_recursor] + common_args + ["--allow-from-file=acl.list"]
1a152698
CH
142
143
144# Now run pdns and the tests.
7992c698
CH
145print "Launching server..."
146print format_call_args(servercmd)
147serverproc = subprocess.Popen(servercmd, close_fds=True)
1a152698 148
a1e51a8a
CH
149print "Waiting for webserver port to become available..."
150available = False
151for try_number in range(0, 10):
152 try:
153 res = requests.get('http://127.0.0.1:%s/' % WEBPORT)
154 available = True
155 break
156 except:
157 time.sleep(0.5)
158
159if not available:
160 print "Webserver port not reachable after 10 tries, giving up."
7992c698
CH
161 serverproc.terminate()
162 serverproc.wait()
a1e51a8a
CH
163 sys.exit(2)
164
7c876c30 165print "Running tests..."
7992c698 166returncode = 0
1a152698
CH
167test_env = {}
168test_env.update(os.environ)
7cbc5255 169test_env.update({
7992c698 170 'WEBPORT': str(WEBPORT),
7cbc5255
CH
171 'APIKEY': APIKEY,
172 'DAEMON': daemon,
173 'SQLITE_DB': SQLITE_DB,
174 'PDNSUTIL_CMD': ' '.join(PDNSUTIL_CMD),
175})
1a152698
CH
176
177try:
178 print ""
7992c698 179 run_check_call(["nosetests", "--with-xunit", "-v"] + tests, env=test_env)
1a152698 180except subprocess.CalledProcessError as ex:
7992c698 181 returncode = ex.returncode
1a152698 182finally:
5f8fa6d4
CH
183 if wait:
184 print "Waiting as requested, press ENTER to stop."
185 raw_input()
7992c698
CH
186 serverproc.terminate()
187 serverproc.wait()
1a152698 188
7992c698 189sys.exit(returncode)