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